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.obfuscation.ObfuscationMapper;
import ftbsc.lll.proxies.ProxyType;
import ftbsc.lll.tools.DescriptorBuilder;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.*;
import javax.lang.model.type.*;
import javax.tools.Diagnostic;
import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.List;
@ -114,15 +114,15 @@ public class ASTUtils {
}
/**
* Gets the internal name from an {@link Element}.
* @param elem the {@link Element} in question
* Gets the internal name from an {@link TypeMirror}.
* @param type the {@link TypeMirror} in question
* @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
* @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
elem = env.getElementUtils().getTypeElement(elem.asType().toString().split("<")[0]);
Element elem = env.getElementUtils().getTypeElement(type.toString().split("<")[0]);
StringBuilder fqnBuilder = new StringBuilder();
while(elem.getEnclosingElement() != null && elem.getEnclosingElement().getKind() != ElementKind.PACKAGE) {
fqnBuilder
@ -130,33 +130,10 @@ public class ASTUtils {
.insert(0, "$");
elem = elem.getEnclosingElement();
}
return fqnBuilder.insert(0, elem.asType().toString().split("<")[0]).toString();
}
/**
* 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);
return fqnBuilder
.insert(0, elem.asType().toString().split("<")[0])
.toString()
.replace('.', '/');
}
/**
@ -177,37 +154,40 @@ public class ASTUtils {
t = ((TypeVariable) t).getUpperBound();
if(t.getKind() == TypeKind.DECLARED)
return descriptorFromElement(env.getTypeUtils().asElement(t), env);
switch(t.getKind()) {
case BOOLEAN:
desc.append("Z");
break;
case CHAR:
desc.append("C");
break;
case BYTE:
desc.append("B");
break;
case SHORT:
desc.append("S");
break;
case INT:
desc.append("I");
break;
case FLOAT:
desc.append("F");
break;
case LONG:
desc.append("J");
break;
case DOUBLE:
desc.append("D");
break;
case VOID:
desc.append("V");
break;
desc
.append("L")
.append(internalNameFromType(t, env))
.append(";");
else {
switch(t.getKind()) {
case BOOLEAN:
desc.append("Z");
break;
case CHAR:
desc.append("C");
break;
case BYTE:
desc.append("B");
break;
case SHORT:
desc.append("S");
break;
case INT:
desc.append("I");
break;
case FLOAT:
desc.append("F");
break;
case LONG:
desc.append("J");
break;
case DOUBLE:
desc.append("D");
break;
case VOID:
desc.append("V");
break;
}
}
return desc.toString();
@ -299,7 +279,7 @@ public class ASTUtils {
if(field) {
//fields can verify the signature for extra safety
//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);
} else {
candidates = candidates.stream()

View file

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

View file

@ -75,7 +75,7 @@ public class FieldContainer {
} else {
this.elem = (VariableElement) findMember(parent, name, descriptor, descriptor != null, true, env);
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.nameObf = findMemberName(parent.fqn, this.name, null, mapper);