From cd21d0973dabe1c6fefe9bbf75a2ce097717e344 Mon Sep 17 00:00:00 2001 From: zaaarf Date: Sat, 25 Mar 2023 19:33:51 +0100 Subject: [PATCH] fix: typo in condition, several exceptions, removed unused --- .../exceptions/TargetNotFoundException.java | 16 +++++-- .../ftbsc/lll/processor/LilleroProcessor.java | 4 +- .../ftbsc/lll/processor/tools/ASTUtils.java | 47 +++++-------------- .../lll/processor/tools/JavaPoetUtils.java | 11 ++--- 4 files changed, 30 insertions(+), 48 deletions(-) diff --git a/src/main/java/ftbsc/lll/exceptions/TargetNotFoundException.java b/src/main/java/ftbsc/lll/exceptions/TargetNotFoundException.java index 45819bf..e3384b8 100644 --- a/src/main/java/ftbsc/lll/exceptions/TargetNotFoundException.java +++ b/src/main/java/ftbsc/lll/exceptions/TargetNotFoundException.java @@ -8,9 +8,19 @@ public class TargetNotFoundException extends RuntimeException { /** * Constructs a new target not found exception for the specified method stub. * @param type the type of element being sought (class, method, etc.) - * @param stub the stub's name (and descriptor possibly) + * @param member the stub's name (and descriptor possibly) */ - public TargetNotFoundException(String type, String stub) { - super(String.format("Could not find target %s %s.", type, stub)); + public TargetNotFoundException(String type, String member) { + super(String.format("Could not find target %s %s.", type, member)); + } + + /** + * Constructs a new target not found exception for the specified method stub. + * @param type the type of element being sought (class, method, etc.) + * @param member the stub's name (and descriptor possibly) + * @param parent the parent of the member + */ + public TargetNotFoundException(String type, String member, String parent) { + super(String.format("Could not find target %s %s in class %s.", type, member, parent)); } } diff --git a/src/main/java/ftbsc/lll/processor/LilleroProcessor.java b/src/main/java/ftbsc/lll/processor/LilleroProcessor.java index 69cc11e..204108c 100644 --- a/src/main/java/ftbsc/lll/processor/LilleroProcessor.java +++ b/src/main/java/ftbsc/lll/processor/LilleroProcessor.java @@ -202,7 +202,7 @@ public class LilleroProcessor extends AbstractProcessor { //take care of TypeProxies and FieldProxies first for(VariableElement proxyVar : finders) { ProxyType type = getProxyType(proxyVar); - if(type == ProxyType.METHOD && !proxyVar.getAnnotation(Find.class).name().equals("")) { + if(type == ProxyType.METHOD && proxyVar.getAnnotation(Find.class).name().equals("")) { //methods without a specified name will be handled later methodFinders.add(proxyVar); continue; @@ -412,7 +412,7 @@ public class LilleroProcessor extends AbstractProcessor { this.injector = injector; this.targetStub = targetStub; this.reason = injector.getAnnotation(Injector.class).reason(); - this.target = findMethodFromStub(targetStub, processingEnv); + this.target = findMethodFromStub(targetStub, null, processingEnv); } } } \ No newline at end of file diff --git a/src/main/java/ftbsc/lll/processor/tools/ASTUtils.java b/src/main/java/ftbsc/lll/processor/tools/ASTUtils.java index 1576a23..7d3ef9f 100644 --- a/src/main/java/ftbsc/lll/processor/tools/ASTUtils.java +++ b/src/main/java/ftbsc/lll/processor/tools/ASTUtils.java @@ -13,7 +13,9 @@ import ftbsc.lll.proxies.ProxyType; import javax.annotation.processing.ProcessingEnvironment; import javax.lang.model.element.*; -import javax.lang.model.type.*; +import javax.lang.model.type.MirroredTypeException; +import javax.lang.model.type.MirroredTypesException; +import javax.lang.model.type.TypeMirror; import javax.lang.model.util.Elements; import java.lang.annotation.Annotation; import java.util.ArrayList; @@ -121,29 +123,6 @@ public class ASTUtils { return fqn; } - /** - * Safely extracts a {@link Class} array from an annotation. - * @param ann the annotation containing the class - * @param fun the annotation function returning the class - * @param elementUtils the element utils corresponding to the {@link ProcessingEnvironment} - * @param the type of the annotation carrying the information - * @return a list of {@link TypeMirror}s representing the classes - * @since 0.3.0 - */ - public static List classArrayFromAnnotation(T ann, Function[]> fun, Elements elementUtils) { - List params = new ArrayList<>(); - try { - params.addAll(Arrays.stream(fun.apply(ann)) - .map(Class::getCanonicalName) - .map(fqn -> elementUtils.getTypeElement(fqn).asType()) - .collect(Collectors.toList())); - } catch(MirroredTypesException e) { - params.addAll(e.getTypeMirrors()); - } - return params; - } - - /** * Finds the class name and maps it to the correct format. * @param name the fully qualified name of the class to convert @@ -164,12 +143,11 @@ public class ASTUtils { * @param fallback the (unobfuscated) FQN to fall back on * @param finderAnn an annotation containing metadata about the target, may be null * @return the fully qualified class name - * @since 0.3.0 + * @since 0.5.0 */ public static String findClassNameFromAnnotations(String fallback, Find finderAnn) { - String fullyQualifiedName; if(finderAnn != null) { - fullyQualifiedName = + String fullyQualifiedName = getClassFullyQualifiedName( finderAnn, Find::value, @@ -194,7 +172,7 @@ public class ASTUtils { patchAnn, Patch::value, patchAnn.className() - ), null); + ), finderAnn); } /** @@ -238,8 +216,9 @@ public class ASTUtils { .filter(e -> (field && e instanceof VariableElement) || e instanceof ExecutableElement) .filter(e -> e.getSimpleName().contentEquals(name)) .collect(Collectors.toList()); + if(candidates.size() == 0) - throw new TargetNotFoundException(field ? "field" : "method", String.format("%s %s", name, descr)); + throw new TargetNotFoundException(field ? "field" : "method", name, parentFQN); if(candidates.size() == 1 && (!strict || field)) return candidates.get(0); if(field || descr == null) { @@ -254,7 +233,7 @@ public class ASTUtils { : c -> descr.split("\\)")[0].equalsIgnoreCase(descriptorFromExecutableElement(c).split("\\)")[0]) ).collect(Collectors.toList()); if(candidates.size() == 0) - throw new TargetNotFoundException("method", String.format("%s %s", name, descr)); + throw new TargetNotFoundException("method", String.format("%s %s", name, descr), parentFQN); if(candidates.size() > 1) throw new AmbiguousDefinitionException( String.format("Found %d methods named %s in class %s!", candidates.size(), name, parentFQN) @@ -266,22 +245,20 @@ public class ASTUtils { /** * Finds the real class method corresponding to a stub annotated with {@link Target}. * @param stub the {@link ExecutableElement} for the stub + * @param f the {@link Find} annotation containing fallback data, may be null * @param env the {@link ProcessingEnvironment} to perform the operation in * @return the {@link ExecutableElement} corresponding to the method * @throws AmbiguousDefinitionException if it finds more than one candidate * @throws TargetNotFoundException if it finds no valid candidate * @since 0.3.0 */ - public static ExecutableElement findMethodFromStub(ExecutableElement stub, ProcessingEnvironment env) { + public static ExecutableElement findMethodFromStub(ExecutableElement stub, Find f, ProcessingEnvironment env) { //the parent always has a @Patch annotation Patch patchAnn = stub.getEnclosingElement().getAnnotation(Patch.class); //there should ever only be one of these two Target targetAnn = stub.getAnnotation(Target.class); //if this is null strict mode is always disabled - Find findAnn = stub.getAnnotation(Find.class); //this may be null, it means no fallback info - String parentFQN = findClassNameFromAnnotations(patchAnn, findAnn); + String parentFQN = findClassNameFromAnnotations(patchAnn, f); String methodName = stub.getSimpleName().toString(); - if(findAnn != null && !findAnn.name().equals("")) - throw new AmbiguousDefinitionException(String.format("Specified name %s in @Find annotation for method stub %s!", findAnn.name(), methodName)); String methodDescriptor = descriptorFromExecutableElement(stub); return (ExecutableElement) findMember( parentFQN, diff --git a/src/main/java/ftbsc/lll/processor/tools/JavaPoetUtils.java b/src/main/java/ftbsc/lll/processor/tools/JavaPoetUtils.java index 32a94bb..2728cc8 100644 --- a/src/main/java/ftbsc/lll/processor/tools/JavaPoetUtils.java +++ b/src/main/java/ftbsc/lll/processor/tools/JavaPoetUtils.java @@ -7,9 +7,9 @@ import ftbsc.lll.processor.tools.containers.ArrayContainer; import ftbsc.lll.processor.tools.containers.ClassContainer; import ftbsc.lll.processor.tools.obfuscation.ObfuscationMapper; import ftbsc.lll.proxies.ProxyType; -import ftbsc.lll.tools.DescriptorBuilder; -import ftbsc.lll.proxies.impl.MethodProxy; import ftbsc.lll.proxies.impl.FieldProxy; +import ftbsc.lll.proxies.impl.MethodProxy; +import ftbsc.lll.tools.DescriptorBuilder; import javax.annotation.processing.ProcessingEnvironment; import javax.lang.model.element.Element; @@ -18,14 +18,9 @@ import javax.lang.model.element.Modifier; import javax.lang.model.element.VariableElement; import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; -import javax.lang.model.util.Elements; import javax.tools.Diagnostic; -import java.lang.annotation.Annotation; -import java.util.List; -import java.util.function.Function; import static ftbsc.lll.processor.tools.ASTUtils.*; -import static ftbsc.lll.processor.tools.ASTUtils.mapModifiers; /** * Collection of static utils that rely on JavaPoet to function. @@ -162,7 +157,7 @@ public class JavaPoetUtils { if(isMethod) { ExecutableElement executableTarget; if(f.name().equals("")) //find and validate from stub - executableTarget = findMethodFromStub(stub, env); + executableTarget = findMethodFromStub(stub, null, env); else { //find and validate by name alone if(LilleroProcessor.badPracticeWarnings) //warn user that he is doing bad stuff env.getMessager().printMessage(Diagnostic.Kind.WARNING,