1 / 20

The Reflection

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.

cael
Download Presentation

The Reflection

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Reflection Lucielle Mendez Antonio Bologna

  2. 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

  3. What Is Reflection? • METADATA • METAOBJECTS • INTROSPECTION

  4. When do you use Reflection? debuggers class browsers GUI builders Don’t misuse Reflection!

  5. Reflection API Helps: • . • Determine the class of an object. • Get metadata about classes and Interfaces • modifiers • Fields • Methods • Constructors • Superclasses • Operate on an instance of a class whose name is not known until runtime • Operate on object fields, even if field names are not known until runtime • Call methods which are not known until runtime • Operate on arrays whose size and component type are not known until runtime

  6. Reflection API • Fetch mechanism built into class named Class • Methods: • getConstrunctor • getMethod/s, getDeclaredMethods • getField/s, getDeclaredFields • getSuperclass • getInterface • getDeclaredClasses

  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? • 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.

  9. Examining Classes • Retrieving Class Objects • Getting the Class Name • Finding Superclasses • Identifying the Interfaces Implemented by a Class • Identifying Class Fields • Discovering Class Constructors • 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: • 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. 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. 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. • 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 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. • 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. • 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 • Invoking Methods

  18. Creating Class Objects • 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: 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.

  20. Invoking Methods Steps • 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.

More Related