diff options
Diffstat (limited to 'src/cuchaz/enigma/bytecode/ClassRenamer.java')
| -rw-r--r-- | src/cuchaz/enigma/bytecode/ClassRenamer.java | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/bytecode/ClassRenamer.java b/src/cuchaz/enigma/bytecode/ClassRenamer.java new file mode 100644 index 0000000..cba5861 --- /dev/null +++ b/src/cuchaz/enigma/bytecode/ClassRenamer.java | |||
| @@ -0,0 +1,115 @@ | |||
| 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 | import java.util.Set; | ||
| 15 | |||
| 16 | import javassist.ClassMap; | ||
| 17 | import javassist.CtClass; | ||
| 18 | import javassist.bytecode.ConstPool; | ||
| 19 | import javassist.bytecode.Descriptor; | ||
| 20 | import javassist.bytecode.InnerClassesAttribute; | ||
| 21 | |||
| 22 | import com.beust.jcommander.internal.Sets; | ||
| 23 | import com.google.common.collect.Maps; | ||
| 24 | |||
| 25 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 26 | |||
| 27 | public class ClassRenamer | ||
| 28 | { | ||
| 29 | public static void renameClasses( CtClass c, Map<ClassEntry,ClassEntry> map ) | ||
| 30 | { | ||
| 31 | // build the map used by javassist | ||
| 32 | ClassMap nameMap = new ClassMap(); | ||
| 33 | for( Map.Entry<ClassEntry,ClassEntry> entry : map.entrySet() ) | ||
| 34 | { | ||
| 35 | nameMap.put( entry.getKey().getName(), entry.getValue().getName() ); | ||
| 36 | } | ||
| 37 | c.replaceClassName( nameMap ); | ||
| 38 | |||
| 39 | // translate the names in the InnerClasses attribute | ||
| 40 | ConstPool constants = c.getClassFile().getConstPool(); | ||
| 41 | InnerClassesAttribute attr = (InnerClassesAttribute)c.getClassFile().getAttribute( InnerClassesAttribute.tag ); | ||
| 42 | if( attr != null ) | ||
| 43 | { | ||
| 44 | for( int i=0; i<attr.tableLength(); i++ ) | ||
| 45 | { | ||
| 46 | ClassEntry inClassEntry = new ClassEntry( Descriptor.toJvmName( attr.innerClass( i ) ) ); | ||
| 47 | ClassEntry outClassEntry = map.get( inClassEntry ); | ||
| 48 | attr.setInnerClassIndex( i, constants.addClassInfo( outClassEntry.getName() ) ); | ||
| 49 | if( attr.outerClassIndex( i ) != 0 ) | ||
| 50 | { | ||
| 51 | attr.setOuterClassIndex( i, constants.addClassInfo( outClassEntry.getOuterClassName() ) ); | ||
| 52 | } | ||
| 53 | if( attr.innerNameIndex( i ) != 0 ) | ||
| 54 | { | ||
| 55 | attr.setInnerNameIndex( i, constants.addUtf8Info( outClassEntry.getInnerClassName() ) ); | ||
| 56 | } | ||
| 57 | |||
| 58 | /* DEBUG | ||
| 59 | System.out.println( String.format( "\tOBF: %s DEOBF: %s-> ATTR: %s,%s,%s", | ||
| 60 | obfClassEntry, deobfClassEntry, | ||
| 61 | attr.outerClass( i ), | ||
| 62 | attr.innerClass( i ), | ||
| 63 | attr.innerName( i ) | ||
| 64 | ) ); | ||
| 65 | */ | ||
| 66 | } | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | public static Set<ClassEntry> getAllClassEntries( CtClass c ) | ||
| 71 | { | ||
| 72 | // get the classes that javassist knows about | ||
| 73 | final Set<ClassEntry> entries = Sets.newHashSet(); | ||
| 74 | ClassMap map = new ClassMap( ) | ||
| 75 | { | ||
| 76 | @Override | ||
| 77 | public Object get( Object obj ) | ||
| 78 | { | ||
| 79 | if( obj instanceof String ) | ||
| 80 | { | ||
| 81 | entries.add( new ClassEntry( (String)obj ) ); | ||
| 82 | } | ||
| 83 | return null; | ||
| 84 | } | ||
| 85 | private static final long serialVersionUID = -202160293602070641L; | ||
| 86 | }; | ||
| 87 | c.replaceClassName( map ); | ||
| 88 | |||
| 89 | // also check InnerClassesAttribute | ||
| 90 | InnerClassesAttribute attr = (InnerClassesAttribute)c.getClassFile().getAttribute( InnerClassesAttribute.tag ); | ||
| 91 | if( attr != null ) | ||
| 92 | { | ||
| 93 | for( int i=0; i<attr.tableLength(); i++ ) | ||
| 94 | { | ||
| 95 | entries.add( new ClassEntry( Descriptor.toJvmName( attr.innerClass( i ) ) ) ); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | return entries; | ||
| 100 | } | ||
| 101 | |||
| 102 | public static void moveAllClassesOutOfDefaultPackage( CtClass c, String newPackageName ) | ||
| 103 | { | ||
| 104 | // rename all classes | ||
| 105 | Map<ClassEntry,ClassEntry> map = Maps.newHashMap(); | ||
| 106 | for( ClassEntry classEntry : ClassRenamer.getAllClassEntries( c ) ) | ||
| 107 | { | ||
| 108 | if( classEntry.isInDefaultPackage() ) | ||
| 109 | { | ||
| 110 | map.put( classEntry, new ClassEntry( newPackageName + "/" + classEntry.getName() ) ); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | ClassRenamer.renameClasses( c, map ); | ||
| 114 | } | ||
| 115 | } | ||