10-javareflection
10-javareflection
Course
Reflection API
1
Session objectives
1. Introduction
2. The Class object
1. Getting Constructors
2. Getting Fields
3. Getting Methods
4. Array Object
3. Invoke methods dynamically
2
Java looking at Java
4
Reflection
5
Kiến trúc của Java Reflection API
• Các lớp được dùng trong reflection nằm trong hai package là
java.lang và java.lang.reflect.
• Package java.lang.reflect bao gồm ba lớp chính cần biết là
Constructor, Field và Method
6
Kiến trúc của Java Reflection API
• Class<T>: lớp này đại diện cho các lớp, interface và chứa các
phương thức dùng để lấy các đối tượng kiểu Constructor, Field,
Method,…
• Field: chứa các thông tin về một field của lớp, interface.
• Method: chứa các thông tin về một phương thức của lớp, interface.
7
Tạo đối tượng Class<>
• Đối tượng kiểu này được tạo ra bằng cách sử dụng phương thức
static Class.forName():
8
The Class class
• To find out about a class, first get its Class object
o If you have an object obj, you can get its class object with
Class c = obj.getClass();
o You can get the class object for the superclass of a Class c
with
Class sup = c.getSuperclass();
o If you know the name of a class (say, Button) at compile
time, you can get its class object with
Class c = Button.class;
o If you know the name of a class at run time (in a String
variable str), you can get its class object with
Class c = class.forName(str);
9
Getting the class name
• If you have a class object c, you can get the name of the class
with c.getName()
• getName returns the fully qualified name; that is,
Class c = Button.class;
String s = c.getName();
System.out.println(s);
will print
java.awt.Button
• Class Class and its methods are in java.lang, which is always
imported and available
10
Getting all the superclasses
• Example
11
Getting the class modifiers I
13
Getting interfaces
14
Examining classes and interfaces
15
Getting Fields
18
Getting Constructors of a class
• if c is a Class, then
• c.getConstructors() : Constructor[] return an array of all public
constructors of class c.
• c.getConstructor( Class … paramTypes ) returns a constructor
whose parameter types match those given paramTypes.
Ex:
• String.class.getConstructors().length
> 15;
• String.class.getConstructor( char[].class, int.class,
int.class).toString()
> String(char[], int,int).
19
Constructors
• String s = c.newInstance(
new char[] {‘a’,’b’,’c’,’d’ }, 1, 2 );
• assert s == “bc”;
21
Example
22
Methods
• getDeclaringClass()
o Returns the Class object representing the class or
25
Methods
• getReturnType()
o Returns a Class object that represents the formal return type
of the method represented by this Method object
• toString()
o Returns a String describing this Method (typically pretty
long)
• public Object invoke(Object obj, Object… args)
o Invokes the underlying method represented by this Method
object, on the specified object with the specified parameters
o Individual parameters are automatically unwrapped to match
primitive formal parameters 26
Examples of invoke()
• “abcdefg”.length()
> 7
• Method lengthMethod = String.class.getMethod(“length”) ;
• lengthMethod.invoke(“abcdefg”)
>7
• “abcdefg”.substring(2, 5)
> cde
• Method substringMethod = String.class.getMethod
( “substring”, int.class, Integer.TYPE ) ;
• substringEMthod.invoke( “abcdefg”, 2, new Integer(5) )
> cde 27
Examples of invoke()
28
Arrays I
• Ex:
o int[].class.isArray() == true ;
o int[].class.getComponentType() == int.class
29
Arrays II
31
Arrays III
32
Examples
33
Array example
Output
34
Getting non-public members of a class
• String.class.getConstructors().length
> 15
• String.class.getDeclaredConstructors().length
> 16.
• Constructor[] cs = String.class.getDeclaredConstructors();
for(Constructor c : cs)
if( ! (Modifier.isPublic(c.getModifiers())))
out.println(c);
> java.lang.String(int,int,char[]) // package
36
Create object at runtime
Example class
37
Create object dynamically
38
Call method dynamically
39
ClassLoader
40
Concluding comments
41
FAQ
42
That’s all for this session!
43
43/2