HoChiMinh University of Science<br />Java Programming Course<br />Java Reflection API<br />& Reference Object<br />Decembe...
Agenda<br />
Introduction<br />What does “Reflection” mean ?<br />What’s Java Reflection API ?<br />Java Reflection API<br />Usage<br /...
Introduction…<br />What does “Reflection” mean ?<br />	In computer science, reflection is the process by which a computer ...
Introduction…<br />5<br />What’s Java Reflection API?<br />An Java program contains many objects. Java Reflection API incl...
Introduction …<br />Java Reflection API<br /><ul><li>Interface
java.lang.reflect.AnnotatedElement
java.lang.reflect.GenericDeclaration
java.lang.reflect.InvocationHandler
java.lang.reflect.Member
java.lang.reflect.Type</li></ul>6<br />
Introduction …<br />Java Reflection API …<br /><ul><li>Class</li></ul>java.lang.reflect.AccessibleObject<br /><ul><li>java...
java.lang.reflect.Field
java.lang.reflect.Method</li></ul>java.lang.reflect.Array<br />java.lang.reflect.Modifier<br />java.lang.reflect.Proxy<br ...
Introduction …<br />Java Reflection API …<br /><ul><li>Exception</li></ul>java.lang.reflect.InvocationTargetException		   ...
Introduction…<br />Usage<br /><ul><li>Program Extensibility
IDE’s Visual Assistant
Visual Debugger & Tester Tools
Object Relational Mapping
Application Server Deploy Configuration</li></ul>9<br />
Agenda<br />
java.lang.Class class<br />Kind of Java types<br />Class object<br />How to get a Class object <br />How to get Class’ mem...
java.lang.Class class …<br />Kind of Java types<br />Primitive type<br />e.g : int, double, float, short, long, byte, char...
java.lang.Class class…<br />Class object<br />Instances of the class Class represent classes and interfaces in a running J...
java.lang.Class class…<br />How to get a Class object<br />getClass() method<br />.class syntax<br />forName() static meth...
java.lang.Class class…<br />How to get Class’ members object<br />15<br />Get Constructors<br />Constructor<T> getConstruc...
java.lang.Class class…<br />How to get Class’ members object<br />16<br />Get Methods<br />Method getMethod(String name, C...
java.lang.Class class…<br />How to get Class’ members object<br />17<br />Get Package<br />Package getPackage()<br />Other...
Agenda<br />
Class member<br />java.lang.reflect<br /><ul><li>Constructor
Field
Upcoming SlideShare
Loading in …5
×

Cuối cùng cũng đã chuẩn bị slide xong cho seminar ngày mai. Mai seminar đầu tiên luôn

1,128 views
1,070 views

Published on

Published in: Technology, News & Politics
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,128
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
25
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Cuối cùng cũng đã chuẩn bị slide xong cho seminar ngày mai. Mai seminar đầu tiên luôn

  1. 1. HoChiMinh University of Science<br />Java Programming Course<br />Java Reflection API<br />& Reference Object<br />December 2010<br />
  2. 2. Agenda<br />
  3. 3. Introduction<br />What does “Reflection” mean ?<br />What’s Java Reflection API ?<br />Java Reflection API<br />Usage<br />“Hello Reflection” Demo<br />3<br />
  4. 4. Introduction…<br />What does “Reflection” mean ?<br /> In computer science, reflection is the process by which a computer program can observe and modify its own structure and behavior at runtime[1]. <br />4<br />
  5. 5. Introduction…<br />5<br />What’s Java Reflection API?<br />An Java program contains many objects. Java Reflection API includes classes, interfaces support developer can retrieve and manipulate its object at runtime.<br />
  6. 6. Introduction …<br />Java Reflection API<br /><ul><li>Interface
  7. 7. java.lang.reflect.AnnotatedElement
  8. 8. java.lang.reflect.GenericDeclaration
  9. 9. java.lang.reflect.InvocationHandler
  10. 10. java.lang.reflect.Member
  11. 11. java.lang.reflect.Type</li></ul>6<br />
  12. 12. Introduction …<br />Java Reflection API …<br /><ul><li>Class</li></ul>java.lang.reflect.AccessibleObject<br /><ul><li>java.lang.reflect.Constructor
  13. 13. java.lang.reflect.Field
  14. 14. java.lang.reflect.Method</li></ul>java.lang.reflect.Array<br />java.lang.reflect.Modifier<br />java.lang.reflect.Proxy<br />7<br />
  15. 15. Introduction …<br />Java Reflection API …<br /><ul><li>Exception</li></ul>java.lang.reflect.InvocationTargetException java.lang.reflect.MalformedParameterizedTypeException<br /> java.lang.reflect.UndeclaredThrowableException<br /><ul><li>Error</li></ul>java.lang.reflect.GenericSignatureFormatError<br />8<br />
  16. 16. Introduction…<br />Usage<br /><ul><li>Program Extensibility
  17. 17. IDE’s Visual Assistant
  18. 18. Visual Debugger & Tester Tools
  19. 19. Object Relational Mapping
  20. 20. Application Server Deploy Configuration</li></ul>9<br />
  21. 21. Agenda<br />
  22. 22. java.lang.Class class<br />Kind of Java types<br />Class object<br />How to get a Class object <br />How to get Class’ members object<br />Instantiate a new object from its Class<br />11<br />
  23. 23. java.lang.Class class …<br />Kind of Java types<br />Primitive type<br />e.g : int, double, float, short, long, byte, char, void<br />Reference type<br />e.g : class, interface, array, enum, annotation<br />12<br />
  24. 24. java.lang.Class class…<br />Class object<br />Instances of the class Class represent classes and interfaces in a running Java application.<br />Has no public constructor.<br />Are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader.<br />13<br />
  25. 25. java.lang.Class class…<br />How to get a Class object<br />getClass() method<br />.class syntax<br />forName() static method <br />.TYPE syntax<br />Other methods<br />14<br />
  26. 26. java.lang.Class class…<br />How to get Class’ members object<br />15<br />Get Constructors<br />Constructor<T> getConstructor(Class<T>… parameterTypes)<br />Constructor<?> [] getConstructors()<br />Constructor<T> getDeclaredConstructor(Class<T>… parameterTypes)<br />Constructor<?> [] getDeclaredConstructors()<br />GetFields<br />Field getField(String fieldName)<br />Field [] getFields()<br />Field getDeclaredField(String fieldName)<br />Field [] getDeclaredFields()<br />
  27. 27. java.lang.Class class…<br />How to get Class’ members object<br />16<br />Get Methods<br />Method getMethod(String name, Class<T>… parameterTypes)<br />Method [] getMethods()<br />Method getDeclaredMethod(String name, Class<T>… parameterTypes)<br />Method [] getDeclaredMethods();<br />GetModifiers<br />int getModifiers()<br />
  28. 28. java.lang.Class class…<br />How to get Class’ members object<br />17<br />Get Package<br />Package getPackage()<br />Other methods<br />Ref : JDK Document<br />
  29. 29. Agenda<br />
  30. 30. Class member<br />java.lang.reflect<br /><ul><li>Constructor
  31. 31. Field
  32. 32. Method</li></ul>19<br />
  33. 33. Class member…<br />Constructor<br /><ul><li>Get information</li></ul>Class<T> getDeclaringClass()<br />Class<T> [] getParameterTypes()<br />int getModifiers()<br /><ul><li>Instantiate new object</li></ul>newInstance(Object … initArgs)<br />20<br />
  34. 34. Class member…<br />Field<br /><ul><li>Get information
  35. 35. Class<T> getDeclaringClass()
  36. 36. Class<T> getType()
  37. 37. int getModifiers()
  38. 38. Get field’s value of an object
  39. 39. Object get(Object obj)
  40. 40. Set field’s value of an object</li></ul>set(Object obj, Object value)<br />21<br />
  41. 41. Class member…<br />Getting Field’s Value Example<br />22<br />Class c = Integer.class; <br />//get a private instant field <br />Field field = c.getDeclaredField("value"); <br />field.setAccessible(true); <br />System.out.format(field.get(new Integer(10)));<br />//get a public static field <br />field = c.getField("MAX_VALUE"); <br />System.out.format(field.get(null));<br />
  42. 42. Class member…<br />Setting Field’s Value Example<br />23<br />Class c = Integer.class; <br />Integer i = new Integer(10);<br />//get a private instant field <br />Field field = c.getDeclaredField("value"); <br />field.setAccessible(true); <br />System.out.format(field.set(i, new Integer(20)));<br />//get a public static field <br />field = c.getField("MAX_VALUE"); <br />System.out.format(field.set(null, new Integer(20)));<br />
  43. 43. Class member…<br />Method<br />Get information<br />Class<T> getDeclaringClass()<br />Class<T> [] getParameterTypes()<br />int getModifiers()<br /> Class<T> getReturnType()<br />Invoke method<br />Object invoke(Object obj, Object … initArgs)<br />24<br />
  44. 44. Class member…<br />Invoke a method example<br />25<br />Method f = Math.class.getMethod("sqrt", new Class[]{double.class}); <br />Object[] args = {new Double(10)};<br />//invoke a static method <br />Double d = (Double) f.invoke(null, args); double y = d.doubleValue(); System.out.println(y);<br />
  45. 45. Agenda<br />
  46. 46. Array and Enum<br />Array (java.lang.reflect package)<br /><ul><li>Get array’s information
  47. 47. Instantiate a new array dynamically
  48. 48. Set/Get array component’s value</li></ul>Enum<br /><ul><li>Get enum’s information
  49. 49. Set/Get enum component’s value</li></ul>27<br />
  50. 50. Agenda<br />
  51. 51. Plug-in Programming<br />29<br />Application<br />Plugin Container<br />SQL<br />MySQL<br />Oracle<br />
  52. 52. Advantage & Disadvantage<br />Advantage<br />Dynamic Programming<br />Avoid hard-coding<br />Disadvantage<br />Performance<br />Security<br />30<br />
  53. 53. 31<br />Q & A<br />Thanks you!<br />

×