@@ -30,61 +30,55 @@ public class ValhallaAttributeGenerator extends ClassLoader {
30
30
31
31
public static Class <?> generateClassWithTwoPreloadAttributes (String name , String [] classList1 , String [] classList2 ) throws Throwable {
32
32
byte [] bytes = generateClass (name , ValhallaUtils .ACC_IDENTITY , new Attribute [] {
33
- new PreloadAttribute (classList1 ),
34
- new PreloadAttribute (classList2 )});
33
+ new ValhallaUtils . PreloadAttribute (classList1 ),
34
+ new ValhallaUtils . PreloadAttribute (classList2 )});
35
35
return generator .defineClass (name , bytes , 0 , bytes .length );
36
36
}
37
37
38
38
public static Class <?> generateClassWithPreloadAttribute (String name , String [] classList ) throws Throwable {
39
- byte [] bytes = generateClass (name , ValhallaUtils .ACC_IDENTITY , new Attribute [] {new PreloadAttribute (classList )});
39
+ byte [] bytes = generateClass (name , ValhallaUtils .ACC_IDENTITY , new Attribute [] {new ValhallaUtils . PreloadAttribute (classList )});
40
40
return generator .defineClass (name , bytes , 0 , bytes .length );
41
41
}
42
42
43
43
public static Class <?> generateClassWithTwoImplicitCreationAttributes (String name ) throws Throwable {
44
44
byte [] bytes = generateClass (name , ACC_FINAL + ValhallaUtils .ACC_VALUE_TYPE ,
45
- new Attribute [] {new ImplicitCreationAttribute (), new ImplicitCreationAttribute ()});
45
+ new Attribute [] {new ValhallaUtils . ImplicitCreationAttribute (), new ValhallaUtils . ImplicitCreationAttribute ()});
46
46
return generator .defineClass (name , bytes , 0 , bytes .length );
47
47
}
48
48
49
49
public static Class <?> generateNonValueTypeClassWithImplicitCreationAttribute (String name ) throws Throwable {
50
- byte [] bytes = generateClass (name , ValhallaUtils .ACC_IDENTITY , new Attribute [] {new ImplicitCreationAttribute ()});
51
- return generator .defineClass (name , bytes , 0 , bytes .length );
52
- }
53
-
54
- public static Class <?> generateValidClassWithImplicitCreationAttribute (String name ) throws Throwable {
55
- byte [] bytes = generateClass (name , ACC_FINAL + ValhallaUtils .ACC_VALUE_TYPE ,
56
- new Attribute [] {new ImplicitCreationAttribute ()});
50
+ byte [] bytes = generateClass (name , ValhallaUtils .ACC_IDENTITY , new Attribute [] {new ValhallaUtils .ImplicitCreationAttribute ()});
57
51
return generator .defineClass (name , bytes , 0 , bytes .length );
58
52
}
59
53
60
54
public static Class <?> generateFieldWithMultipleNullRestrictedAttributes (String className , String fieldClassName ) throws Throwable {
61
55
/* Generate field class - value class with ImplicitCreation attribute and ACC_DEFAULT flag set */
62
56
byte [] fieldClassBytes = generateClass (fieldClassName , ACC_FINAL + ValhallaUtils .ACC_VALUE_TYPE ,
63
- new Attribute [] {new ImplicitCreationAttribute ()});
57
+ new Attribute [] {new ValhallaUtils . ImplicitCreationAttribute ()});
64
58
Class <?> fieldClass = generator .defineClass (fieldClassName , fieldClassBytes , 0 , fieldClassBytes .length );
65
59
66
60
/* Generate class with field and multiple NullRestricted attributes */
67
61
byte [] classBytes = generateIdentityClassWithField (className , 0 ,
68
- "field" , fieldClass .descriptorString (), new Attribute []{new NullRestrictedAttribute (), new NullRestrictedAttribute ()});
62
+ "field" , fieldClass .descriptorString (), new Attribute []{new ValhallaUtils . NullRestrictedAttribute (), new ValhallaUtils . NullRestrictedAttribute ()});
69
63
return generator .defineClass (className , classBytes , 0 , classBytes .length );
70
64
}
71
65
72
66
public static Class <?> generateNullRestrictedAttributeInPrimitiveField (String className ) throws Throwable {
73
67
/* Generate class with primitive field and a NullRestricted attribute */
74
68
byte [] classBytes = generateIdentityClassWithField (className , 0 ,
75
- "field" , "I" , new Attribute []{new NullRestrictedAttribute ()});
69
+ "field" , "I" , new Attribute []{new ValhallaUtils . NullRestrictedAttribute ()});
76
70
return generator .defineClass (className , classBytes , 0 , classBytes .length );
77
71
}
78
72
79
73
public static Class <?> generateNullRestrictedAttributeInArrayField (String className , String fieldClassName ) throws Throwable {
80
74
/* Generate field class - value class with ImplicitCreation attribute and ACC_DEFAULT flag set. */
81
75
byte [] fieldClassBytes = generateClass (fieldClassName , ACC_FINAL + ValhallaUtils .ACC_VALUE_TYPE ,
82
- new Attribute [] {new ImplicitCreationAttribute ()});
76
+ new Attribute [] {new ValhallaUtils . ImplicitCreationAttribute ()});
83
77
Class <?> fieldClass = generator .defineClass (fieldClassName , fieldClassBytes , 0 , fieldClassBytes .length );
84
78
85
79
/* Generate class field field that is an array with a NullRestricted attribute */
86
80
byte [] classBytes = generateIdentityClassWithField (className , 0 ,
87
- "field" , "[" + fieldClass .descriptorString (), new Attribute []{new NullRestrictedAttribute ()});
81
+ "field" , "[" + fieldClass .descriptorString (), new Attribute []{new ValhallaUtils . NullRestrictedAttribute ()});
88
82
return generator .defineClass (className , classBytes , 0 , classBytes .length );
89
83
}
90
84
@@ -93,15 +87,15 @@ public static Class<?> generatePutStaticNullToNullRestrictedField(String classNa
93
87
94
88
/* Generate field class - value class with ImplicitCreation attribute and ACC_DEFAULT flag set. */
95
89
byte [] fieldClassBytes = generateClass (fieldClassName , ACC_PUBLIC + ACC_FINAL + ValhallaUtils .ACC_VALUE_TYPE ,
96
- new Attribute [] {new ImplicitCreationAttribute ()});
90
+ new Attribute [] {new ValhallaUtils . ImplicitCreationAttribute ()});
97
91
Class <?> fieldClass = generator .defineClass (fieldClassName , fieldClassBytes , 0 , fieldClassBytes .length );
98
92
99
93
ClassWriter classWriter = new ClassWriter (0 );
100
94
classWriter .visit (ValhallaUtils .CLASS_FILE_MAJOR_VERSION , ACC_PUBLIC + ValhallaUtils .ACC_IDENTITY , className , null , "java/lang/Object" , null );
101
95
102
96
/* static field of previously generated field class with NullRestrictd attribute */
103
97
FieldVisitor fieldVisitor = classWriter .visitField (ACC_PUBLIC + ACC_STATIC , fieldName , fieldClass .descriptorString (), null , null );
104
- fieldVisitor .visitAttribute (new NullRestrictedAttribute ());
98
+ fieldVisitor .visitAttribute (new ValhallaUtils . NullRestrictedAttribute ());
105
99
106
100
/* assign field to null in <clinit> */
107
101
MethodVisitor mvClinit = classWriter .visitMethod (ACC_STATIC , "<clinit>" , "()V" , null , null );
@@ -131,15 +125,15 @@ public static Class<?> generatePutFieldNullToNullRestrictedField(String classNam
131
125
132
126
/* Generate field class - value class with ImplicitCreation attribute and ACC_DEFAULT flag set. */
133
127
byte [] fieldClassBytes = generateClass (fieldClassName , ACC_PUBLIC + ACC_FINAL + ValhallaUtils .ACC_VALUE_TYPE ,
134
- new Attribute [] {new ImplicitCreationAttribute ()});
128
+ new Attribute [] {new ValhallaUtils . ImplicitCreationAttribute ()});
135
129
Class <?> fieldClass = generator .defineClass (fieldClassName , fieldClassBytes , 0 , fieldClassBytes .length );
136
130
137
131
ClassWriter classWriter = new ClassWriter (0 );
138
132
classWriter .visit (ValhallaUtils .CLASS_FILE_MAJOR_VERSION , ACC_PUBLIC + ValhallaUtils .ACC_IDENTITY , className , null , "java/lang/Object" , null );
139
133
140
134
/* instance field of previously generated field class with NullRestrictd attribute */
141
135
FieldVisitor fieldVisitor = classWriter .visitField (ACC_PUBLIC , fieldName , fieldClass .descriptorString (), null , null );
142
- fieldVisitor .visitAttribute (new NullRestrictedAttribute ());
136
+ fieldVisitor .visitAttribute (new ValhallaUtils . NullRestrictedAttribute ());
143
137
144
138
/* assign field to null in <init> */
145
139
MethodVisitor mvInit = classWriter .visitMethod (ACC_PUBLIC , "<init>" , "()V" , null , null );
@@ -165,7 +159,7 @@ public static Class<?> generateNullRestrictedAttributeInIdentityClass(boolean is
165
159
166
160
/* Generate class with field that is an identity class with a NullRestricted attribute */
167
161
byte [] classBytes = generateIdentityClassWithField (className , isStatic ? ACC_STATIC : 0 ,
168
- "field" , fieldClass .descriptorString (), new Attribute []{new NullRestrictedAttribute ()});
162
+ "field" , fieldClass .descriptorString (), new Attribute []{new ValhallaUtils . NullRestrictedAttribute ()});
169
163
return generator .defineClass (className , classBytes , 0 , classBytes .length );
170
164
}
171
165
@@ -176,19 +170,19 @@ public static Class<?> generateNullRestrictedAttributeInValueClassWithoutIC(bool
176
170
177
171
/* Generate class with field that has a NullRestricted attribute */
178
172
byte [] classBytes = generateIdentityClassWithField (className , isStatic ? ACC_STATIC : 0 ,
179
- "field" , fieldClass .descriptorString (), new Attribute []{new NullRestrictedAttribute ()});
173
+ "field" , fieldClass .descriptorString (), new Attribute []{new ValhallaUtils . NullRestrictedAttribute ()});
180
174
return generator .defineClass (className , classBytes , 0 , classBytes .length );
181
175
}
182
176
183
177
public static Class <?> generateNullRestrictedFieldWhereICHasNoDefaultFlag (boolean isStatic , String className , String fieldClassName ) throws Throwable {
184
178
/* Generate field class - value type with ImplicitCreation attribute, no flags */
185
179
byte [] fieldClassBytes = generateClass (fieldClassName , ACC_FINAL + ValhallaUtils .ACC_VALUE_TYPE ,
186
- new Attribute [] {new ImplicitCreationAttribute (0 )});
180
+ new Attribute [] {new ValhallaUtils . ImplicitCreationAttribute (0 )});
187
181
Class <?> fieldClass = generator .defineClass (fieldClassName , fieldClassBytes , 0 , fieldClassBytes .length );
188
182
189
183
/* Generate class with field with NullRestricted attribute */
190
184
byte [] classBytes = generateIdentityClassWithField (className , isStatic ? ACC_STATIC : 0 ,
191
- "field" , fieldClass .descriptorString (), new Attribute []{new NullRestrictedAttribute ()});
185
+ "field" , fieldClass .descriptorString (), new Attribute []{new ValhallaUtils . NullRestrictedAttribute ()});
192
186
return generator .defineClass (className , classBytes , 0 , classBytes .length );
193
187
}
194
188
@@ -197,15 +191,15 @@ public static Class<?> generateWithFieldStoreNullToNullRestrictedField(String cl
197
191
198
192
/* Generate field class - value class with ImplicitCreation attribute and ACC_DEFAULT flag set. */
199
193
byte [] fieldClassBytes = generateClass (fieldClassName , ACC_PUBLIC + ACC_FINAL + ValhallaUtils .ACC_VALUE_TYPE ,
200
- new Attribute [] {new ImplicitCreationAttribute (ValhallaUtils .ACC_DEFAULT )});
194
+ new Attribute [] {new ValhallaUtils . ImplicitCreationAttribute (ValhallaUtils .ACC_DEFAULT )});
201
195
Class <?> fieldClass = generator .defineClass (fieldClassName , fieldClassBytes , 0 , fieldClassBytes .length );
202
196
203
197
ClassWriter classWriter = new ClassWriter (0 );
204
198
classWriter .visit (ValhallaUtils .CLASS_FILE_MAJOR_VERSION , ACC_PUBLIC + ValhallaUtils .ACC_IDENTITY , className , null , "java/lang/Object" , null );
205
199
206
200
/* instance field of previously generated field class with NullRestrictd attribute */
207
201
FieldVisitor fieldVisitor = classWriter .visitField (ACC_PUBLIC , fieldName , fieldClass .descriptorString (), null , null );
208
- fieldVisitor .visitAttribute (new NullRestrictedAttribute ());
202
+ fieldVisitor .visitAttribute (new ValhallaUtils . NullRestrictedAttribute ());
209
203
210
204
MethodVisitor mvInit = classWriter .visitMethod (ACC_PUBLIC , "<init>" , "()V" , null , null );
211
205
mvInit .visitCode ();
@@ -269,93 +263,4 @@ public static byte[] generateIdentityClassWithField(String name, int fieldFlags,
269
263
classWriter .visitEnd ();
270
264
return classWriter .toByteArray ();
271
265
}
272
-
273
- final static class PreloadAttribute extends Attribute {
274
- private final String [] classes ;
275
-
276
- public PreloadAttribute (String [] classes ) {
277
- super ("Preload" );
278
- this .classes = classes ;
279
- }
280
-
281
- public Label [] getLabels () {
282
- return null ;
283
- }
284
-
285
- public boolean isCodeAttribute () {
286
- return false ;
287
- }
288
-
289
- public boolean isUnknown () {
290
- return true ;
291
- }
292
-
293
- public ByteVector write (ClassWriter cw , byte [] code , int len , int maxStack , int maxLocals ) {
294
- ByteVector b = new ByteVector ();
295
-
296
- b .putShort (classes .length );
297
-
298
- int cpIndex ;
299
- for (int i = 0 ; i < classes .length ; i ++) {
300
- cpIndex = cw .newClass (classes [i ].replace ('.' , '/' ));
301
- b .putShort (cpIndex );
302
- }
303
- return b ;
304
- }
305
- }
306
-
307
- final static class ImplicitCreationAttribute extends Attribute {
308
- private final int flags ;
309
-
310
- public ImplicitCreationAttribute () {
311
- super ("ImplicitCreation" );
312
- /* this is the default flag generated by the compiler for the implicit modifier. */
313
- this .flags = ValhallaUtils .ACC_DEFAULT ;
314
- }
315
-
316
- public ImplicitCreationAttribute (int flags ) {
317
- super ("ImplicitCreation" );
318
- this .flags = flags ;
319
- }
320
-
321
- public Label [] getLabels () {
322
- return null ;
323
- }
324
-
325
- public boolean isCodeAttribute () {
326
- return false ;
327
- }
328
-
329
- public boolean isUnknown () {
330
- return true ;
331
- }
332
-
333
- public ByteVector write (ClassWriter cw , byte [] code , int len , int maxStack , int maxLocals ) {
334
- ByteVector b = new ByteVector ();
335
- b .putShort (flags );
336
- return b ;
337
- }
338
- }
339
-
340
- final static class NullRestrictedAttribute extends Attribute {
341
- public NullRestrictedAttribute () {
342
- super ("NullRestricted" );
343
- }
344
-
345
- public Label [] getLabels () {
346
- return null ;
347
- }
348
-
349
- public boolean isCodeAttribute () {
350
- return false ;
351
- }
352
-
353
- public boolean isUnknown () {
354
- return true ;
355
- }
356
-
357
- public ByteVector write (ClassWriter cw , byte [] code , int len , int maxStack , int maxLocals ) {
358
- return new ByteVector ();
359
- }
360
- }
361
266
}
0 commit comments