Professional Documents
Culture Documents
Reflections On Reflection
Reflections On Reflection
REFLECTION
ri ek sh n
... the throwing back by a body or surface of light, heat, or sound without absorbing it; an idea about something; the conceptual operation of inverting a system or event with respect to a plane.
Oxford Dictionary
SELF-REFERENTIAL
[xkcd #688]
SELF-AWARENESS
Here I am walking down the street in the rain... Since Im starting to get drenched, I should open my umbrella!
BACK TO COMPUTERS
The previous thought fragment reveals a self-awareness of behavior and state, one that leads to a change in that selfsame behavior and state. It would be desirable for computations to avail themselves of these reective capabilities, examining themselves in order to make use of meta-level information in decisions about what to do next.
ARTIFICIAL INTELLIGENCE?
REFLECTION
ri ek sh n
... the process by which a computer program can observe, or introspect, and modify, or intersect, its own structure and behavior on-the-y.
Wikipedia
e.g. query for a list of dened classes. Intersect, e.g. create a new class.
REIFICATION
rf k sh n
and methods become objects. Behavioral, computational mechanisms, e.g. call stack.
META-*
met str
... denoting something of a higher or second-order kind; referring to itself or to the conventions of its genre; self-referential.
Oxford Dictionary
Meta-information (or meta-data) is information on the information held within a data set; a programs metainformation is information about the program itself.
[this slide is a blatant oversimplication]
IN SUMMARY...
Meta-Level
Introspection Reection
Intersection
Reication
Operational Level
META-PROGRAMMING
met pr grami ng
... the writing of computer programs that write or manipulate other programs, or themselves.
Wikipedia
Y COMBINATOR
Quines are similar to xed-point combinators in -calculus:
Y g = (f . (x . f (x x)) (x . f (x x))) g = (x . g (x x)) (x . g (x x)) = (y . g (y y)) (x . g (x x)) = g ((x . g (x x)) (x . g (x x))) = g (Y g) by denition of Y by -reduction of f by -conversion by -reduction of y by second equality
PRAGMATICS
REFLECTING IN JAVA
Java stores metadata in classes (reication): ... for a class ... for a constructor ... for a eld ... for a method
java.lang.Class java.lang.reflect.Constructor java.lang.reflect.Field java.lang.reflect.Method
It is also possible to store user-dened metadata by decorating structural elements (classes, methods, elds...) with @annotations.
OBTAINING A CLASS
There are two ways to access a class: Starting from an object and using Searching with
forName()
JAVA
getClass()
Board b = new Board(); Class<?> bClass = b.getClass(); assertEquals("Board", bClass.getName()); Class<?> bClass2 = Class.forName("Board"); assertSame(bClass2, bClass);
INTROSPECTION
JAVA
protected void java.lang.Object.finalize() throws java.lang.Throwable public final void java.lang.Object.wait() throws java.lang.InterruptedException public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException public boolean java.lang.Object.equals(java.lang.Object) public boolean java.lang.Object.equals(java.lang.Object) public java.lang.String java.lang.Object.toString() public native int java.lang.Object.hashCode() public final native java.lang.Class java.lang.Object.getClass() protected native java.lang.Object java.lang.Object.clone() throws CloneNotSupportedException private static native void java.lang.Object.registerNatives() public final native void java.lang.Object.notify() public final native void java.lang.Object.notifyAll()
OUTPUT
CREATING AN OBJECT
The constructor can be called using
// without reflection new Board().hello(); // with reflection Class<?> bClass = Class.forName("Board"); Board x = (Board) bClass.newInstance(); bClass.getMethod("hello").invoke(x);
newInstance()
JAVA
XML-CONFIGURATION
<configuration> <plugins> <load name=WriteToConsole /> <load name=TweetIt /> <plugins> </configuration>
XML
JAVA
Vector<Plugin> plugins = new Vector<Plugin>(); Vector<String> names = new Vector<String>(); // Now load XML into names vector, and then... for (String x : names) { ! plugins.add((Plugin) Class.forName(x).newInstance());!
RESIZING AN ARRAY
Thought arrays were xed in size? Think again...
JAVA
public Object growArray(Object array, int size) { Class type = array.getClass().getComponentType(); Object grown = Array.newInstance(type, size); System.arraycopy(array, 0, grown, 0, Math.min(Array.getLength(array), size)); return grown; }
The getComponentType() method returns the array contents type: for int[] it would return int.
MISBEHAVING
How to access a eld that has been marked as private?
if (!Modifier.isPublic(field.getModifiers())) ! field.setAccessible(true); Object obj = field.get(obj);
JAVA
And heres the rst example of negative side-effects of reection, security. The next is...
PERFORMANCE
Field access times using get() and set() in ms log scale:
Direct
Reference
[source http://www.ibm.com/developerworks/library/j-dyn0603/]
REFLECTIVE TOWER
M3
Class
instanceOf M2
instanceOf
instanceOf classier
Attribute
Class
Instance
instanceOf M1
instanceOf snapshot
instanceOf
instanceOf M0
Matrix
APPLICATIONS
There are many uses to reection, including: Plugins to extend functionalities Auto-completion in IDE Perform actions based on Annotations Tie components together Bytecode manipulation Runtime code generation Evaluate Java expressions interactively Ant Eclipse JUnit Spring AOP Proxies, JSP Eclipse
ALWAYS REMEMBER