15
15
import java .io .FileOutputStream ;
16
16
import java .io .IOException ;
17
17
import java .util .Collection ;
18
+ import java .util .Collections ;
18
19
import java .util .List ;
19
20
import java .util .Map ;
20
21
import java .util .logging .Logger ;
@@ -28,9 +29,9 @@ public class NanoTransformer implements Runnable {
28
29
private static final String LISTENER_INVOKER_TYPE = 'L' + Type .getInternalName (ListenerInvoker .class ) + ';' ;
29
30
30
31
private static final Logger LOGGER = Logger .getLogger ("NanoTransformer" );
32
+
31
33
static {
32
- if (DEBUG_TRANSFORMER )
33
- LOGGER .info ("Transformer debugging is enabled!" );
34
+ if (DEBUG_TRANSFORMER ) LOGGER .info ("Transformer debugging is enabled!" );
34
35
}
35
36
36
37
@ Override
@@ -40,7 +41,11 @@ public void run() {
40
41
Id id = evt .getId ();
41
42
List <String > listeners = NanoEvents .LISTENERS .get (id );
42
43
String invokerType = invoker .replace ('.' , '/' );
43
- ClassTinkerers .addTransformation (invokerType , node -> transformClass (listeners , node , id , find (listeners , node , id )));
44
+ ClassTinkerers .addTransformation (invokerType , node -> {
45
+ List <String > list = listeners ;
46
+ if (list == null ) list = Collections .emptyList ();
47
+ transformClass (list , node , id , find (list , node , id ));
48
+ });
44
49
}
45
50
}
46
51
@@ -64,21 +69,21 @@ public static void transformClass(List<String> listeners, ClassNode node, Id id,
64
69
MethodNode listenerInvoker = nodes .get ("listener_invoker" );
65
70
66
71
String desc = listenerInvoker == null ? invokerMethod .desc : listenerInvoker .desc ;
67
- if (single == null ) {
72
+ if (single == null ) {
68
73
String name = listenerInvoker == null ? invokerMethod .name : listenerInvoker .name ;
69
74
transform (listeners , invokerMethod .instructions , name , desc , node .name );
70
75
}
71
76
72
77
// single transformation
73
- if (single != null ) {
78
+ if (single != null ) {
74
79
String name = listenerInvoker == null ? single .name : listenerInvoker .name ;
75
80
invokerMethod .instructions = replace (single .instructions , name , node .name , desc , listeners .get (0 ));
76
81
}
77
82
78
- if (DEBUG_TRANSFORMER ) {
83
+ if (DEBUG_TRANSFORMER ) {
79
84
File file = new File ("nano_debug/" + node .name + ".class" );
80
85
File parent = file .getParentFile ();
81
- if (!parent .exists ()) parent .mkdirs ();
86
+ if (!parent .exists ()) parent .mkdirs ();
82
87
try (FileOutputStream output = new FileOutputStream (file )) {
83
88
ClassWriter writer = new ClassWriter (0 );
84
89
node .accept (writer );
@@ -141,7 +146,6 @@ private static void transform(Collection<String> listeners, InsnList insns, Stri
141
146
}
142
147
143
148
144
-
145
149
/**
146
150
* replace all of shallow recursive call with a listener reference in a newly copied list
147
151
*
@@ -159,14 +163,14 @@ private static InsnList replace(InsnList list, String nodeName, String nodeOwner
159
163
// check if method call is the right one
160
164
if (replacementNode .name .equals (nodeName ) && replacementNode .owner .equals (nodeOwner ) && replacementNode .desc .equals (nodeDescriptor )) {
161
165
// parse the listener reference
162
- int classIndex = listenerReference .indexOf ('#' );
166
+ int classIndex = listenerReference .indexOf ("::" );
163
167
if (classIndex == -1 ) {
164
168
LOGGER .severe ("Bad method signature " + listenerReference );
165
169
replacementNode .owner = "null" ;
166
170
replacementNode .name = "READ_THE_LOGS" ;
167
171
} else {
168
172
replacementNode .owner = listenerReference .substring (0 , classIndex ).replace ('.' , '/' );
169
- replacementNode .name = listenerReference .substring (classIndex + 1 );
173
+ replacementNode .name = listenerReference .substring (classIndex + 2 );
170
174
}
171
175
}
172
176
}
0 commit comments