diff options
| author | 2015-01-13 23:25:04 -0500 | |
|---|---|---|
| committer | 2015-01-13 23:25:04 -0500 | |
| commit | 959cb5fd4f9586ec3bd265b452fe25fe1db82e3f (patch) | |
| tree | bdd8a2c52c2fe053ba3460614bde8542e5378dbe /src/cuchaz/enigma/bytecode/ClassTranslator.java | |
| parent | got rid of gradle in favor of ivy+ssjb (diff) | |
| download | enigma-fork-959cb5fd4f9586ec3bd265b452fe25fe1db82e3f.tar.gz enigma-fork-959cb5fd4f9586ec3bd265b452fe25fe1db82e3f.tar.xz enigma-fork-959cb5fd4f9586ec3bd265b452fe25fe1db82e3f.zip | |
source format change
don't hate me too much if you were planning a big merge. =P
Diffstat (limited to 'src/cuchaz/enigma/bytecode/ClassTranslator.java')
| -rw-r--r-- | src/cuchaz/enigma/bytecode/ClassTranslator.java | 104 |
1 files changed, 45 insertions, 59 deletions
diff --git a/src/cuchaz/enigma/bytecode/ClassTranslator.java b/src/cuchaz/enigma/bytecode/ClassTranslator.java index 181fadb..bc12405 100644 --- a/src/cuchaz/enigma/bytecode/ClassTranslator.java +++ b/src/cuchaz/enigma/bytecode/ClassTranslator.java | |||
| @@ -28,116 +28,102 @@ import cuchaz.enigma.mapping.FieldEntry; | |||
| 28 | import cuchaz.enigma.mapping.MethodEntry; | 28 | import cuchaz.enigma.mapping.MethodEntry; |
| 29 | import cuchaz.enigma.mapping.Translator; | 29 | import cuchaz.enigma.mapping.Translator; |
| 30 | 30 | ||
| 31 | public class ClassTranslator | 31 | public class ClassTranslator { |
| 32 | { | 32 | |
| 33 | private Translator m_translator; | 33 | private Translator m_translator; |
| 34 | 34 | ||
| 35 | public ClassTranslator( Translator translator ) | 35 | public ClassTranslator(Translator translator) { |
| 36 | { | ||
| 37 | m_translator = translator; | 36 | m_translator = translator; |
| 38 | } | 37 | } |
| 39 | 38 | ||
| 40 | public void translate( CtClass c ) | 39 | public void translate(CtClass c) { |
| 41 | { | ||
| 42 | // NOTE: the order of these translations is very important | 40 | // NOTE: the order of these translations is very important |
| 43 | 41 | ||
| 44 | // translate all the field and method references in the code by editing the constant pool | 42 | // translate all the field and method references in the code by editing the constant pool |
| 45 | ConstPool constants = c.getClassFile().getConstPool(); | 43 | ConstPool constants = c.getClassFile().getConstPool(); |
| 46 | ConstPoolEditor editor = new ConstPoolEditor( constants ); | 44 | ConstPoolEditor editor = new ConstPoolEditor(constants); |
| 47 | for( int i=1; i<constants.getSize(); i++ ) | 45 | for (int i = 1; i < constants.getSize(); i++) { |
| 48 | { | 46 | switch (constants.getTag(i)) { |
| 49 | switch( constants.getTag( i ) ) | 47 | case ConstPool.CONST_Fieldref: { |
| 50 | { | ||
| 51 | case ConstPool.CONST_Fieldref: | ||
| 52 | { | ||
| 53 | // translate the name | 48 | // translate the name |
| 54 | FieldEntry entry = new FieldEntry( | 49 | FieldEntry entry = new FieldEntry( |
| 55 | new ClassEntry( Descriptor.toJvmName( constants.getFieldrefClassName( i ) ) ), | 50 | new ClassEntry(Descriptor.toJvmName(constants.getFieldrefClassName(i))), |
| 56 | constants.getFieldrefName( i ) | 51 | constants.getFieldrefName(i) |
| 57 | ); | 52 | ); |
| 58 | FieldEntry translatedEntry = m_translator.translateEntry( entry ); | 53 | FieldEntry translatedEntry = m_translator.translateEntry(entry); |
| 59 | 54 | ||
| 60 | // translate the type | 55 | // translate the type |
| 61 | String type = constants.getFieldrefType( i ); | 56 | String type = constants.getFieldrefType(i); |
| 62 | String translatedType = m_translator.translateSignature( type ); | 57 | String translatedType = m_translator.translateSignature(type); |
| 63 | 58 | ||
| 64 | if( !entry.equals( translatedEntry ) || !type.equals( translatedType ) ) | 59 | if (!entry.equals(translatedEntry) || !type.equals(translatedType)) { |
| 65 | { | 60 | editor.changeMemberrefNameAndType(i, translatedEntry.getName(), translatedType); |
| 66 | editor.changeMemberrefNameAndType( i, translatedEntry.getName(), translatedType ); | ||
| 67 | } | 61 | } |
| 68 | } | 62 | } |
| 69 | break; | 63 | break; |
| 70 | 64 | ||
| 71 | case ConstPool.CONST_Methodref: | 65 | case ConstPool.CONST_Methodref: |
| 72 | case ConstPool.CONST_InterfaceMethodref: | 66 | case ConstPool.CONST_InterfaceMethodref: { |
| 73 | { | ||
| 74 | // translate the name and type | 67 | // translate the name and type |
| 75 | BehaviorEntry entry = BehaviorEntryFactory.create( | 68 | BehaviorEntry entry = BehaviorEntryFactory.create( |
| 76 | Descriptor.toJvmName( editor.getMemberrefClassname( i ) ), | 69 | Descriptor.toJvmName(editor.getMemberrefClassname(i)), |
| 77 | editor.getMemberrefName( i ), | 70 | editor.getMemberrefName(i), |
| 78 | editor.getMemberrefType( i ) | 71 | editor.getMemberrefType(i) |
| 79 | ); | 72 | ); |
| 80 | BehaviorEntry translatedEntry = m_translator.translateEntry( entry ); | 73 | BehaviorEntry translatedEntry = m_translator.translateEntry(entry); |
| 81 | 74 | ||
| 82 | if( !entry.getName().equals( translatedEntry.getName() ) || !entry.getSignature().equals( translatedEntry.getSignature() ) ) | 75 | if (!entry.getName().equals(translatedEntry.getName()) || !entry.getSignature().equals(translatedEntry.getSignature())) { |
| 83 | { | 76 | editor.changeMemberrefNameAndType(i, translatedEntry.getName(), translatedEntry.getSignature()); |
| 84 | editor.changeMemberrefNameAndType( i, translatedEntry.getName(), translatedEntry.getSignature() ); | ||
| 85 | } | 77 | } |
| 86 | } | 78 | } |
| 87 | break; | 79 | break; |
| 88 | } | 80 | } |
| 89 | } | 81 | } |
| 90 | 82 | ||
| 91 | ClassEntry classEntry = new ClassEntry( Descriptor.toJvmName( c.getName() ) ); | 83 | ClassEntry classEntry = new ClassEntry(Descriptor.toJvmName(c.getName())); |
| 92 | 84 | ||
| 93 | // translate all the fields | 85 | // translate all the fields |
| 94 | for( CtField field : c.getDeclaredFields() ) | 86 | for (CtField field : c.getDeclaredFields()) { |
| 95 | { | 87 | |
| 96 | // translate the name | 88 | // translate the name |
| 97 | FieldEntry entry = new FieldEntry( classEntry, field.getName() ); | 89 | FieldEntry entry = new FieldEntry(classEntry, field.getName()); |
| 98 | String translatedName = m_translator.translate( entry ); | 90 | String translatedName = m_translator.translate(entry); |
| 99 | if( translatedName != null ) | 91 | if (translatedName != null) { |
| 100 | { | 92 | field.setName(translatedName); |
| 101 | field.setName( translatedName ); | ||
| 102 | } | 93 | } |
| 103 | 94 | ||
| 104 | // translate the type | 95 | // translate the type |
| 105 | String translatedType = m_translator.translateSignature( field.getFieldInfo().getDescriptor() ); | 96 | String translatedType = m_translator.translateSignature(field.getFieldInfo().getDescriptor()); |
| 106 | field.getFieldInfo().setDescriptor( translatedType ); | 97 | field.getFieldInfo().setDescriptor(translatedType); |
| 107 | } | 98 | } |
| 108 | 99 | ||
| 109 | // translate all the methods and constructors | 100 | // translate all the methods and constructors |
| 110 | for( CtBehavior behavior : c.getDeclaredBehaviors() ) | 101 | for (CtBehavior behavior : c.getDeclaredBehaviors()) { |
| 111 | { | 102 | if (behavior instanceof CtMethod) { |
| 112 | if( behavior instanceof CtMethod ) | ||
| 113 | { | ||
| 114 | CtMethod method = (CtMethod)behavior; | 103 | CtMethod method = (CtMethod)behavior; |
| 115 | 104 | ||
| 116 | // translate the name | 105 | // translate the name |
| 117 | MethodEntry entry = new MethodEntry( classEntry, method.getName(), method.getSignature() ); | 106 | MethodEntry entry = new MethodEntry(classEntry, method.getName(), method.getSignature()); |
| 118 | String translatedName = m_translator.translate( entry ); | 107 | String translatedName = m_translator.translate(entry); |
| 119 | if( translatedName != null ) | 108 | if (translatedName != null) { |
| 120 | { | 109 | method.setName(translatedName); |
| 121 | method.setName( translatedName ); | ||
| 122 | } | 110 | } |
| 123 | } | 111 | } |
| 124 | 112 | ||
| 125 | // translate the type | 113 | // translate the type |
| 126 | String translatedSignature = m_translator.translateSignature( behavior.getMethodInfo().getDescriptor() ); | 114 | String translatedSignature = m_translator.translateSignature(behavior.getMethodInfo().getDescriptor()); |
| 127 | behavior.getMethodInfo().setDescriptor( translatedSignature ); | 115 | behavior.getMethodInfo().setDescriptor(translatedSignature); |
| 128 | } | 116 | } |
| 129 | 117 | ||
| 130 | // translate all the class names referenced in the code | 118 | // translate all the class names referenced in the code |
| 131 | // the above code only changed method/field/reference names and types, but not the class names themselves | 119 | // the above code only changed method/field/reference names and types, but not the class names themselves |
| 132 | Map<ClassEntry,ClassEntry> map = Maps.newHashMap(); | 120 | Map<ClassEntry,ClassEntry> map = Maps.newHashMap(); |
| 133 | for( ClassEntry obfClassEntry : ClassRenamer.getAllClassEntries( c ) ) | 121 | for (ClassEntry obfClassEntry : ClassRenamer.getAllClassEntries(c)) { |
| 134 | { | 122 | ClassEntry deobfClassEntry = m_translator.translateEntry(obfClassEntry); |
| 135 | ClassEntry deobfClassEntry = m_translator.translateEntry( obfClassEntry ); | 123 | if (!obfClassEntry.equals(deobfClassEntry)) { |
| 136 | if( !obfClassEntry.equals( deobfClassEntry ) ) | 124 | map.put(obfClassEntry, deobfClassEntry); |
| 137 | { | ||
| 138 | map.put( obfClassEntry, deobfClassEntry ); | ||
| 139 | } | 125 | } |
| 140 | } | 126 | } |
| 141 | ClassRenamer.renameClasses( c, map ); | 127 | ClassRenamer.renameClasses(c, map); |
| 142 | } | 128 | } |
| 143 | } | 129 | } |