Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
REFLECTION
REFLECTION
In computer science, reflection is the process
by which a computer program can observe and
modify its own structure and behavior at
runtime.
- http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29
REFLECTION
In many computer architectures, program instructions are stored as
data - hence the distinction between instruction and data is merely a
matter of how the information is treated by the computer and
programming language.
Normally, instructions are executed and data is [sic] processed;
however, in some languages, programs can also treat instructions as
data and therefore make reflective modifications.
Reflection is most commonly used in high-level virtual machine
programming languages like Smalltalk and scripting languages, and
less commonly used in manifestly typed and/or statically typed
programming languages such as Java, C, ML or Haskell.
- http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29
REFLECTION
Brian Cantwell Smith's 1982 doctoral
dissertation introduced the notion of
computational reflection in programming
languages, and the notion of the meta-circular
interpreter as a component of 3-Lisp.
- http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29
REFLECTION USED BY THE UNITY
INTERFACE
REFLECTION USED BY THE UNITY INTERFACE
REFLECTION IMPLEMENTED IN JAVA
REFLECTION IMPLEMENTED IN JAVA
Java’s Class class (java.lang.Class)
Instances of the class Class represent classes and
interfaces in a running Java program.
Every array also belongs to a class that is reflected as a
Class object that is shared by all arrays with the same
element type and number of dimensions.
The primitive Java types (boolean, byte, char, short, int,
long, float, and double), and the keyword void are also
represented as Class objects.
- http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html
REFLECTION IN JAVA
The Class class has no ctors. But it does have
the following interesting method:
static Class forName ( String className )
Returns
the Class object associated with the class or
interface with the given string name.
Ex.
Class c = Class.forName( "java.lang.String" );
REFLECTION IN JAVA
We can use this to get the name of the class.
//class name
System.out.println( "class: " + c.getName() );
We can also use this to get the name of its
superclass.
//superclass name
Class s = c.getSuperclass();
System.out.println( "superclass: " + s.getName() );
But that’s not all that exciting.
REFLECTION IN JAVA
Introducing the Java reflection API!
1.
java.lang.reflect.Constructor
2.
java.lang.reflect.Field
3.
java.lang.reflect.Method
REFLECTION IN JAVA
java.lang.reflect.Constructor
Back to the Class class:
Constructor[] getConstructors ( )
Returns an array containing Constructor objects reflecting all the
public constructors of the class represented by this Class object.
Constructor[] getDeclaredConstructors ( )
Returns an array of Constructor objects reflecting all the constructors
declared by the class represented by this Class object.
REFLECTION IN JAVA
java.lang.reflect.Field
Back to the Class class:
Field[] getFields ( )
Returns an array containing Field objects reflecting all the accessible
public fields of the class or interface represented by this Class
object.
Field[] getDeclaredFields ( )
Returns an array of Field objects reflecting all the fields declared by
the class or interface represented by this Class object.
REFLECTION IN JAVA
java.lang.reflect.Method
Back to the Class class:
Method[] getMethods ( )
Returns an array containing Method objects reflecting all the public
member methods of the class or interface represented by this Class
object, including those declared by the class or interface and those
inherited from superclasses and superinterfaces.
Method[] getDeclaredMethods ( )
Returns an array of Method objects reflecting all the methods declared
by the class or interface represented by this Class object.
REFLECTION IN JAVA
Also . . .
Class[]
getInterfaces ( )
Determines
the interfaces implemented by the class or
interface represented by this object.
REFLECTION IN JAVA
Other useful Class class methods:
boolean isArray ( ) - Determines if this Class object represents an array
class.
boolean isAssignableFrom ( Class cls ) - Determines if the class or
interface represented by this Class object is either the same as, or is a
superclass or superinterface of, the class or interface represented by
the specified Class parameter.
boolean isInstance ( Object obj ) - Determines if the specified Object is
assignment-compatible with the object represented by this Class.
boolean isInterface ( ) - Determines if the specified Class object represents
an interface type.
boolean isPrimitive ( ) - Determines if the specified Class object represents
a primitive type.
Object newInstance ( ) - Creates a new instance of the class represented
by this Class object.
NOW LET’S GET MORE SPECIFIC
REFLECTION IN JAVA
Using the Class class methods, we can also be more
specific for ctors.
Constructor getConstructor ( Class[] parameterTypes )
Returns a Constructor object that reflects the specified public
constructor of the class represented by this Class object.
Constructor getDeclaredConstructor ( Class[]
parameterTypes )
Returns a Constructor object that reflects the specified constructor
of the class or interface represented by this Class object.
REFLECTION IN JAVA
Using the Class class methods, we can be more
specific for methods, too.
Method getMethod ( String name, Class[] parameterTypes )
Returns a Method object that reflects the specified public member
method of the class or interface represented by this Class object.
Method getDeclaredMethod ( String name, Class[]
parameterTypes )
Returns a Method object that reflects the specified declared method
of the class or interface represented by this Class object.
REFLECTION IN JAVA
Using the Class class methods, we can be more
specific for fields also.
Field getField ( String name )
Field getDeclaredField ( String name )
Returns a Field object that reflects the specified public member field
of the class or interface represented by this Class object.
Returns a Field object that reflects the specified declared field of the
class or interface represented by this Class object.
What methods does the Field class provide?
FIELD CLASS METHODS
REFLECTION IN JAVA
Some interesting Field class methods:
Class getType ( ) - Returns a Class object that identifies the declared type for the field
represented by this Field object.
REFLECTION IN JAVA
Some interesting Field class methods:
Object get ( Object obj ) - Returns the value of the field represented by this Field, on the
specified object.
boolean getBoolean ( Object obj ) - Gets the value of a static or instance boolean field.
byte getByte ( Object obj ) - Gets the value of a static or instance byte field.
char getChar ( Object obj ) - Gets the value of a static or instance field of type char or of
another primitive type convertible to type char via a widening conversion.
double getDouble ( Object obj ) - Gets the value of a static or instance field of type double or
of another primitive type convertible to type double via a widening conversion.
float getFloat ( Object obj ) - Gets the value of a static or instance field of type float or of
another primitive type convertible to type float via a widening conversion.
int getInt ( Object obj ) - Gets the value of a static or instance field of type int or of another
primitive type convertible to type int via a widening conversion.
long getLong ( Object obj ) - Gets the value of a static or instance field of type long or of
another primitive type convertible to type long via a widening conversion.
short getShort ( Object obj ) - Gets the value of a static or instance field of type short or of
another primitive type convertible to type short via a widening conversion.
REFLECTION IN JAVA
Some interesting Field class methods:
void set ( Object obj, Object value ) - Sets the field represented by this Field object on the
specified object argument to the specified new value.
void setBoolean ( Object obj, boolean z ) - Sets the value of a field as a boolean on the
specified object.
void setByte ( Object obj, byte b ) - Sets the value of a field as a byte on the specified object.
void setChar ( Object obj, char c ) - Sets the value of a field as a char on the specified object.
void setDouble ( Object obj, double d ) - Sets the value of a field as a double on the specified
object.
void setFloat ( Object obj, float f ) - Sets the value of a field as a float on the specified object.
void setInt ( Object obj, int I ) - Sets the value of a field as an int on the specified object.
void setLong ( Object obj, long l ) - Sets the value of a field as a long on the specified object.
void setShort ( Object obj, short s ) - Sets the value of a field as a short on the specified
object.
METHOD CLASS METHODS
REFLECTION IN JAVA
Some interesting Method class methods:
Class[] getParameterTypes ( ) - Returns an array of Class objects that represent the formal
parameter types, in declaration order, of the method represented by this Method object.
Class getReturnType ( ) - Returns a Class object that represents the formal return type of the
method represented by this Method object.
Object invoke ( Object obj, Object[] args ) - Invokes the underlying method represented by this
Method object, on the specified object with the specified parameters.
WRAP UP
REFLECTION IN JAVA
//basic framework for reflection
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ReflectMe {
public static void main ( String args[] ) {
String cName = "Point";
try {
Class c = Class.forName( cName );
…
} catch (Exception e) {
System.out.println( e );
}
}
}
class Point {
…
}
REFLECTION IN JAVA
So wrapping it all up . . . given a string that contains the
name of a class, we can write a program that:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
Prints out the class and superclass names.
Lists the ctors.
Lists the methods.
Lists the fields (data members).
Creates an instance of the class using the no-arg ctor.
Creates an instance using another ctor (other than no-arg).
First, you need to get an
Gets the value of a field.
instance of the appropriate
Changes the value of a field.
ctor (using getConstructor(…)
from Class), and then invoke
Calls a method w/out args.
that ctor (using
newInstance(…) from
Calls a method w/ args.
Constructor).
REFLECTION IN JAVA
Assignment:
Using the basic framework presented previously and the
following definition for the Point class, do all of the previous
steps with 2 chilis or less.
class Point {
public int mX, mY;
public Point ( ) {
System.out.println( "in Point()" );
mX = mY = 0;
}
public Point ( int x, int y ) {
System.out.println( "in Point(x,y)" );
mX = x;
mY = y;
}
public double getLength ( ) {
System.out.println( "in getLength()" );
return Math.sqrt( mX*mX + mY*mY );
}
}
(Hint:
For 7 and 8, you may assume that
you know the name and type of the
field; similarly for 9, you may
assume that you know the name of
the method.)