Docsity

Java Reflection: The Ability of Java Programs to Examine Themselves, Slides of Computer Science

Partial preview of the text

Download Java Reflection: The Ability of Java Programs to Examine Themselves and more Slides Computer Science in PDF only on Docsity!

Reflection

Docsity.com

Java looking at Java

  • One of the unusual capabilities of Java is that a program can examine itself - You can determine the class of an object - You can find out all about a class: its access modifiers, superclass, fields, constructors, and methods - You can find out what is in an interface - Even if you don’t know the names of things when you write the program, you can: - Create an instance of a class - Get and set instance variables - Invoke a method on an object - Create and manipulate arrays
  • I guess this is called “reflection” because it’s as if a Java program could “look in a mirror” at itself

Docsity.com

BlueJ

  • BlueJ is a Java program—what can it do?
    • It can compile a program (easy—just a system call)
    • It can load in your program after compilation
    • It can find out what classes you have, and what their constructors and methods are
    • It can execute your main method
    • It can create objects for you even without running your main method
    • It can send messages (call methods) to those objects and display the results
  • All these capabilities, except compilation, are

done with reflection

Docsity.com

The Class class

  • To find out about a class, first get its Class object
    • If you have an object obj, you can get its class object with Class c = obj.getClass();
    • You can get the class object for the superclass of a Class c with Class sup = c.getSuperclass();
    • If you know the name of a class (say, Button) at compile time, you can get its class object with Class c = Button.class;
    • If you know the name of a class at run time (in a String variable str), you can get its class object with Class c = Class.forName(str); Docsity.com

Getting all the superclasses

  • getSuperclass() returns a Class object (or null if you call it on Object, which has no superclass)
  • The following code is from the Sun tutorial:

static void printSuperclasses(Object o) { Class subclass = o.getClass(); Class superclass = subclass.getSuperclass(); while (superclass != null) { String className = superclass.getName(); System.out.println(className); subclass = superclass; superclass = subclass.getSuperclass(); } } Docsity.com

Getting the class modifiers I

  • A Class object has an instance method

getModifiers() that returns an int

  • To “decipher” the int result, we need methods

of the Modifier class, which is in java.lang.reflect, so: import java.lang.reflect.*;

  • Now we can do things like:

if (Modifier.isPublic(m)) { System.out.println("public"); Docsity.com

Getting interfaces

  • A class can implement zero or more interfaces
  • getInterfaces() returns an array of Class objects
    • These are the interfaces implemented by the class
  • More code from Sun: static void printInterfaceNames(Object o) { Class c = o.getClass(); Class[ ] theInterfaces = c.getInterfaces(); for (int i = 0; i < theInterfaces.length; i++) { String interfaceName = theInterfaces[i].getName(); System.out.println(interfaceName); } }
  • Note that zero-length arrays are perfectly legal in Java

Docsity.com

Examining classes and interfaces

  • The class Class represents both classes and

interfaces

  • To determine if a given Class object c is an

interface, use c .isInterface()

  • To find out more about a class object, use:
    • getModifiers()
    • getFields() // "fields" == "instance variables"
    • getConstructors()
    • getMethods() Docsity.com

Using Fields, I

  • If f is a Field object, then
    • f .getName() returns the simple name of the field
    • f .getType() returns the type (Class) of the field
    • f .getModifiers() returns the Modifiers of the field
    • f .toString() returns a String containing access modifiers, the type, and the fully qualified field name - Example: public java.lang.String Person.name
    • f .getDeclaringClass() returns the Class in which this field is declared Docsity.com

Using Fields, II

  • The values of the fields of an object obj may

be accessed with:

  • boolean f .getBoolean( obj ), int f .getInt( obj ), double f .getDouble( obj ), etc., return the value of the field, assuming it is that type or can be widened to that type
  • Object f .get(obj) returns the value of the field, assuming it is an Object
  • void f .set( obj , value ), void f .setBoolean( obj , bool ), void f .setInt( obj , i ), void f .getDouble( obj , d ), etc. set the value of a field Docsity.com

Methods

  • public Method[] getMethods() throws SecurityException
  • Returns an array of Method objects
  • These are the public member methods of the class or interface, including inherited methods
  • The methods are returned in no particular order
  • public Method getMethod(String name, Class[] parameterTypes) throws NoSuchMethodException, SecurityException

Docsity.com

Method methods, I

  • getDeclaringClass()
    • Returns the Class object representing the class or interface that declares the method represented by this Method object
  • getName()
    • Returns the name of the method represented by this Method object, as a String
  • getModifiers()
    • Returns the Java language modifiers for the method represented by this Method object, as an integer
  • getParameterTypes()
    • Returns an array of Class objects that represent the formal parameter types, in declaration order, of the method represented by this Method object

Docsity.com

Arrays I

  • To determine whether an object obj is an

array,

  • Get its class c with Class c = obj.getClass();
  • Test with c.isArray()
  • To find the type of components of the array,
  • c.getComponentType()
  • Returns null if c is not the class of an array
  • There is an Array class in java.lang.reflect

that provides static methods for working with arrays Docsity.com

Arrays II

  • To create an array,
  • Array.newInstance(Class componentType, int size)
    • This returns, as an Object, the newly created array - You can cast it to the desired type if you like
    • The componentType may itself be an array
      • This would create a multiply-dimensioned array
      • The limit on the number of dimensions is usually 255
  • Array.newInstance(Class componentType, int[] size)
    • This returns, as an Object, the newly created multidimensional array (with size.length Docsity.com