|
1 | 1 | package org.ikvm.javarefplugin; |
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
4 | 5 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
5 | 6 | import static org.junit.jupiter.api.Assertions.assertThrows; |
6 | 7 | import static org.junit.jupiter.api.Assertions.assertTrue; |
@@ -481,4 +482,77 @@ void staticInitializerOptimizationHandlesAllFieldTypes() throws Exception { |
481 | 482 | assertTrue(mutableFailure.getCause() instanceof NullPointerException); |
482 | 483 | } |
483 | 484 |
|
| 485 | + @Test |
| 486 | + void staticFieldsAllCombinations() throws Exception { |
| 487 | + Map<String, String> sources = new LinkedHashMap<>(); |
| 488 | + sources.put( |
| 489 | + "example/AllStaticFields.java", |
| 490 | + joinLines( |
| 491 | + "package example;", |
| 492 | + "", |
| 493 | + "public class AllStaticFields {", |
| 494 | + " // static final WITH initializer - should NOT be reassigned", |
| 495 | + " static final long WITH_INIT_FINAL = 42L;", |
| 496 | + "", |
| 497 | + " // static final WITHOUT initializer - MUST be assigned", |
| 498 | + " static final String WITHOUT_INIT_FINAL;", |
| 499 | + "", |
| 500 | + " // static non-final WITH initializer - should NOT be reassigned?", |
| 501 | + " static int WITH_INIT_MUTABLE = 100;", |
| 502 | + "", |
| 503 | + " // static non-final WITHOUT initializer - should be assigned to 0", |
| 504 | + " static int WITHOUT_INIT_MUTABLE;", |
| 505 | + "", |
| 506 | + " static {", |
| 507 | + " WITHOUT_INIT_FINAL = \"initialized\";", |
| 508 | + " WITHOUT_INIT_MUTABLE = 200;", |
| 509 | + " }", |
| 510 | + "", |
| 511 | + " public static void dummy() { }", |
| 512 | + "}" |
| 513 | + ) |
| 514 | + ); |
| 515 | + |
| 516 | + // Just verify the plugin can compile this without errors |
| 517 | + CompilationResult result = compile(sources); |
| 518 | + assertNotNull(result.loadClass("example.AllStaticFields")); |
| 519 | + } |
| 520 | + |
| 521 | + @Test |
| 522 | + void staticBooleanFieldsCombinationsAreHandled() throws Exception { |
| 523 | + Map<String, String> sources = new LinkedHashMap<>(); |
| 524 | + sources.put( |
| 525 | + "example/StaticBooleanFields.java", |
| 526 | + joinLines( |
| 527 | + "package example;", |
| 528 | + "", |
| 529 | + "public class StaticBooleanFields {", |
| 530 | + " static final boolean WITH_INIT_FINAL = true;", |
| 531 | + " static final boolean WITHOUT_INIT_FINAL;", |
| 532 | + " static boolean WITH_INIT_MUTABLE = true;", |
| 533 | + " static boolean WITHOUT_INIT_MUTABLE;", |
| 534 | + "", |
| 535 | + " static {", |
| 536 | + " WITHOUT_INIT_FINAL = true;", |
| 537 | + " WITHOUT_INIT_MUTABLE = true;", |
| 538 | + " }", |
| 539 | + "}" |
| 540 | + ) |
| 541 | + ); |
| 542 | + |
| 543 | + CompilationResult result = compile(sources); |
| 544 | + Class<?> type = result.loadClass("example.StaticBooleanFields"); |
| 545 | + |
| 546 | + assertTrue(getStaticBoolean(type, "WITH_INIT_FINAL")); |
| 547 | + assertFalse(getStaticBoolean(type, "WITHOUT_INIT_FINAL")); |
| 548 | + assertTrue(getStaticBoolean(type, "WITH_INIT_MUTABLE")); |
| 549 | + assertFalse(getStaticBoolean(type, "WITHOUT_INIT_MUTABLE")); |
| 550 | + } |
| 551 | + |
| 552 | + private static boolean getStaticBoolean(Class<?> type, String fieldName) throws Exception { |
| 553 | + java.lang.reflect.Field field = type.getDeclaredField(fieldName); |
| 554 | + field.setAccessible(true); |
| 555 | + return field.getBoolean(null); |
| 556 | + } |
| 557 | + |
484 | 558 | } |
0 commit comments