Reflection is the ability of a computer program to examine and manipulate its own state. Maes define a reflective system as "..a system incorporating structures representing (aspects of) itself (the sum of these structures is the system’s self-representation).." [1]. The base-level of the language is extended by a meta-level which represents entities and operations on the language itself. In mainstream languages like Java, C# and Smalltalk the base-level and meta-level typically coexist within the same language [2].
Reflection can take place at compile-time, load-time or run-time. Two central, conceptual aspects of reflection are introspection and intercession. Introspection is the examination of a program's state and properties. Intercession is the manipulation of a program's state and properties. Another important distinction is between structural and behavioural reflection. Structural reflection concerns the access to structural parts of a program, e.g., classes, methods, and fields. Behavioural reflection concerns the access to run-time aspects of the program, e.g., how objects are created, how methods are called [3].
Reflection has a wide array of applications which include activities such as dynamic loading of plugins, maitnance of running systems, object browsers, persistence frameworks and debugging. The following example from the Java Reflection API tutorial [4] shows how reflection can be used to print the interfaces that a Java object implements. This is accomplished by using the meta-level constructs within the Java language.:
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);
}
}
References: