summaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/lll/mapper/tools/MappingUtils.java
diff options
context:
space:
mode:
author zaaarf <zaaarf@proton.me>2023-06-11 14:48:24 +0200
committer zaaarf <zaaarf@proton.me>2023-06-11 14:48:24 +0200
commit7c427316a675cbe7e81a04294781c59f2606239d (patch)
tree3a4efc720f44205db6c518ee854f03eebf215e7c /src/main/java/ftbsc/lll/mapper/tools/MappingUtils.java
feat: initial implementation, created interface and moved stuff from processor
Diffstat (limited to 'src/main/java/ftbsc/lll/mapper/tools/MappingUtils.java')
-rw-r--r--src/main/java/ftbsc/lll/mapper/tools/MappingUtils.java56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/main/java/ftbsc/lll/mapper/tools/MappingUtils.java b/src/main/java/ftbsc/lll/mapper/tools/MappingUtils.java
new file mode 100644
index 0000000..918052a
--- /dev/null
+++ b/src/main/java/ftbsc/lll/mapper/tools/MappingUtils.java
@@ -0,0 +1,56 @@
+package ftbsc.lll.mapper.tools;
+
+import ftbsc.lll.exceptions.MappingNotFoundException;
+import ftbsc.lll.mapper.IMapper;
+import ftbsc.lll.tools.DescriptorBuilder;
+import org.objectweb.asm.Type;
+
+public class MappingUtils {
+ /**
+ * Obfuscates a method descriptor, replacing its class references
+ * with their obfuscated counterparts.
+ * @param descriptor a {@link String} containing the descriptor
+ * @return the obfuscated descriptor
+ */
+ public static String obfuscateMethodDescriptor(String descriptor, IMapper mapper) {
+ Type method = Type.getMethodType(descriptor);
+ Type[] arguments = method.getArgumentTypes();
+ Type returnType = method.getReturnType();
+
+ Type[] obfArguments = new Type[arguments.length];
+ for(int i = 0; i < obfArguments.length; i++)
+ obfArguments[i] = obfuscateType(arguments[i], mapper);
+
+ return Type.getMethodDescriptor(obfuscateType(returnType, mapper), obfArguments);
+ }
+
+ /**
+ * Given a {@link Type} and a valid {@link IMapper} it returns its obfuscated
+ * counterpart.
+ * @param type the type in question
+ * @return the obfuscated type
+ */
+ public static Type obfuscateType(Type type, IMapper mapper) {
+ //unwrap arrays
+ Type unwrapped = type;
+ int arrayLevel = 0;
+ while(unwrapped.getSort() == Type.ARRAY) {
+ unwrapped = unwrapped.getElementType();
+ arrayLevel++;
+ }
+
+ //if it's a primitive no operation is needed
+ if(type.getSort() < Type.ARRAY)
+ return type;
+
+ String internalName = type.getInternalName();
+
+ String internalNameObf;
+ try {
+ internalNameObf = mapper.obfuscateClass(internalName);
+ return Type.getType(DescriptorBuilder.nameToDescriptor(internalNameObf, arrayLevel));
+ } catch(MappingNotFoundException e) {
+ return type;
+ }
+ }
+}