230 likes | 425 Views
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.
E N D
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
When do you use Reflection? debuggers class browsers GUI builders Don’t misuse Reflection!
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
Reflection API • Fetch mechanism built into class named Class • Methods: • getConstrunctor • getMethod/s, getDeclaredMethods • getField/s, getDeclaredFields • getSuperclass • getInterface • getDeclaredClasses
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.
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.
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
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();
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();
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();
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();
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();
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();
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();
Manipulating Objects • Creating Objects • Invoking Methods
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(); …..
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.
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.