summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/analysis/BridgeFixer.java
blob: ad23b0001de19827d6393dedde4f3ffeb5ca49e7 (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
/*******************************************************************************
 * 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.analysis;

import javassist.CtClass;
import javassist.CtMethod;
import javassist.bytecode.ConstPool;
import javassist.bytecode.Descriptor;
import cuchaz.enigma.bytecode.ConstPoolEditor;
import cuchaz.enigma.mapping.BehaviorEntry;
import cuchaz.enigma.mapping.BehaviorEntryFactory;
import cuchaz.enigma.mapping.ClassEntry;
import cuchaz.enigma.mapping.MethodEntry;

public class BridgeFixer {
	
	private JarIndex m_index;
	
	public BridgeFixer(JarIndex index) {
		m_index = index;
	}
	
	public void fixBridges(CtClass c) {
		// rename declared methods
		for (CtMethod method : c.getDeclaredMethods()) {
			// get the method entry
			MethodEntry methodEntry = new MethodEntry(
				new ClassEntry(Descriptor.toJvmName(c.getName())),
				method.getName(),
				method.getSignature()
			);
			MethodEntry bridgeMethodEntry = m_index.getBridgeMethod(methodEntry);
			if (bridgeMethodEntry != null) {
				// fix this bridged method
				method.setName(bridgeMethodEntry.getName());
			}
		}
		
		// rename method references
		// translate all the field and method references in the code by editing the constant pool
		ConstPool constants = c.getClassFile().getConstPool();
		ConstPoolEditor editor = new ConstPoolEditor(constants);
		for (int i = 1; i < constants.getSize(); i++) {
			switch (constants.getTag(i)) {
				case ConstPool.CONST_Methodref:
				case ConstPool.CONST_InterfaceMethodref: {
					BehaviorEntry behaviorEntry = BehaviorEntryFactory.create(Descriptor.toJvmName(editor.getMemberrefClassname(i)), editor.getMemberrefName(i), editor.getMemberrefType(i));
					
					if (behaviorEntry instanceof MethodEntry) {
						MethodEntry methodEntry = (MethodEntry)behaviorEntry;
						
						// translate the name and type
						MethodEntry bridgeMethodEntry = m_index.getBridgeMethod(methodEntry);
						if (bridgeMethodEntry != null) {
							// FIXIT FIXIT FIXIT FIXIT FIXIT FIXIT FIXIT
							editor.changeMemberrefNameAndType(i, bridgeMethodEntry.getName(), bridgeMethodEntry.getSignature());
						}
					}
				}
				break;
			}
		}
	}
}