fix: override packageprivate, minor fixes in ObfuscationMapper

This commit is contained in:
zaaarf 2023-03-15 19:44:36 +01:00
parent f4b43e77be
commit d93f9388e8
No known key found for this signature in database
GPG key ID: 82240E075E31FA4C
3 changed files with 45 additions and 7 deletions

View file

@ -16,6 +16,7 @@ import javax.lang.model.SourceVersion;
import javax.lang.model.element.*; import javax.lang.model.element.*;
import javax.lang.model.type.ExecutableType; import javax.lang.model.type.ExecutableType;
import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeMirror;
import javax.lang.model.type.TypeVariable;
import javax.tools.Diagnostic; import javax.tools.Diagnostic;
import javax.tools.FileObject; import javax.tools.FileObject;
import javax.tools.JavaFileObject; import javax.tools.JavaFileObject;
@ -153,7 +154,11 @@ public class LilleroProcessor extends AbstractProcessor {
* @since 0.3.0 * @since 0.3.0
*/ */
private static String findClassName(String name, ObfuscationMapper mapper) { private static String findClassName(String name, ObfuscationMapper mapper) {
return mapper == null ? name : mapper.obfuscateClass(name).replace('/', '.'); try {
return mapper == null ? name : mapper.obfuscateClass(name).replace('/', '.');
} catch(MappingNotFoundException e) {
return name;
}
} }
/** /**
@ -192,7 +197,11 @@ public class LilleroProcessor extends AbstractProcessor {
* @since 0.3.0 * @since 0.3.0
*/ */
private static String findMemberName(String parentFQN, String memberName, String methodDescriptor, ObfuscationMapper mapper) { private static String findMemberName(String parentFQN, String memberName, String methodDescriptor, ObfuscationMapper mapper) {
return mapper == null ? memberName : mapper.obfuscateMember(parentFQN, memberName, methodDescriptor); try {
return mapper == null ? memberName : mapper.obfuscateMember(parentFQN, memberName, methodDescriptor);
} catch(MappingNotFoundException e) {
return memberName;
}
} }
/** /**
@ -423,7 +432,7 @@ public class LilleroProcessor extends AbstractProcessor {
String targetMethodDescriptor = descriptorFromExecutableElement(toGenerate.get(injName).target); String targetMethodDescriptor = descriptorFromExecutableElement(toGenerate.get(injName).target);
String targetMethodName = findMemberName(targetClassFQN, toGenerate.get(injName).target.getSimpleName().toString(), targetMethodDescriptor, this.mapper); String targetMethodName = findMemberName(targetClassFQN, toGenerate.get(injName).target.getSimpleName().toString(), targetMethodDescriptor, this.mapper);
MethodSpec stubOverride = MethodSpec.overriding(toGenerate.get(injName).target) MethodSpec stubOverride = unsafeOverriding(toGenerate.get(injName).target)
.addStatement("throw new $T($S)", RuntimeException.class, "This is a stub and should not have been called") .addStatement("throw new $T($S)", RuntimeException.class, "This is a stub and should not have been called")
.build(); .build();
@ -487,6 +496,35 @@ public class LilleroProcessor extends AbstractProcessor {
.build(); .build();
} }
/**
* Identical to {@link MethodSpec#overriding(ExecutableElement)}, but does not check for
* the accessor level of the target method. May be used to override package-private methods
* that are technically classified as private, but may be accessed under certain conditions.
* @param target the method to override
* @return the built method spec
* @since 0.3.0
* @see MethodSpec#overriding(ExecutableElement)
*/
private static MethodSpec.Builder unsafeOverriding(ExecutableElement target) {
MethodSpec.Builder bd =
MethodSpec.methodBuilder(target.getSimpleName().toString())
.addAnnotation(Override.class)
.addModifiers(target.getModifiers())
.returns(TypeName.get(target.getReturnType()));
bd.modifiers.remove(Modifier.DEFAULT);
bd.modifiers.remove(Modifier.ABSTRACT);
for (TypeParameterElement typeParameterElement : target.getTypeParameters()) {
TypeVariable var = (TypeVariable) typeParameterElement.asType();
bd.addTypeVariable(TypeVariableName.get(var));
}
for(VariableElement p : target.getParameters())
bd.addParameter(TypeName.get(p.asType()), p.getSimpleName().toString(), p.getModifiers().toArray(new Modifier[0]));
bd.varargs(target.isVarArgs());
for(TypeMirror thrownType : target.getThrownTypes())
bd.addException(TypeName.get(thrownType));
return bd;
}
/** /**
* Finds any method annotated with {@link FindMethod} or {@link FindField} within the given * Finds any method annotated with {@link FindMethod} or {@link FindField} within the given
* class, and builds the {@link MethodSpec} necessary for building it. * class, and builds the {@link MethodSpec} necessary for building it.
@ -502,7 +540,7 @@ public class LilleroProcessor extends AbstractProcessor {
.filter(m -> !m.getModifiers().contains(Modifier.FINAL)) //in case someone is trying to be funny .filter(m -> !m.getModifiers().contains(Modifier.FINAL)) //in case someone is trying to be funny
.forEach(m -> { .forEach(m -> {
ExecutableElement targetMethod = findRealMethodFromStub(m); ExecutableElement targetMethod = findRealMethodFromStub(m);
MethodSpec.Builder b = MethodSpec.overriding(m); MethodSpec.Builder b = unsafeOverriding(m);
String targetParentFQN = findClassName(((TypeElement) targetMethod.getEnclosingElement()).getQualifiedName().toString(), mapper); String targetParentFQN = findClassName(((TypeElement) targetMethod.getEnclosingElement()).getQualifiedName().toString(), mapper);
@ -531,7 +569,7 @@ public class LilleroProcessor extends AbstractProcessor {
.filter(m -> !m.getModifiers().contains(Modifier.FINAL)) .filter(m -> !m.getModifiers().contains(Modifier.FINAL))
.forEach(m -> { .forEach(m -> {
VariableElement targetField = findField(m); VariableElement targetField = findField(m);
MethodSpec.Builder b = MethodSpec.overriding(m); MethodSpec.Builder b = unsafeOverriding(m);
String targetParentFQN = findClassName(((TypeElement) targetField.getEnclosingElement()).getQualifiedName().toString(), mapper); String targetParentFQN = findClassName(((TypeElement) targetField.getEnclosingElement()).getQualifiedName().toString(), mapper);

View file

@ -21,7 +21,7 @@ public @interface Target {
* When set to false, tells the processor to first try to match a single method by name, * When set to false, tells the processor to first try to match a single method by name,
* and to only check parameters if further clarification is needed. * and to only check parameters if further clarification is needed.
* @implNote While non-strict mode is more computationally efficient, it's ultimately not * @implNote While non-strict mode is more computationally efficient, it's ultimately not
* relevant, as it only matters at compile time. Do not set this to true unless * relevant, as it only matters at compile time. Do not set this to false unless
* you are sure know what you're doing. * you are sure know what you're doing.
* @since 0.3.0 * @since 0.3.0
*/ */

View file

@ -183,7 +183,7 @@ public class ObfuscationMapper {
public String get(String memberName, String methodDescriptor) { public String get(String memberName, String methodDescriptor) {
if(methodDescriptor == null) if(methodDescriptor == null)
return members.get(memberName); return members.get(memberName);
List<String> candidates = members.values().stream().filter(m -> m.startsWith(memberName)).collect(Collectors.toList()); List<String> candidates = members.keySet().stream().filter(m -> m.startsWith(memberName)).collect(Collectors.toList());
if(candidates.size() == 1) if(candidates.size() == 1)
return members.get(candidates.get(0)); return members.get(candidates.get(0));
String signature = memberName + " " + methodDescriptor; String signature = memberName + " " + methodDescriptor;