reflection in java
play

Reflection in Java Manuel Oriol - May 3rd, 2007 Introductory - PowerPoint PPT Presentation

Reflection in Java Manuel Oriol - May 3rd, 2007 Introductory Example 2 Reflection? Introspection Dynamic reflexive invocations Meta-Classes: classes are instances of a meta-class 3 In Java Limited to introspection and


  1. Reflection in Java Manuel Oriol - May 3rd, 2007

  2. Introductory Example 2

  3. Reflection? • Introspection • Dynamic reflexive invocations • Meta-Classes: classes are instances of a meta-class 3

  4. In Java • Limited to introspection and reflexive invocation • Package: java.lang.reflect • class Class, class Method, class Field • Built-In: instanceof 4

  5. Class Object • getClass() 5

  6. Class Class<?> • static forName • getClasses • getConstructor/getConstructors • getField/geFields • getMethod/getMethods • getModifiers • getName • isInstance 6

  7. Class Field • get, getInt, getDouble... • getDeclaringClass • getModifiers • getName • getType • set(obj,value) 7

  8. Example is equivalent to: 8

  9. Class Constructor • getDeclaringClass • getModifiers • getName • getTypeParameters • isVarArgs • newInstance 9

  10. Example is equivalent to: 10

  11. Class Method • getDeclaringClass • getModifiers • getName • getTypeParameters • isVarArgs • invoke(obj,args...) 11

  12. Example is equivalent to: 12

  13. Deeper Example 13

  14. Proxy Classes • Meant to be used as a simple way to implement some interfaces. • Created at runtime • Use a handler to treat requests • instanceof is true with a proxy • cast is working correctly 14

  15. Class Proxy • getInvocationHandler • getProxyClass • isProxyClass • newProxyInstance 15

  16. InvocationHandler • invoke 16

  17. Example 17

  18. Performance (1/3) 18

  19. Performances (2/3) for the JIT “T esting direct calls” 19

  20. Performance (3/3) “T esting reflexive calls” 20

  21. Result 10 -2 10 -1 ~ 10 10 2 10 3 Mean time (ns) for regular call: 7 Mean time (ns) for reflexive call: 262 ------- Slowdown factor: 33x 21

  22. Security Issues with Reflection ... 22

  23. So... what about visibility of the method??? • Short answer: it is respected through the SecurityException • Long answer? If you define your own SecurityManager and your own ClassLoader then you can do what you want!!! 23

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend