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/ClassRenamer.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/ClassRenamer.java')
| -rw-r--r-- | src/cuchaz/enigma/bytecode/ClassRenamer.java | 103 |
1 files changed, 41 insertions, 62 deletions
diff --git a/src/cuchaz/enigma/bytecode/ClassRenamer.java b/src/cuchaz/enigma/bytecode/ClassRenamer.java index 849a323..f8e63d1 100644 --- a/src/cuchaz/enigma/bytecode/ClassRenamer.java +++ b/src/cuchaz/enigma/bytecode/ClassRenamer.java | |||
| @@ -27,55 +27,43 @@ import cuchaz.enigma.mapping.ClassEntry; | |||
| 27 | import cuchaz.enigma.mapping.SignatureUpdater; | 27 | import cuchaz.enigma.mapping.SignatureUpdater; |
| 28 | import cuchaz.enigma.mapping.SignatureUpdater.ClassNameUpdater; | 28 | import cuchaz.enigma.mapping.SignatureUpdater.ClassNameUpdater; |
| 29 | 29 | ||
| 30 | public class ClassRenamer | 30 | public class ClassRenamer { |
| 31 | { | 31 | |
| 32 | public static void renameClasses( CtClass c, Map<ClassEntry,ClassEntry> map ) | 32 | public static void renameClasses(CtClass c, Map<ClassEntry,ClassEntry> map) { |
| 33 | { | 33 | |
| 34 | // build the map used by javassist | 34 | // build the map used by javassist |
| 35 | ClassMap nameMap = new ClassMap(); | 35 | ClassMap nameMap = new ClassMap(); |
| 36 | for( Map.Entry<ClassEntry,ClassEntry> entry : map.entrySet() ) | 36 | for (Map.Entry<ClassEntry,ClassEntry> entry : map.entrySet()) { |
| 37 | { | 37 | nameMap.put(entry.getKey().getName(), entry.getValue().getName()); |
| 38 | nameMap.put( entry.getKey().getName(), entry.getValue().getName() ); | ||
| 39 | } | 38 | } |
| 40 | 39 | ||
| 41 | c.replaceClassName( nameMap ); | 40 | c.replaceClassName(nameMap); |
| 42 | 41 | ||
| 43 | // replace simple names in the InnerClasses attribute too | 42 | // replace simple names in the InnerClasses attribute too |
| 44 | ConstPool constants = c.getClassFile().getConstPool(); | 43 | ConstPool constants = c.getClassFile().getConstPool(); |
| 45 | InnerClassesAttribute attr = (InnerClassesAttribute)c.getClassFile().getAttribute( InnerClassesAttribute.tag ); | 44 | InnerClassesAttribute attr = (InnerClassesAttribute)c.getClassFile().getAttribute(InnerClassesAttribute.tag); |
| 46 | if( attr != null ) | 45 | if (attr != null) { |
| 47 | { | 46 | for (int i = 0; i < attr.tableLength(); i++) { |
| 48 | for( int i=0; i<attr.tableLength(); i++ ) | 47 | ClassEntry classEntry = new ClassEntry(Descriptor.toJvmName(attr.innerClass(i))); |
| 49 | { | 48 | if (attr.innerNameIndex(i) != 0) { |
| 50 | ClassEntry classEntry = new ClassEntry( Descriptor.toJvmName( attr.innerClass( i ) ) ); | 49 | attr.setInnerNameIndex(i, constants.addUtf8Info(classEntry.getInnerClassName())); |
| 51 | if( attr.innerNameIndex( i ) != 0 ) | ||
| 52 | { | ||
| 53 | attr.setInnerNameIndex( i, constants.addUtf8Info( classEntry.getInnerClassName() ) ); | ||
| 54 | } | 50 | } |
| 55 | 51 | ||
| 56 | /* DEBUG | 52 | /* DEBUG |
| 57 | System.out.println( String.format( "\tDEOBF: %s-> ATTR: %s,%s,%s", | 53 | System.out.println(String.format("\tDEOBF: %s-> ATTR: %s,%s,%s", classEntry, attr.outerClass(i), attr.innerClass(i), attr.innerName(i))); |
| 58 | classEntry, | ||
| 59 | attr.outerClass( i ), | ||
| 60 | attr.innerClass( i ), | ||
| 61 | attr.innerName( i ) | ||
| 62 | ) ); | ||
| 63 | */ | 54 | */ |
| 64 | } | 55 | } |
| 65 | } | 56 | } |
| 66 | } | 57 | } |
| 67 | 58 | ||
| 68 | public static Set<ClassEntry> getAllClassEntries( final CtClass c ) | 59 | public static Set<ClassEntry> getAllClassEntries(final CtClass c) { |
| 69 | { | 60 | |
| 70 | // get the classes that javassist knows about | 61 | // get the classes that javassist knows about |
| 71 | final Set<ClassEntry> entries = Sets.newHashSet(); | 62 | final Set<ClassEntry> entries = Sets.newHashSet(); |
| 72 | ClassMap map = new ClassMap( ) | 63 | ClassMap map = new ClassMap() { |
| 73 | { | ||
| 74 | @Override | 64 | @Override |
| 75 | public Object get( Object obj ) | 65 | public Object get(Object obj) { |
| 76 | { | 66 | if (obj instanceof String) { |
| 77 | if( obj instanceof String ) | ||
| 78 | { | ||
| 79 | String str = (String)obj; | 67 | String str = (String)obj; |
| 80 | 68 | ||
| 81 | // javassist throws a lot of weird things at this map | 69 | // javassist throws a lot of weird things at this map |
| @@ -83,69 +71,60 @@ public class ClassRenamer | |||
| 83 | // I'm opting to filter out the weirdness for now | 71 | // I'm opting to filter out the weirdness for now |
| 84 | 72 | ||
| 85 | // skip anything with generic arguments | 73 | // skip anything with generic arguments |
| 86 | if( str.indexOf( '<' ) >= 0 || str.indexOf( '>' ) >= 0 || str.indexOf( ';' ) >= 0 ) | 74 | if (str.indexOf('<') >= 0 || str.indexOf('>') >= 0 || str.indexOf(';') >= 0) { |
| 87 | { | ||
| 88 | return null; | 75 | return null; |
| 89 | } | 76 | } |
| 90 | 77 | ||
| 91 | // convert path/to/class.inner to path/to/class$inner | 78 | // convert path/to/class.inner to path/to/class$inner |
| 92 | str = str.replace( '.', '$' ); | 79 | str = str.replace('.', '$'); |
| 93 | 80 | ||
| 94 | // remember everything else | 81 | // remember everything else |
| 95 | entries.add( new ClassEntry( str ) ); | 82 | entries.add(new ClassEntry(str)); |
| 96 | } | 83 | } |
| 97 | return null; | 84 | return null; |
| 98 | } | 85 | } |
| 86 | |||
| 99 | private static final long serialVersionUID = -202160293602070641L; | 87 | private static final long serialVersionUID = -202160293602070641L; |
| 100 | }; | 88 | }; |
| 101 | c.replaceClassName( map ); | 89 | c.replaceClassName(map); |
| 102 | 90 | ||
| 103 | return entries; | 91 | return entries; |
| 104 | } | 92 | } |
| 105 | 93 | ||
| 106 | public static void moveAllClassesOutOfDefaultPackage( CtClass c, String newPackageName ) | 94 | public static void moveAllClassesOutOfDefaultPackage(CtClass c, String newPackageName) { |
| 107 | { | 95 | |
| 108 | // rename all classes | 96 | // rename all classes |
| 109 | Map<ClassEntry,ClassEntry> map = Maps.newHashMap(); | 97 | Map<ClassEntry,ClassEntry> map = Maps.newHashMap(); |
| 110 | for( ClassEntry classEntry : ClassRenamer.getAllClassEntries( c ) ) | 98 | for (ClassEntry classEntry : ClassRenamer.getAllClassEntries(c)) { |
| 111 | { | 99 | if (classEntry.isInDefaultPackage()) { |
| 112 | if( classEntry.isInDefaultPackage() ) | 100 | map.put(classEntry, new ClassEntry(newPackageName + "/" + classEntry.getName())); |
| 113 | { | ||
| 114 | map.put( classEntry, new ClassEntry( newPackageName + "/" + classEntry.getName() ) ); | ||
| 115 | } | 101 | } |
| 116 | } | 102 | } |
| 117 | ClassRenamer.renameClasses( c, map ); | 103 | ClassRenamer.renameClasses(c, map); |
| 118 | 104 | ||
| 119 | // TEMP | 105 | // TEMP |
| 120 | for( ClassEntry classEntry : ClassRenamer.getAllClassEntries( c ) ) | 106 | for (ClassEntry classEntry : ClassRenamer.getAllClassEntries(c)) { |
| 121 | { | 107 | if (classEntry.isInDefaultPackage()) { |
| 122 | if( classEntry.isInDefaultPackage() ) | 108 | throw new Error("!!! " + classEntry); |
| 123 | { | ||
| 124 | throw new Error( "!!! " + classEntry ); | ||
| 125 | } | 109 | } |
| 126 | } | 110 | } |
| 127 | 111 | ||
| 128 | // TEMP | 112 | // TEMP |
| 129 | for( CtBehavior behavior : c.getDeclaredBehaviors() ) | 113 | for (CtBehavior behavior : c.getDeclaredBehaviors()) { |
| 130 | { | 114 | if (behavior.getSignature() == null) { |
| 131 | if( behavior.getSignature() == null ) | ||
| 132 | { | ||
| 133 | continue; | 115 | continue; |
| 134 | } | 116 | } |
| 135 | 117 | ||
| 136 | SignatureUpdater.update( behavior.getSignature(), new ClassNameUpdater( ) | 118 | SignatureUpdater.update(behavior.getSignature(), new ClassNameUpdater() { |
| 137 | { | ||
| 138 | @Override | 119 | @Override |
| 139 | public String update( String className ) | 120 | public String update(String className) { |
| 140 | { | 121 | ClassEntry classEntry = new ClassEntry(className); |
| 141 | ClassEntry classEntry = new ClassEntry( className ); | 122 | if (classEntry.isInDefaultPackage()) { |
| 142 | if( classEntry.isInDefaultPackage() ) | 123 | throw new Error("!!! " + className); |
| 143 | { | ||
| 144 | throw new Error( "!!! " + className ); | ||
| 145 | } | 124 | } |
| 146 | return className; | 125 | return className; |
| 147 | } | 126 | } |
| 148 | } ); | 127 | }); |
| 149 | } | 128 | } |
| 150 | } | 129 | } |
| 151 | } | 130 | } |