Reflection (computer programming)

In programming, reflection means (English reflection ) or introspection that a program knows its own structure and this can, if necessary, modify it.

At the lowest level may be machine code in the RAM, which is executed by a microprocessor will be referred to as reflective. Such a program is able to treat its instructions as data and can therefore analyze and change its structure. Reflection is often supported by high-level languages ​​that are running in a virtual machine, such as Java or Smalltalk.

An important role is played by reflection in the context of type-safe programming, but also in matters of persistence ( persistent data storage of objects and their relationships).

Reflection allows for object-oriented programming, for example, at run -time information about classes or their instances to query. In one method, that is, among other things, their visibility, the data type of the return value or the type of transfer parameters. The implementation of the query options is language-specific.

For the realization of the reflection storing meta information in the binary code of the program is necessary. In interpretive programming is at execution time before the original program code, which also provides access to the implementation in addition to access to the structural information ( method declaration ). Examples of this are PHP, Lisp and Python.

But Java and all languages ​​for use with the. NET Framework, such as C #, VB.NET or IronPython support reflection, which provides the. NET Framework by itself. All languages ​​using the. NET Framework, according to CLS (Common language specification ) must save the information as metadata.

The execution speed of code using reflection is slower than the static code. This is partly due to the string comparing the corresponding name of the methods, properties, etc., with the entries in the metadata. However, reflection offers a very high run-time flexibility, since code can be called dynamically create new instances or even types and objects can be dynamically restructured.

Examples

C #

The following example illustrates a method, which corresponds to the functionality of Java example.

Public String GetStringProperty (Object obj, String methodName )    {      String value = null;      try {           MethodInfo method = info obj.GetType () GetMethod ( methodname ). ;           value = (String) MethodInfo.Invoke (obj, new Object );      } Catch (Exception e ) {}      / / Error handling for clarity not implemented.        returnValue;    } Common Lisp

( FUNCALL ( find- symbol " SIN " ) 3) Java

The following example shows a method that invokes any other method of a given object and returns its return value. For simplicity, this example only supports the invocation of methods with no parameters, the strings ( " string" ) return.

Public String GetStringProperty (Object object, String methodName ) {      String value = null;      try {          Method getter = object.getClass () getMethod ( methodName, new Class). ;          value = (String) getter.invoke (object, new Object );      } Catch (Exception e ) {}      / / Error handling for clarity not implemented.        returnValue; } The following statement would then call the method getVorname ( ) of the object person and spend their return value.

System.out.println (" first name of " person "is"                     GetStringProperty (person, " getVorname ")); PHP

The specified statement would then properties of the class " ExampleClass " return as output.

The following statement returns, for example, the static variables of the class, if any exist:

getStaticVariables ();? > The specified statement would then properties of the class " ExampleClass " return as output.

Python

# >>> Class >>> Class Person (object): Def __ ... init__ (self, name): Self.name = name ... Say_hello ... def ( self): ... Return ' Hello % s! ' % self.name ... # >>> The object Ute >>> = Person ( ' Ute ') # >>> Directly Ute.say_hello >>> print () Hi Ute! # >>> Reflection >>> M = getattr ( ute, ' say_hello ') # >>> (Equivalent ute.say_hello ()) M >>> print () Hi Ute! Ruby

"a string". # class returns " String". ? "a string" respond_to (: size ) # evaluates to true -> Object can perform size method "a string". # methods results in a array of all methods of the object "a string " method. ( concat ) arity # indicates the number of parameters.                                    # Which calls the concat method class Book    def initialize (* parameters)      @ title, @ author, @ chapters = parameters    end end a_book = Book.new ( " Book Title ", " Someone", [ "chapter I", "chapter II", "chapter III " ] ) a_book.instance_variables # gives an array of all object instance variables:                            # [ "@ Title", " author @ ", " @ chapters " ] Book.instance_methods # returns all instance methods of the class Book. Web Links

  • Reflection in Java
  • CLR Inside Out: Reflection in NET (C #) on MSDN.
  • Seminar paper on the subject of reflexive programming languages ​​( concepts and implementation in Java)
  • Reflection in PHP
  • Programming
415436
de