summaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc
diff options
context:
space:
mode:
author zaaarf <zaaarf@proton.me>2023-04-11 22:14:08 +0200
committer zaaarf <zaaarf@proton.me>2023-04-11 22:14:08 +0200
commit415f1eef8b9a564eb6c2848986865e1fc8d4a5c1 (patch)
tree7515df9eedac49490a55bf54e73bb4fcfc9e6d06 /src/main/java/ftbsc
parent673b5064e71a00791ff872e66a6a96fe2951d3d1 (diff)
fix: eliminated redundant code, fixed bug with arrays in descriptors being sometimes ignored
Diffstat (limited to 'src/main/java/ftbsc')
-rw-r--r--src/main/java/ftbsc/lll/processor/tools/ASTUtils.java108
-rw-r--r--src/main/java/ftbsc/lll/processor/tools/containers/ClassContainer.java4
-rw-r--r--src/main/java/ftbsc/lll/processor/tools/containers/FieldContainer.java2
3 files changed, 48 insertions, 66 deletions
diff --git a/src/main/java/ftbsc/lll/processor/tools/ASTUtils.java b/src/main/java/ftbsc/lll/processor/tools/ASTUtils.java
index 6e0a13e..0ada98e 100644
--- a/src/main/java/ftbsc/lll/processor/tools/ASTUtils.java
+++ b/src/main/java/ftbsc/lll/processor/tools/ASTUtils.java
@@ -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()
diff --git a/src/main/java/ftbsc/lll/processor/tools/containers/ClassContainer.java b/src/main/java/ftbsc/lll/processor/tools/containers/ClassContainer.java
index 202c30e..769651c 100644
--- a/src/main/java/ftbsc/lll/processor/tools/containers/ClassContainer.java
+++ b/src/main/java/ftbsc/lll/processor/tools/containers/ClassContainer.java
@@ -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) {
diff --git a/src/main/java/ftbsc/lll/processor/tools/containers/FieldContainer.java b/src/main/java/ftbsc/lll/processor/tools/containers/FieldContainer.java
index 0261663..f46acc7 100644
--- a/src/main/java/ftbsc/lll/processor/tools/containers/FieldContainer.java
+++ b/src/main/java/ftbsc/lll/processor/tools/containers/FieldContainer.java
@@ -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);