diff options
| author | 2020-06-03 13:39:42 -0400 | |
|---|---|---|
| committer | 2020-06-03 18:39:42 +0100 | |
| commit | 0f47403d0220757fed189b76e2071e25b1025cb8 (patch) | |
| tree | 879bf72c4476f0a5e0d82da99d7ff2b2276bcaca /src/main/java/cuchaz/enigma/bytecode | |
| parent | Fix search dialog hanging for a short time sometimes (#250) (diff) | |
| download | enigma-fork-0f47403d0220757fed189b76e2071e25b1025cb8.tar.gz enigma-fork-0f47403d0220757fed189b76e2071e25b1025cb8.tar.xz enigma-fork-0f47403d0220757fed189b76e2071e25b1025cb8.zip | |
Split GUI code to separate module (#242)
* Split into modules
* Post merge compile fixes
Co-authored-by: modmuss50 <modmuss50@gmail.com>
Diffstat (limited to 'src/main/java/cuchaz/enigma/bytecode')
8 files changed, 0 insertions, 671 deletions
diff --git a/src/main/java/cuchaz/enigma/bytecode/translators/AsmObjectTranslator.java b/src/main/java/cuchaz/enigma/bytecode/translators/AsmObjectTranslator.java deleted file mode 100644 index 1a2b47f..0000000 --- a/src/main/java/cuchaz/enigma/bytecode/translators/AsmObjectTranslator.java +++ /dev/null | |||
| @@ -1,46 +0,0 @@ | |||
| 1 | package cuchaz.enigma.bytecode.translators; | ||
| 2 | |||
| 3 | import cuchaz.enigma.translation.Translator; | ||
| 4 | import cuchaz.enigma.translation.representation.MethodDescriptor; | ||
| 5 | import cuchaz.enigma.translation.representation.TypeDescriptor; | ||
| 6 | import cuchaz.enigma.translation.representation.entry.ClassEntry; | ||
| 7 | import cuchaz.enigma.translation.representation.entry.MethodEntry; | ||
| 8 | import org.objectweb.asm.Handle; | ||
| 9 | import org.objectweb.asm.Type; | ||
| 10 | |||
| 11 | public class AsmObjectTranslator { | ||
| 12 | public static Type translateType(Translator translator, Type type) { | ||
| 13 | String descString = type.getDescriptor(); | ||
| 14 | switch (type.getSort()) { | ||
| 15 | case Type.OBJECT: { | ||
| 16 | ClassEntry classEntry = new ClassEntry(type.getInternalName()); | ||
| 17 | return Type.getObjectType(translator.translate(classEntry).getFullName()); | ||
| 18 | } | ||
| 19 | case Type.ARRAY: { | ||
| 20 | TypeDescriptor descriptor = new TypeDescriptor(descString); | ||
| 21 | return Type.getType(translator.translate(descriptor).toString()); | ||
| 22 | } | ||
| 23 | case Type.METHOD: { | ||
| 24 | MethodDescriptor descriptor = new MethodDescriptor(descString); | ||
| 25 | return Type.getMethodType(translator.translate(descriptor).toString()); | ||
| 26 | } | ||
| 27 | } | ||
| 28 | return type; | ||
| 29 | } | ||
| 30 | |||
| 31 | public static Handle translateHandle(Translator translator, Handle handle) { | ||
| 32 | MethodEntry entry = new MethodEntry(new ClassEntry(handle.getOwner()), handle.getName(), new MethodDescriptor(handle.getDesc())); | ||
| 33 | MethodEntry translatedMethod = translator.translate(entry); | ||
| 34 | ClassEntry ownerClass = translatedMethod.getParent(); | ||
| 35 | return new Handle(handle.getTag(), ownerClass.getFullName(), translatedMethod.getName(), translatedMethod.getDesc().toString(), handle.isInterface()); | ||
| 36 | } | ||
| 37 | |||
| 38 | public static Object translateValue(Translator translator, Object value) { | ||
| 39 | if (value instanceof Type) { | ||
| 40 | return translateType(translator, (Type) value); | ||
| 41 | } else if (value instanceof Handle) { | ||
| 42 | return translateHandle(translator, (Handle) value); | ||
| 43 | } | ||
| 44 | return value; | ||
| 45 | } | ||
| 46 | } | ||
diff --git a/src/main/java/cuchaz/enigma/bytecode/translators/LocalVariableFixVisitor.java b/src/main/java/cuchaz/enigma/bytecode/translators/LocalVariableFixVisitor.java deleted file mode 100644 index cfd8fbe..0000000 --- a/src/main/java/cuchaz/enigma/bytecode/translators/LocalVariableFixVisitor.java +++ /dev/null | |||
| @@ -1,126 +0,0 @@ | |||
| 1 | package cuchaz.enigma.bytecode.translators; | ||
| 2 | |||
| 3 | import com.google.common.base.CharMatcher; | ||
| 4 | import cuchaz.enigma.translation.LocalNameGenerator; | ||
| 5 | import cuchaz.enigma.translation.representation.TypeDescriptor; | ||
| 6 | import cuchaz.enigma.translation.representation.entry.ClassDefEntry; | ||
| 7 | import cuchaz.enigma.translation.representation.entry.MethodDefEntry; | ||
| 8 | import org.objectweb.asm.ClassVisitor; | ||
| 9 | import org.objectweb.asm.Label; | ||
| 10 | import org.objectweb.asm.MethodVisitor; | ||
| 11 | import org.objectweb.asm.Opcodes; | ||
| 12 | |||
| 13 | import java.util.HashMap; | ||
| 14 | import java.util.List; | ||
| 15 | import java.util.Map; | ||
| 16 | |||
| 17 | public class LocalVariableFixVisitor extends ClassVisitor { | ||
| 18 | private ClassDefEntry ownerEntry; | ||
| 19 | |||
| 20 | public LocalVariableFixVisitor(int api, ClassVisitor visitor) { | ||
| 21 | super(api, visitor); | ||
| 22 | } | ||
| 23 | |||
| 24 | @Override | ||
| 25 | public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { | ||
| 26 | ownerEntry = ClassDefEntry.parse(access, name, signature, superName, interfaces); | ||
| 27 | super.visit(version, access, name, signature, superName, interfaces); | ||
| 28 | } | ||
| 29 | |||
| 30 | @Override | ||
| 31 | public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) { | ||
| 32 | MethodDefEntry methodEntry = MethodDefEntry.parse(ownerEntry, access, name, descriptor, signature); | ||
| 33 | return new Method(api, methodEntry, super.visitMethod(access, name, descriptor, signature, exceptions)); | ||
| 34 | } | ||
| 35 | |||
| 36 | private class Method extends MethodVisitor { | ||
| 37 | private final MethodDefEntry methodEntry; | ||
| 38 | private final Map<Integer, String> parameterNames = new HashMap<>(); | ||
| 39 | private final Map<Integer, Integer> parameterIndices = new HashMap<>(); | ||
| 40 | private boolean hasParameterTable; | ||
| 41 | private int parameterIndex = 0; | ||
| 42 | |||
| 43 | Method(int api, MethodDefEntry methodEntry, MethodVisitor visitor) { | ||
| 44 | super(api, visitor); | ||
| 45 | this.methodEntry = methodEntry; | ||
| 46 | |||
| 47 | int lvIndex = methodEntry.getAccess().isStatic() ? 0 : 1; | ||
| 48 | List<TypeDescriptor> parameters = methodEntry.getDesc().getArgumentDescs(); | ||
| 49 | for (int parameterIndex = 0; parameterIndex < parameters.size(); parameterIndex++) { | ||
| 50 | TypeDescriptor param = parameters.get(parameterIndex); | ||
| 51 | parameterIndices.put(lvIndex, parameterIndex); | ||
| 52 | lvIndex += param.getSize(); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | @Override | ||
| 57 | public void visitParameter(String name, int access) { | ||
| 58 | hasParameterTable = true; | ||
| 59 | super.visitParameter(fixParameterName(parameterIndex, name), fixParameterAccess(parameterIndex, access)); | ||
| 60 | parameterIndex++; | ||
| 61 | } | ||
| 62 | |||
| 63 | @Override | ||
| 64 | public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) { | ||
| 65 | if (index == 0 && !methodEntry.getAccess().isStatic()) { | ||
| 66 | name = "this"; | ||
| 67 | } else if (parameterIndices.containsKey(index)) { | ||
| 68 | name = fixParameterName(parameterIndices.get(index), name); | ||
| 69 | } else if (isInvalidName(name)) { | ||
| 70 | name = LocalNameGenerator.generateLocalVariableName(index, new TypeDescriptor(desc)); | ||
| 71 | } | ||
| 72 | |||
| 73 | super.visitLocalVariable(name, desc, signature, start, end, index); | ||
| 74 | } | ||
| 75 | |||
| 76 | private boolean isInvalidName(String name) { | ||
| 77 | return name == null || name.isEmpty() || !CharMatcher.ascii().matchesAllOf(name); | ||
| 78 | } | ||
| 79 | |||
| 80 | @Override | ||
| 81 | public void visitEnd() { | ||
| 82 | if (!hasParameterTable) { | ||
| 83 | List<TypeDescriptor> arguments = methodEntry.getDesc().getArgumentDescs(); | ||
| 84 | for (int argumentIndex = 0; argumentIndex < arguments.size(); argumentIndex++) { | ||
| 85 | super.visitParameter(fixParameterName(argumentIndex, null), fixParameterAccess(argumentIndex, 0)); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | super.visitEnd(); | ||
| 90 | } | ||
| 91 | |||
| 92 | private String fixParameterName(int index, String name) { | ||
| 93 | if (parameterNames.get(index) != null) { | ||
| 94 | return parameterNames.get(index); // to make sure that LVT names are consistent with parameter table names | ||
| 95 | } | ||
| 96 | |||
| 97 | if (isInvalidName(name)) { | ||
| 98 | List<TypeDescriptor> arguments = methodEntry.getDesc().getArgumentDescs(); | ||
| 99 | name = LocalNameGenerator.generateArgumentName(index, arguments.get(index), arguments); | ||
| 100 | } | ||
| 101 | |||
| 102 | if (index == 0 && ownerEntry.getAccess().isEnum() && methodEntry.getName().equals("<init>")) { | ||
| 103 | name = "name"; | ||
| 104 | } | ||
| 105 | |||
| 106 | if (index == 1 && ownerEntry.getAccess().isEnum() && methodEntry.getName().equals("<init>")) { | ||
| 107 | name = "ordinal"; | ||
| 108 | } | ||
| 109 | |||
| 110 | parameterNames.put(index, name); | ||
| 111 | return name; | ||
| 112 | } | ||
| 113 | |||
| 114 | private int fixParameterAccess(int index, int access) { | ||
| 115 | if (index == 0 && ownerEntry.getAccess().isEnum() && methodEntry.getName().equals("<init>")) { | ||
| 116 | access |= Opcodes.ACC_SYNTHETIC; | ||
| 117 | } | ||
| 118 | |||
| 119 | if (index == 1 && ownerEntry.getAccess().isEnum() && methodEntry.getName().equals("<init>")) { | ||
| 120 | access |= Opcodes.ACC_SYNTHETIC; | ||
| 121 | } | ||
| 122 | |||
| 123 | return access; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | } | ||
diff --git a/src/main/java/cuchaz/enigma/bytecode/translators/SourceFixVisitor.java b/src/main/java/cuchaz/enigma/bytecode/translators/SourceFixVisitor.java deleted file mode 100644 index 2b750ea..0000000 --- a/src/main/java/cuchaz/enigma/bytecode/translators/SourceFixVisitor.java +++ /dev/null | |||
| @@ -1,39 +0,0 @@ | |||
| 1 | package cuchaz.enigma.bytecode.translators; | ||
| 2 | |||
| 3 | import cuchaz.enigma.analysis.index.BridgeMethodIndex; | ||
| 4 | import cuchaz.enigma.analysis.index.JarIndex; | ||
| 5 | import cuchaz.enigma.translation.representation.entry.ClassDefEntry; | ||
| 6 | import cuchaz.enigma.translation.representation.entry.MethodDefEntry; | ||
| 7 | import org.objectweb.asm.ClassVisitor; | ||
| 8 | import org.objectweb.asm.MethodVisitor; | ||
| 9 | import org.objectweb.asm.Opcodes; | ||
| 10 | |||
| 11 | public class SourceFixVisitor extends ClassVisitor { | ||
| 12 | private final JarIndex index; | ||
| 13 | private ClassDefEntry ownerEntry; | ||
| 14 | |||
| 15 | public SourceFixVisitor(int api, ClassVisitor visitor, JarIndex index) { | ||
| 16 | super(api, visitor); | ||
| 17 | this.index = index; | ||
| 18 | } | ||
| 19 | |||
| 20 | @Override | ||
| 21 | public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { | ||
| 22 | ownerEntry = ClassDefEntry.parse(access, name, signature, superName, interfaces); | ||
| 23 | super.visit(version, access, name, signature, superName, interfaces); | ||
| 24 | } | ||
| 25 | |||
| 26 | @Override | ||
| 27 | public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) { | ||
| 28 | MethodDefEntry methodEntry = MethodDefEntry.parse(ownerEntry, access, name, descriptor, signature); | ||
| 29 | |||
| 30 | BridgeMethodIndex bridgeIndex = index.getBridgeMethodIndex(); | ||
| 31 | if (bridgeIndex.isBridgeMethod(methodEntry)) { | ||
| 32 | access |= Opcodes.ACC_BRIDGE; | ||
| 33 | } else if (bridgeIndex.isSpecializedMethod(methodEntry)) { | ||
| 34 | name = bridgeIndex.getBridgeFromSpecialized(methodEntry).getName(); | ||
| 35 | } | ||
| 36 | |||
| 37 | return super.visitMethod(access, name, descriptor, signature, exceptions); | ||
| 38 | } | ||
| 39 | } | ||
diff --git a/src/main/java/cuchaz/enigma/bytecode/translators/TranslationAnnotationVisitor.java b/src/main/java/cuchaz/enigma/bytecode/translators/TranslationAnnotationVisitor.java deleted file mode 100644 index cb843ad..0000000 --- a/src/main/java/cuchaz/enigma/bytecode/translators/TranslationAnnotationVisitor.java +++ /dev/null | |||
| @@ -1,51 +0,0 @@ | |||
| 1 | package cuchaz.enigma.bytecode.translators; | ||
| 2 | |||
| 3 | import cuchaz.enigma.translation.Translator; | ||
| 4 | import cuchaz.enigma.translation.representation.TypeDescriptor; | ||
| 5 | import cuchaz.enigma.translation.representation.entry.ClassEntry; | ||
| 6 | import cuchaz.enigma.translation.representation.entry.FieldEntry; | ||
| 7 | import org.objectweb.asm.AnnotationVisitor; | ||
| 8 | |||
| 9 | public class TranslationAnnotationVisitor extends AnnotationVisitor { | ||
| 10 | private final Translator translator; | ||
| 11 | private final ClassEntry annotationEntry; | ||
| 12 | |||
| 13 | public TranslationAnnotationVisitor(Translator translator, ClassEntry annotationEntry, int api, AnnotationVisitor av) { | ||
| 14 | super(api, av); | ||
| 15 | this.translator = translator; | ||
| 16 | this.annotationEntry = annotationEntry; | ||
| 17 | } | ||
| 18 | |||
| 19 | @Override | ||
| 20 | public void visit(String name, Object value) { | ||
| 21 | super.visit(name, AsmObjectTranslator.translateValue(translator, value)); | ||
| 22 | } | ||
| 23 | |||
| 24 | @Override | ||
| 25 | public AnnotationVisitor visitArray(String name) { | ||
| 26 | return new TranslationAnnotationVisitor(translator, annotationEntry, api, super.visitArray(name)); | ||
| 27 | } | ||
| 28 | |||
| 29 | @Override | ||
| 30 | public AnnotationVisitor visitAnnotation(String name, String desc) { | ||
| 31 | TypeDescriptor type = new TypeDescriptor(desc); | ||
| 32 | if (name != null) { | ||
| 33 | FieldEntry annotationField = translator.translate(new FieldEntry(annotationEntry, name, type)); | ||
| 34 | return super.visitAnnotation(annotationField.getName(), annotationField.getDesc().toString()); | ||
| 35 | } else { | ||
| 36 | return super.visitAnnotation(null, translator.translate(type).toString()); | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | @Override | ||
| 41 | public void visitEnum(String name, String desc, String value) { | ||
| 42 | TypeDescriptor type = new TypeDescriptor(desc); | ||
| 43 | FieldEntry enumField = translator.translate(new FieldEntry(type.getTypeEntry(), value, type)); | ||
| 44 | if (name != null) { | ||
| 45 | FieldEntry annotationField = translator.translate(new FieldEntry(annotationEntry, name, type)); | ||
| 46 | super.visitEnum(annotationField.getName(), annotationField.getDesc().toString(), enumField.getName()); | ||
| 47 | } else { | ||
| 48 | super.visitEnum(null, translator.translate(type).toString(), enumField.getName()); | ||
| 49 | } | ||
| 50 | } | ||
| 51 | } | ||
diff --git a/src/main/java/cuchaz/enigma/bytecode/translators/TranslationClassVisitor.java b/src/main/java/cuchaz/enigma/bytecode/translators/TranslationClassVisitor.java deleted file mode 100644 index e4c41d3..0000000 --- a/src/main/java/cuchaz/enigma/bytecode/translators/TranslationClassVisitor.java +++ /dev/null | |||
| @@ -1,102 +0,0 @@ | |||
| 1 | /******************************************************************************* | ||
| 2 | * Copyright (c) 2015 Jeff Martin. | ||
| 3 | * All rights reserved. This program and the accompanying materials | ||
| 4 | * are made available under the terms of the GNU Lesser General Public | ||
| 5 | * License v3.0 which accompanies this distribution, and is available at | ||
| 6 | * http://www.gnu.org/licenses/lgpl.html | ||
| 7 | * <p> | ||
| 8 | * Contributors: | ||
| 9 | * Jeff Martin - initial API and implementation | ||
| 10 | ******************************************************************************/ | ||
| 11 | |||
| 12 | package cuchaz.enigma.bytecode.translators; | ||
| 13 | |||
| 14 | import cuchaz.enigma.translation.Translator; | ||
| 15 | import cuchaz.enigma.translation.representation.MethodDescriptor; | ||
| 16 | import cuchaz.enigma.translation.representation.TypeDescriptor; | ||
| 17 | import cuchaz.enigma.translation.representation.entry.*; | ||
| 18 | import org.objectweb.asm.*; | ||
| 19 | |||
| 20 | import java.util.Arrays; | ||
| 21 | |||
| 22 | public class TranslationClassVisitor extends ClassVisitor { | ||
| 23 | private final Translator translator; | ||
| 24 | |||
| 25 | private ClassDefEntry obfClassEntry; | ||
| 26 | |||
| 27 | public TranslationClassVisitor(Translator translator, int api, ClassVisitor cv) { | ||
| 28 | super(api, cv); | ||
| 29 | this.translator = translator; | ||
| 30 | } | ||
| 31 | |||
| 32 | @Override | ||
| 33 | public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { | ||
| 34 | obfClassEntry = ClassDefEntry.parse(access, name, signature, superName, interfaces); | ||
| 35 | |||
| 36 | ClassDefEntry translatedEntry = translator.translate(obfClassEntry); | ||
| 37 | String translatedSuper = translatedEntry.getSuperClass() != null ? translatedEntry.getSuperClass().getFullName() : null; | ||
| 38 | String[] translatedInterfaces = Arrays.stream(translatedEntry.getInterfaces()).map(ClassEntry::getFullName).toArray(String[]::new); | ||
| 39 | |||
| 40 | super.visit(version, translatedEntry.getAccess().getFlags(), translatedEntry.getFullName(), translatedEntry.getSignature().toString(), translatedSuper, translatedInterfaces); | ||
| 41 | } | ||
| 42 | |||
| 43 | @Override | ||
| 44 | public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { | ||
| 45 | FieldDefEntry entry = FieldDefEntry.parse(obfClassEntry, access, name, desc, signature); | ||
| 46 | FieldDefEntry translatedEntry = translator.translate(entry); | ||
| 47 | FieldVisitor fv = super.visitField(translatedEntry.getAccess().getFlags(), translatedEntry.getName(), translatedEntry.getDesc().toString(), translatedEntry.getSignature().toString(), value); | ||
| 48 | return new TranslationFieldVisitor(translator, translatedEntry, api, fv); | ||
| 49 | } | ||
| 50 | |||
| 51 | @Override | ||
| 52 | public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { | ||
| 53 | MethodDefEntry entry = MethodDefEntry.parse(obfClassEntry, access, name, desc, signature); | ||
| 54 | MethodDefEntry translatedEntry = translator.translate(entry); | ||
| 55 | String[] translatedExceptions = new String[exceptions.length]; | ||
| 56 | for (int i = 0; i < exceptions.length; i++) { | ||
| 57 | translatedExceptions[i] = translator.translate(new ClassEntry(exceptions[i])).getFullName(); | ||
| 58 | } | ||
| 59 | MethodVisitor mv = super.visitMethod(translatedEntry.getAccess().getFlags(), translatedEntry.getName(), translatedEntry.getDesc().toString(), translatedEntry.getSignature().toString(), translatedExceptions); | ||
| 60 | return new TranslationMethodVisitor(translator, obfClassEntry, entry, api, mv); | ||
| 61 | } | ||
| 62 | |||
| 63 | @Override | ||
| 64 | public void visitInnerClass(String name, String outerName, String innerName, int access) { | ||
| 65 | ClassDefEntry classEntry = ClassDefEntry.parse(access, name, obfClassEntry.getSignature().toString(), null, new String[0]); | ||
| 66 | ClassDefEntry translatedEntry = translator.translate(classEntry); | ||
| 67 | ClassEntry translatedOuterClass = translatedEntry.getOuterClass(); | ||
| 68 | if (translatedOuterClass == null) { | ||
| 69 | throw new IllegalStateException("Translated inner class did not have outer class"); | ||
| 70 | } | ||
| 71 | |||
| 72 | // Anonymous classes do not specify an outer or inner name. As we do not translate from the given parameter, ignore if the input is null | ||
| 73 | String translatedName = translatedEntry.getFullName(); | ||
| 74 | String translatedOuterName = outerName != null ? translatedOuterClass.getFullName() : null; | ||
| 75 | String translatedInnerName = innerName != null ? translatedEntry.getName() : null; | ||
| 76 | super.visitInnerClass(translatedName, translatedOuterName, translatedInnerName, translatedEntry.getAccess().getFlags()); | ||
| 77 | } | ||
| 78 | |||
| 79 | @Override | ||
| 80 | public void visitOuterClass(String owner, String name, String desc) { | ||
| 81 | if (desc != null) { | ||
| 82 | MethodEntry translatedEntry = translator.translate(new MethodEntry(new ClassEntry(owner), name, new MethodDescriptor(desc))); | ||
| 83 | super.visitOuterClass(translatedEntry.getParent().getFullName(), translatedEntry.getName(), translatedEntry.getDesc().toString()); | ||
| 84 | } else { | ||
| 85 | super.visitOuterClass(owner, name, desc); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | @Override | ||
| 90 | public AnnotationVisitor visitAnnotation(String desc, boolean visible) { | ||
| 91 | TypeDescriptor translatedDesc = translator.translate(new TypeDescriptor(desc)); | ||
| 92 | AnnotationVisitor av = super.visitAnnotation(translatedDesc.toString(), visible); | ||
| 93 | return new TranslationAnnotationVisitor(translator, translatedDesc.getTypeEntry(), api, av); | ||
| 94 | } | ||
| 95 | |||
| 96 | @Override | ||
| 97 | public AnnotationVisitor visitTypeAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) { | ||
| 98 | TypeDescriptor translatedDesc = translator.translate(new TypeDescriptor(desc)); | ||
| 99 | AnnotationVisitor av = super.visitTypeAnnotation(typeRef, typePath, translatedDesc.toString(), visible); | ||
| 100 | return new TranslationAnnotationVisitor(translator, translatedDesc.getTypeEntry(), api, av); | ||
| 101 | } | ||
| 102 | } | ||
diff --git a/src/main/java/cuchaz/enigma/bytecode/translators/TranslationFieldVisitor.java b/src/main/java/cuchaz/enigma/bytecode/translators/TranslationFieldVisitor.java deleted file mode 100644 index 28fc199..0000000 --- a/src/main/java/cuchaz/enigma/bytecode/translators/TranslationFieldVisitor.java +++ /dev/null | |||
| @@ -1,33 +0,0 @@ | |||
| 1 | package cuchaz.enigma.bytecode.translators; | ||
| 2 | |||
| 3 | import cuchaz.enigma.translation.Translator; | ||
| 4 | import cuchaz.enigma.translation.representation.TypeDescriptor; | ||
| 5 | import cuchaz.enigma.translation.representation.entry.FieldDefEntry; | ||
| 6 | import org.objectweb.asm.AnnotationVisitor; | ||
| 7 | import org.objectweb.asm.FieldVisitor; | ||
| 8 | import org.objectweb.asm.TypePath; | ||
| 9 | |||
| 10 | public class TranslationFieldVisitor extends FieldVisitor { | ||
| 11 | private final FieldDefEntry fieldEntry; | ||
| 12 | private final Translator translator; | ||
| 13 | |||
| 14 | public TranslationFieldVisitor(Translator translator, FieldDefEntry fieldEntry, int api, FieldVisitor fv) { | ||
| 15 | super(api, fv); | ||
| 16 | this.translator = translator; | ||
| 17 | this.fieldEntry = fieldEntry; | ||
| 18 | } | ||
| 19 | |||
| 20 | @Override | ||
| 21 | public AnnotationVisitor visitAnnotation(String desc, boolean visible) { | ||
| 22 | TypeDescriptor typeDesc = translator.translate(new TypeDescriptor(desc)); | ||
| 23 | AnnotationVisitor av = super.visitAnnotation(typeDesc.toString(), visible); | ||
| 24 | return new TranslationAnnotationVisitor(translator, typeDesc.getTypeEntry(), api, av); | ||
| 25 | } | ||
| 26 | |||
| 27 | @Override | ||
| 28 | public AnnotationVisitor visitTypeAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) { | ||
| 29 | TypeDescriptor typeDesc = translator.translate(new TypeDescriptor(desc)); | ||
| 30 | AnnotationVisitor av = super.visitAnnotation(typeDesc.toString(), visible); | ||
| 31 | return new TranslationAnnotationVisitor(translator, typeDesc.getTypeEntry(), api, av); | ||
| 32 | } | ||
| 33 | } | ||
diff --git a/src/main/java/cuchaz/enigma/bytecode/translators/TranslationMethodVisitor.java b/src/main/java/cuchaz/enigma/bytecode/translators/TranslationMethodVisitor.java deleted file mode 100644 index a82df1b..0000000 --- a/src/main/java/cuchaz/enigma/bytecode/translators/TranslationMethodVisitor.java +++ /dev/null | |||
| @@ -1,145 +0,0 @@ | |||
| 1 | package cuchaz.enigma.bytecode.translators; | ||
| 2 | |||
| 3 | import cuchaz.enigma.translation.Translator; | ||
| 4 | import cuchaz.enigma.translation.representation.MethodDescriptor; | ||
| 5 | import cuchaz.enigma.translation.representation.Signature; | ||
| 6 | import cuchaz.enigma.translation.representation.TypeDescriptor; | ||
| 7 | import cuchaz.enigma.translation.representation.entry.*; | ||
| 8 | import org.objectweb.asm.*; | ||
| 9 | |||
| 10 | public class TranslationMethodVisitor extends MethodVisitor { | ||
| 11 | private final MethodDefEntry methodEntry; | ||
| 12 | private final Translator translator; | ||
| 13 | |||
| 14 | private int parameterIndex = 0; | ||
| 15 | private int parameterLvIndex; | ||
| 16 | |||
| 17 | public TranslationMethodVisitor(Translator translator, ClassDefEntry ownerEntry, MethodDefEntry methodEntry, int api, MethodVisitor mv) { | ||
| 18 | super(api, mv); | ||
| 19 | this.translator = translator; | ||
| 20 | this.methodEntry = methodEntry; | ||
| 21 | |||
| 22 | parameterLvIndex = methodEntry.getAccess().isStatic() ? 0 : 1; | ||
| 23 | } | ||
| 24 | |||
| 25 | @Override | ||
| 26 | public void visitParameter(String name, int access) { | ||
| 27 | name = translateVariableName(parameterLvIndex, name); | ||
| 28 | parameterLvIndex += methodEntry.getDesc().getArgumentDescs().get(parameterIndex++).getSize(); | ||
| 29 | |||
| 30 | super.visitParameter(name, access); | ||
| 31 | } | ||
| 32 | |||
| 33 | @Override | ||
| 34 | public void visitFieldInsn(int opcode, String owner, String name, String desc) { | ||
| 35 | FieldEntry entry = new FieldEntry(new ClassEntry(owner), name, new TypeDescriptor(desc)); | ||
| 36 | FieldEntry translatedEntry = translator.translate(entry); | ||
| 37 | super.visitFieldInsn(opcode, translatedEntry.getParent().getFullName(), translatedEntry.getName(), translatedEntry.getDesc().toString()); | ||
| 38 | } | ||
| 39 | |||
| 40 | @Override | ||
| 41 | public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) { | ||
| 42 | MethodEntry entry = new MethodEntry(new ClassEntry(owner), name, new MethodDescriptor(desc)); | ||
| 43 | MethodEntry translatedEntry = translator.translate(entry); | ||
| 44 | super.visitMethodInsn(opcode, translatedEntry.getParent().getFullName(), translatedEntry.getName(), translatedEntry.getDesc().toString(), itf); | ||
| 45 | } | ||
| 46 | |||
| 47 | @Override | ||
| 48 | public void visitFrame(int type, int localCount, Object[] locals, int stackCount, Object[] stack) { | ||
| 49 | Object[] translatedLocals = this.getTranslatedFrame(locals, localCount); | ||
| 50 | Object[] translatedStack = this.getTranslatedFrame(stack, stackCount); | ||
| 51 | super.visitFrame(type, localCount, translatedLocals, stackCount, translatedStack); | ||
| 52 | } | ||
| 53 | |||
| 54 | private Object[] getTranslatedFrame(Object[] array, int count) { | ||
| 55 | if (array == null) { | ||
| 56 | return null; | ||
| 57 | } | ||
| 58 | for (int i = 0; i < count; i++) { | ||
| 59 | Object object = array[i]; | ||
| 60 | if (object instanceof String) { | ||
| 61 | String type = (String) object; | ||
| 62 | array[i] = translator.translate(new ClassEntry(type)).getFullName(); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | return array; | ||
| 66 | } | ||
| 67 | |||
| 68 | @Override | ||
| 69 | public AnnotationVisitor visitAnnotation(String desc, boolean visible) { | ||
| 70 | TypeDescriptor typeDesc = translator.translate(new TypeDescriptor(desc)); | ||
| 71 | AnnotationVisitor av = super.visitAnnotation(typeDesc.toString(), visible); | ||
| 72 | return new TranslationAnnotationVisitor(translator, typeDesc.getTypeEntry(), api, av); | ||
| 73 | } | ||
| 74 | |||
| 75 | @Override | ||
| 76 | public AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible) { | ||
| 77 | TypeDescriptor typeDesc = translator.translate(new TypeDescriptor(desc)); | ||
| 78 | AnnotationVisitor av = super.visitParameterAnnotation(parameter, typeDesc.toString(), visible); | ||
| 79 | return new TranslationAnnotationVisitor(translator, typeDesc.getTypeEntry(), api, av); | ||
| 80 | } | ||
| 81 | |||
| 82 | @Override | ||
| 83 | public AnnotationVisitor visitTypeAnnotation(int typeRef, TypePath typePath, String desc, boolean visible) { | ||
| 84 | TypeDescriptor typeDesc = translator.translate(new TypeDescriptor(desc)); | ||
| 85 | AnnotationVisitor av = super.visitTypeAnnotation(typeRef, typePath, typeDesc.toString(), visible); | ||
| 86 | return new TranslationAnnotationVisitor(translator, typeDesc.getTypeEntry(), api, av); | ||
| 87 | } | ||
| 88 | |||
| 89 | @Override | ||
| 90 | public void visitTypeInsn(int opcode, String type) { | ||
| 91 | ClassEntry translatedEntry = translator.translate(new ClassEntry(type)); | ||
| 92 | super.visitTypeInsn(opcode, translatedEntry.getFullName()); | ||
| 93 | } | ||
| 94 | |||
| 95 | @Override | ||
| 96 | public void visitInvokeDynamicInsn(String name, String desc, Handle bsm, Object... bsmArgs) { | ||
| 97 | MethodDescriptor translatedMethodDesc = translator.translate(new MethodDescriptor(desc)); | ||
| 98 | Object[] translatedBsmArgs = new Object[bsmArgs.length]; | ||
| 99 | for (int i = 0; i < bsmArgs.length; i++) { | ||
| 100 | translatedBsmArgs[i] = AsmObjectTranslator.translateValue(translator, bsmArgs[i]); | ||
| 101 | } | ||
| 102 | super.visitInvokeDynamicInsn(name, translatedMethodDesc.toString(), AsmObjectTranslator.translateHandle(translator, bsm), translatedBsmArgs); | ||
| 103 | } | ||
| 104 | |||
| 105 | @Override | ||
| 106 | public void visitLdcInsn(Object cst) { | ||
| 107 | super.visitLdcInsn(AsmObjectTranslator.translateValue(translator, cst)); | ||
| 108 | } | ||
| 109 | |||
| 110 | @Override | ||
| 111 | public void visitMultiANewArrayInsn(String desc, int dims) { | ||
| 112 | super.visitMultiANewArrayInsn(translator.translate(new TypeDescriptor(desc)).toString(), dims); | ||
| 113 | } | ||
| 114 | |||
| 115 | @Override | ||
| 116 | public void visitTryCatchBlock(Label start, Label end, Label handler, String type) { | ||
| 117 | if (type != null) { | ||
| 118 | ClassEntry translatedEntry = translator.translate(new ClassEntry(type)); | ||
| 119 | super.visitTryCatchBlock(start, end, handler, translatedEntry.getFullName()); | ||
| 120 | } else { | ||
| 121 | super.visitTryCatchBlock(start, end, handler, type); | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | @Override | ||
| 126 | public void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index) { | ||
| 127 | signature = translator.translate(Signature.createTypedSignature(signature)).toString(); | ||
| 128 | name = translateVariableName(index, name); | ||
| 129 | desc = translator.translate(new TypeDescriptor(desc)).toString(); | ||
| 130 | |||
| 131 | super.visitLocalVariable(name, desc, signature, start, end, index); | ||
| 132 | } | ||
| 133 | |||
| 134 | private String translateVariableName(int index, String name) { | ||
| 135 | LocalVariableEntry entry = new LocalVariableEntry(methodEntry, index, "", true,null); | ||
| 136 | LocalVariableEntry translatedEntry = translator.translate(entry); | ||
| 137 | String translatedName = translatedEntry.getName(); | ||
| 138 | |||
| 139 | if (!translatedName.isEmpty()) { | ||
| 140 | return translatedName; | ||
| 141 | } | ||
| 142 | |||
| 143 | return name; | ||
| 144 | } | ||
| 145 | } | ||
diff --git a/src/main/java/cuchaz/enigma/bytecode/translators/TranslationSignatureVisitor.java b/src/main/java/cuchaz/enigma/bytecode/translators/TranslationSignatureVisitor.java deleted file mode 100644 index eebd650..0000000 --- a/src/main/java/cuchaz/enigma/bytecode/translators/TranslationSignatureVisitor.java +++ /dev/null | |||
| @@ -1,129 +0,0 @@ | |||
| 1 | package cuchaz.enigma.bytecode.translators; | ||
| 2 | |||
| 3 | import cuchaz.enigma.utils.Utils; | ||
| 4 | import org.objectweb.asm.signature.SignatureVisitor; | ||
| 5 | |||
| 6 | import java.util.Stack; | ||
| 7 | import java.util.function.Function; | ||
| 8 | |||
| 9 | public class TranslationSignatureVisitor extends SignatureVisitor { | ||
| 10 | private final Function<String, String> remapper; | ||
| 11 | |||
| 12 | private final SignatureVisitor sv; | ||
| 13 | private final Stack<String> classStack = new Stack<>(); | ||
| 14 | |||
| 15 | public TranslationSignatureVisitor(Function<String, String> remapper, SignatureVisitor sv) { | ||
| 16 | super(Utils.ASM_VERSION); | ||
| 17 | this.remapper = remapper; | ||
| 18 | this.sv = sv; | ||
| 19 | } | ||
| 20 | |||
| 21 | @Override | ||
| 22 | public void visitClassType(String name) { | ||
| 23 | classStack.push(name); | ||
| 24 | String translatedEntry = this.remapper.apply(name); | ||
| 25 | this.sv.visitClassType(translatedEntry); | ||
| 26 | } | ||
| 27 | |||
| 28 | @Override | ||
| 29 | public void visitInnerClassType(String name) { | ||
| 30 | String lastClass = classStack.pop(); | ||
| 31 | if (!name.startsWith(lastClass+"$")){//todo see if there's a way to base this on whether there were type params or not | ||
| 32 | name = lastClass+"$"+name; | ||
| 33 | } | ||
| 34 | String translatedEntry = this.remapper.apply(name); | ||
| 35 | if (translatedEntry.contains("/")){ | ||
| 36 | translatedEntry = translatedEntry.substring(translatedEntry.lastIndexOf("/")+1); | ||
| 37 | } | ||
| 38 | if (translatedEntry.contains("$")){ | ||
| 39 | translatedEntry = translatedEntry.substring(translatedEntry.lastIndexOf("$")+1); | ||
| 40 | } | ||
| 41 | this.sv.visitInnerClassType(translatedEntry); | ||
| 42 | } | ||
| 43 | |||
| 44 | @Override | ||
| 45 | public void visitFormalTypeParameter(String name) { | ||
| 46 | this.sv.visitFormalTypeParameter(name); | ||
| 47 | } | ||
| 48 | |||
| 49 | @Override | ||
| 50 | public void visitTypeVariable(String name) { | ||
| 51 | this.sv.visitTypeVariable(name); | ||
| 52 | } | ||
| 53 | |||
| 54 | @Override | ||
| 55 | public SignatureVisitor visitArrayType() { | ||
| 56 | this.sv.visitArrayType(); | ||
| 57 | return this; | ||
| 58 | } | ||
| 59 | |||
| 60 | @Override | ||
| 61 | public void visitBaseType(char descriptor) { | ||
| 62 | this.sv.visitBaseType(descriptor); | ||
| 63 | } | ||
| 64 | |||
| 65 | @Override | ||
| 66 | public SignatureVisitor visitClassBound() { | ||
| 67 | this.sv.visitClassBound(); | ||
| 68 | return this; | ||
| 69 | } | ||
| 70 | |||
| 71 | @Override | ||
| 72 | public SignatureVisitor visitExceptionType() { | ||
| 73 | this.sv.visitExceptionType(); | ||
| 74 | return this; | ||
| 75 | } | ||
| 76 | |||
| 77 | @Override | ||
| 78 | public SignatureVisitor visitInterface() { | ||
| 79 | this.sv.visitInterface(); | ||
| 80 | return this; | ||
| 81 | } | ||
| 82 | |||
| 83 | @Override | ||
| 84 | public SignatureVisitor visitInterfaceBound() { | ||
| 85 | this.sv.visitInterfaceBound(); | ||
| 86 | return this; | ||
| 87 | } | ||
| 88 | |||
| 89 | @Override | ||
| 90 | public SignatureVisitor visitParameterType() { | ||
| 91 | this.sv.visitParameterType(); | ||
| 92 | return this; | ||
| 93 | } | ||
| 94 | |||
| 95 | @Override | ||
| 96 | public SignatureVisitor visitReturnType() { | ||
| 97 | this.sv.visitReturnType(); | ||
| 98 | return this; | ||
| 99 | } | ||
| 100 | |||
| 101 | @Override | ||
| 102 | public SignatureVisitor visitSuperclass() { | ||
| 103 | this.sv.visitSuperclass(); | ||
| 104 | return this; | ||
| 105 | } | ||
| 106 | |||
| 107 | @Override | ||
| 108 | public void visitTypeArgument() { | ||
| 109 | this.sv.visitTypeArgument(); | ||
| 110 | } | ||
| 111 | |||
| 112 | @Override | ||
| 113 | public SignatureVisitor visitTypeArgument(char wildcard) { | ||
| 114 | this.sv.visitTypeArgument(wildcard); | ||
| 115 | return this; | ||
| 116 | } | ||
| 117 | |||
| 118 | @Override | ||
| 119 | public void visitEnd() { | ||
| 120 | this.sv.visitEnd(); | ||
| 121 | if (!classStack.empty()) | ||
| 122 | classStack.pop(); | ||
| 123 | } | ||
| 124 | |||
| 125 | @Override | ||
| 126 | public String toString() { | ||
| 127 | return this.sv.toString(); | ||
| 128 | } | ||
| 129 | } | ||