Reflection (computer science)
From Wikipedia, the free encyclopedia
In computer science, reflection is the process by which a computer program of the appropriate type can be modified in the process of being executed, in a manner that depends on abstract features of its code and its runtime behavior. Figuratively speaking, it is then said that the program has the ability to "observe" and possibly to modify its own structure and behavior. The programming paradigm driven by reflection is called reflective programming.
Typically, reflection refers to runtime or dynamic reflection, though some programming languages support compile time or static reflection. It is most common in high-level virtual machine programming languages like Smalltalk, and less common in lower-level programming languages like C.
At the lowest level, machine code can be treated reflectively because the distinction between instruction and data becomes just a matter of how the information is treated by the computer. Normally, 'instructions' are 'executed' and 'data' are 'processed', however, the program can also treat instructions as data and therefore make reflective modifications.
With higher level languages, when program source code is compiled, information about the structure of the program is normally lost as lower level code (typically assembly language code) is produced. If a system supports reflection, the structure is preserved as metadata with the emitted code.
In languages that do not make a distinction between runtime and compile-time (Lisp, Forth and MUMPS, for example), there is no difference between compilation or interpretation of code and reflection.
Contents |
[edit] Reflective paradigm
Reflective programming is a programming paradigm, used as an extension to the object-oriented programming paradigm, to add self-optimization to application programs, and to improve their flexibility. In this paradigm, computation is equated not with a program but with execution of a program. Other imperative approaches, such as procedural or object-oriented paradigm, specify that there is a pre-determined sequence of operations (function or method calls), that modify any data or object they are given. In contrast, the reflective paradigm states that the sequence of operations won't be decided at compile time, rather the flow of sequence will be decided dynamically, based on the data that needs to be operated upon, and what operation needs to be performed. The program will only code the sequence of how to identify the data and how to decide which operation to perform.
Any computation can be classified as either of two:
- Atomic - The operation completes in a single logical step, such as addition of two numbers.
- Compound - Defined as a sequence of multiple atomic operations.
A compound statement, in classic procedural or object-oriented programming, loses its structure once it is compiled. The reflective paradigm introduces the concept of meta-information, which keeps knowledge of this structure. Meta-information stores information such as the name of the contained methods, name of the class, name of parent classes, or even what the compound statement is supposed to do. This is achieved by keeping information of the change of states that the statement causes the data to go through. So, when a datum (object) is encountered, it can be reflected to find out the operations that it supports, and the one that causes the required state transition can be chosen at run-time, without the need to specify it in code.
[edit] Uses of reflection
Reflection can be used for self-optimization or self-modification of a program. A reflective sub-component of a program will monitor the execution of a program and will optimize or modify itself according to the function the program is solving. This is done by modifying the program's own memory area, where the code is stored.
Reflection can also be used to adapt a given system dynamically to different situations. Consider, for example, an application that uses some class X to communicate with some service. Now suppose it needed to communicate with a different service, via a different class Y, which has different method names. If the method names were hard coded into the application, it would need to be rewritten, but if it used reflection this could be avoided. Using reflection, the application would have a knowledge about the methods in class X. And class X could be designed to provide information regarding which method is being used for what purpose. The application, depending on what it has to do, would select the required method and use it. Now, when the different service is being used, via class Y, the application would search the methods in the new class to find the required methods and use them. No modification of the code is necessary. Even the class name need not be hard coded, rather it can be stored in a configuration file, it will be correctly searched for and loaded at run time.
[edit] Implementation
A language supporting reflection provides a number of features available at runtime that would otherwise be very obscure or impossible to accomplish in a lower-level language. Some of these features are the abilities to:
- Discover and modify source code constructions (such as code blocks, classes, methods, protocols, etc.) as first-class objects at runtime.
- Convert a string matching the symbolic name of a class or function into a reference to or invocation of that class or function.
- Evaluate a string as if it were a source code statement at runtime.
These features can be implemented in different ways. Interpreted programming languages, such as Ruby and PHP, are ideally suited to reflection, since their source code is never lost in the process of translation to machine language—the interpreter has the source readily available.
In MOO, reflection forms a natural part of everyday programming idiom. When verbs (methods) are called, various variables such as verb (the name of the verb being called) and this (the object on which the verb is called) are populated to give the context of the call. Security is typically managed by accessing the caller stack programmatically: Since callers() is a list of the methods by which the current verb was eventually called, performing tests on callers()[1] (the command invoked by the original user) allows the verb to protect itself against unauthorised use.
Compiled languages rely on their runtime system to provide information about the source code. A compiled Objective-C executable, for example, records the names of all methods in a block of the executable, providing a table to correspond these with the underlying methods (or selectors for these methods) compiled into the program. In a compiled language that supports runtime creation of functions, such as Common Lisp, the runtime environment must include a compiler or an interpreter.
Reflection can be implemented for languages not having built in reflection facilities by using a program transformation system to define automated source code changes..
[edit] Examples
[edit] Java
The following is an example in Java using the Java package java.lang.reflect
. Consider two pieces of code
// Without reflection Foo foo = new Foo(); foo.hello();
// With reflection Class cls = Class.forName("Foo"); Method method = cls.getMethod("hello", null); method.invoke(cls.newInstance(), null);
Both code fragments create an instance of a class Foo
and call its hello()
method. The difference is that, in the first fragment, the names of the class and method are hard-coded; it is not possible to use a class of another name. In the second fragment, the names of the class and method can easily be made to vary at runtime. The downside is that the second version is harder to read, and is not protected by compile-time syntax and semantic checking. For example, if no class Foo exists, an error will be generated at compile time for the first version. The equivalent error will only be generated at run time for the second version.
[edit] Perl
Here is an equivalent example in Perl:
# without reflection Foo->new->hello;
# with reflection my $class = "Foo"; my $method = "hello"; $class->new->$method;
[edit] PHP
Here is an equivalent example in PHP:
# without reflection $Foo = new Foo(); $Foo->hello();
# with reflection $class = "Foo"; $method = "hello"; $$class = new $class(); $$class->$method();
[edit] Ruby
Here is an equivalent example in Ruby:
# without reflection Foo.new.hello
# with reflection Class.const_get("Foo").new.method("hello").call
[edit] Python
Here is an equivalent example in Python:
# without reflection Foo().hello()
# with reflection getattr(globals()['Foo'](), 'hello')()
[edit] Objective-C
Here is an equivalent example in Objective-C (using the most popular Cocoa runtime):
// Without reflection [[[Foo alloc] init] hello];
// With reflection id aClass = objc_getClass("Foo"); SEL aSelector = NSSelectorFromString(@"hello"); // or @selector(hello) if the method // name is known at compile time objc_msgSend([[aClass alloc] init], aSelector, nil);
[edit] ActionScript
Here is an equivalent example in ActionScript:
// Without reflection var foo:Foo = new Foo(); foo.hello();
// With reflection var ClassReference:Class = flash.utils.getDefinitionByName("Foo"); var instance:Object = new ClassReference(); instance.hello();
Even with an import statement on “Foo”, the method call to “getDefinitionByName” will break without an internal reference to the class. The reason for this is because runtime compilation of source is NOT allowable at the current time. Maybe in the future it might, but not now. To get around this, you have to have at least one instantiation of the class type in your code for the above to work. So in your class definition you declare a variable of the custom type you want to use:
var customType : Foo;
So for the idea of dynamically creating a set of views in Flex 2 using these methods in conjunction with an xml file that may hold the names of the views you want to use, in order for that to work, you will have to instantiate a variable of each type of view that you want to utilize.
[edit] Smalltalk
Here is an equivalent example in Smalltalk:
"Without reflection" Foo new hello
"With reflection" (Compiler evaluate: 'Foo') new perform: #hello
The class name and the method name will often be stored in variables and in practice runtime checks need to be made to ensure that it is safe to perform the operations:
"With reflection" | x y className methodName | className := 'Foo'. methodName := 'hello'. x := (Compiler evaluate: className). (x isKindOf: Class) ifTrue: [ y := x new. (y respondsTo: methodName asSymbol) ifTrue: [ y perform: methodName asSymbol ] ]
Smalltalk also makes use of blocks of compiled code that are passed around as objects. This means that generalised frameworks can be given variations in behavior for them to execute. Blocks allow delayed and conditional execution. They can be parameterised. (Blocks are implemented by the class Context).
X := [ :op | 99 perform: op with: 1]. ".....then later we can execute either of:" X value: #+ "which gives 100, or" X value: #- "which gives 98."
[edit] C#
The following example, written in C#, demonstrates the use of advanced features of reflection. The program takes the name of an assembly as input from the command-line. An assembly can be thought of as a class library. The assembly is loaded, and reflection is used to find its methods. For each method, it uses reflection to find whether it was recently modified or not. If it was recently modified, and if it does not require any parameters, the name and return type of the method is displayed.
To find whether a method was recently modified or not, the developer needs to use a custom attribute, to define the method as recently modified, and the assembly which contains the class is attributed to be supporting the attribute. The presence of the attribute denotes the method to be recently modified, and its absence denotes it to be old. An attribute is metadata regarding the program structure, here a method. An attribute is itself implemented as a class. When using the assembly, the program loads the assembly dynamically at runtime, and checks the assembly for the attribute which says that the assembly supports the attribute which specifies methods as old or recently modified. If it is supported, names of all the methods are then retrieved. For each method, its attributes are retrieved. If the attribute which marks the method as new is present, the list of parameters which the method takes is retrieved. If the list is empty, i.e., the method does not take any arguments, it is printed along with the return type of the method and the comment that the developer might have added to make the attribute, which defines the method to be recent, more informative.
using System; using System.Collections.Generic; using System.Text; using System.Reflection; using Recent; namespace Reflect { class Program { private Assembly a; Program(String assemblyName) { a = Assembly.Load(new AssemblyName(assemblyName)); //Make sure the assembly supports the DateCreated attribute Attribute c=Attribute.GetCustomAttribute(a, typeof(SupportsRecentlyModifiedAttribute)); if (c == null) { //Retrieval of "SupportsRecentlyModified" attribute failed. //This means that the assembly probably doesn't support it. throw new Exception(assemblyName + " does not support required attributes"); } Console.WriteLine("Loaded: " + a.FullName); } public void FindNewMethodsWithNoArgs() { //Get all types defined in the assembly Type[] t = a.GetTypes(); foreach (Type type in t) { //If this type is not a class, skip it and try the next type if (!type.IsClass) continue; Console.WriteLine("Class: " + type.FullName); MethodInfo[] methods = type.GetMethods(); foreach (MethodInfo method in methods) { object[] b = method.GetCustomAttributes(typeof(RecentlyModifiedAttribute), true); //If no attribute is retrieved, the method is old. if (b.Length != 0) { //Otherwise, only one attribute will be received, as "RecentlyModified" does //not support usage with another attribute Console.Write("\tNew Method: " + method.Name); //A class representing "RecentlyModifiedAttribute" is instantiated //from the attribute and it is used to retrieve the comment that //the developer put in. ParameterInfo[] pinfo = method.GetParameters(); if (pinfo.Length > 0) break; Console.WriteLine("\t" + (b[0] as RecentlyModifiedAttribute).comment); Console.WriteLine("\t\tReturn type: " + method.ReturnType.Name); } } } } static void Main(string[] args) { try { Program reflector = new Program("UseAttributes");//Console.ReadLine()); reflector.FindNewMethodsWithNoArgs(); } catch (Exception e) { Console.Error.WriteLine(e.Message); } } } }
The implementation of the custom attributes is shown here.
using System; using System.Collections.Generic; using System.Text; namespace Recent { //Make sure the attribute is applied only to methods //and that it can not be used with other attributes. [AttributeUsage(AttributeTargets.Method, AllowMultiple=false, Inherited=true)] public class RecentlyModifiedAttribute : Attribute { //The Attribute is expected to be used on methods without any arguments //(as in [RecentlyModified]) //or with comments (as in [RecentlyModified(comment="<someComment>")]) private String Comment = "This method has recently been modified"; public RecentlyModifiedAttribute() { //This is an empty constructor handling instantiation of the attribute //It is empty because no arguments is necessary for use of the attribute } //Optional named argument "comment" has to be supported. So a property "comment" is defined //specifying how to handle the comment. It has to be used as a named argument when the //Attribute is being used. This will tell the compiler which property handles the argument. public String comment { get { return Comment; } set { Comment = comment; } } } [AttributeUsage(AttributeTargets.Assembly, AllowMultiple=false)] public class SupportsRecentlyModifiedAttribute : Attribute { //The attribute is to be used with classes without any arguments //as in [SupportsRecentlyModified] public SupportsRecentlyModifiedAttribute() { //The constructor is empty because no arguments are required to use the attribute } } }
And the usage of the custom attributes, defined above, in building a class is shown here.
using System; using System.Collections.Generic; using System.Text; using Recent; //Use "SupportsRecentlyModified" attribute to specify that the //assembly supports the "RecentlyModified" attribute [assembly: SupportsRecentlyModified] namespace Reflect { class UseAttributes { private Object info; public UseAttributes() { info = (object) "Hello World"; } public void OldMethodWithNoArgs() { Console.WriteLine("This is an old method which takes no arguments."); } //Use the "RecentlyModified" attribute to specify that the method was recently modified. [RecentlyModified] public void NewMethodWithNoArgs() { Console.WriteLine("This is a recently modified method that takes no arguments."); } public void OldMethodWithOneArg(object something) { info = something; Console.WriteLine("This is an old method that takes one argument."); } [RecentlyModified] public void NewMethodWithOneArg(object something) { info = something; Console.WriteLine("This is a new method that takes one argument."); } } }
[edit] See also
[edit] References
[edit] External links
- Reflection in logic, functional and object-oriented programming: a Short Comparative Study (Citeseer page).
- An Introduction to Reflection-Oriented Programming
[edit] Further reading
- Ira R. Forman and Nate Forman, Java Reflection in Action (2005), ISBN 1932394184
- Ira R. Forman and Scott Danforth, Putting Metaclasses to Work (1999), ISBN 0-201-43305-2