summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/bytecode/ClassRenamer.java
blob: 849a32339f6047e709812eca19d0682fd51fc7b1 (plain) (blame)
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
/*******************************************************************************
 * 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.google.common.collect.Maps;
import com.google.common.collect.Sets;

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 );
		
		// replace simple names in the InnerClasses attribute too
		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 classEntry = new ClassEntry( Descriptor.toJvmName( attr.innerClass( i ) ) );
				if( attr.innerNameIndex( i ) != 0 )
				{
					attr.setInnerNameIndex( i, constants.addUtf8Info( classEntry.getInnerClassName() ) );
				}
				
				/* DEBUG
				System.out.println( String.format( "\tDEOBF: %s-> ATTR: %s,%s,%s",
					classEntry,
					attr.outerClass( i ),
					attr.innerClass( i ),
					attr.innerName( i )
				) );
				*/
			}
		}
	}
	
	public static Set<ClassEntry> getAllClassEntries( final 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 )
				{
					String str = (String)obj;
					
					// javassist throws a lot of weird things at this map
					// I either have to implement my on class scanner, or just try to filter out the weirdness
					// I'm opting to filter out the weirdness for now
					
					// skip anything with generic arguments
					if( str.indexOf( '<' ) >= 0 || str.indexOf( '>' ) >= 0 || str.indexOf( ';' ) >= 0 )
					{
						return null;
					}
					
					// convert path/to/class.inner to path/to/class$inner
					str = str.replace( '.', '$' );
					
					// remember everything else
					entries.add( new ClassEntry( str ) );
				}
				return null;
			}
			private static final long serialVersionUID = -202160293602070641L;
		};
		c.replaceClassName( map );
		
		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;
				}
			} );
		}
	}
}