2
2
3
3
import codechicken .lib .config .ConfigFile ;
4
4
import codechicken .lib .config .DefaultingConfigFile ;
5
+ import java .io .File ;
6
+ import java .io .IOException ;
7
+ import java .io .InputStream ;
8
+ import java .io .PrintWriter ;
9
+ import java .util .ArrayList ;
10
+ import java .util .List ;
11
+ import java .util .Map ;
5
12
import org .apache .logging .log4j .LogManager ;
6
13
import org .apache .logging .log4j .Logger ;
7
14
import org .objectweb .asm .ClassReader ;
12
19
import org .objectweb .asm .util .Textifier ;
13
20
import org .objectweb .asm .util .TraceClassVisitor ;
14
21
15
- import java .io .File ;
16
- import java .io .IOException ;
17
- import java .io .InputStream ;
18
- import java .io .PrintWriter ;
19
- import java .util .ArrayList ;
20
- import java .util .List ;
21
- import java .util .Map ;
22
-
23
- public class ASMHelper
24
- {
22
+ public class ASMHelper {
25
23
public static ConfigFile config = loadConfig ();
26
24
public static Logger logger = LogManager .getLogger ("CCL ASM" );
27
25
28
26
private static ConfigFile loadConfig () {
29
- try {//weak reference for environments without FML
30
- File mcDir = (File )((Object [])Class .forName ("cpw.mods.fml.relauncher.FMLInjectionData" ).getMethod ("data" ).invoke (null ))[6 ];
27
+ try { // weak reference for environments without FML
28
+ File mcDir = (File ) ((Object []) Class .forName ("cpw.mods.fml.relauncher.FMLInjectionData" )
29
+ .getMethod ("data" )
30
+ .invoke (null ))
31
+ [6 ];
31
32
File file = new File (mcDir , "config/CodeChickenLib.cfg" );
32
- if (ObfMapping .obfuscated )
33
- return new DefaultingConfigFile (file );
34
- else
35
- return new ConfigFile (file ).setComment ("CodeChickenLib development configuration file." );
33
+ if (ObfMapping .obfuscated ) return new DefaultingConfigFile (file );
34
+ else return new ConfigFile (file ).setComment ("CodeChickenLib development configuration file." );
36
35
} catch (Exception ignored ) {
37
- return null ;// no config for these systems
36
+ return null ; // no config for these systems
38
37
}
39
38
}
40
39
41
- public static interface Acceptor
42
- {
40
+ public static interface Acceptor {
43
41
public void accept (ClassVisitor cv ) throws IOException ;
44
42
}
45
43
46
44
public static MethodNode findMethod (ObfMapping methodmap , ClassNode cnode ) {
47
- for (MethodNode mnode : cnode .methods )
48
- if (methodmap .matches (mnode ))
49
- return mnode ;
45
+ for (MethodNode mnode : cnode .methods ) if (methodmap .matches (mnode )) return mnode ;
50
46
return null ;
51
47
}
52
48
53
49
public static FieldNode findField (ObfMapping fieldmap , ClassNode cnode ) {
54
- for (FieldNode fnode : cnode .fields )
55
- if (fieldmap .matches (fnode ))
56
- return fnode ;
50
+ for (FieldNode fnode : cnode .fields ) if (fieldmap .matches (fnode )) return fnode ;
57
51
return null ;
58
52
}
59
53
@@ -86,23 +80,24 @@ public static InsnList cloneInsnList(Map<LabelNode, LabelNode> labelMap, InsnLis
86
80
return new InsnListSection (list ).copy (labelMap ).list ;
87
81
}
88
82
89
- public static List <TryCatchBlockNode > cloneTryCatchBlocks (Map <LabelNode , LabelNode > labelMap , List <TryCatchBlockNode > tcblocks ) {
83
+ public static List <TryCatchBlockNode > cloneTryCatchBlocks (
84
+ Map <LabelNode , LabelNode > labelMap , List <TryCatchBlockNode > tcblocks ) {
90
85
ArrayList <TryCatchBlockNode > clone = new ArrayList <TryCatchBlockNode >();
91
86
for (TryCatchBlockNode node : tcblocks )
92
87
clone .add (new TryCatchBlockNode (
93
- labelMap .get (node .start ),
94
- labelMap .get (node .end ),
95
- labelMap .get (node .handler ),
96
- node .type ));
88
+ labelMap .get (node .start ), labelMap .get (node .end ), labelMap .get (node .handler ), node .type ));
97
89
98
90
return clone ;
99
91
}
100
92
101
- public static List <LocalVariableNode > cloneLocals (Map <LabelNode , LabelNode > labelMap , List <LocalVariableNode > locals ) {
93
+ public static List <LocalVariableNode > cloneLocals (
94
+ Map <LabelNode , LabelNode > labelMap , List <LocalVariableNode > locals ) {
102
95
ArrayList <LocalVariableNode > clone = new ArrayList <LocalVariableNode >(locals .size ());
103
96
for (LocalVariableNode node : locals )
104
97
clone .add (new LocalVariableNode (
105
- node .name , node .desc , node .signature ,
98
+ node .name ,
99
+ node .desc ,
100
+ node .signature ,
106
101
labelMap .get (node .start ),
107
102
labelMap .get (node .end ),
108
103
node .index ));
@@ -114,8 +109,7 @@ public static void copy(MethodNode src, MethodNode dst) {
114
109
Map <LabelNode , LabelNode > labelMap = cloneLabels (src .instructions );
115
110
dst .instructions = cloneInsnList (labelMap , src .instructions );
116
111
dst .tryCatchBlocks = cloneTryCatchBlocks (labelMap , src .tryCatchBlocks );
117
- if (src .localVariables != null )
118
- dst .localVariables = cloneLocals (labelMap , src .localVariables );
112
+ if (src .localVariables != null ) dst .localVariables = cloneLocals (labelMap , src .localVariables );
119
113
dst .visibleAnnotations = src .visibleAnnotations ;
120
114
dst .invisibleAnnotations = src .invisibleAnnotations ;
121
115
dst .visitMaxs (src .maxStack , src .maxLocals );
@@ -130,7 +124,8 @@ public static int getLocal(List<LocalVariableNode> list, String name) {
130
124
for (LocalVariableNode node : list ) {
131
125
if (node .name .equals (name )) {
132
126
if (found >= 0 )
133
- throw new RuntimeException ("Duplicate local variable: " + name + " not coded to handle this scenario." );
127
+ throw new RuntimeException (
128
+ "Duplicate local variable: " + name + " not coded to handle this scenario." );
134
129
135
130
found = node .index ;
136
131
}
@@ -140,24 +135,21 @@ public static int getLocal(List<LocalVariableNode> list, String name) {
140
135
141
136
public static void replaceMethod (MethodNode original , MethodNode replacement ) {
142
137
original .instructions .clear ();
143
- if (original .localVariables != null )
144
- original .localVariables .clear ();
145
- if (original .tryCatchBlocks != null )
146
- original .tryCatchBlocks .clear ();
138
+ if (original .localVariables != null ) original .localVariables .clear ();
139
+ if (original .tryCatchBlocks != null ) original .tryCatchBlocks .clear ();
147
140
replacement .accept (original );
148
141
}
149
142
150
- public static void dump (Acceptor acceptor , File file , boolean filterImportant , boolean sortLocals , boolean textify ) {
143
+ public static void dump (
144
+ Acceptor acceptor , File file , boolean filterImportant , boolean sortLocals , boolean textify ) {
151
145
try {
152
- if (!file .getParentFile ().exists ())
153
- file .getParentFile ().mkdirs ();
154
- if (!file .exists ())
155
- file .createNewFile ();
146
+ if (!file .getParentFile ().exists ()) file .getParentFile ().mkdirs ();
147
+ if (!file .exists ()) file .createNewFile ();
156
148
157
149
PrintWriter pout = new PrintWriter (file );
158
150
ClassVisitor cv = new TraceClassVisitor (null , textify ? new Textifier () : new ASMifier (), pout );
159
- if (filterImportant ) cv = new ImportantInsnVisitor (cv );
160
- if (sortLocals ) cv = new LocalVariablesSorterVisitor (cv );
151
+ if (filterImportant ) cv = new ImportantInsnVisitor (cv );
152
+ if (sortLocals ) cv = new LocalVariablesSorterVisitor (cv );
161
153
acceptor .accept (cv );
162
154
pout .close ();
163
155
} catch (IOException e ) {
@@ -166,36 +158,50 @@ public static void dump(Acceptor acceptor, File file, boolean filterImportant, b
166
158
}
167
159
168
160
public static void dump (Acceptor acceptor , File file , boolean filterImportant , boolean sortLocals ) {
169
- dump (acceptor , file , filterImportant , sortLocals , config .getTag ("textify" ).getBooleanValue (true ));
161
+ dump (
162
+ acceptor ,
163
+ file ,
164
+ filterImportant ,
165
+ sortLocals ,
166
+ config .getTag ("textify" ).getBooleanValue (true ));
170
167
}
171
168
172
169
public static void dump (final byte [] bytes , File file , boolean filterImportant , boolean sortLocals ) {
173
- dump (new Acceptor ()
174
- {
175
- @ Override
176
- public void accept (ClassVisitor cv ) {
177
- new ClassReader (bytes ).accept (cv , ClassReader .EXPAND_FRAMES );
178
- }
179
- }, file , filterImportant , sortLocals );
170
+ dump (
171
+ new Acceptor () {
172
+ @ Override
173
+ public void accept (ClassVisitor cv ) {
174
+ new ClassReader (bytes ).accept (cv , ClassReader .EXPAND_FRAMES );
175
+ }
176
+ },
177
+ file ,
178
+ filterImportant ,
179
+ sortLocals );
180
180
}
181
181
182
182
public static void dump (final InputStream is , File file , boolean filterImportant , boolean sortLocals ) {
183
- dump (new Acceptor ()
184
- {
185
- @ Override
186
- public void accept (ClassVisitor cv ) throws IOException {
187
- new ClassReader (is ).accept (cv , ClassReader .EXPAND_FRAMES );
188
- }
189
- }, file , filterImportant , sortLocals );
183
+ dump (
184
+ new Acceptor () {
185
+ @ Override
186
+ public void accept (ClassVisitor cv ) throws IOException {
187
+ new ClassReader (is ).accept (cv , ClassReader .EXPAND_FRAMES );
188
+ }
189
+ },
190
+ file ,
191
+ filterImportant ,
192
+ sortLocals );
190
193
}
191
194
192
195
public static void dump (final ClassNode cnode , File file , boolean filterImportant , boolean sortLocals ) {
193
- dump (new Acceptor ()
194
- {
195
- @ Override
196
- public void accept (ClassVisitor cv ) {
197
- cnode .accept (cv );
198
- }
199
- }, file , filterImportant , sortLocals );
196
+ dump (
197
+ new Acceptor () {
198
+ @ Override
199
+ public void accept (ClassVisitor cv ) {
200
+ cnode .accept (cv );
201
+ }
202
+ },
203
+ file ,
204
+ filterImportant ,
205
+ sortLocals );
200
206
}
201
207
}
0 commit comments