Click here to load reader
Upload
dhrubo-kayal
View
1.037
Download
0
Embed Size (px)
DESCRIPTION
Citation preview
ReflectionDhrubojyoti Kayal
Java's Reflection API's makes it possible to inspect classes, interfaces, fields and methods at runtime, without knowing the names of the classes, methods etc. at compile time
It is also possible to instantiate new objects, invoke methods and get/set field values using reflection
What is reflection?
Using Java Reflection you can inspect Java classes at runtime
From the classes you can obtain information about ◦ Class Name◦ Class Modifies (public, private, synchronized etc.)◦ Package Info◦ Superclass◦ Implemented Interfaces◦ Constructors◦ Methods◦ Fields◦ Annotations
Classes
First step in reflection is to get hold of the Class object
When you know the name at compile time◦ Class myObjectClass = MyObject.class
If you don't know the name at compile time, but have the class name as a string at runtime or from some configuration file◦ String className = Class.forName(className); ◦ className = fully qualified class name◦ ClassNotFoundException
Class object
From a Class object you can obtain its name in two versions
Fully qualified◦ String className = aClass.getName();
Simple name◦ String simpleClassName =
aClass.getSimpleName();
Class Name
You can access the modifiers of a class via the Class object The class modifiers are the keywords "public", "private",
"static" etc int modifiers = aClass.getModifiers(); You can check the modifiers using these methods in the
class java.lang.reflect.Modifiers: ◦ Modifiers.isAbstract(int modifiers) ◦ Modifiers.isFinal(int modifiers) ◦ Modifiers.isInterface(int modifiers) ◦ Modifiers.isNative(int modifiers) ◦ Modifiers.isPrivate(int modifiers) ◦ Modifiers.isProtected(int modifiers) ◦ Modifiers.isPublic(int modifiers) ◦ Modifiers.isSynchronized(int modifiers) ◦ Modifiers.isTransient(int modifiers)
Modifiers
Package package = aClass.getPackage();
Package Information
Class superclass = aClass.getSuperclass(); The superclass class object is a Class object
like any other, so you can continue doing class reflection on that too.
Superclass
Class[] interfaces = aClass.getInterfaces(); A class can implement many interfaces.
Therefore an array of Class is returned. Interfaces are also represented by Class
objects in Java Reflection. To get a complete list of the interfaces
implemented by a given class you will have to consult both the class and its superclasses recursively.
Implemented interfaces
Constructor[] constructors = aClass.getConstructors();
Method[] method = aClass.getMethods(); Method[] method = aClass.getFields(); Annotation[] annotations =
aClass.getAnnotations();
Other information
Gettting specific constructor◦ Constructor constructor =
aClass.getConstructor(new Class[]{String.class}); If no constructor matches the given
constructor arguments, in this case String.class, a NoSuchMethodException is thrown.
You can read what parameters a given constructor takes like this: ◦ Class[] parameterTypes =
constructor.getParameterTypes();
Constructor
Constructor constructor = MyObject.class.getConstructor(String.class);
MyObject myObject = (MyObject) constructor.newInstance("constructor-arg1");
Instantiate object sans new
Class aClass = ...//obtain class object Field[] fields = aClass.getFields(); The Field[] array will have one Field instance
for each public field declared in the class If you know the name of the field you want
to access, you can access it like this Class aClass = MyObject.class Field field = aClass.getField("someField");
Fields
String fieldName = field.getName(); Object fieldType = field.getType(); Getting and Setting Field Values
◦ Class aClass = MyObject.class Field field = aClass.getField("someField"); MyObject objectInstance = new MyObject(); Object value = field.get(objectInstance); field.set(objetInstance, value);
Fields
Class aClass = ...//obtain class object Method[] methods = aClass.getMethods();
The Method[] array will have one Method instance for each public method declared in the class.
Get the public method named "doSomething“◦ Method method = aClass.getMethod("doSomething",
new Class[]{String.class}); ◦ NoSuchMethodException
Get no argument method◦ Method method = aClass.getMethod("doSomething",
null);
Methods
Method parameters◦ parameterTypes = method.getParameterTypes();
Return type◦ Class returnType = method.getReturnType();
Methods
//get method that takes a String as argument Method method = MyObject.class.getMethod("doSomething", String.class);
Object returnValue = method.invoke(myInstance, "parameter-value1");
Execute a static method?
Invoking methods
Dynamic code addition at runtime Building frameworks.
Why is reflection important?
Q&A