Upload
khoa-nguyen
View
1.006
Download
0
Tags:
Embed Size (px)
Citation preview
Java Reflection API& Reference Object
Prepared by: Group 5
Nguyen Dang Khoa 0712220
Diep Lan Quynh 0712360
Le Thi Nhu Quynh 0712358
HoChiMinh University of ScienceJava Programming Course
December 2010
Agenda
Introduction Java Reflection API
java.lang.Class class
Class Member
Array and Enum
Advantage & Disadvantage
Plug-in Programming
Introduction
• What does “Reflection” mean ?• What’s Java Reflection API ?• Java Reflection API• Usage• “Hello Reflection” Demo
3
Introduction…
• What does “Reflection” mean ?In computer science, reflection is the process by which a computer program can observe and modify its own structure and behavior at runtime[1].
4
Introduction…
5
An Java program contains many objects. Java Reflection API includes classes, interfaces support developer can retrieve and manipulate its object at runtime.
What’s Java Reflection API?
Introduction …
• Java Reflection APIInterface
java.lang.reflect.AnnotatedElement java.lang.reflect.GenericDeclaration java.lang.reflect.InvocationHandler java.lang.reflect.Member java.lang.reflect.Type
6
Introduction …
• Java Reflection API …Class
• java.lang.reflect.AccessibleObject java.lang.reflect.Constructor java.lang.reflect.Field java.lang.reflect.Method
• java.lang.reflect.Array• java.lang.reflect.Modifier• java.lang.reflect.Proxy
7
Introduction …
• Java Reflection API …Exception
java.lang.reflect.InvocationTargetException java.lang.reflect.MalformedParameterizedTypeException
java.lang.reflect.UndeclaredThrowableException
Errorjava.lang.reflect.GenericSignatureFormatError
8
Introduction…
• Usage Program Extensibility IDE’s Visual Assistant Visual Debugger & Tester Tools Object Relational Mapping Application Server Deploy Configuration
9
Agenda
Introduction Java Reflection API
java.lang.Class class
Class Member
Array and Enum
Advantage & Disadvantage
Plug-in Programming
java.lang.Class class
• Kind of Java types• Class object• How to get a Class object • How to get Class’ members object• Instantiate a new object from its Class
11
java.lang.Class class …
Kind of Java types• Primitive type
e.g : int, double, float, short, long, byte, char, void• Reference type
e.g : class, interface, array, enum, annotation
12
java.lang.Class class…
Class object• Instances of the class Class represent
classes and interfaces in a running Java application.
• Has no public constructor.• Are constructed automatically by the Java
Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader.
13
java.lang.Class class…
How to get a Class object• getClass() method• .class syntax• forName() static method • .TYPE syntax• Other methods
14
java.lang.Class class…
How to get Class’ members object
15
Get Constructors
Constructor<T> getConstructor(Class<T>… parameterTypes)
Constructor<?> [] getConstructors()
Constructor<T> getDeclaredConstructor(Class<T>… parameterTypes)
Constructor<?> [] getDeclaredConstructors()Get Fields Field getField(String fieldName)Field [] getFields()Field getDeclaredField(String fieldName)Field [] getDeclaredFields()
java.lang.Class class…
How to get Class’ members object
16
Get Methods
Method getMethod(String name, Class<T>… parameterTypes)
Method [] getMethods()
Method getDeclaredMethod(String name, Class<T>… parameterTypes)
Method [] getDeclaredMethods();Get Modifiers int getModifiers()
java.lang.Class class…
How to get Class’ members object
17
Get Package
Package getPackage()
Other methods
Ref : JDK Document
Agenda
Introduction Java Reflection API
java.lang.Class class
Class Member
Array and Enum
Advantage & Disadvantage
Plug-in Programming
Class member
• java.lang.reflect Constructor Field Method
19
Class member…
• ConstructorGet information
• Class<T> getDeclaringClass()• Class<T> [] getParameterTypes()• int getModifiers()
Instantiate new object• newInstance(Object … initArgs)
20
Class member…
• FieldGet information
Class<T> getDeclaringClass() Class<T> getType() int getModifiers()
Get field’s value of an object Object get(Object obj)
Set field’s value of an object• set(Object obj, Object value)
21
Class member…
• Getting Field’s Value Example
22
Class c = Integer.class; //get a private instant field Field field = c.getDeclaredField("value"); field.setAccessible(true); System.out.format(field.get(new Integer(10)));
//get a public static field field = c.getField("MAX_VALUE"); System.out.format(field.get(null));
Class member…
• Setting Field’s Value Example
23
Class c = Integer.class; Integer i = new Integer(10);//get a private instant field Field field = c.getDeclaredField("value"); field.setAccessible(true); System.out.format(field.set(i, new Integer(20)));
//get a public static field field = c.getField("MAX_VALUE"); System.out.format(field.set(null, new Integer(20)));
Class member…
• Method–Get information
• Class<T> getDeclaringClass()• Class<T> [] getParameterTypes()• int getModifiers()• Class<T> getReturnType()
– Invoke method• Object invoke(Object obj, Object … initArgs)
24
Class member…
• Invoke a method example
25
Method f = Math.class.getMethod("sqrt", new Class[]{double.class});
Object[] args = {new Double(10)};//invoke a static method Double d = (Double) f.invoke(null, args); double y = d.doubleValue(); System.out.println(y);
Agenda
Introduction Java Reflection API
java.lang.Class class
Class Member
Array and Enum
Advantage & Disadvantage
Plug-in Programming
Array and Enum
• Array (java.lang.reflect package) Get array’s information Instantiate a new array dynamically Set/Get array component’s value
• Enum Get enum’s information Set/Get enum component’s value
27
Agenda
Introduction Java Reflection API
java.lang.Class class
Class Member
Array and Enum
Plug-in Programming
Advantage & Disadvantage
Plug-in Programming
29
ApplicationApplication
Plugin Container
SQL MySQL Oracle
Advantage & Disadvantage
Advantage• Dynamic Programming• Avoid hard-coding
Disadvantage• Performance• Security
30
31
Q & AThanks you!