the reflection

20
The Reflection Lucielle Mendez Antonio Bologna

Upload: cael

Post on 25-Feb-2016

33 views

Category:

Documents


0 download

DESCRIPTION

The Reflection. Lucielle Mendez. Antonio Bologna. What Is Reflection?. The ability of a running program to examine itself and its software environment, and to change what it does depending on what it finds . What Is Reflection?. METADATA METAOBJECTS INTROSPECTION. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: The Reflection

The Reflection

Lucielle Mendez Antonio Bologna

Page 2: The Reflection

What Is Reflection?What Is Reflection?• The ability of a running The ability of a running

program to examine itself program to examine itself and its software and its software environment, and to environment, and to change what it does change what it does depending on what it depending on what it finds finds

Page 3: The Reflection

What Is Reflection?What Is Reflection?

• METADATAMETADATA

• METAOBJECTSMETAOBJECTS

• INTROSPECTIONINTROSPECTION

Page 4: The Reflection

When do you use Reflection?When do you use Reflection?

• debuggersdebuggers

• class browsersclass browsers

• GUI buildersGUI builders

•Don’t misuse Reflection!Don’t misuse Reflection!

Page 5: The Reflection

Reflection API Helps:Reflection API Helps: • .

• Determine the class of an object. Determine the class of an object. • Get metadata about classes and InterfacesGet metadata about classes and Interfaces

• modifiersmodifiers• FieldsFields• MethodsMethods• ConstructorsConstructors• SuperclassesSuperclasses

• Operate on an instance of a class whose name is not Operate on an instance of a class whose name is not known until runtimeknown until runtime

• Operate on object fields, even if field names are not Operate on object fields, even if field names are not known until runtimeknown until runtime

• Call methods which are not known until runtimeCall methods which are not known until runtime• Operate on arrays whose size and component type are Operate on arrays whose size and component type are

not known until runtimenot known until runtime

Page 6: The Reflection

Reflection APIReflection API• Fetch mechanism built into class named Fetch mechanism built into class named ClassClass• Methods:Methods:

• getConstrunctorgetConstrunctor• getMethod/s, getDeclaredMethodsgetMethod/s, getDeclaredMethods• getField/s, getDeclaredFieldsgetField/s, getDeclaredFields• getSuperclassgetSuperclass• getInterfacegetInterface• getDeclaredClassesgetDeclaredClasses

Page 7: The Reflection

The Meaning of The Meaning of ReflectionReflection • The Reflection API is located in The Reflection API is located in

the java.lang.reflect package. the java.lang.reflect package. • The Reflection API is all about The Reflection API is all about

providing a "reflection" of the providing a "reflection" of the inner workings of a class.inner workings of a class.

Page 8: The Reflection

Examining ClassesExamining Classes• Why do you want to examine Why do you want to examine

Classes?Classes?• For example, if you’re writing a class For example, if you’re writing a class

browser application, you may need browser application, you may need Reflection to get information at runtime.Reflection to get information at runtime.

• JREJRE: Java runtime environment : Java runtime environment maintains a copy of the class as an maintains a copy of the class as an object which contains information about object which contains information about the class.the class.

Page 9: The Reflection

Examining ClassesExamining Classes• Retrieving Class ObjectsRetrieving Class Objects• Getting the Class NameGetting the Class Name• Finding SuperclassesFinding Superclasses• Identifying the Interfaces Identifying the Interfaces

Implemented by a ClassImplemented by a Class• Identifying Class FieldsIdentifying Class Fields• Discovering Class ConstructorsDiscovering Class Constructors• Obtaining Method InformationObtaining Method Information

Page 10: The Reflection

Retrieving Class ObjectsRetrieving Class Objects

• If the instance of a class is available If the instance of a class is available at runtime, you can Object.getClass() at runtime, you can Object.getClass() to obtain information about that to obtain information about that object:object:

• Class c = drawLine.getClass();Class c = drawLine.getClass();

Page 11: The Reflection

Getting the Class NameGetting the Class Name

• At runtime, you can determine the At runtime, you can determine the name of a Class object by invoking name of a Class object by invoking the getName method. The String the getName method. The String returned by getName is the fully-returned by getName is the fully-qualified name of the class. qualified name of the class.

Class c = o.getClass(); Class c = o.getClass(); String s = c.getName();String s = c.getName();

Page 12: The Reflection

Finding SuperclassesFinding Superclasses

• To determine the superclass of a class, To determine the superclass of a class, you invoke the getSuperclass method. you invoke the getSuperclass method. This method returns a Class object This method returns a Class object representing the superclass, or returns representing the superclass, or returns null if the class has no superclass. null if the class has no superclass.

Class subclass = o.getClass(); Class subclass = o.getClass(); Class superclass = subclass.getSuperclass(); Class superclass = subclass.getSuperclass();

Page 13: The Reflection

Identifying the Interfaces Identifying the Interfaces Implemented by a ClassImplemented by a Class• With reflection not only we can know an With reflection not only we can know an

object class and superclass, but also we can object class and superclass, but also we can know its interfaces.know its interfaces.

• You invoke the getInterfaces method to You invoke the getInterfaces method to determine which interfaces a class determine which interfaces a class implements. implements.

Class c = o.getClass(); Class c = o.getClass(); Class[] theInterfaces = c.getInterfaces();Class[] theInterfaces = c.getInterfaces();

Page 14: The Reflection

Identifying Class FieldsIdentifying Class Fields

• You might want to find out what You might want to find out what fields belong to a particular class. fields belong to a particular class.

• You can identify a class's fields by You can identify a class's fields by invoking the getFields method on a invoking the getFields method on a Class object. Class object.

Class c = o.getClass(); Class c = o.getClass(); Field[] fields = c.getFields(); Field[] fields = c.getFields();

Page 15: The Reflection

Discovering Class Discovering Class ConstructorsConstructors• Typically to create an instance of a Typically to create an instance of a

class you invoke the class constructor.class you invoke the class constructor.• To get information about a class's To get information about a class's

constructors you invoke the constructors you invoke the getConstructors method, which returns getConstructors method, which returns an array of Constructor objects.an array of Constructor objects.

Class c = o.getClass(); Class c = o.getClass(); Constructor[] theConstructors = c.getConstructors();Constructor[] theConstructors = c.getConstructors();

Page 16: The Reflection

Obtaining Method Obtaining Method InformationInformation• To find out what To find out what publicpublic methods methods

belong to a class, invoke the method belong to a class, invoke the method named getMethods. named getMethods.

• You can use a Method object to You can use a Method object to uncover a method's name, return type, uncover a method's name, return type, parameter types, set of modifiers, and parameter types, set of modifiers, and set of throwable exceptions. set of throwable exceptions.

Class c = o.getClass(); Class c = o.getClass(); Method[] theMethods = c.getMethods(); Method[] theMethods = c.getMethods();

Page 17: The Reflection

Manipulating ObjectsManipulating Objects

• Creating ObjectsCreating Objects• Invoking MethodsInvoking Methods

Page 18: The Reflection

Creating Class ObjectsCreating Class Objects• Simplest way to create an object in the Java :Simplest way to create an object in the Java :

Point p1 = new Point(x1, y1);Point p1 = new Point(x1, y1); • You may not know the class of an object until You may not know the class of an object until

runtime:runtime:

Point p = (Point) createObject("java.awt.Point");Point p = (Point) createObject("java.awt.Point");

static Object createObject(String className) { static Object createObject(String className) { Object object = null; Object object = null; try { try {

Class classDefinition = Class.forName(className); Class classDefinition = Class.forName(className); object = classDefinition.newInstance();object = classDefinition.newInstance();

……....

Page 19: The Reflection

Invoking MethodsInvoking Methods

• Suppose that you are writing a Suppose that you are writing a debugger that allows the user to debugger that allows the user to select and then invoke methods select and then invoke methods during a debugging session. Since during a debugging session. Since you don't know at compile time you don't know at compile time which methods the user will invoke, which methods the user will invoke, you cannot hardcode the method you cannot hardcode the method name in your source code. name in your source code.

Page 20: The Reflection

Invoking Methods StepsInvoking Methods Steps

• Create a Class object that Create a Class object that corresponds to the object whose corresponds to the object whose method you want to invoke. method you want to invoke.

• Create a Create a Method Method object by invoking object by invoking getMethod on the Class object. getMethod on the Class object.