summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/bytecode
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/cuchaz/enigma/bytecode')
-rw-r--r--src/main/java/cuchaz/enigma/bytecode/CheckCastIterator.java14
-rw-r--r--src/main/java/cuchaz/enigma/bytecode/ClassRenamer.java10
-rw-r--r--src/main/java/cuchaz/enigma/bytecode/ConstPoolEditor.java96
-rw-r--r--src/main/java/cuchaz/enigma/bytecode/accessors/ClassInfoAccessor.java4
-rw-r--r--src/main/java/cuchaz/enigma/bytecode/accessors/ConstInfoAccessor.java23
-rw-r--r--src/main/java/cuchaz/enigma/bytecode/accessors/InvokeDynamicInfoAccessor.java4
-rw-r--r--src/main/java/cuchaz/enigma/bytecode/accessors/MemberRefInfoAccessor.java4
-rw-r--r--src/main/java/cuchaz/enigma/bytecode/accessors/MethodHandleInfoAccessor.java4
-rw-r--r--src/main/java/cuchaz/enigma/bytecode/accessors/MethodTypeInfoAccessor.java4
-rw-r--r--src/main/java/cuchaz/enigma/bytecode/accessors/NameAndTypeInfoAccessor.java4
-rw-r--r--src/main/java/cuchaz/enigma/bytecode/accessors/StringInfoAccessor.java4
-rw-r--r--src/main/java/cuchaz/enigma/bytecode/accessors/Utf8InfoAccessor.java28
12 files changed, 5 insertions, 194 deletions
diff --git a/src/main/java/cuchaz/enigma/bytecode/CheckCastIterator.java b/src/main/java/cuchaz/enigma/bytecode/CheckCastIterator.java
index 19c39d3..c47a770 100644
--- a/src/main/java/cuchaz/enigma/bytecode/CheckCastIterator.java
+++ b/src/main/java/cuchaz/enigma/bytecode/CheckCastIterator.java
@@ -22,8 +22,8 @@ public class CheckCastIterator implements Iterator<CheckCast> {
22 22
23 public static class CheckCast { 23 public static class CheckCast {
24 24
25 public String className; 25 public final String className;
26 public MethodEntry prevMethodEntry; 26 public final MethodEntry prevMethodEntry;
27 27
28 public CheckCast(String className, MethodEntry prevMethodEntry) { 28 public CheckCast(String className, MethodEntry prevMethodEntry) {
29 this.className = className; 29 this.className = className;
@@ -31,9 +31,9 @@ public class CheckCastIterator implements Iterator<CheckCast> {
31 } 31 }
32 } 32 }
33 33
34 private ConstPool constants; 34 private final ConstPool constants;
35 private CodeAttribute attribute; 35 private final CodeAttribute attribute;
36 private CodeIterator iter; 36 private final CodeIterator iter;
37 private CheckCast next; 37 private CheckCast next;
38 38
39 public CheckCastIterator(CodeAttribute codeAttribute) throws BadBytecode { 39 public CheckCastIterator(CodeAttribute codeAttribute) throws BadBytecode {
@@ -110,8 +110,4 @@ public class CheckCastIterator implements Iterator<CheckCast> {
110 } 110 }
111 return null; 111 return null;
112 } 112 }
113
114 public Iterable<CheckCast> casts() {
115 return () -> CheckCastIterator.this;
116 }
117} 113}
diff --git a/src/main/java/cuchaz/enigma/bytecode/ClassRenamer.java b/src/main/java/cuchaz/enigma/bytecode/ClassRenamer.java
index d8e7971..944e486 100644
--- a/src/main/java/cuchaz/enigma/bytecode/ClassRenamer.java
+++ b/src/main/java/cuchaz/enigma/bytecode/ClassRenamer.java
@@ -90,16 +90,6 @@ public class ClassRenamer {
90 }); 90 });
91 } 91 }
92 92
93 public static void moveAllClassesIntoDefaultPackage(CtClass c, final String oldPackageName) {
94 renameClasses(c, className -> {
95 ClassEntry entry = new ClassEntry(className);
96 if (entry.getPackageName().equals(oldPackageName)) {
97 return entry.getSimpleName();
98 }
99 return null;
100 });
101 }
102
103 @SuppressWarnings("unchecked") 93 @SuppressWarnings("unchecked")
104 public static void renameClasses(CtClass c, ClassNameReplacer replacer) { 94 public static void renameClasses(CtClass c, ClassNameReplacer replacer) {
105 95
diff --git a/src/main/java/cuchaz/enigma/bytecode/ConstPoolEditor.java b/src/main/java/cuchaz/enigma/bytecode/ConstPoolEditor.java
index 256df61..e49ffdb 100644
--- a/src/main/java/cuchaz/enigma/bytecode/ConstPoolEditor.java
+++ b/src/main/java/cuchaz/enigma/bytecode/ConstPoolEditor.java
@@ -78,22 +78,6 @@ public class ConstPoolEditor {
78 this.pool = pool; 78 this.pool = pool;
79 } 79 }
80 80
81 public void writePool(DataOutputStream out) {
82 try {
83 methodWritePool.invoke(this.pool, out);
84 } catch (Exception ex) {
85 throw new Error(ex);
86 }
87 }
88
89 public static ConstPool readPool(DataInputStream in) {
90 try {
91 return constructorPool.newInstance(in);
92 } catch (Exception ex) {
93 throw new Error(ex);
94 }
95 }
96
97 public String getMemberrefClassname(int memberrefIndex) { 81 public String getMemberrefClassname(int memberrefIndex) {
98 return Descriptor.toJvmName(this.pool.getClassInfo(this.pool.getMemberClass(memberrefIndex))); 82 return Descriptor.toJvmName(this.pool.getClassInfo(this.pool.getMemberClass(memberrefIndex)));
99 } 83 }
@@ -117,23 +101,6 @@ public class ConstPoolEditor {
117 throw new Error(ex); 101 throw new Error(ex);
118 } 102 }
119 } 103 }
120
121 public int addItem(Object item) {
122 try {
123 return (Integer) addItem.invoke(this.pool, item);
124 } catch (Exception ex) {
125 throw new Error(ex);
126 }
127 }
128
129 public int addItemForceNew(Object item) {
130 try {
131 return (Integer) addItem0.invoke(this.pool, item);
132 } catch (Exception ex) {
133 throw new Error(ex);
134 }
135 }
136
137 @SuppressWarnings("rawtypes") 104 @SuppressWarnings("rawtypes")
138 public void removeLastItem() { 105 public void removeLastItem() {
139 try { 106 try {
@@ -197,67 +164,4 @@ public class ConstPoolEditor {
197 assert (newName.equals(getMemberrefName(memberrefIndex))); 164 assert (newName.equals(getMemberrefName(memberrefIndex)));
198 assert (newType.equals(getMemberrefType(memberrefIndex))); 165 assert (newType.equals(getMemberrefType(memberrefIndex)));
199 } 166 }
200
201 @SuppressWarnings({"rawtypes", "unchecked"})
202 public void changeClassName(int classNameIndex, String newName) {
203 // NOTE: when changing values, we always need to copy-on-write
204 try {
205 // get the class item
206 Object item = getItem(classNameIndex).getItem();
207
208 // update the cache
209 HashMap cache = getCache();
210 if (cache != null) {
211 cache.remove(item);
212 }
213
214 // add the new name and repoint the name-and-type to it
215 new ClassInfoAccessor(item).setNameIndex(this.pool.addUtf8Info(newName));
216
217 // update the cache
218 if (cache != null) {
219 cache.put(item, item);
220 }
221 } catch (Exception ex) {
222 throw new Error(ex);
223 }
224 }
225
226 public static ConstPool newConstPool() {
227 // const pool expects the name of a class to initialize itself
228 // but we want an empty pool
229 // so give it a bogus name, and then clear the entries afterwards
230 ConstPool pool = new ConstPool("a");
231
232 ConstPoolEditor editor = new ConstPoolEditor(pool);
233 int size = pool.getSize();
234 for (int i = 0; i < size - 1; i++) {
235 editor.removeLastItem();
236 }
237
238 // make sure the pool is actually empty
239 // although, in this case "empty" means one thing in it
240 // the JVM spec says index 0 should be reserved
241 assert (pool.getSize() == 1);
242 assert (editor.getItem(0) == null);
243 assert (editor.getItem(1) == null);
244 assert (editor.getItem(2) == null);
245 assert (editor.getItem(3) == null);
246
247 // also, clear the cache
248 editor.getCache().clear();
249
250 return pool;
251 }
252
253 public String dump() {
254 StringBuilder buf = new StringBuilder();
255 for (int i = 1; i < this.pool.getSize(); i++) {
256 buf.append(String.format("%4d", i));
257 buf.append(" ");
258 buf.append(getItem(i).toString());
259 buf.append("\n");
260 }
261 return buf.toString();
262 }
263} 167}
diff --git a/src/main/java/cuchaz/enigma/bytecode/accessors/ClassInfoAccessor.java b/src/main/java/cuchaz/enigma/bytecode/accessors/ClassInfoAccessor.java
index 66f2283..316bb5e 100644
--- a/src/main/java/cuchaz/enigma/bytecode/accessors/ClassInfoAccessor.java
+++ b/src/main/java/cuchaz/enigma/bytecode/accessors/ClassInfoAccessor.java
@@ -39,10 +39,6 @@ public class ClassInfoAccessor {
39 } 39 }
40 } 40 }
41 41
42 public static boolean isType(ConstInfoAccessor accessor) {
43 return clazz.isAssignableFrom(accessor.getItem().getClass());
44 }
45
46 static { 42 static {
47 try { 43 try {
48 clazz = Class.forName("javassist.bytecode.ClassInfo"); 44 clazz = Class.forName("javassist.bytecode.ClassInfo");
diff --git a/src/main/java/cuchaz/enigma/bytecode/accessors/ConstInfoAccessor.java b/src/main/java/cuchaz/enigma/bytecode/accessors/ConstInfoAccessor.java
index ea775e9..2ecda1d 100644
--- a/src/main/java/cuchaz/enigma/bytecode/accessors/ConstInfoAccessor.java
+++ b/src/main/java/cuchaz/enigma/bytecode/accessors/ConstInfoAccessor.java
@@ -80,29 +80,6 @@ public class ConstInfoAccessor {
80 } 80 }
81 } 81 }
82 82
83 public ConstInfoAccessor copy() {
84 return new ConstInfoAccessor(copyItem());
85 }
86
87 public Object copyItem() {
88 // I don't know of a simpler way to copy one of these silly things...
89 try {
90 // serialize the item
91 ByteArrayOutputStream buf = new ByteArrayOutputStream();
92 DataOutputStream out = new DataOutputStream(buf);
93 write(out);
94
95 // deserialize the item
96 DataInputStream in = new DataInputStream(new ByteArrayInputStream(buf.toByteArray()));
97 Object item = new ConstInfoAccessor(in).getItem();
98 in.close();
99
100 return item;
101 } catch (Exception ex) {
102 throw new Error(ex);
103 }
104 }
105
106 public void write(DataOutputStream out) throws IOException { 83 public void write(DataOutputStream out) throws IOException {
107 try { 84 try {
108 out.writeUTF(this.item.getClass().getName()); 85 out.writeUTF(this.item.getClass().getName());
diff --git a/src/main/java/cuchaz/enigma/bytecode/accessors/InvokeDynamicInfoAccessor.java b/src/main/java/cuchaz/enigma/bytecode/accessors/InvokeDynamicInfoAccessor.java
index 69aee16..a158394 100644
--- a/src/main/java/cuchaz/enigma/bytecode/accessors/InvokeDynamicInfoAccessor.java
+++ b/src/main/java/cuchaz/enigma/bytecode/accessors/InvokeDynamicInfoAccessor.java
@@ -57,10 +57,6 @@ public class InvokeDynamicInfoAccessor {
57 } 57 }
58 } 58 }
59 59
60 public static boolean isType(ConstInfoAccessor accessor) {
61 return clazz.isAssignableFrom(accessor.getItem().getClass());
62 }
63
64 static { 60 static {
65 try { 61 try {
66 clazz = Class.forName("javassist.bytecode.InvokeDynamicInfo"); 62 clazz = Class.forName("javassist.bytecode.InvokeDynamicInfo");
diff --git a/src/main/java/cuchaz/enigma/bytecode/accessors/MemberRefInfoAccessor.java b/src/main/java/cuchaz/enigma/bytecode/accessors/MemberRefInfoAccessor.java
index 0e0297b..2835508 100644
--- a/src/main/java/cuchaz/enigma/bytecode/accessors/MemberRefInfoAccessor.java
+++ b/src/main/java/cuchaz/enigma/bytecode/accessors/MemberRefInfoAccessor.java
@@ -56,10 +56,6 @@ public class MemberRefInfoAccessor {
56 } 56 }
57 } 57 }
58 58
59 public static boolean isType(ConstInfoAccessor accessor) {
60 return clazz.isAssignableFrom(accessor.getItem().getClass());
61 }
62
63 static { 59 static {
64 try { 60 try {
65 clazz = Class.forName("javassist.bytecode.MemberrefInfo"); 61 clazz = Class.forName("javassist.bytecode.MemberrefInfo");
diff --git a/src/main/java/cuchaz/enigma/bytecode/accessors/MethodHandleInfoAccessor.java b/src/main/java/cuchaz/enigma/bytecode/accessors/MethodHandleInfoAccessor.java
index 9a7dd69..a203b43 100644
--- a/src/main/java/cuchaz/enigma/bytecode/accessors/MethodHandleInfoAccessor.java
+++ b/src/main/java/cuchaz/enigma/bytecode/accessors/MethodHandleInfoAccessor.java
@@ -56,10 +56,6 @@ public class MethodHandleInfoAccessor {
56 } 56 }
57 } 57 }
58 58
59 public static boolean isType(ConstInfoAccessor accessor) {
60 return clazz.isAssignableFrom(accessor.getItem().getClass());
61 }
62
63 static { 59 static {
64 try { 60 try {
65 clazz = Class.forName("javassist.bytecode.MethodHandleInfo"); 61 clazz = Class.forName("javassist.bytecode.MethodHandleInfo");
diff --git a/src/main/java/cuchaz/enigma/bytecode/accessors/MethodTypeInfoAccessor.java b/src/main/java/cuchaz/enigma/bytecode/accessors/MethodTypeInfoAccessor.java
index 5ec9c3b..993c79b 100644
--- a/src/main/java/cuchaz/enigma/bytecode/accessors/MethodTypeInfoAccessor.java
+++ b/src/main/java/cuchaz/enigma/bytecode/accessors/MethodTypeInfoAccessor.java
@@ -39,10 +39,6 @@ public class MethodTypeInfoAccessor {
39 } 39 }
40 } 40 }
41 41
42 public static boolean isType(ConstInfoAccessor accessor) {
43 return clazz.isAssignableFrom(accessor.getItem().getClass());
44 }
45
46 static { 42 static {
47 try { 43 try {
48 clazz = Class.forName("javassist.bytecode.MethodTypeInfo"); 44 clazz = Class.forName("javassist.bytecode.MethodTypeInfo");
diff --git a/src/main/java/cuchaz/enigma/bytecode/accessors/NameAndTypeInfoAccessor.java b/src/main/java/cuchaz/enigma/bytecode/accessors/NameAndTypeInfoAccessor.java
index 95df37c..d6c2531 100644
--- a/src/main/java/cuchaz/enigma/bytecode/accessors/NameAndTypeInfoAccessor.java
+++ b/src/main/java/cuchaz/enigma/bytecode/accessors/NameAndTypeInfoAccessor.java
@@ -56,10 +56,6 @@ public class NameAndTypeInfoAccessor {
56 } 56 }
57 } 57 }
58 58
59 public static boolean isType(ConstInfoAccessor accessor) {
60 return clazz.isAssignableFrom(accessor.getItem().getClass());
61 }
62
63 static { 59 static {
64 try { 60 try {
65 clazz = Class.forName("javassist.bytecode.NameAndTypeInfo"); 61 clazz = Class.forName("javassist.bytecode.NameAndTypeInfo");
diff --git a/src/main/java/cuchaz/enigma/bytecode/accessors/StringInfoAccessor.java b/src/main/java/cuchaz/enigma/bytecode/accessors/StringInfoAccessor.java
index 1c55a44..e211381 100644
--- a/src/main/java/cuchaz/enigma/bytecode/accessors/StringInfoAccessor.java
+++ b/src/main/java/cuchaz/enigma/bytecode/accessors/StringInfoAccessor.java
@@ -39,10 +39,6 @@ public class StringInfoAccessor {
39 } 39 }
40 } 40 }
41 41
42 public static boolean isType(ConstInfoAccessor accessor) {
43 return clazz.isAssignableFrom(accessor.getItem().getClass());
44 }
45
46 static { 42 static {
47 try { 43 try {
48 clazz = Class.forName("javassist.bytecode.StringInfo"); 44 clazz = Class.forName("javassist.bytecode.StringInfo");
diff --git a/src/main/java/cuchaz/enigma/bytecode/accessors/Utf8InfoAccessor.java b/src/main/java/cuchaz/enigma/bytecode/accessors/Utf8InfoAccessor.java
deleted file mode 100644
index 7a2cb66..0000000
--- a/src/main/java/cuchaz/enigma/bytecode/accessors/Utf8InfoAccessor.java
+++ /dev/null
@@ -1,28 +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.accessors;
12
13public class Utf8InfoAccessor {
14
15 private static Class<?> clazz;
16
17 static {
18 try {
19 clazz = Class.forName("javassist.bytecode.Utf8Info");
20 } catch (Exception ex) {
21 throw new Error(ex);
22 }
23 }
24
25 public static boolean isType(ConstInfoAccessor accessor) {
26 return clazz.isAssignableFrom(accessor.getItem().getClass());
27 }
28}