/
ClassNodeEx.java
469 lines (417 loc) · 16.1 KB
/
ClassNodeEx.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package com.github.mouse0w0.asm.extree;
import org.objectweb.asm.*;
import org.objectweb.asm.commons.Method;
import org.objectweb.asm.tree.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class ClassNodeEx extends ClassVisitor {
/**
* The class version. The minor version is stored in the 16 most significant bits, and the major
* version in the 16 least significant bits.
*/
public int version;
/**
* The class's access flags (see {@link org.objectweb.asm.Opcodes}). This field also indicates if
* the class is deprecated {@link Opcodes#ACC_DEPRECATED} or a record {@link Opcodes#ACC_RECORD}.
*/
public int access;
/**
* The internal name of this class (see {@link org.objectweb.asm.Type#getInternalName}).
*/
public String name;
/**
* The signature of this class. May be {@literal null}.
*/
public String signature;
/**
* The internal of name of the super class (see {@link org.objectweb.asm.Type#getInternalName}).
* For interfaces, the super class is {@link Object}. May be {@literal null}, but only for the
* {@link Object} class.
*/
public String superName;
/**
* The internal names of the interfaces directly implemented by this class (see {@link
* org.objectweb.asm.Type#getInternalName}).
*/
public List<String> interfaces;
/**
* The name of the source file from which this class was compiled. May be {@literal null}.
*/
public String sourceFile;
/**
* The correspondence between source and compiled elements of this class. May be {@literal null}.
*/
public String sourceDebug;
/**
* The module stored in this class. May be {@literal null}.
*/
public ModuleNode module;
/**
* The internal name of the enclosing class of this class. May be {@literal null}.
*/
public String outerClass;
/**
* The name of the method that contains this class, or {@literal null} if this class is not
* enclosed in a method.
*/
public String outerMethod;
/**
* The descriptor of the method that contains this class, or {@literal null} if this class is not
* enclosed in a method.
*/
public String outerMethodDesc;
public Map<String, AnnotationNodeEx> annotations;
/**
* The runtime visible type annotations of this class. May be {@literal null}.
*/
public List<TypeAnnotationNode> visibleTypeAnnotations;
/**
* The runtime invisible type annotations of this class. May be {@literal null}.
*/
public List<TypeAnnotationNode> invisibleTypeAnnotations;
/**
* The non standard attributes of this class. May be {@literal null}.
*/
public List<Attribute> attrs;
/**
* The inner classes of this class.
*/
public List<InnerClassNode> innerClasses;
/**
* The internal name of the nest host class of this class. May be {@literal null}.
*/
public String nestHostClass;
/**
* The internal names of the nest members of this class. May be {@literal null}.
*/
public List<String> nestMembers;
/**
* <b>Experimental, use at your own risk. This method will be renamed when it becomes stable, this
* will break existing code using it</b>. The internal names of the permitted subtypes of this
* class. May be {@literal null}.
*
* @deprecated this API is experimental.
*/
@Deprecated
public List<String> permittedSubtypesExperimental;
/**
* The record components of this class. May be {@literal null}.
*/
public List<RecordComponentNode> recordComponents;
/**
* The fields of this class.
*/
public Map<String, FieldNodeEx> fields;
/**
* The methods of this class.
*/
public Map<Method, MethodNodeEx> methods;
/**
* Constructs a new {@link org.objectweb.asm.tree.ClassNode}. <i>Subclasses must not use this constructor</i>. Instead,
* they must use the {@link #ClassNodeEx(int)} version.
*
* @throws IllegalStateException If a subclass calls this constructor.
*/
public ClassNodeEx() {
this(Opcodes.ASM8);
}
/**
* Constructs a new {@link org.objectweb.asm.tree.ClassNode}.
*
* @param api the ASM API version implemented by this visitor. Must be one of {@link
* Opcodes#ASM4}, {@link Opcodes#ASM5}, {@link Opcodes#ASM6}, {@link Opcodes#ASM7} or {@link
* Opcodes#ASM8}.
*/
public ClassNodeEx(final int api) {
super(api);
this.interfaces = new ArrayList<>();
this.innerClasses = new ArrayList<>();
this.fields = new LinkedHashMap<>();
this.methods = new LinkedHashMap<>();
}
public AnnotationNodeEx getAnnotation(String descriptor) {
return annotations == null ? null : annotations.get(descriptor);
}
public void addAnnotation(AnnotationNodeEx annotationNode) {
if (annotations == null) {
annotations = new LinkedHashMap<>(2);
}
annotations.put(annotationNode.desc, annotationNode);
}
public FieldNodeEx getField(String name) {
return fields.get(name);
}
public void addField(FieldNodeEx fieldNode) {
fields.put(fieldNode.name, fieldNode);
}
public MethodNodeEx getMethod(Method method) {
return methods.get(method);
}
public void addMethod(MethodNodeEx methodNode) {
methods.put(new Method(methodNode.name, methodNode.desc), methodNode);
}
// -----------------------------------------------------------------------------------------------
// Implementation of the ClassVisitor abstract class
// -----------------------------------------------------------------------------------------------
@Override
public void visit(
final int version,
final int access,
final String name,
final String signature,
final String superName,
final String[] interfaces) {
this.version = version;
this.access = access;
this.name = name;
this.signature = signature;
this.superName = superName;
this.interfaces = Util.asArrayList(interfaces);
}
@Override
public void visitSource(final String file, final String debug) {
sourceFile = file;
sourceDebug = debug;
}
@Override
public ModuleVisitor visitModule(final String name, final int access, final String version) {
module = new ModuleNode(name, access, version);
return module;
}
@Override
public void visitNestHost(final String nestHost) {
this.nestHostClass = nestHost;
}
@Override
public void visitOuterClass(final String owner, final String name, final String descriptor) {
outerClass = owner;
outerMethod = name;
outerMethodDesc = descriptor;
}
@Override
public AnnotationVisitor visitAnnotation(final String descriptor, final boolean visible) {
AnnotationNodeEx annotation = new AnnotationNodeEx(descriptor, visible);
if (annotations == null) {
annotations = new LinkedHashMap<>(2);
}
annotations.put(descriptor, annotation);
return annotation;
}
@Override
public AnnotationVisitor visitTypeAnnotation(
final int typeRef, final TypePath typePath, final String descriptor, final boolean visible) {
TypeAnnotationNode typeAnnotation = new TypeAnnotationNode(typeRef, typePath, descriptor);
if (visible) {
visibleTypeAnnotations = Util.add(visibleTypeAnnotations, typeAnnotation);
} else {
invisibleTypeAnnotations = Util.add(invisibleTypeAnnotations, typeAnnotation);
}
return typeAnnotation;
}
@Override
public void visitAttribute(final Attribute attribute) {
attrs = Util.add(attrs, attribute);
}
@Override
public void visitNestMember(final String nestMember) {
nestMembers = Util.add(nestMembers, nestMember);
}
/**
* <b>Experimental, use at your own risk.</b>.
*
* @param permittedSubtype the internal name of a permitted subtype.
* @deprecated this API is experimental.
*/
@Override
@Deprecated
public void visitPermittedSubtypeExperimental(final String permittedSubtype) {
permittedSubtypesExperimental = Util.add(permittedSubtypesExperimental, permittedSubtype);
}
@Override
public void visitInnerClass(
final String name, final String outerName, final String innerName, final int access) {
InnerClassNode innerClass = new InnerClassNode(name, outerName, innerName, access);
innerClasses.add(innerClass);
}
@Override
public RecordComponentVisitor visitRecordComponent(
final String name, final String descriptor, final String signature) {
RecordComponentNode recordComponent = new RecordComponentNode(name, descriptor, signature);
recordComponents = Util.add(recordComponents, recordComponent);
return recordComponent;
}
@Override
public FieldVisitor visitField(
final int access,
final String name,
final String descriptor,
final String signature,
final Object value) {
FieldNodeEx field = new FieldNodeEx(access, name, descriptor, signature, value);
fields.put(name, field);
return field;
}
@Override
public MethodVisitor visitMethod(
final int access,
final String name,
final String descriptor,
final String signature,
final String[] exceptions) {
MethodNodeEx method = new MethodNodeEx(access, name, descriptor, signature, exceptions);
methods.put(new Method(name, descriptor), method);
return method;
}
@Override
public void visitEnd() {
// Nothing to do.
}
// -----------------------------------------------------------------------------------------------
// Accept method
// -----------------------------------------------------------------------------------------------
/**
* Checks that this class node is compatible with the given ASM API version. This method checks
* that this node, and all its children recursively, do not contain elements that were introduced
* in more recent versions of the ASM API than the given version.
*
* @param api an ASM API version. Must be one of {@link Opcodes#ASM4}, {@link Opcodes#ASM5},
* {@link Opcodes#ASM6}, {@link Opcodes#ASM7}. or {@link Opcodes#ASM8}.
*/
public void check(final int api) {
if (api != Opcodes.ASM9_EXPERIMENTAL && permittedSubtypesExperimental != null) {
throw new UnsupportedClassVersionException();
}
if (api < Opcodes.ASM8 && ((access & Opcodes.ACC_RECORD) != 0 || recordComponents != null)) {
throw new UnsupportedClassVersionException();
}
if (api < Opcodes.ASM7 && (nestHostClass != null || nestMembers != null)) {
throw new UnsupportedClassVersionException();
}
if (api < Opcodes.ASM6 && module != null) {
throw new UnsupportedClassVersionException();
}
if (api < Opcodes.ASM5) {
if (visibleTypeAnnotations != null && !visibleTypeAnnotations.isEmpty()) {
throw new UnsupportedClassVersionException();
}
if (invisibleTypeAnnotations != null && !invisibleTypeAnnotations.isEmpty()) {
throw new UnsupportedClassVersionException();
}
}
// Check the annotations.
if (annotations != null) {
for (AnnotationNodeEx annotation : annotations.values()) {
annotation.check(api);
}
}
if (visibleTypeAnnotations != null) {
for (int i = visibleTypeAnnotations.size() - 1; i >= 0; --i) {
visibleTypeAnnotations.get(i).check(api);
}
}
if (invisibleTypeAnnotations != null) {
for (int i = invisibleTypeAnnotations.size() - 1; i >= 0; --i) {
invisibleTypeAnnotations.get(i).check(api);
}
}
if (recordComponents != null) {
for (int i = recordComponents.size() - 1; i >= 0; --i) {
recordComponents.get(i).check(api);
}
}
for (int i = fields.size() - 1; i >= 0; --i) {
fields.get(i).check(api);
}
for (int i = methods.size() - 1; i >= 0; --i) {
methods.get(i).check(api);
}
}
/**
* Makes the given class visitor visit this class.
*
* @param classVisitor a class visitor.
*/
public void accept(final ClassVisitor classVisitor) {
// Visit the header.
String[] interfacesArray = new String[this.interfaces.size()];
this.interfaces.toArray(interfacesArray);
classVisitor.visit(version, access, name, signature, superName, interfacesArray);
// Visit the source.
if (sourceFile != null || sourceDebug != null) {
classVisitor.visitSource(sourceFile, sourceDebug);
}
// Visit the module.
if (module != null) {
module.accept(classVisitor);
}
// Visit the nest host class.
if (nestHostClass != null) {
classVisitor.visitNestHost(nestHostClass);
}
// Visit the outer class.
if (outerClass != null) {
classVisitor.visitOuterClass(outerClass, outerMethod, outerMethodDesc);
}
// Visit the annotations.
if (annotations != null) {
for (AnnotationNodeEx annotation : annotations.values()) {
annotation.accept(classVisitor.visitAnnotation(annotation.desc, annotation.visible));
}
}
if (visibleTypeAnnotations != null) {
for (int i = 0, n = visibleTypeAnnotations.size(); i < n; ++i) {
TypeAnnotationNode typeAnnotation = visibleTypeAnnotations.get(i);
typeAnnotation.accept(
classVisitor.visitTypeAnnotation(
typeAnnotation.typeRef, typeAnnotation.typePath, typeAnnotation.desc, true));
}
}
if (invisibleTypeAnnotations != null) {
for (int i = 0, n = invisibleTypeAnnotations.size(); i < n; ++i) {
TypeAnnotationNode typeAnnotation = invisibleTypeAnnotations.get(i);
typeAnnotation.accept(
classVisitor.visitTypeAnnotation(
typeAnnotation.typeRef, typeAnnotation.typePath, typeAnnotation.desc, false));
}
}
// Visit the non standard attributes.
if (attrs != null) {
for (int i = 0, n = attrs.size(); i < n; ++i) {
classVisitor.visitAttribute(attrs.get(i));
}
}
// Visit the nest members.
if (nestMembers != null) {
for (int i = 0, n = nestMembers.size(); i < n; ++i) {
classVisitor.visitNestMember(nestMembers.get(i));
}
}
// Visit the permitted subtypes.
if (permittedSubtypesExperimental != null) {
for (int i = 0, n = permittedSubtypesExperimental.size(); i < n; ++i) {
classVisitor.visitPermittedSubtypeExperimental(permittedSubtypesExperimental.get(i));
}
}
// Visit the inner classes.
for (int i = 0, n = innerClasses.size(); i < n; ++i) {
innerClasses.get(i).accept(classVisitor);
}
// Visit the record components.
if (recordComponents != null) {
for (int i = 0, n = recordComponents.size(); i < n; ++i) {
recordComponents.get(i).accept(classVisitor);
}
}
// Visit the fields.
for (int i = 0, n = fields.size(); i < n; ++i) {
fields.get(i).accept(classVisitor);
}
// Visit the methods.
for (int i = 0, n = methods.size(); i < n; ++i) {
methods.get(i).accept(classVisitor);
}
classVisitor.visitEnd();
}
}