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:

[1]
Maes, P. 1987. Concepts and experiments in computational reflection. In Conference Proceedings on Object-Oriented Programming Systems, Languages and Applications (Orlando, Florida, United States, October 04 - 08, 1987). N. Meyrowitz, Ed. OOPSLA '87. ACM Press, New York, NY, 147-155.
[2]
Bracha, G. and Ungar, D. 2004. Mirrors: design principles for meta-level facilities of object-oriented programming languages. In Proceedings of the 19th Annual ACM SIGPLAN Conference on Object-Oriented Programming, Systems, Languages, and Applications (Vancouver, BC, Canada, October 24 - 28, 2004). OOPSLA '04. ACM Press, New York, NY, 331-344.
[3]
Czarnecki, K. and Eisenecker, U.W. 2000. Generative Programming: Methods, Tools, and Applications. Addison Wesley
[4]
The Java Tutorial: http://java.sun.com/docs/books/tutorial/index.html (last accessed January 30, 2006)
 
Back to the Glossary of Generative Programming Terms
Back to the course home page
Contributor: Anders Hessellund
Last modification: 30/1 2006