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,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
|
||||
Reference in New Issue
Block a user