1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
/*******************************************************************************
* Copyright (c) 2014 Jeff Martin.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*
* Contributors:
* Jeff Martin - initial API and implementation
******************************************************************************/
package cuchaz.enigma.bytecode;
import java.util.Map;
import java.util.Set;
import javassist.ClassMap;
import javassist.CtBehavior;
import javassist.CtClass;
import javassist.bytecode.ConstPool;
import javassist.bytecode.Descriptor;
import javassist.bytecode.InnerClassesAttribute;
import com.beust.jcommander.internal.Sets;
import com.google.common.collect.Maps;
import cuchaz.enigma.mapping.ClassEntry;
import cuchaz.enigma.mapping.SignatureUpdater;
import cuchaz.enigma.mapping.SignatureUpdater.ClassNameUpdater;
public class ClassRenamer
{
public static void renameClasses( CtClass c, Map<ClassEntry,ClassEntry> map )
{
// build the map used by javassist
ClassMap nameMap = new ClassMap();
for( Map.Entry<ClassEntry,ClassEntry> entry : map.entrySet() )
{
nameMap.put( entry.getKey().getName(), entry.getValue().getName() );
}
c.replaceClassName( nameMap );
// translate the names in the InnerClasses attribute
ConstPool constants = c.getClassFile().getConstPool();
InnerClassesAttribute attr = (InnerClassesAttribute)c.getClassFile().getAttribute( InnerClassesAttribute.tag );
if( attr != null )
{
for( int i=0; i<attr.tableLength(); i++ )
{
ClassEntry inClassEntry = new ClassEntry( Descriptor.toJvmName( attr.innerClass( i ) ) );
ClassEntry outClassEntry = map.get( inClassEntry );
if( outClassEntry == null )
{
continue;
}
attr.setInnerClassIndex( i, constants.addClassInfo( outClassEntry.getName() ) );
if( attr.outerClassIndex( i ) != 0 )
{
attr.setOuterClassIndex( i, constants.addClassInfo( outClassEntry.getOuterClassName() ) );
}
if( attr.innerNameIndex( i ) != 0 )
{
attr.setInnerNameIndex( i, constants.addUtf8Info( outClassEntry.getInnerClassName() ) );
}
/* DEBUG
System.out.println( String.format( "\tOBF: %s DEOBF: %s-> ATTR: %s,%s,%s",
obfClassEntry, deobfClassEntry,
attr.outerClass( i ),
attr.innerClass( i ),
attr.innerName( i )
) );
*/
}
}
}
public static Set<ClassEntry> getAllClassEntries( CtClass c )
{
// get the classes that javassist knows about
final Set<ClassEntry> entries = Sets.newHashSet();
ClassMap map = new ClassMap( )
{
@Override
public Object get( Object obj )
{
if( obj instanceof String )
{
entries.add( new ClassEntry( (String)obj ) );
}
return null;
}
private static final long serialVersionUID = -202160293602070641L;
};
c.replaceClassName( map );
// also check InnerClassesAttribute
InnerClassesAttribute attr = (InnerClassesAttribute)c.getClassFile().getAttribute( InnerClassesAttribute.tag );
if( attr != null )
{
for( int i=0; i<attr.tableLength(); i++ )
{
entries.add( new ClassEntry( Descriptor.toJvmName( attr.innerClass( i ) ) ) );
}
}
return entries;
}
public static void moveAllClassesOutOfDefaultPackage( CtClass c, String newPackageName )
{
// rename all classes
Map<ClassEntry,ClassEntry> map = Maps.newHashMap();
for( ClassEntry classEntry : ClassRenamer.getAllClassEntries( c ) )
{
if( classEntry.isInDefaultPackage() )
{
map.put( classEntry, new ClassEntry( newPackageName + "/" + classEntry.getName() ) );
}
}
ClassRenamer.renameClasses( c, map );
// TEMP
for( ClassEntry classEntry : ClassRenamer.getAllClassEntries( c ) )
{
if( classEntry.isInDefaultPackage() )
{
throw new Error( "!!! " + classEntry );
}
}
// TEMP
for( CtBehavior behavior : c.getDeclaredBehaviors() )
{
if( behavior.getSignature() == null )
{
continue;
}
SignatureUpdater.update( behavior.getSignature(), new ClassNameUpdater( )
{
@Override
public String update( String className )
{
ClassEntry classEntry = new ClassEntry( className );
if( classEntry.isInDefaultPackage() )
{
throw new Error( "!!! " + className );
}
return className;
}
} );
}
}
}
|