Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Reflection Lucielle Mendez Antonio Bologna.

Similar presentations


Presentation on theme: "The Reflection Lucielle Mendez Antonio Bologna."— Presentation transcript:

1 The Reflection Lucielle Mendez Antonio Bologna

2

3

4

5

6

7 The Meaning of Reflection The Reflection API is located in the java.lang.reflect package. The Reflection API is all about providing a "reflection" of the inner workings of a class.

8 Examining Classes Why do you want to examine Classes? Why do you want to examine Classes? For example, if you’re writing a class browser application, you may need Reflection to get information at runtime. For example, if you’re writing a class browser application, you may need Reflection to get information at runtime. JRE: Java runtime environment maintains a copy of the class as an object which contains information about the class. JRE: Java runtime environment maintains a copy of the class as an object which contains information about the class.

9 Examining Classes Retrieving Class Objects Retrieving Class Objects Getting the Class Name Getting the Class Name Finding Superclasses Finding Superclasses Identifying the Interfaces Implemented by a Class Identifying the Interfaces Implemented by a Class Identifying Class Fields Identifying Class Fields Discovering Class Constructors Discovering Class Constructors Obtaining Method Information Obtaining Method Information

10 Retrieving Class Objects If the instance of a class is available at runtime, you can Object.getClass() to obtain information about that object: If the instance of a class is available at runtime, you can Object.getClass() to obtain information about that object: Class c = drawLine.getClass(); Class c = drawLine.getClass();

11 Getting the Class Name At runtime, you can determine the name of a Class object by invoking the getName method. The String returned by getName is the fully-qualified name of the class. At runtime, you can determine the name of a Class object by invoking the getName method. The String returned by getName is the fully-qualified name of the class. Class c = o.getClass(); String s = c.getName();

12 Finding Superclasses To determine the superclass of a class, you invoke the getSuperclass method. This method returns a Class object representing the superclass, or returns null if the class has no superclass. To determine the superclass of a class, you invoke the getSuperclass method. This method returns a Class object representing the superclass, or returns null if the class has no superclass. Class subclass = o.getClass(); Class superclass = subclass.getSuperclass();

13 Identifying the Interfaces Implemented by a Class With reflection not only we can know an object class and superclass, but also we can know its interfaces. With reflection not only we can know an object class and superclass, but also we can know its interfaces. You invoke the getInterfaces method to determine which interfaces a class implements. You invoke the getInterfaces method to determine which interfaces a class implements. Class c = o.getClass(); Class[] theInterfaces = c.getInterfaces();

14 Identifying Class Fields You might want to find out what fields belong to a particular class. You might want to find out what fields belong to a particular class. You can identify a class's fields by invoking the getFields method on a Class object. You can identify a class's fields by invoking the getFields method on a Class object. Class c = o.getClass(); Field[] fields = c.getFields();

15 Discovering Class Constructors Typically to create an instance of a class you invoke the class constructor. Typically to create an instance of a class you invoke the class constructor. To get information about a class's constructors you invoke the getConstructors method, which returns an array of Constructor objects. To get information about a class's constructors you invoke the getConstructors method, which returns an array of Constructor objects. Class c = o.getClass(); Constructor[] theConstructors = c.getConstructors();

16 Obtaining Method Information To find out what public methods belong to a class, invoke the method named getMethods. To find out what public methods belong to a class, invoke the method named getMethods. You can use a Method object to uncover a method's name, return type, parameter types, set of modifiers, and set of throwable exceptions. You can use a Method object to uncover a method's name, return type, parameter types, set of modifiers, and set of throwable exceptions. Class c = o.getClass(); Method[] theMethods = c.getMethods();

17 Manipulating Objects Creating Objects Creating Objects Invoking Methods Invoking Methods

18 Creating 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); You may not know the class of an object until runtime: You may not know the class of an object until runtime: Point p = (Point) createObject("java.awt.Point"); static Object createObject(String className) { Object object = null; try { Class classDefinition = Class.forName(className); object = classDefinition.newInstance(); …..

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

20 Invoking Methods Steps Create a Class object that corresponds to the object whose method you want to invoke. Create a Class object that corresponds to the object whose method you want to invoke. Create a Method object by invoking getMethod on the Class object. Create a Method object by invoking getMethod on the Class object.Method


Download ppt "The Reflection Lucielle Mendez Antonio Bologna."

Similar presentations


Ads by Google