diff options
Diffstat (limited to 'src/cuchaz/enigma/bytecode/ClassTranslator.java')
| -rw-r--r-- | src/cuchaz/enigma/bytecode/ClassTranslator.java | 157 |
1 files changed, 0 insertions, 157 deletions
diff --git a/src/cuchaz/enigma/bytecode/ClassTranslator.java b/src/cuchaz/enigma/bytecode/ClassTranslator.java deleted file mode 100644 index 7402459..0000000 --- a/src/cuchaz/enigma/bytecode/ClassTranslator.java +++ /dev/null | |||
| @@ -1,157 +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 | * | ||
| 8 | * Contributors: | ||
| 9 | * Jeff Martin - initial API and implementation | ||
| 10 | ******************************************************************************/ | ||
| 11 | package cuchaz.enigma.bytecode; | ||
| 12 | |||
| 13 | import javassist.CtBehavior; | ||
| 14 | import javassist.CtClass; | ||
| 15 | import javassist.CtField; | ||
| 16 | import javassist.CtMethod; | ||
| 17 | import javassist.bytecode.ConstPool; | ||
| 18 | import javassist.bytecode.Descriptor; | ||
| 19 | import javassist.bytecode.EnclosingMethodAttribute; | ||
| 20 | import javassist.bytecode.SourceFileAttribute; | ||
| 21 | import cuchaz.enigma.mapping.BehaviorEntry; | ||
| 22 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 23 | import cuchaz.enigma.mapping.EntryFactory; | ||
| 24 | import cuchaz.enigma.mapping.FieldEntry; | ||
| 25 | import cuchaz.enigma.mapping.Signature; | ||
| 26 | import cuchaz.enigma.mapping.Translator; | ||
| 27 | import cuchaz.enigma.mapping.Type; | ||
| 28 | |||
| 29 | public class ClassTranslator { | ||
| 30 | |||
| 31 | private Translator m_translator; | ||
| 32 | |||
| 33 | public ClassTranslator(Translator translator) { | ||
| 34 | m_translator = translator; | ||
| 35 | } | ||
| 36 | |||
| 37 | public void translate(CtClass c) { | ||
| 38 | |||
| 39 | // NOTE: the order of these translations is very important | ||
| 40 | |||
| 41 | // translate all the field and method references in the code by editing the constant pool | ||
| 42 | ConstPool constants = c.getClassFile().getConstPool(); | ||
| 43 | ConstPoolEditor editor = new ConstPoolEditor(constants); | ||
| 44 | for (int i = 1; i < constants.getSize(); i++) { | ||
| 45 | switch (constants.getTag(i)) { | ||
| 46 | |||
| 47 | case ConstPool.CONST_Fieldref: { | ||
| 48 | |||
| 49 | // translate the name and type | ||
| 50 | FieldEntry entry = EntryFactory.getFieldEntry( | ||
| 51 | Descriptor.toJvmName(constants.getFieldrefClassName(i)), | ||
| 52 | constants.getFieldrefName(i), | ||
| 53 | constants.getFieldrefType(i) | ||
| 54 | ); | ||
| 55 | FieldEntry translatedEntry = m_translator.translateEntry(entry); | ||
| 56 | if (!entry.equals(translatedEntry)) { | ||
| 57 | editor.changeMemberrefNameAndType(i, translatedEntry.getName(), translatedEntry.getType().toString()); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | break; | ||
| 61 | |||
| 62 | case ConstPool.CONST_Methodref: | ||
| 63 | case ConstPool.CONST_InterfaceMethodref: { | ||
| 64 | |||
| 65 | // translate the name and type (ie signature) | ||
| 66 | BehaviorEntry entry = EntryFactory.getBehaviorEntry( | ||
| 67 | Descriptor.toJvmName(editor.getMemberrefClassname(i)), | ||
| 68 | editor.getMemberrefName(i), | ||
| 69 | editor.getMemberrefType(i) | ||
| 70 | ); | ||
| 71 | BehaviorEntry translatedEntry = m_translator.translateEntry(entry); | ||
| 72 | if (!entry.equals(translatedEntry)) { | ||
| 73 | editor.changeMemberrefNameAndType(i, translatedEntry.getName(), translatedEntry.getSignature().toString()); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | break; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | ClassEntry classEntry = new ClassEntry(Descriptor.toJvmName(c.getName())); | ||
| 81 | |||
| 82 | // translate all the fields | ||
| 83 | for (CtField field : c.getDeclaredFields()) { | ||
| 84 | |||
| 85 | // translate the name | ||
| 86 | FieldEntry entry = EntryFactory.getFieldEntry(field); | ||
| 87 | String translatedName = m_translator.translate(entry); | ||
| 88 | if (translatedName != null) { | ||
| 89 | field.setName(translatedName); | ||
| 90 | } | ||
| 91 | |||
| 92 | // translate the type | ||
| 93 | Type translatedType = m_translator.translateType(entry.getType()); | ||
| 94 | field.getFieldInfo().setDescriptor(translatedType.toString()); | ||
| 95 | } | ||
| 96 | |||
| 97 | // translate all the methods and constructors | ||
| 98 | for (CtBehavior behavior : c.getDeclaredBehaviors()) { | ||
| 99 | |||
| 100 | BehaviorEntry entry = EntryFactory.getBehaviorEntry(behavior); | ||
| 101 | |||
| 102 | if (behavior instanceof CtMethod) { | ||
| 103 | CtMethod method = (CtMethod)behavior; | ||
| 104 | |||
| 105 | // translate the name | ||
| 106 | String translatedName = m_translator.translate(entry); | ||
| 107 | if (translatedName != null) { | ||
| 108 | method.setName(translatedName); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | if (entry.getSignature() != null) { | ||
| 113 | // translate the signature | ||
| 114 | Signature translatedSignature = m_translator.translateSignature(entry.getSignature()); | ||
| 115 | behavior.getMethodInfo().setDescriptor(translatedSignature.toString()); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | // translate the EnclosingMethod attribute | ||
| 120 | EnclosingMethodAttribute enclosingMethodAttr = (EnclosingMethodAttribute)c.getClassFile().getAttribute(EnclosingMethodAttribute.tag); | ||
| 121 | if (enclosingMethodAttr != null) { | ||
| 122 | |||
| 123 | if (enclosingMethodAttr.methodIndex() == 0) { | ||
| 124 | BehaviorEntry obfBehaviorEntry = EntryFactory.getBehaviorEntry(Descriptor.toJvmName(enclosingMethodAttr.className())); | ||
| 125 | BehaviorEntry deobfBehaviorEntry = m_translator.translateEntry(obfBehaviorEntry); | ||
| 126 | c.getClassFile().addAttribute(new EnclosingMethodAttribute( | ||
| 127 | constants, | ||
| 128 | deobfBehaviorEntry.getClassName() | ||
| 129 | )); | ||
| 130 | } else { | ||
| 131 | BehaviorEntry obfBehaviorEntry = EntryFactory.getBehaviorEntry( | ||
| 132 | Descriptor.toJvmName(enclosingMethodAttr.className()), | ||
| 133 | enclosingMethodAttr.methodName(), | ||
| 134 | enclosingMethodAttr.methodDescriptor() | ||
| 135 | ); | ||
| 136 | BehaviorEntry deobfBehaviorEntry = m_translator.translateEntry(obfBehaviorEntry); | ||
| 137 | c.getClassFile().addAttribute(new EnclosingMethodAttribute( | ||
| 138 | constants, | ||
| 139 | deobfBehaviorEntry.getClassName(), | ||
| 140 | deobfBehaviorEntry.getName(), | ||
| 141 | deobfBehaviorEntry.getSignature().toString() | ||
| 142 | )); | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | // translate all the class names referenced in the code | ||
| 147 | // the above code only changed method/field/reference names and types, but not the rest of the class references | ||
| 148 | ClassRenamer.renameClasses(c, m_translator); | ||
| 149 | |||
| 150 | // translate the source file attribute too | ||
| 151 | ClassEntry deobfClassEntry = m_translator.translateEntry(classEntry); | ||
| 152 | if (deobfClassEntry != null) { | ||
| 153 | String sourceFile = Descriptor.toJvmName(deobfClassEntry.getOutermostClassEntry().getSimpleName()) + ".java"; | ||
| 154 | c.getClassFile().addAttribute(new SourceFileAttribute(constants, sourceFile)); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | } | ||