@@ -121,27 +121,21 @@ public byte[] generate() {
121
121
for (final Property property : properties )
122
122
interfaces .add (type (property .getInterfacePath ()).getInternalName ());
123
123
cw .visit (Opcodes .V17 ,
124
- Opcodes .ACC_PUBLIC ,
124
+ Opcodes .ACC_PUBLIC | Opcodes . ACC_FINAL ,
125
125
type (path ).getInternalName (),
126
126
null ,
127
127
type (BLOCKDATA_CLASS ).getInternalName (),
128
128
interfaces .toArray (new String [0 ]));
129
129
CodeGenerator .visitGeneratedAnnotation (cw , BlockDataLibGenerator .class );
130
130
131
131
// Fields
132
- FieldVisitor fv = cw .visitField (Opcodes .ACC_PRIVATE | Opcodes .ACC_STATIC | Opcodes .ACC_FINAL ,
133
- "BLOCKDATA_MAP" ,
134
- Type .getType (HashMap .class ).getDescriptor (),
135
- "Ljava/util/HashMap<Ljava/lang/Integer;" + type (path ).getDescriptor () + ">;" ,
136
- null );
137
- fv .visitEnd ();
138
132
for (final Property property : properties ) {
139
133
final String descriptor = switch (property .getType ()) {
140
134
case BOOLEAN -> Type .BOOLEAN_TYPE .getDescriptor ();
141
135
case NUMBER -> Type .INT_TYPE .getDescriptor ();
142
136
case OTHER -> type (property .getPath ()).getDescriptor ();
143
137
};
144
- fv = cw .visitField (Opcodes .ACC_PRIVATE ,
138
+ final FieldVisitor fv = cw .visitField (Opcodes .ACC_PRIVATE ,
145
139
toCamelCase (property .getName (), false ),
146
140
descriptor ,
147
141
null ,
@@ -286,86 +280,6 @@ public byte[] generate() {
286
280
mv .visitEnd ();
287
281
cw .visitEnd ();
288
282
289
- // Static block
290
- mv = cw .visitMethod (Opcodes .ACC_STATIC ,
291
- "<clinit>" ,
292
- "()V" ,
293
- null ,
294
- new String [0 ]);
295
- mv .visitCode ();
296
- mv .visitTypeInsn (Opcodes .NEW , Type .getType (HashMap .class ).getInternalName ());
297
- mv .visitInsn (Opcodes .DUP );
298
- mv .visitMethodInsn (Opcodes .INVOKESPECIAL ,
299
- Type .getType (HashMap .class ).getInternalName (),
300
- "<init>" ,
301
- "()V" ,
302
- false );
303
- mv .visitVarInsn (Opcodes .ASTORE , i );
304
- mv .visitVarInsn (Opcodes .ALOAD , i );
305
- mv .visitFieldInsn (Opcodes .PUTSTATIC ,
306
- type (path ).getInternalName (),
307
- "BLOCKDATA_MAP" ,
308
- Type .getType (HashMap .class ).getDescriptor ());
309
- mv .visitMethodInsn (Opcodes .INVOKESTATIC ,
310
- type (path ).getInternalName (),
311
- "initBlockDataMap" ,
312
- "()V" ,
313
- false );
314
- mv .visitInsn (Opcodes .RETURN );
315
- mv .visitMaxs (0 , 0 );
316
- mv .visitEnd ();
317
- cw .visitEnd ();
318
-
319
- // Init block data map method
320
- mv = cw .visitMethod (Opcodes .ACC_PROTECTED | Opcodes .ACC_STATIC ,
321
- "initBlockDataMap" ,
322
- "()V" ,
323
- null ,
324
- new String [0 ]);
325
- mv .visitCode ();
326
- for (final Integer id : blockDataMap .keySet ()) {
327
- mv .visitFieldInsn (Opcodes .GETSTATIC ,
328
- type (path ).getInternalName (),
329
- "BLOCKDATA_MAP" ,
330
- Type .getType (HashMap .class ).getDescriptor ());
331
- final StringBuilder stateKeyBuilder = new StringBuilder ();
332
- for (final Map .Entry <Property , String > property : blockDataMap .get (id ))
333
- stateKeyBuilder .append (property .getValue ().toLowerCase ()).append (";" );
334
- pushValue (mv , idMap .get (stateKeyBuilder .toString ()));
335
- mv .visitMethodInsn (Opcodes .INVOKESTATIC ,
336
- Type .getType (Integer .class ).getInternalName (),
337
- "valueOf" ,
338
- "(I)Ljava/lang/Integer;" ,
339
- false );
340
- mv .visitTypeInsn (Opcodes .NEW , type (path ).getInternalName ());
341
- mv .visitInsn (Opcodes .DUP );
342
- for (final Map .Entry <Property , String > property : blockDataMap .get (id )) {
343
- switch (property .getKey ().getType ()) {
344
- case BOOLEAN -> pushValue (mv , Boolean .parseBoolean (property .getValue ()));
345
- case NUMBER -> pushValue (mv , Integer .parseInt (property .getValue ()));
346
- case OTHER -> mv .visitFieldInsn (Opcodes .GETSTATIC ,
347
- type (property .getKey ().getPath ()).getInternalName (),
348
- property .getValue ().toUpperCase (),
349
- type (property .getKey ().getPath ()).getDescriptor ());
350
- }
351
- }
352
- mv .visitMethodInsn (Opcodes .INVOKESPECIAL ,
353
- type (path ).getInternalName (),
354
- "<init>" ,
355
- descriptorBuilder .toString (),
356
- false );
357
- mv .visitMethodInsn (Opcodes .INVOKEVIRTUAL ,
358
- Type .getType (HashMap .class ).getInternalName (),
359
- "put" ,
360
- "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;" ,
361
- false );
362
- mv .visitInsn (Opcodes .POP );
363
- }
364
- mv .visitInsn (Opcodes .RETURN );
365
- mv .visitMaxs (0 , 0 );
366
- mv .visitEnd ();
367
- cw .visitEnd ();
368
-
369
283
// getData method
370
284
mv = cw .visitMethod (Opcodes .ACC_PROTECTED ,
371
285
"getData" ,
@@ -505,20 +419,56 @@ else if (property.getType() == Property.Type.NUMBER)
505
419
mv .visitEnd ();
506
420
cw .visitEnd ();
507
421
508
- // getIdMap method
422
+ // loadProperties method
509
423
mv = cw .visitMethod (Opcodes .ACC_PROTECTED ,
510
- "getIdMap " ,
511
- "() " + Type .getType (Map .class ).getDescriptor (),
424
+ "loadProperties " ,
425
+ "(" + Type .getType (Object [] .class ).getDescriptor () + ")V" ,
512
426
null ,
513
427
new String [0 ]);
514
428
mv .visitAnnotation (Type .getType (Override .class ).getDescriptor (), true ).visitEnd ();
515
429
mv .visitEnd ();
516
430
mv .visitCode ();
517
- mv .visitFieldInsn (Opcodes .GETSTATIC ,
518
- type (path ).getInternalName (),
519
- "BLOCKDATA_MAP" ,
520
- Type .getType (HashMap .class ).getDescriptor ());
521
- mv .visitInsn (Opcodes .ARETURN );
431
+
432
+ int j = 0 ;
433
+ for (final Property property : properties ) {
434
+
435
+ mv .visitVarInsn (Opcodes .ALOAD , 0 );
436
+ mv .visitVarInsn (Opcodes .ALOAD , 1 );
437
+ pushValue (mv , j );
438
+ mv .visitInsn (Opcodes .AALOAD );
439
+ mv .visitTypeInsn (Opcodes .CHECKCAST , (
440
+ switch (property .getType ()) {
441
+ case BOOLEAN -> Type .getType (Boolean .class );
442
+ case NUMBER -> Type .getType (Integer .class );
443
+ case OTHER -> type (property .getPath ()); }
444
+ ).getInternalName ());
445
+
446
+ if (property .getType () == Property .Type .BOOLEAN )
447
+ mv .visitMethodInsn (Opcodes .INVOKEVIRTUAL ,
448
+ Type .getType (Boolean .class ).getInternalName (),
449
+ "booleanValue" ,
450
+ "()Z" ,
451
+ false );
452
+ else if (property .getType () == Property .Type .NUMBER )
453
+ mv .visitMethodInsn (Opcodes .INVOKEVIRTUAL ,
454
+ Type .getType (Integer .class ).getInternalName (),
455
+ "intValue" ,
456
+ "()I" ,
457
+ false );
458
+
459
+ mv .visitFieldInsn (Opcodes .PUTFIELD ,
460
+ type (path ).getInternalName (),
461
+ toCamelCase (property .getName (), false ), (
462
+ switch (property .getType ()) {
463
+ case BOOLEAN -> Type .BOOLEAN_TYPE ;
464
+ case NUMBER -> Type .INT_TYPE ;
465
+ case OTHER -> type (property .getPath ()); }
466
+ ).getDescriptor ());
467
+
468
+ j ++;
469
+ }
470
+
471
+ mv .visitInsn (Opcodes .RETURN );
522
472
mv .visitMaxs (0 , 0 );
523
473
mv .visitEnd ();
524
474
cw .visitEnd ();
0 commit comments