chore: added container class for options

This commit is contained in:
zaaarf 2023-05-26 17:13:48 +02:00
parent 15dece7d33
commit d3ebad7260
No known key found for this signature in database
GPG key ID: 6445A5CD15E5B40C
8 changed files with 170 additions and 185 deletions

View file

@ -1,15 +1,17 @@
package ftbsc.lll.processor;
import com.squareup.javapoet.*;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;
import ftbsc.lll.IInjector;
import ftbsc.lll.exceptions.AmbiguousDefinitionException;
import ftbsc.lll.exceptions.InvalidResourceException;
import ftbsc.lll.exceptions.OrphanElementException;
import ftbsc.lll.processor.annotations.*;
import ftbsc.lll.processor.tools.ProcessorOptions;
import ftbsc.lll.processor.tools.containers.ClassContainer;
import ftbsc.lll.processor.tools.containers.InjectorInfo;
import ftbsc.lll.processor.tools.containers.MethodContainer;
import ftbsc.lll.processor.tools.obfuscation.ObfuscationMapper;
import ftbsc.lll.proxies.ProxyType;
import ftbsc.lll.proxies.impl.TypeProxy;
@ -22,10 +24,8 @@ import javax.tools.Diagnostic;
import javax.tools.FileObject;
import javax.tools.JavaFileObject;
import javax.tools.StandardLocation;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import java.util.stream.Collectors;
@ -47,79 +47,9 @@ public class LilleroProcessor extends AbstractProcessor {
private final Set<String> injectors = new HashSet<>();
/**
* The {@link ObfuscationMapper} used to convert classes and variables
* to their obfuscated equivalent. Will be null when no mapper is in use.
* An object representing the various options passed to the processor.
*/
private ObfuscationMapper mapper = null;
/**
* Whether the processor should issue warnings when compiling code anonymous
* classes which can't be checked for validity.
*/
public static boolean anonymousClassWarning = true;
/**
* Whether injector metadata (what is returned by the functions of {@link IInjector})
* is to use obfuscated names instead of its normal names.
*/
public static boolean obfuscateInjectorMetadata = true;
/**
* Initializes the processor with the processing environment by setting the
* {@code processingEnv} field to the value of the {@code processingEnv} argument.
* @param processingEnv environment to access facilities the tool framework
* provides to the processor
* @throws IllegalStateException if this method is called more than once.
* @since 0.3.0
*/
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
String location = processingEnv.getOptions().get("mappingsFile");
if(location == null)
mapper = null;
else {
InputStream targetStream;
try {
URI target = new URI(location);
targetStream = target.toURL().openStream();
} catch(URISyntaxException | IOException e) {
//may be a local file path
File f = new File(location);
if(!f.exists())
throw new InvalidResourceException(location);
try {
targetStream = new FileInputStream(f);
} catch(FileNotFoundException ex) {
throw new InvalidResourceException(location);
}
}
//assuming its tsrg file
//todo: replace crappy homebaked parser with actual library
this.mapper = new ObfuscationMapper(new BufferedReader(new InputStreamReader(targetStream,
StandardCharsets.UTF_8)).lines());
}
String anonymousClassWarn = processingEnv.getOptions().get("anonymousClassWarning");
if(anonymousClassWarn != null)
anonymousClassWarning = parseBooleanArg(anonymousClassWarn);
String obfuscateInj = processingEnv.getOptions().get("obfuscateInjectorMetadata");
if(obfuscateInj != null)
obfuscateInjectorMetadata = parseBooleanArg(obfuscateInj);
}
/**
* Parses a boolean arg from a String.
* @param arg the arg to parse
* @return the parsed boolean
*/
private static boolean parseBooleanArg(String arg) {
try { // 0 = false, any other integer = true
int i = Integer.parseInt(arg);
return i != 0;
} catch(NumberFormatException ignored) {
return Boolean.parseBoolean(arg);
}
}
public final ProcessorOptions options = new ProcessorOptions(processingEnv);
/**
* Where the actual processing happens.
@ -196,11 +126,7 @@ public class LilleroProcessor extends AbstractProcessor {
//find class information
Patch patchAnn = cl.getAnnotation(Patch.class);
ClassContainer targetClass = ClassContainer.from(
patchAnn,
Patch::value,
patchAnn.innerName(),
this.processingEnv,
this.mapper
patchAnn, Patch::value, patchAnn.innerName(), this.options
);
//find package information
Element packageElement = cl.getEnclosingElement();
@ -231,11 +157,10 @@ public class LilleroProcessor extends AbstractProcessor {
if(type == ProxyType.TYPE) {
//find and validate
ClassContainer clazz = ClassContainer.findOrFallback(
ClassContainer.from(cl, this.processingEnv, this.mapper),
ClassContainer.from(cl, this.options),
patchAnn,
proxyVar.getAnnotation(Find.class),
this.processingEnv,
this.mapper
this.options
);
//types can be generated with a single instruction
constructorBuilder.addStatement(
@ -251,8 +176,7 @@ public class LilleroProcessor extends AbstractProcessor {
null,
null,
constructorBuilder,
this.processingEnv,
this.mapper
this.options
);
}
@ -311,7 +235,7 @@ public class LilleroProcessor extends AbstractProcessor {
matchedInjectors.add(injector);
toGenerate.put(
String.format("%sInjector%d", cl.getSimpleName(), iterationNumber),
new InjectorInfo(injector, tg, targetAnn, this.processingEnv, this.mapper)
new InjectorInfo(injector, tg, targetAnn, this.options)
);
iterationNumber++; //increment is only used by injectors
} else {
@ -323,8 +247,7 @@ public class LilleroProcessor extends AbstractProcessor {
tg,
targetAnn,
constructorBuilder,
this.processingEnv,
this.mapper
this.options
);
}
}
@ -349,9 +272,9 @@ public class LilleroProcessor extends AbstractProcessor {
.addMethod(constructorBuilder.build())
.addMethod(buildStringReturnMethod("name", injName))
.addMethod(buildStringReturnMethod("reason", toGenerate.get(injName).reason))
.addMethod(buildStringReturnMethod("targetClass", obfuscateInjectorMetadata ? targetClass.fqnObf : targetClass.fqn))
.addMethod(buildStringReturnMethod("methodName", obfuscateInjectorMetadata ? target.nameObf : target.name))
.addMethod(buildStringReturnMethod("methodDesc", obfuscateInjectorMetadata ? target.descriptorObf : target.descriptor))
.addMethod(buildStringReturnMethod("targetClass", this.options.obfuscateInjectorMetadata ? targetClass.fqnObf : targetClass.fqn))
.addMethod(buildStringReturnMethod("methodName", this.options.obfuscateInjectorMetadata ? target.nameObf : target.name))
.addMethod(buildStringReturnMethod("methodDesc", this.options.obfuscateInjectorMetadata ? target.descriptorObf : target.descriptor))
.addMethods(generateDummies(cl))
.addMethod(generateInjector(toGenerate.get(injName), this.processingEnv))
.build();

View file

@ -212,13 +212,13 @@ public class ASTUtils {
/**
* Finds the class name and maps it to the correct format.
* @param name the fully qualified name of the class to convert
* @param mapper the {@link ObfuscationMapper} to use, may be null
* @param options the {@link ProcessorOptions} to be used
* @return the fully qualified class name
* @since 0.3.0
*/
public static String findClassName(String name, ObfuscationMapper mapper) {
public static String findClassName(String name, ProcessorOptions options) {
try {
return mapper == null ? name : mapper.obfuscateClass(name).replace('/', '.');
return options.mapper == null ? name : options.mapper.obfuscateClass(name).replace('/', '.');
} catch(MappingNotFoundException e) {
return name;
}

View file

@ -7,7 +7,6 @@ import ftbsc.lll.processor.tools.containers.ClassContainer;
import ftbsc.lll.processor.tools.containers.FieldContainer;
import ftbsc.lll.processor.tools.containers.InjectorInfo;
import ftbsc.lll.processor.tools.containers.MethodContainer;
import ftbsc.lll.processor.tools.obfuscation.ObfuscationMapper;
import ftbsc.lll.proxies.ProxyType;
import ftbsc.lll.proxies.impl.FieldProxy;
import ftbsc.lll.proxies.impl.MethodProxy;
@ -44,13 +43,12 @@ public class JavaPoetUtils {
* @param stub the stub {@link ExecutableElement} if present or relevant, null otherwise
* @param t the {@link Target} relevant to this finder if present or relevant, null otherwise
* @param con the {@link MethodSpec.Builder} to append to
* @param env the {@link ProcessingEnvironment} to perform the operation in
* @param mapper the {@link ObfuscationMapper} to use, may be null
* @param options the {@link ProcessorOptions} to be used
* @since 0.5.0
*/
public static void appendMemberFinderDefinition(
VariableElement var, ExecutableElement stub, Target t,
MethodSpec.Builder con, ProcessingEnvironment env, ObfuscationMapper mapper) {
MethodSpec.Builder con, ProcessorOptions options) {
ProxyType type = getProxyType(var);
if(type != ProxyType.METHOD && type != ProxyType.FIELD)
return; //this method is irrelevant to everyone else
@ -65,13 +63,13 @@ public class JavaPoetUtils {
Element target;
if(isMethod) {
MethodContainer mc = MethodContainer.from(stub, t, f, env, mapper);
MethodContainer mc = MethodContainer.from(stub, t, f, options);
descriptorObf = mc.descriptorObf;
nameObf = mc.nameObf;
parent = mc.parent;
target = mc.elem;
} else {
FieldContainer fc = FieldContainer.from(var, env, mapper);
FieldContainer fc = FieldContainer.from(var, options);
descriptorObf = fc.descriptorObf;
nameObf = fc.nameObf;
parent = fc.parent;

View file

@ -0,0 +1,90 @@
package ftbsc.lll.processor.tools;
import ftbsc.lll.IInjector;
import ftbsc.lll.exceptions.InvalidResourceException;
import ftbsc.lll.processor.tools.obfuscation.ObfuscationMapper;
import javax.annotation.processing.ProcessingEnvironment;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
/**
* Class in charge of containing, parsing and processing all processor options,
* from the simpler booleans to the more complicated mapper.
*/
public class ProcessorOptions {
/**
* The environment the processor is acting in.
*/
public final ProcessingEnvironment env;
/**
* The {@link ObfuscationMapper} used to convert classes and variables
* to their obfuscated equivalent. Will be null when no mapper is in use.
*/
public final ObfuscationMapper mapper;
/**
* Whether the processor should issue warnings when compiling code anonymous
* classes which can't be checked for validity.
*/
public final boolean anonymousClassWarning;
/**
* Whether injector metadata (what is returned by the functions of {@link IInjector})
* is to use obfuscated names instead of its normal names.
*/
public final boolean obfuscateInjectorMetadata;
/**
* The public constructor, parses and stores all given arguments.
* @param env the environment the processor is working in
*/
public ProcessorOptions(ProcessingEnvironment env) {
this.env = env;
String location = env.getOptions().get("mappingsFile");
if(location == null)
this.mapper = null;
else {
InputStream targetStream;
try {
URI target = new URI(location);
targetStream = target.toURL().openStream();
} catch(URISyntaxException | IOException e) {
//may be a local file path
File f = new File(location);
if(!f.exists())
throw new InvalidResourceException(location);
try {
targetStream = new FileInputStream(f);
} catch(FileNotFoundException ex) {
throw new InvalidResourceException(location);
}
}
//assuming its tsrg file
//todo: replace crappy homebaked parser with actual library
this.mapper = new ObfuscationMapper(new BufferedReader(new InputStreamReader(targetStream,
StandardCharsets.UTF_8)).lines());
}
this.anonymousClassWarning = parseBooleanArg(env.getOptions().get("anonymousClassWarning"), true);
this.obfuscateInjectorMetadata = parseBooleanArg(env.getOptions().get("obfuscateInjectorMetadata"), true);
}
/**
* Parses a boolean arg from a String.
* @param arg the arg to parse
* @return the parsed boolean
*/
private static boolean parseBooleanArg(String arg, boolean defaultValue) {
if(arg == null) return defaultValue;
try { // 0 = false, any other integer = true
int i = Integer.parseInt(arg);
return i != 0;
} catch(NumberFormatException ignored) {
return Boolean.parseBoolean(arg);
}
}
}

View file

@ -1,12 +1,10 @@
package ftbsc.lll.processor.tools.containers;
import ftbsc.lll.exceptions.TargetNotFoundException;
import ftbsc.lll.processor.LilleroProcessor;
import ftbsc.lll.processor.annotations.Find;
import ftbsc.lll.processor.annotations.Patch;
import ftbsc.lll.processor.tools.obfuscation.ObfuscationMapper;
import ftbsc.lll.processor.tools.ProcessorOptions;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
@ -40,21 +38,20 @@ public class ClassContainer {
public final Element elem;
/**
* Private constructor, called from {@link #from(Annotation, Function, String, ProcessingEnvironment, ObfuscationMapper)}.
* Private constructor, called from {@link #from(Annotation, Function, String, ProcessorOptions)}.
* @param fqn the fully-qualified name of the target class
* @param innerNames an array of Strings containing the path to the inner class, may be null
* @param env the {@link ProcessingEnvironment} to be used to locate the class
* @param mapper the {@link ObfuscationMapper} to be used, may be null
* @param options the {@link ProcessorOptions} to be used
*/
private ClassContainer(String fqn, String[] innerNames, ProcessingEnvironment env, ObfuscationMapper mapper) {
private ClassContainer(String fqn, String[] innerNames, ProcessorOptions options) {
//find and validate
Element elem = env.getElementUtils().getTypeElement(fqn);
Element elem = options.env.getElementUtils().getTypeElement(fqn);
if(elem == null)
throw new TargetNotFoundException("class", fqn);
StringBuilder fqnBuilder = new StringBuilder(
internalNameFromType(elem.asType(), env).replace('/', '.')
internalNameFromType(elem.asType(), options.env).replace('/', '.')
);
if(innerNames != null) {
@ -64,8 +61,8 @@ public class ClassContainer {
try {
int anonClassCounter = Integer.parseInt(inner);
//anonymous classes cannot be validated!
if(LilleroProcessor.anonymousClassWarning)
env.getMessager().printMessage(
if(options.anonymousClassWarning)
options.env.getMessager().printMessage(
Diagnostic.Kind.WARNING,
String.format(
"Anonymous classes cannot be verified by the processor. The existence of %s$%s is not guaranteed!",
@ -88,7 +85,7 @@ public class ClassContainer {
}
}
this.fqn = fqnBuilder.toString();
this.fqnObf = findClassName(this.fqn, mapper);
this.fqnObf = findClassName(this.fqn, options);
this.elem = elem;
}
@ -97,33 +94,28 @@ public class ClassContainer {
* @param ann the annotation containing the class
* @param classFunction the annotation function returning the class
* @param innerName a string containing the inner class name or nothing
* @param env the {@link ProcessingEnvironment} to be used to locate the class
* @param mapper the {@link ObfuscationMapper} to be used, may be null
* @param options the {@link ProcessorOptions} to be used
* @param <T> the type of the annotation carrying the information
* @return the fully qualified name of the given class
* @since 0.5.0
*/
public static <T extends Annotation> ClassContainer from(T ann, Function<T, Class<?>> classFunction, String innerName,
ProcessingEnvironment env, ObfuscationMapper mapper) {
public static <T extends Annotation> ClassContainer from(T ann, Function<T, Class<?>> classFunction, String innerName, ProcessorOptions options) {
String fqn;
String[] inner;
fqn = getTypeFromAnnotation(ann, classFunction, env).toString();
fqn = getTypeFromAnnotation(ann, classFunction, options.env).toString();
inner = innerName.equals("") ? null : innerName.split("//$");
return new ClassContainer(fqn, inner, env, mapper);
return new ClassContainer(fqn, inner, options);
}
/**
* Safely extracts a {@link Class} from an annotation and gets its fully qualified name.
* @param cl the {@link TypeElement} representing the class
* @param env the {@link ProcessingEnvironment} to be used to locate the class
* @param mapper the {@link ObfuscationMapper} to be used, may be null
* @param <T> the type of the annotation carrying the information
* @param options the {@link ProcessorOptions} to be used
* @return the fully qualified name of the given class
* @since 0.6.0
*/
public static <T extends Annotation> ClassContainer from(
TypeElement cl, ProcessingEnvironment env, ObfuscationMapper mapper) {
return new ClassContainer(cl.getQualifiedName().toString(), null, env, mapper);
public static ClassContainer from(TypeElement cl, ProcessorOptions options) {
return new ClassContainer(cl.getQualifiedName().toString(), null, options);
}
/**
@ -132,16 +124,13 @@ public class ClassContainer {
* @param fallback the {@link ClassContainer} it falls back on
* @param p the {@link Patch} annotation to get info from
* @param f the {@link Find} annotation to get info from
* @param env the {@link ProcessingEnvironment} to perform the operation in
* @param mapper the {@link ObfuscationMapper} to use, may be null
* @param options the {@link ProcessorOptions} to be used
* @return the built {@link ClassContainer} or the fallback if not enough information was present
* @since 0.5.0
*/
public static ClassContainer findOrFallback(ClassContainer fallback, Patch p, Find f, ProcessingEnvironment env, ObfuscationMapper mapper) {
if(f == null) return ClassContainer.from(p, Patch::value, p.innerName(), env, mapper);
ClassContainer cl = ClassContainer.from(f, Find::value, f.innerName(), env, mapper);
return cl.fqn.equals("java.lang.Object")
? fallback
: cl;
public static ClassContainer findOrFallback(ClassContainer fallback, Patch p, Find f, ProcessorOptions options) {
if(f == null) return ClassContainer.from(p, Patch::value, p.innerName(), options);
ClassContainer cl = ClassContainer.from(f, Find::value, f.innerName(), options);
return cl.fqn.equals("java.lang.Object") ? fallback : cl;
}
}

View file

@ -3,17 +3,15 @@ package ftbsc.lll.processor.tools.containers;
import ftbsc.lll.exceptions.AmbiguousDefinitionException;
import ftbsc.lll.processor.annotations.Find;
import ftbsc.lll.processor.annotations.Patch;
import ftbsc.lll.processor.tools.obfuscation.ObfuscationMapper;
import ftbsc.lll.processor.tools.ProcessorOptions;
import org.objectweb.asm.Type;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import static ftbsc.lll.processor.tools.ASTUtils.*;
import static ftbsc.lll.processor.tools.ASTUtils.descriptorFromType;
/**
* Container for information about a field.
@ -56,16 +54,13 @@ public class FieldContainer {
public final VariableElement elem;
/**
* Private constructor, called from {@link #from(VariableElement, ProcessingEnvironment, ObfuscationMapper)}.
* Private constructor, called from {@link #from(VariableElement, ProcessorOptions)}.
* @param parent the {@link ClassContainer} representing the parent
* @param name the fully-qualified name of the target field
* @param descriptor the descriptor of the target field, may be null for verifiable fields
* @param env the {@link ProcessingEnvironment} to perform the operation in
* @param mapper the {@link ObfuscationMapper} to be used, may be null
* @param options the {@link ProcessorOptions} to be used
*/
private FieldContainer(
ClassContainer parent, String name, String descriptor,
ProcessingEnvironment env, ObfuscationMapper mapper) {
private FieldContainer(ClassContainer parent, String name, String descriptor, ProcessorOptions options) {
this.parent = parent;
if(parent.elem == null) { //unverified
if(descriptor == null)
@ -74,47 +69,45 @@ public class FieldContainer {
this.name = name;
this.descriptor = descriptor;
} else {
this.elem = (VariableElement) findMember(parent, name, descriptor, descriptor != null, true, env);
this.elem = (VariableElement) findMember(parent, name, descriptor, descriptor != null, true, options.env);
this.name = this.elem.getSimpleName().toString();
this.descriptor = descriptorFromType(this.elem.asType(), env);
this.descriptor = descriptorFromType(this.elem.asType(), options.env);
}
this.descriptorObf = mapper == null ? this.descriptor : mapper.obfuscateType(Type.getType(this.descriptor)).getDescriptor();
this.nameObf = findMemberName(parent.fqn, this.name, null, mapper);
this.descriptorObf = options.mapper == null ? this.descriptor : options.mapper.obfuscateType(Type.getType(this.descriptor)).getDescriptor();
this.nameObf = findMemberName(parent.fqn, this.name, null, options.mapper);
}
/**
* Finds a {@link FieldContainer} from a finder.
* @param finder the {@link VariableElement} annotated with {@link Find} for this field
* @param env the {@link ProcessingEnvironment} to perform the operation in
* @param mapper the {@link ObfuscationMapper} to be used, may be null
* @param options the {@link ProcessorOptions} to be used
* @return the built {@link FieldContainer}
* @since 0.5.0
*/
public static FieldContainer from(VariableElement finder, ProcessingEnvironment env, ObfuscationMapper mapper) {
public static FieldContainer from(VariableElement finder, ProcessorOptions options) {
//the parent always has a @Patch annotation
Patch patchAnn = finder.getEnclosingElement().getAnnotation(Patch.class);
//the finder always has a @Find annotation
Find f = finder.getAnnotation(Find.class);
ClassContainer parent = ClassContainer.findOrFallback(
ClassContainer.from((TypeElement) finder.getEnclosingElement(), env, mapper),
patchAnn, f, env, mapper
ClassContainer.from((TypeElement) finder.getEnclosingElement(), options), patchAnn, f, options
);
String name = f.name().equals("") ? finder.getSimpleName().toString() : f.name();
String descriptor;
TypeMirror fieldType = getTypeFromAnnotation(f, Find::type, env);
TypeMirror fieldType = getTypeFromAnnotation(f, Find::type, options.env);
if(fieldType.toString().equals("java.lang.Object")) {
descriptor = null;
} else {
if(fieldType.getKind() != TypeKind.VOID && !fieldType.getKind().isPrimitive())
descriptor = //jank af but this is temporary anyway
"L" + ClassContainer.from(
f, Find::type, f.typeInner(), env, mapper
f, Find::type, f.typeInner(), options
).fqnObf.replace('.', '/') + ";";
else descriptor = descriptorFromType(fieldType, env);
else descriptor = descriptorFromType(fieldType, options.env);
}
return new FieldContainer(parent, name, descriptor, env, mapper);
return new FieldContainer(parent, name, descriptor, options);
}
}

View file

@ -2,9 +2,8 @@ package ftbsc.lll.processor.tools.containers;
import ftbsc.lll.processor.annotations.Injector;
import ftbsc.lll.processor.annotations.Target;
import ftbsc.lll.processor.tools.obfuscation.ObfuscationMapper;
import ftbsc.lll.processor.tools.ProcessorOptions;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.ExecutableElement;
/**
@ -36,13 +35,12 @@ public class InjectorInfo {
* @param injector the injector {@link ExecutableElement}
* @param targetStub the target {@link ExecutableElement}
* @param targetAnn the relevant {@link Target} annotation
* @param env the {@link ProcessingEnvironment} to be used to locate the class
* @param mapper the {@link ObfuscationMapper} to be used, may be null
* @param options the {@link ProcessorOptions} to be used
*/
public InjectorInfo(ExecutableElement injector, ExecutableElement targetStub, Target targetAnn, ProcessingEnvironment env, ObfuscationMapper mapper) {
public InjectorInfo(ExecutableElement injector, ExecutableElement targetStub, Target targetAnn, ProcessorOptions options) {
this.injector = injector;
this.targetStub = targetStub;
this.reason = injector.getAnnotation(Injector.class).reason();
this.target = MethodContainer.from(targetStub, targetAnn, null, env, mapper);
this.target = MethodContainer.from(targetStub, targetAnn, null, options);
}
}

View file

@ -5,9 +5,8 @@ import ftbsc.lll.exceptions.TargetNotFoundException;
import ftbsc.lll.processor.annotations.Find;
import ftbsc.lll.processor.annotations.Patch;
import ftbsc.lll.processor.annotations.Target;
import ftbsc.lll.processor.tools.obfuscation.ObfuscationMapper;
import ftbsc.lll.processor.tools.ProcessorOptions;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
@ -55,18 +54,15 @@ public class MethodContainer {
/**
* Private constructor, called from
* {@link #from(ExecutableElement, Target, Find, ProcessingEnvironment, ObfuscationMapper)}.
* {@link #from(ExecutableElement, Target, Find, ProcessorOptions)}.
* @param parent the {@link ClassContainer} representing the parent
* @param name the fully-qualified name of the target method
* @param descriptor the descriptor of the target method
* @param strict whether the matching should be strict (see {@link Target#strict()} for more info)
* @param bridge whether the "bridge" should be matched isntead (see {@link Target#bridge()} for more info)
* @param env the {@link ProcessingEnvironment} to perform the operation in
* @param mapper the {@link ObfuscationMapper} to be used, may be null
* @param bridge whether the "bridge" should be matched instead (see {@link Target#bridge()} for more info)
* @param options the {@link ProcessorOptions} to be used
*/
private MethodContainer(
ClassContainer parent, String name, String descriptor, boolean strict,
boolean bridge, ProcessingEnvironment env, ObfuscationMapper mapper) {
private MethodContainer(ClassContainer parent, String name, String descriptor, boolean strict, boolean bridge, ProcessorOptions options) {
this.parent = parent;
if(parent.elem == null) { //unverified
if(descriptor == null)
@ -76,14 +72,14 @@ public class MethodContainer {
this.descriptor = descriptor;
} else {
ExecutableElement tmp = (ExecutableElement) findMember(
parent, name, descriptor, descriptor != null && strict,false, env
parent, name, descriptor, descriptor != null && strict,false, options.env
);
this.elem = bridge ? findSyntheticBridge((TypeElement) this.parent.elem, tmp, env) : tmp;
this.elem = bridge ? findSyntheticBridge((TypeElement) this.parent.elem, tmp, options.env) : tmp;
this.name = this.elem.getSimpleName().toString();
this.descriptor = descriptorFromExecutableElement(this.elem, env);
this.descriptor = descriptorFromExecutableElement(this.elem, options.env);
}
this.descriptorObf = mapper == null ? this.descriptor : mapper.obfuscateMethodDescriptor(this.descriptor);
this.nameObf = findMemberName(parent.fqn, this.name, this.descriptor, mapper);
this.descriptorObf = options.mapper == null ? this.descriptor : options.mapper.obfuscateMethodDescriptor(this.descriptor);
this.nameObf = findMemberName(parent.fqn, this.name, this.descriptor, options.mapper);
}
/**
@ -91,27 +87,25 @@ public class MethodContainer {
* @param stub the {@link ExecutableElement} for the stub
* @param t the {@link Target} annotation relevant to this case
* @param f the {@link Find} annotation containing fallback data, may be null
* @param env the {@link ProcessingEnvironment} to perform the operation in
* @param mapper the {@link ObfuscationMapper} to be used, may be null
* @param options the {@link ProcessorOptions} to be used
* @return the {@link MethodContainer} 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 MethodContainer from(ExecutableElement stub, Target t, Find f, ProcessingEnvironment env, ObfuscationMapper mapper) {
public static MethodContainer from(ExecutableElement stub, Target t, Find f, ProcessorOptions options) {
//the parent always has a @Patch annotation
Patch patchAnn = stub.getEnclosingElement().getAnnotation(Patch.class);
ClassContainer parent = ClassContainer.findOrFallback(
ClassContainer.from((TypeElement) stub.getEnclosingElement(), env, mapper),
patchAnn, f, env, mapper
ClassContainer.from((TypeElement) stub.getEnclosingElement(), options), patchAnn, f, options
);
String name = !t.methodName().equals("")
? t.methodName() //name was specified in target
: stub.getSimpleName().toString();
String descriptor = t.strict()
? descriptorFromExecutableElement(stub, env)
? descriptorFromExecutableElement(stub, options.env)
: null;
return new MethodContainer(parent, name, descriptor, t.strict(), t.bridge(), env, mapper);
return new MethodContainer(parent, name, descriptor, t.strict(), t.bridge(), options);
}
}