19

Click here to load reader

19 reflection

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: 19   reflection

ReflectionDhrubojyoti Kayal

Page 2: 19   reflection

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?

Page 3: 19   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

Page 4: 19   reflection

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

Page 5: 19   reflection

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

Page 6: 19   reflection

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

Page 7: 19   reflection

Package package = aClass.getPackage();

Package Information

Page 8: 19   reflection

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

Page 9: 19   reflection

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

Page 10: 19   reflection

Constructor[] constructors = aClass.getConstructors();

Method[] method = aClass.getMethods(); Method[] method = aClass.getFields(); Annotation[] annotations =

aClass.getAnnotations();

Other information

Page 11: 19   reflection

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

Page 12: 19   reflection

Constructor constructor = MyObject.class.getConstructor(String.class);

MyObject myObject = (MyObject) constructor.newInstance("constructor-arg1");

Instantiate object sans new

Page 13: 19   reflection

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

Page 14: 19   reflection

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

Page 15: 19   reflection

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

Page 16: 19   reflection

Method parameters◦ parameterTypes = method.getParameterTypes();

Return type◦ Class returnType = method.getReturnType();

Methods

Page 17: 19   reflection

//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

Page 18: 19   reflection

Dynamic code addition at runtime Building frameworks.

Why is reflection important?

Page 19: 19   reflection

Q&A