fix: eliminated redundant code, fixed bug with arrays in descriptors being sometimes ignored

This commit is contained in:
zaaarf 2023-04-11 22:14:08 +02:00
parent 673b5064e7
commit 415f1eef8b
No known key found for this signature in database
GPG key ID: 82240E075E31FA4C
3 changed files with 48 additions and 66 deletions

View file

@ -8,11 +8,11 @@ import ftbsc.lll.processor.annotations.Target;
import ftbsc.lll.processor.tools.containers.ClassContainer; import ftbsc.lll.processor.tools.containers.ClassContainer;
import ftbsc.lll.processor.tools.obfuscation.ObfuscationMapper; import ftbsc.lll.processor.tools.obfuscation.ObfuscationMapper;
import ftbsc.lll.proxies.ProxyType; import ftbsc.lll.proxies.ProxyType;
import ftbsc.lll.tools.DescriptorBuilder;
import javax.annotation.processing.ProcessingEnvironment; import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.*; import javax.lang.model.element.*;
import javax.lang.model.type.*; import javax.lang.model.type.*;
import javax.tools.Diagnostic;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -114,15 +114,15 @@ public class ASTUtils {
} }
/** /**
* Gets the internal name from an {@link Element}. * Gets the internal name from an {@link TypeMirror}.
* @param elem the {@link Element} in question * @param type the {@link TypeMirror} in question
* @param env the {@link ProcessingEnvironment} to perform the operation in * @param env the {@link ProcessingEnvironment} to perform the operation in
* @return the internal name at compile time, or null if it wasn't a qualifiable * @return the internal name at compile time, or null if it wasn't a qualifiable
* @since 0.5.1 * @since 0.5.1
*/ */
public static String internalNameFromElement(Element elem, ProcessingEnvironment env) { public static String internalNameFromType(TypeMirror type, ProcessingEnvironment env) {
//needed to actually turn elem into a TypeVariable, find it ignoring generics //needed to actually turn elem into a TypeVariable, find it ignoring generics
elem = env.getElementUtils().getTypeElement(elem.asType().toString().split("<")[0]); Element elem = env.getElementUtils().getTypeElement(type.toString().split("<")[0]);
StringBuilder fqnBuilder = new StringBuilder(); StringBuilder fqnBuilder = new StringBuilder();
while(elem.getEnclosingElement() != null && elem.getEnclosingElement().getKind() != ElementKind.PACKAGE) { while(elem.getEnclosingElement() != null && elem.getEnclosingElement().getKind() != ElementKind.PACKAGE) {
fqnBuilder fqnBuilder
@ -130,33 +130,10 @@ public class ASTUtils {
.insert(0, "$"); .insert(0, "$");
elem = elem.getEnclosingElement(); elem = elem.getEnclosingElement();
} }
return fqnBuilder.insert(0, elem.asType().toString().split("<")[0]).toString(); return fqnBuilder
} .insert(0, elem.asType().toString().split("<")[0])
.toString()
/** .replace('.', '/');
* Gets the descriptor from an {@link Element}.
* In particular, it will ensure to conver the internal class name to the
* one used at compile time
* @param elem the {@link Element} in question
* @param env the {@link ProcessingEnvironment} to perform the operation in
* @return the descriptor
* @since 0.5.1
*/
public static String descriptorFromElement(Element elem, ProcessingEnvironment env) {
if(elem instanceof ExecutableElement)
return descriptorFromExecutableElement((ExecutableElement) elem, env);
TypeMirror tk = elem.asType();
int arrayLevel = 0;
while(tk.getKind() == TypeKind.ARRAY) {
tk = ((ArrayType) tk).getComponentType();
arrayLevel++;
}
if(tk.getKind() != TypeKind.DECLARED)
return descriptorFromType(elem.asType(), env);
return DescriptorBuilder.nameToDescriptor(internalNameFromElement(elem, env), arrayLevel);
} }
/** /**
@ -177,37 +154,40 @@ public class ASTUtils {
t = ((TypeVariable) t).getUpperBound(); t = ((TypeVariable) t).getUpperBound();
if(t.getKind() == TypeKind.DECLARED) if(t.getKind() == TypeKind.DECLARED)
return descriptorFromElement(env.getTypeUtils().asElement(t), env); desc
.append("L")
.append(internalNameFromType(t, env))
switch(t.getKind()) { .append(";");
case BOOLEAN: else {
desc.append("Z"); switch(t.getKind()) {
break; case BOOLEAN:
case CHAR: desc.append("Z");
desc.append("C"); break;
break; case CHAR:
case BYTE: desc.append("C");
desc.append("B"); break;
break; case BYTE:
case SHORT: desc.append("B");
desc.append("S"); break;
break; case SHORT:
case INT: desc.append("S");
desc.append("I"); break;
break; case INT:
case FLOAT: desc.append("I");
desc.append("F"); break;
break; case FLOAT:
case LONG: desc.append("F");
desc.append("J"); break;
break; case LONG:
case DOUBLE: desc.append("J");
desc.append("D"); break;
break; case DOUBLE:
case VOID: desc.append("D");
desc.append("V"); break;
break; case VOID:
desc.append("V");
break;
}
} }
return desc.toString(); return desc.toString();
@ -299,7 +279,7 @@ public class ASTUtils {
if(field) { if(field) {
//fields can verify the signature for extra safety //fields can verify the signature for extra safety
//but there can only be 1 field with a given name //but there can only be 1 field with a given name
if(!descriptorFromElement(candidates.get(0), env).equals(descr)) if(!descriptorFromType(candidates.get(0).asType(), env).equals(descr))
throw new TargetNotFoundException("field", String.format("%s with descriptor %s", name, descr), parent.fqn); throw new TargetNotFoundException("field", String.format("%s with descriptor %s", name, descr), parent.fqn);
} else { } else {
candidates = candidates.stream() candidates = candidates.stream()

View file

@ -53,7 +53,9 @@ public class ClassContainer {
if(elem == null) if(elem == null)
throw new TargetNotFoundException("class", fqn); throw new TargetNotFoundException("class", fqn);
StringBuilder fqnBuilder = new StringBuilder(internalNameFromElement(elem, env)); StringBuilder fqnBuilder = new StringBuilder(
internalNameFromType(elem.asType(), env).replace('/', '.')
);
if(innerNames != null) { if(innerNames != null) {
for(String inner : innerNames) { for(String inner : innerNames) {

View file

@ -75,7 +75,7 @@ public class FieldContainer {
} else { } else {
this.elem = (VariableElement) findMember(parent, name, descriptor, descriptor != null, true, env); this.elem = (VariableElement) findMember(parent, name, descriptor, descriptor != null, true, env);
this.name = this.elem.getSimpleName().toString(); this.name = this.elem.getSimpleName().toString();
this.descriptor = descriptorFromElement(this.elem, env); this.descriptor = descriptorFromType(this.elem.asType(), env);
} }
this.descriptorObf = mapper == null ? this.descriptor : mapper.obfuscateType(Type.getType(this.descriptor)).getDescriptor(); this.descriptorObf = mapper == null ? this.descriptor : mapper.obfuscateType(Type.getType(this.descriptor)).getDescriptor();
this.nameObf = findMemberName(parent.fqn, this.name, null, mapper); this.nameObf = findMemberName(parent.fqn, this.name, null, mapper);