reflection: Method/Field/MethodHandle/VarHandle demos, module and final-field rules (JEP 500 on JDK 26), JMH call-cost numbers, contract tests
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b6NtPJHrkan4BXhCAM
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
$ java -cp target/classes com.ankurm.reflection.FinalFieldDemo # JDK 25
|
||||
|
||||
java.version = 25.0.5
|
||||
|
||||
== 1. final instance field ==
|
||||
owner() now: mallory
|
||||
|
||||
== 2. static final field ==
|
||||
IllegalAccessException: Can not set static final int field com.ankurm.reflection.Account.MAX_DEPOSIT to java.lang.Integer
|
||||
|
||||
== 3. record component ==
|
||||
IllegalAccessException: Can not set final int field com.ankurm.reflection.FinalFieldDemo$Point.x to java.lang.Integer
|
||||
|
||||
== 4. constant inlining: the write 'works' but the getter never notices ==
|
||||
Field.get -> 99
|
||||
max() -> 5
|
||||
|
||||
$ java -cp target/classes com.ankurm.reflection.FinalFieldDemo # JDK 26
|
||||
|
||||
java.version = 26.0.2.1
|
||||
|
||||
== 1. final instance field ==
|
||||
WARNING: Final field owner in class com.ankurm.reflection.Account has been mutated reflectively by class com.ankurm.reflection.FinalFieldDemo in unnamed module @18b4aac2 (file:/tmp/work/java-core-examples/reflection/target/classes/)
|
||||
WARNING: Use --enable-final-field-mutation=ALL-UNNAMED to avoid a warning
|
||||
WARNING: Mutating final fields will be blocked in a future release unless final field mutation is enabled
|
||||
owner() now: mallory
|
||||
|
||||
== 2. static final field ==
|
||||
IllegalAccessException: Can not set static final int field com.ankurm.reflection.Account.MAX_DEPOSIT to java.lang.Integer
|
||||
|
||||
== 3. record component ==
|
||||
IllegalAccessException: Can not set final int field com.ankurm.reflection.FinalFieldDemo$Point.x to java.lang.Integer
|
||||
|
||||
== 4. constant inlining: the write 'works' but the getter never notices ==
|
||||
Field.get -> 99
|
||||
max() -> 5
|
||||
|
||||
$ java --illegal-final-field-mutation=deny -cp target/classes com.ankurm.reflection.FinalFieldDemo # JDK 26
|
||||
|
||||
java.version = 26.0.2.1
|
||||
|
||||
== 1. final instance field ==
|
||||
Exception in thread "main" java.lang.IllegalAccessException: class com.ankurm.reflection.FinalFieldDemo (in unnamed module @18b4aac2) cannot set final field com.ankurm.reflection.Account.owner (in unnamed module @18b4aac2), unnamed module @18b4aac2 is not allowed to mutate final fields
|
||||
at java.base/java.lang.reflect.Field.preSetFinal(Field.java:1504)
|
||||
at java.base/java.lang.reflect.Field.setFinal(Field.java:1447)
|
||||
at java.base/java.lang.reflect.Field.set(Field.java:909)
|
||||
at com.ankurm.reflection.FinalFieldDemo.main(FinalFieldDemo.java:30)
|
||||
Reference in New Issue
Block a user