summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/bytecode/LocalVariableRenamer.java
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/cuchaz/enigma/bytecode/LocalVariableRenamer.java123
1 files changed, 0 insertions, 123 deletions
diff --git a/src/cuchaz/enigma/bytecode/LocalVariableRenamer.java b/src/cuchaz/enigma/bytecode/LocalVariableRenamer.java
deleted file mode 100644
index ae0455f..0000000
--- a/src/cuchaz/enigma/bytecode/LocalVariableRenamer.java
+++ /dev/null
@@ -1,123 +0,0 @@
1/*******************************************************************************
2 * Copyright (c) 2015 Jeff Martin.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Lesser General Public
5 * License v3.0 which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/lgpl.html
7 *
8 * Contributors:
9 * Jeff Martin - initial API and implementation
10 ******************************************************************************/
11package cuchaz.enigma.bytecode;
12
13import javassist.CtBehavior;
14import javassist.CtClass;
15import javassist.bytecode.ByteArray;
16import javassist.bytecode.CodeAttribute;
17import javassist.bytecode.ConstPool;
18import javassist.bytecode.LocalVariableAttribute;
19import javassist.bytecode.LocalVariableTypeAttribute;
20import cuchaz.enigma.mapping.ArgumentEntry;
21import cuchaz.enigma.mapping.BehaviorEntry;
22import cuchaz.enigma.mapping.EntryFactory;
23import cuchaz.enigma.mapping.Translator;
24
25
26public class LocalVariableRenamer {
27
28 private Translator m_translator;
29
30 public LocalVariableRenamer(Translator translator) {
31 m_translator = translator;
32 }
33
34 public void rename(CtClass c) {
35 for (CtBehavior behavior : c.getDeclaredBehaviors()) {
36
37 // if there's a local variable table, just rename everything to v1, v2, v3, ... for now
38 CodeAttribute codeAttribute = behavior.getMethodInfo().getCodeAttribute();
39 if (codeAttribute == null) {
40 continue;
41 }
42
43 BehaviorEntry behaviorEntry = EntryFactory.getBehaviorEntry(behavior);
44 ConstPool constants = c.getClassFile().getConstPool();
45
46 LocalVariableAttribute table = (LocalVariableAttribute)codeAttribute.getAttribute(LocalVariableAttribute.tag);
47 if (table != null) {
48 renameLVT(behaviorEntry, constants, table);
49 }
50
51 LocalVariableTypeAttribute typeTable = (LocalVariableTypeAttribute)codeAttribute.getAttribute(LocalVariableAttribute.typeTag);
52 if (typeTable != null) {
53 renameLVTT(typeTable, table);
54 }
55 }
56 }
57
58 // DEBUG
59 @SuppressWarnings("unused")
60 private void dumpTable(LocalVariableAttribute table) {
61 for (int i=0; i<table.tableLength(); i++) {
62 System.out.println(String.format("\t%d (%d): %s %s",
63 i, table.index(i), table.variableName(i), table.descriptor(i)
64 ));
65 }
66 }
67
68 private void renameLVT(BehaviorEntry behaviorEntry, ConstPool constants, LocalVariableAttribute table) {
69
70 // skip empty tables
71 if (table.tableLength() <= 0) {
72 return;
73 }
74
75 // where do we start counting variables?
76 int starti = 0;
77 if (table.variableName(0).equals("this")) {
78 // skip the "this" variable
79 starti = 1;
80 }
81
82 // rename method arguments first
83 int numArgs = 0;
84 if (behaviorEntry.getSignature() != null) {
85 numArgs = behaviorEntry.getSignature().getArgumentTypes().size();
86 for (int i=starti; i<starti + numArgs && i<table.tableLength(); i++) {
87 int argi = i - starti;
88 String argName = m_translator.translate(new ArgumentEntry(behaviorEntry, argi, ""));
89 if (argName == null) {
90 argName = "a" + (argi + 1);
91 }
92 renameVariable(table, i, constants.addUtf8Info(argName));
93 }
94 }
95
96 // then rename the rest of the args, if any
97 for (int i=starti + numArgs; i<table.tableLength(); i++) {
98 int firstIndex = table.index(starti + numArgs);
99 renameVariable(table, i, constants.addUtf8Info("v" + (table.index(i) - firstIndex + 1)));
100 }
101 }
102
103 private void renameLVTT(LocalVariableTypeAttribute typeTable, LocalVariableAttribute table) {
104 // rename args to the same names as in the LVT
105 for (int i=0; i<typeTable.tableLength(); i++) {
106 renameVariable(typeTable, i, getNameIndex(table, typeTable.index(i)));
107 }
108 }
109
110 private void renameVariable(LocalVariableAttribute table, int i, int stringId) {
111 // based off of LocalVariableAttribute.nameIndex()
112 ByteArray.write16bit(stringId, table.get(), i*10 + 6);
113 }
114
115 private int getNameIndex(LocalVariableAttribute table, int index) {
116 for (int i=0; i<table.tableLength(); i++) {
117 if (table.index(i) == index) {
118 return table.nameIndex(i);
119 }
120 }
121 return 0;
122 }
123}