diff options
| author | 2015-02-03 22:00:53 -0500 | |
|---|---|---|
| committer | 2015-02-03 22:00:53 -0500 | |
| commit | 52ab426d8fad3dbee7e728f523a35af94facebda (patch) | |
| tree | 146fadfd8e639a909d6c1d6a193e7eddeab0be4a /src/cuchaz/enigma/bytecode/ClassTranslator.java | |
| download | enigma-fork-52ab426d8fad3dbee7e728f523a35af94facebda.tar.gz enigma-fork-52ab426d8fad3dbee7e728f523a35af94facebda.tar.xz enigma-fork-52ab426d8fad3dbee7e728f523a35af94facebda.zip | |
oops, don't depend on local procyon project
Diffstat (limited to 'src/cuchaz/enigma/bytecode/ClassTranslator.java')
| -rw-r--r-- | src/cuchaz/enigma/bytecode/ClassTranslator.java | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/bytecode/ClassTranslator.java b/src/cuchaz/enigma/bytecode/ClassTranslator.java new file mode 100644 index 0000000..fb2fb27 --- /dev/null +++ b/src/cuchaz/enigma/bytecode/ClassTranslator.java | |||
| @@ -0,0 +1,141 @@ | |||
| 1 | /******************************************************************************* | ||
| 2 | * Copyright (c) 2014 Jeff Martin. | ||
| 3 | * All rights reserved. This program and the accompanying materials | ||
| 4 | * are made available under the terms of the GNU Public License v3.0 | ||
| 5 | * which accompanies this distribution, and is available at | ||
| 6 | * http://www.gnu.org/licenses/gpl.html | ||
| 7 | * | ||
| 8 | * Contributors: | ||
| 9 | * Jeff Martin - initial API and implementation | ||
| 10 | ******************************************************************************/ | ||
| 11 | package cuchaz.enigma.bytecode; | ||
| 12 | |||
| 13 | import java.util.Map; | ||
| 14 | |||
| 15 | import javassist.CtBehavior; | ||
| 16 | import javassist.CtClass; | ||
| 17 | import javassist.CtField; | ||
| 18 | import javassist.CtMethod; | ||
| 19 | import javassist.bytecode.ConstPool; | ||
| 20 | import javassist.bytecode.Descriptor; | ||
| 21 | import javassist.bytecode.SourceFileAttribute; | ||
| 22 | |||
| 23 | import com.google.common.collect.Maps; | ||
| 24 | |||
| 25 | import cuchaz.enigma.mapping.BehaviorEntry; | ||
| 26 | import cuchaz.enigma.mapping.BehaviorEntryFactory; | ||
| 27 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 28 | import cuchaz.enigma.mapping.FieldEntry; | ||
| 29 | import cuchaz.enigma.mapping.MethodEntry; | ||
| 30 | import cuchaz.enigma.mapping.Translator; | ||
| 31 | |||
| 32 | public class ClassTranslator { | ||
| 33 | |||
| 34 | private Translator m_translator; | ||
| 35 | |||
| 36 | public ClassTranslator(Translator translator) { | ||
| 37 | m_translator = translator; | ||
| 38 | } | ||
| 39 | |||
| 40 | public void translate(CtClass c) { | ||
| 41 | |||
| 42 | // NOTE: the order of these translations is very important | ||
| 43 | |||
| 44 | // translate all the field and method references in the code by editing the constant pool | ||
| 45 | ConstPool constants = c.getClassFile().getConstPool(); | ||
| 46 | ConstPoolEditor editor = new ConstPoolEditor(constants); | ||
| 47 | for (int i = 1; i < constants.getSize(); i++) { | ||
| 48 | switch (constants.getTag(i)) { | ||
| 49 | |||
| 50 | case ConstPool.CONST_Fieldref: { | ||
| 51 | |||
| 52 | // translate the name | ||
| 53 | FieldEntry entry = new FieldEntry( | ||
| 54 | new ClassEntry(Descriptor.toJvmName(constants.getFieldrefClassName(i))), | ||
| 55 | constants.getFieldrefName(i) | ||
| 56 | ); | ||
| 57 | FieldEntry translatedEntry = m_translator.translateEntry(entry); | ||
| 58 | |||
| 59 | // translate the type | ||
| 60 | String type = constants.getFieldrefType(i); | ||
| 61 | String translatedType = m_translator.translateSignature(type); | ||
| 62 | |||
| 63 | if (!entry.equals(translatedEntry) || !type.equals(translatedType)) { | ||
| 64 | editor.changeMemberrefNameAndType(i, translatedEntry.getName(), translatedType); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | break; | ||
| 68 | |||
| 69 | case ConstPool.CONST_Methodref: | ||
| 70 | case ConstPool.CONST_InterfaceMethodref: { | ||
| 71 | |||
| 72 | // translate the name and type | ||
| 73 | BehaviorEntry entry = BehaviorEntryFactory.create( | ||
| 74 | Descriptor.toJvmName(editor.getMemberrefClassname(i)), | ||
| 75 | editor.getMemberrefName(i), | ||
| 76 | editor.getMemberrefType(i) | ||
| 77 | ); | ||
| 78 | BehaviorEntry translatedEntry = m_translator.translateEntry(entry); | ||
| 79 | |||
| 80 | if (!entry.getName().equals(translatedEntry.getName()) || !entry.getSignature().equals(translatedEntry.getSignature())) { | ||
| 81 | editor.changeMemberrefNameAndType(i, translatedEntry.getName(), translatedEntry.getSignature()); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | break; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | ClassEntry classEntry = new ClassEntry(Descriptor.toJvmName(c.getName())); | ||
| 89 | |||
| 90 | // translate all the fields | ||
| 91 | for (CtField field : c.getDeclaredFields()) { | ||
| 92 | |||
| 93 | // translate the name | ||
| 94 | FieldEntry entry = new FieldEntry(classEntry, field.getName()); | ||
| 95 | String translatedName = m_translator.translate(entry); | ||
| 96 | if (translatedName != null) { | ||
| 97 | field.setName(translatedName); | ||
| 98 | } | ||
| 99 | |||
| 100 | // translate the type | ||
| 101 | String translatedType = m_translator.translateSignature(field.getFieldInfo().getDescriptor()); | ||
| 102 | field.getFieldInfo().setDescriptor(translatedType); | ||
| 103 | } | ||
| 104 | |||
| 105 | // translate all the methods and constructors | ||
| 106 | for (CtBehavior behavior : c.getDeclaredBehaviors()) { | ||
| 107 | if (behavior instanceof CtMethod) { | ||
| 108 | CtMethod method = (CtMethod)behavior; | ||
| 109 | |||
| 110 | // translate the name | ||
| 111 | MethodEntry entry = new MethodEntry(classEntry, method.getName(), method.getSignature()); | ||
| 112 | String translatedName = m_translator.translate(entry); | ||
| 113 | if (translatedName != null) { | ||
| 114 | method.setName(translatedName); | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | // translate the type | ||
| 119 | String translatedSignature = m_translator.translateSignature(behavior.getMethodInfo().getDescriptor()); | ||
| 120 | behavior.getMethodInfo().setDescriptor(translatedSignature); | ||
| 121 | } | ||
| 122 | |||
| 123 | // translate all the class names referenced in the code | ||
| 124 | // the above code only changed method/field/reference names and types, but not the class names themselves | ||
| 125 | Map<ClassEntry,ClassEntry> map = Maps.newHashMap(); | ||
| 126 | for (ClassEntry obfClassEntry : ClassRenamer.getAllClassEntries(c)) { | ||
| 127 | ClassEntry deobfClassEntry = m_translator.translateEntry(obfClassEntry); | ||
| 128 | if (!obfClassEntry.equals(deobfClassEntry)) { | ||
| 129 | map.put(obfClassEntry, deobfClassEntry); | ||
| 130 | } | ||
| 131 | } | ||
| 132 | ClassRenamer.renameClasses(c, map); | ||
| 133 | |||
| 134 | // translate the source file attribute too | ||
| 135 | ClassEntry deobfClassEntry = map.get(classEntry); | ||
| 136 | if (deobfClassEntry != null) { | ||
| 137 | String sourceFile = Descriptor.toJvmName(deobfClassEntry.getOuterClassName()) + ".java"; | ||
| 138 | c.getClassFile().addAttribute(new SourceFileAttribute(constants, sourceFile)); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | } | ||