summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/bytecode
diff options
context:
space:
mode:
authorGravatar lclc982016-07-04 18:14:22 +1000
committerGravatar lclc982016-07-04 18:14:22 +1000
commit59e189bef2b5e6d129fb7c2c988ed0b2130e36ac (patch)
tree2b638e60905251de85a4917152d6fc39a4112194 /src/main/java/cuchaz/enigma/bytecode
parentFixed Obf Class list order (diff)
downloadenigma-fork-59e189bef2b5e6d129fb7c2c988ed0b2130e36ac.tar.gz
enigma-fork-59e189bef2b5e6d129fb7c2c988ed0b2130e36ac.tar.xz
enigma-fork-59e189bef2b5e6d129fb7c2c988ed0b2130e36ac.zip
Reformat
Diffstat (limited to 'src/main/java/cuchaz/enigma/bytecode')
-rw-r--r--src/main/java/cuchaz/enigma/bytecode/CheckCastIterator.java113
-rw-r--r--src/main/java/cuchaz/enigma/bytecode/ConstPoolEditor.java26
-rw-r--r--src/main/java/cuchaz/enigma/bytecode/InfoType.java62
-rw-r--r--src/main/java/cuchaz/enigma/bytecode/accessors/ConstInfoAccessor.java47
4 files changed, 17 insertions, 231 deletions
diff --git a/src/main/java/cuchaz/enigma/bytecode/CheckCastIterator.java b/src/main/java/cuchaz/enigma/bytecode/CheckCastIterator.java
deleted file mode 100644
index c47a770..0000000
--- a/src/main/java/cuchaz/enigma/bytecode/CheckCastIterator.java
+++ /dev/null
@@ -1,113 +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 * <p>
8 * Contributors:
9 * Jeff Martin - initial API and implementation
10 ******************************************************************************/
11package cuchaz.enigma.bytecode;
12
13import java.util.Iterator;
14
15import cuchaz.enigma.bytecode.CheckCastIterator.CheckCast;
16import cuchaz.enigma.mapping.ClassEntry;
17import cuchaz.enigma.mapping.MethodEntry;
18import cuchaz.enigma.mapping.Signature;
19import javassist.bytecode.*;
20
21public class CheckCastIterator implements Iterator<CheckCast> {
22
23 public static class CheckCast {
24
25 public final String className;
26 public final MethodEntry prevMethodEntry;
27
28 public CheckCast(String className, MethodEntry prevMethodEntry) {
29 this.className = className;
30 this.prevMethodEntry = prevMethodEntry;
31 }
32 }
33
34 private final ConstPool constants;
35 private final CodeAttribute attribute;
36 private final CodeIterator iter;
37 private CheckCast next;
38
39 public CheckCastIterator(CodeAttribute codeAttribute) throws BadBytecode {
40 this.constants = codeAttribute.getConstPool();
41 this.attribute = codeAttribute;
42 this.iter = this.attribute.iterator();
43
44 this.next = getNext();
45 }
46
47 @Override
48 public boolean hasNext() {
49 return this.next != null;
50 }
51
52 @Override
53 public CheckCast next() {
54 CheckCast out = this.next;
55 try {
56 this.next = getNext();
57 } catch (BadBytecode ex) {
58 throw new Error(ex);
59 }
60 return out;
61 }
62
63 @Override
64 public void remove() {
65 throw new UnsupportedOperationException();
66 }
67
68 private CheckCast getNext() throws BadBytecode {
69 int prevPos = 0;
70 while (this.iter.hasNext()) {
71 int pos = this.iter.next();
72 int opcode = this.iter.byteAt(pos);
73 switch (opcode) {
74 case Opcode.CHECKCAST:
75
76 // get the type of this op code (next two bytes are a classinfo index)
77 MethodEntry prevMethodEntry = getMethodEntry(prevPos);
78 if (prevMethodEntry != null) {
79 return new CheckCast(this.constants.getClassInfo(this.iter.s16bitAt(pos + 1)), prevMethodEntry);
80 }
81 break;
82 }
83 prevPos = pos;
84 }
85 return null;
86 }
87
88 private MethodEntry getMethodEntry(int pos) {
89 switch (this.iter.byteAt(pos)) {
90 case Opcode.INVOKEVIRTUAL:
91 case Opcode.INVOKESTATIC:
92 case Opcode.INVOKEDYNAMIC:
93 case Opcode.INVOKESPECIAL: {
94 int index = this.iter.s16bitAt(pos + 1);
95 return new MethodEntry(
96 new ClassEntry(Descriptor.toJvmName(this.constants.getMethodrefClassName(index))),
97 this.constants.getMethodrefName(index),
98 new Signature(this.constants.getMethodrefType(index))
99 );
100 }
101
102 case Opcode.INVOKEINTERFACE: {
103 int index = this.iter.s16bitAt(pos + 1);
104 return new MethodEntry(
105 new ClassEntry(Descriptor.toJvmName(this.constants.getInterfaceMethodrefClassName(index))),
106 this.constants.getInterfaceMethodrefName(index),
107 new Signature(this.constants.getInterfaceMethodrefType(index))
108 );
109 }
110 }
111 return null;
112 }
113}
diff --git a/src/main/java/cuchaz/enigma/bytecode/ConstPoolEditor.java b/src/main/java/cuchaz/enigma/bytecode/ConstPoolEditor.java
index e49ffdb..af8c79a 100644
--- a/src/main/java/cuchaz/enigma/bytecode/ConstPoolEditor.java
+++ b/src/main/java/cuchaz/enigma/bytecode/ConstPoolEditor.java
@@ -17,7 +17,6 @@ import java.lang.reflect.Field;
17import java.lang.reflect.Method; 17import java.lang.reflect.Method;
18import java.util.HashMap; 18import java.util.HashMap;
19 19
20import cuchaz.enigma.bytecode.accessors.ClassInfoAccessor;
21import cuchaz.enigma.bytecode.accessors.ConstInfoAccessor; 20import cuchaz.enigma.bytecode.accessors.ConstInfoAccessor;
22import cuchaz.enigma.bytecode.accessors.MemberRefInfoAccessor; 21import cuchaz.enigma.bytecode.accessors.MemberRefInfoAccessor;
23import javassist.bytecode.ConstPool; 22import javassist.bytecode.ConstPool;
@@ -101,32 +100,7 @@ public class ConstPoolEditor {
101 throw new Error(ex); 100 throw new Error(ex);
102 } 101 }
103 } 102 }
104 @SuppressWarnings("rawtypes")
105 public void removeLastItem() {
106 try {
107 // remove the item from the cache
108 HashMap cache = getCache();
109 if (cache != null) {
110 Object item = getItem(this.pool.getSize() - 1);
111 cache.remove(item);
112 }
113 103
114 // remove the actual item
115 // based off of LongVector.addElement()
116 Object item = items.get(this.pool);
117 Object[][] object = (Object[][]) objects.get(items);
118 int numElements = (Integer) elements.get(items) - 1;
119 int nth = numElements >> 7;
120 int offset = numElements & (128 - 1);
121 object[nth][offset] = null;
122
123 // decrement the number of items
124 elements.set(item, numElements);
125 numItems.set(this.pool, (Integer) numItems.get(this.pool) - 1);
126 } catch (Exception ex) {
127 throw new Error(ex);
128 }
129 }
130 104
131 @SuppressWarnings("rawtypes") 105 @SuppressWarnings("rawtypes")
132 public HashMap getCache() { 106 public HashMap getCache() {
diff --git a/src/main/java/cuchaz/enigma/bytecode/InfoType.java b/src/main/java/cuchaz/enigma/bytecode/InfoType.java
index bd9e7d1..21b0417 100644
--- a/src/main/java/cuchaz/enigma/bytecode/InfoType.java
+++ b/src/main/java/cuchaz/enigma/bytecode/InfoType.java
@@ -10,23 +10,21 @@
10 ******************************************************************************/ 10 ******************************************************************************/
11package cuchaz.enigma.bytecode; 11package cuchaz.enigma.bytecode;
12 12
13import com.google.common.collect.Lists;
14import com.google.common.collect.Maps; 13import com.google.common.collect.Maps;
15 14
16import java.util.Collection; 15import java.util.Collection;
17import java.util.List;
18import java.util.Map; 16import java.util.Map;
19 17
20import cuchaz.enigma.bytecode.accessors.*; 18import cuchaz.enigma.bytecode.accessors.*;
21 19
22public enum InfoType { 20public enum InfoType {
23 21
24 Utf8Info(1, 0), 22 Utf8Info(1),
25 IntegerInfo(3, 0), 23 IntegerInfo(3),
26 FloatInfo(4, 0), 24 FloatInfo(4),
27 LongInfo(5, 0), 25 LongInfo(5),
28 DoubleInfo(6, 0), 26 DoubleInfo(6),
29 ClassInfo(7, 1) { 27 ClassInfo(7) {
30 @Override 28 @Override
31 public void gatherIndexTree(Collection<Integer> indices, ConstPoolEditor editor, ConstInfoAccessor entry) { 29 public void gatherIndexTree(Collection<Integer> indices, ConstPoolEditor editor, ConstInfoAccessor entry) {
32 ClassInfoAccessor accessor = new ClassInfoAccessor(entry.getItem()); 30 ClassInfoAccessor accessor = new ClassInfoAccessor(entry.getItem());
@@ -46,7 +44,7 @@ public enum InfoType {
46 return nameEntry != null && nameEntry.getTag() == Utf8Info.getTag(); 44 return nameEntry != null && nameEntry.getTag() == Utf8Info.getTag();
47 } 45 }
48 }, 46 },
49 StringInfo(8, 1) { 47 StringInfo(8) {
50 @Override 48 @Override
51 public void gatherIndexTree(Collection<Integer> indices, ConstPoolEditor editor, ConstInfoAccessor entry) { 49 public void gatherIndexTree(Collection<Integer> indices, ConstPoolEditor editor, ConstInfoAccessor entry) {
52 StringInfoAccessor accessor = new StringInfoAccessor(entry.getItem()); 50 StringInfoAccessor accessor = new StringInfoAccessor(entry.getItem());
@@ -66,7 +64,7 @@ public enum InfoType {
66 return stringEntry != null && stringEntry.getTag() == Utf8Info.getTag(); 64 return stringEntry != null && stringEntry.getTag() == Utf8Info.getTag();
67 } 65 }
68 }, 66 },
69 FieldRefInfo(9, 2) { 67 FieldRefInfo(9) {
70 @Override 68 @Override
71 public void gatherIndexTree(Collection<Integer> indices, ConstPoolEditor editor, ConstInfoAccessor entry) { 69 public void gatherIndexTree(Collection<Integer> indices, ConstPoolEditor editor, ConstInfoAccessor entry) {
72 MemberRefInfoAccessor accessor = new MemberRefInfoAccessor(entry.getItem()); 70 MemberRefInfoAccessor accessor = new MemberRefInfoAccessor(entry.getItem());
@@ -90,7 +88,7 @@ public enum InfoType {
90 } 88 }
91 }, 89 },
92 // same as FieldRefInfo 90 // same as FieldRefInfo
93 MethodRefInfo(10, 2) { 91 MethodRefInfo(10) {
94 @Override 92 @Override
95 public void gatherIndexTree(Collection<Integer> indices, ConstPoolEditor editor, ConstInfoAccessor entry) { 93 public void gatherIndexTree(Collection<Integer> indices, ConstPoolEditor editor, ConstInfoAccessor entry) {
96 FieldRefInfo.gatherIndexTree(indices, editor, entry); 94 FieldRefInfo.gatherIndexTree(indices, editor, entry);
@@ -107,7 +105,7 @@ public enum InfoType {
107 } 105 }
108 }, 106 },
109 // same as FieldRefInfo 107 // same as FieldRefInfo
110 InterfaceMethodRefInfo(11, 2) { 108 InterfaceMethodRefInfo(11) {
111 @Override 109 @Override
112 public void gatherIndexTree(Collection<Integer> indices, ConstPoolEditor editor, ConstInfoAccessor entry) { 110 public void gatherIndexTree(Collection<Integer> indices, ConstPoolEditor editor, ConstInfoAccessor entry) {
113 FieldRefInfo.gatherIndexTree(indices, editor, entry); 111 FieldRefInfo.gatherIndexTree(indices, editor, entry);
@@ -123,7 +121,7 @@ public enum InfoType {
123 return FieldRefInfo.subIndicesAreValid(entry, pool); 121 return FieldRefInfo.subIndicesAreValid(entry, pool);
124 } 122 }
125 }, 123 },
126 NameAndTypeInfo(12, 1) { 124 NameAndTypeInfo(12) {
127 @Override 125 @Override
128 public void gatherIndexTree(Collection<Integer> indices, ConstPoolEditor editor, ConstInfoAccessor entry) { 126 public void gatherIndexTree(Collection<Integer> indices, ConstPoolEditor editor, ConstInfoAccessor entry) {
129 NameAndTypeInfoAccessor accessor = new NameAndTypeInfoAccessor(entry.getItem()); 127 NameAndTypeInfoAccessor accessor = new NameAndTypeInfoAccessor(entry.getItem());
@@ -146,7 +144,7 @@ public enum InfoType {
146 return nameEntry != null && nameEntry.getTag() == Utf8Info.getTag() && typeEntry != null && typeEntry.getTag() == Utf8Info.getTag(); 144 return nameEntry != null && nameEntry.getTag() == Utf8Info.getTag() && typeEntry != null && typeEntry.getTag() == Utf8Info.getTag();
147 } 145 }
148 }, 146 },
149 MethodHandleInfo(15, 3) { 147 MethodHandleInfo(15) {
150 @Override 148 @Override
151 public void gatherIndexTree(Collection<Integer> indices, ConstPoolEditor editor, ConstInfoAccessor entry) { 149 public void gatherIndexTree(Collection<Integer> indices, ConstPoolEditor editor, ConstInfoAccessor entry) {
152 MethodHandleInfoAccessor accessor = new MethodHandleInfoAccessor(entry.getItem()); 150 MethodHandleInfoAccessor accessor = new MethodHandleInfoAccessor(entry.getItem());
@@ -169,7 +167,7 @@ public enum InfoType {
169 return typeEntry != null && typeEntry.getTag() == Utf8Info.getTag() && methodRefEntry != null && methodRefEntry.getTag() == MethodRefInfo.getTag(); 167 return typeEntry != null && typeEntry.getTag() == Utf8Info.getTag() && methodRefEntry != null && methodRefEntry.getTag() == MethodRefInfo.getTag();
170 } 168 }
171 }, 169 },
172 MethodTypeInfo(16, 1) { 170 MethodTypeInfo(16) {
173 @Override 171 @Override
174 public void gatherIndexTree(Collection<Integer> indices, ConstPoolEditor editor, ConstInfoAccessor entry) { 172 public void gatherIndexTree(Collection<Integer> indices, ConstPoolEditor editor, ConstInfoAccessor entry) {
175 MethodTypeInfoAccessor accessor = new MethodTypeInfoAccessor(entry.getItem()); 173 MethodTypeInfoAccessor accessor = new MethodTypeInfoAccessor(entry.getItem());
@@ -189,7 +187,7 @@ public enum InfoType {
189 return typeEntry != null && typeEntry.getTag() == Utf8Info.getTag(); 187 return typeEntry != null && typeEntry.getTag() == Utf8Info.getTag();
190 } 188 }
191 }, 189 },
192 InvokeDynamicInfo(18, 2) { 190 InvokeDynamicInfo(18) {
193 @Override 191 @Override
194 public void gatherIndexTree(Collection<Integer> indices, ConstPoolEditor editor, ConstInfoAccessor entry) { 192 public void gatherIndexTree(Collection<Integer> indices, ConstPoolEditor editor, ConstInfoAccessor entry) {
195 InvokeDynamicInfoAccessor accessor = new InvokeDynamicInfoAccessor(entry.getItem()); 193 InvokeDynamicInfoAccessor accessor = new InvokeDynamicInfoAccessor(entry.getItem());
@@ -223,21 +221,15 @@ public enum InfoType {
223 } 221 }
224 222
225 private int tag; 223 private int tag;
226 private int level;
227 224
228 InfoType(int tag, int level) { 225 InfoType(int tag) {
229 this.tag = tag; 226 this.tag = tag;
230 this.level = level;
231 } 227 }
232 228
233 public int getTag() { 229 public int getTag() {
234 return this.tag; 230 return this.tag;
235 } 231 }
236 232
237 public int getLevel() {
238 return this.level;
239 }
240
241 public void gatherIndexTree(Collection<Integer> indices, ConstPoolEditor editor, ConstInfoAccessor entry) { 233 public void gatherIndexTree(Collection<Integer> indices, ConstPoolEditor editor, ConstInfoAccessor entry) {
242 // by default, do nothing 234 // by default, do nothing
243 } 235 }
@@ -251,34 +243,10 @@ public enum InfoType {
251 return true; 243 return true;
252 } 244 }
253 245
254 public boolean selfIndexIsValid(ConstInfoAccessor entry, ConstPoolEditor pool) {
255 ConstInfoAccessor entryCheck = pool.getItem(entry.getIndex());
256 return entryCheck != null && entryCheck.getItem().equals(entry.getItem());
257 }
258
259 public static InfoType getByTag(int tag) { 246 public static InfoType getByTag(int tag) {
260 return types.get(tag); 247 return types.get(tag);
261 } 248 }
262 249
263 public static List<InfoType> getByLevel(int level) {
264 List<InfoType> types = Lists.newArrayList();
265 for (InfoType type : values()) {
266 if (type.getLevel() == level) {
267 types.add(type);
268 }
269 }
270 return types;
271 }
272
273 public static List<InfoType> getSortedByLevel() {
274 List<InfoType> types = Lists.newArrayList();
275 types.addAll(getByLevel(0));
276 types.addAll(getByLevel(1));
277 types.addAll(getByLevel(2));
278 types.addAll(getByLevel(3));
279 return types;
280 }
281
282 public static void gatherIndexTree(Collection<Integer> indices, ConstPoolEditor editor, int index) { 250 public static void gatherIndexTree(Collection<Integer> indices, ConstPoolEditor editor, int index) {
283 // add own index 251 // add own index
284 indices.add(index); 252 indices.add(index);
diff --git a/src/main/java/cuchaz/enigma/bytecode/accessors/ConstInfoAccessor.java b/src/main/java/cuchaz/enigma/bytecode/accessors/ConstInfoAccessor.java
index 2ecda1d..0ea2d02 100644
--- a/src/main/java/cuchaz/enigma/bytecode/accessors/ConstInfoAccessor.java
+++ b/src/main/java/cuchaz/enigma/bytecode/accessors/ConstInfoAccessor.java
@@ -10,8 +10,8 @@
10 ******************************************************************************/ 10 ******************************************************************************/
11package cuchaz.enigma.bytecode.accessors; 11package cuchaz.enigma.bytecode.accessors;
12 12
13import java.io.*; 13import java.io.ByteArrayOutputStream;
14import java.lang.reflect.Constructor; 14import java.io.PrintWriter;
15import java.lang.reflect.Field; 15import java.lang.reflect.Field;
16import java.lang.reflect.Method; 16import java.lang.reflect.Method;
17 17
@@ -32,26 +32,6 @@ public class ConstInfoAccessor {
32 this.item = item; 32 this.item = item;
33 } 33 }
34 34
35 public ConstInfoAccessor(DataInputStream in) throws IOException {
36 try {
37 // read the entry
38 String className = in.readUTF();
39 int oldIndex = in.readInt();
40
41 // NOTE: ConstInfo instances write a type id (a "tag"), but they don't read it back
42 // so we have to read it here
43 in.readByte();
44
45 Constructor<?> constructor = Class.forName(className).getConstructor(DataInputStream.class, int.class);
46 constructor.setAccessible(true);
47 this.item = constructor.newInstance(in, oldIndex);
48 } catch (IOException ex) {
49 throw ex;
50 } catch (Exception ex) {
51 throw new Error(ex);
52 }
53 }
54
55 public Object getItem() { 35 public Object getItem() {
56 return this.item; 36 return this.item;
57 } 37 }
@@ -64,14 +44,6 @@ public class ConstInfoAccessor {
64 } 44 }
65 } 45 }
66 46
67 public void setIndex(int val) {
68 try {
69 index.set(this.item, val);
70 } catch (Exception ex) {
71 throw new Error(ex);
72 }
73 }
74
75 public int getTag() { 47 public int getTag() {
76 try { 48 try {
77 return (Integer) getTag.invoke(this.item); 49 return (Integer) getTag.invoke(this.item);
@@ -80,21 +52,6 @@ public class ConstInfoAccessor {
80 } 52 }
81 } 53 }
82 54
83 public void write(DataOutputStream out) throws IOException {
84 try {
85 out.writeUTF(this.item.getClass().getName());
86 out.writeInt(getIndex());
87
88 Method method = this.item.getClass().getMethod("write", DataOutputStream.class);
89 method.setAccessible(true);
90 method.invoke(this.item, out);
91 } catch (IOException ex) {
92 throw ex;
93 } catch (Exception ex) {
94 throw new Error(ex);
95 }
96 }
97
98 @Override 55 @Override
99 public String toString() { 56 public String toString() {
100 try { 57 try {