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:
2026-11-08 18:14:04 +00:00
co-authored by Claude Sonnet 5.5
parent d7d5689f05
commit f6237d4117
19 changed files with 910 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
$ java -cp target/classes com.ankurm.reflection.ReflectionBasicsDemo
== 1. Inspect the class ==
class: com.ankurm.reflection.Account
public long balance[]
public void deposit[long]
public String owner[]
private String secret[]
public String[] stack[]
public String toString[]
public static int twice[int]
== 2. Create and call ==
created: Account[alice, 50]
after deposit(25): Account[alice, 75]
== 3. The exceptions you will meet ==
wrong name -> NoSuchMethodException: com.ankurm.reflection.Account.nope()
wrong arg type -> IllegalArgumentException: argument type mismatch
method threw -> InvocationTargetException wrapping java.lang.IllegalArgumentException: over limit: 5000
private, no opt-in -> IllegalAccessException: class com.ankurm.reflection.ReflectionBasicsDemo cannot access a member of class com.ankurm.reflection.Account with modifiers "private"
after setAccessible(true): secret-of-alice
== 4. What reflection adds to the stack ==
direct call:
com.ankurm.reflection.Account.stack
com.ankurm.reflection.ReflectionBasicsDemo.main
through Method.invoke:
com.ankurm.reflection.Account.stack
jdk.internal.reflect.DirectMethodHandleAccessor.invoke
java.lang.reflect.Method.invoke
com.ankurm.reflection.ReflectionBasicsDemo.main