CSC/ECE 517 Fall 2010/ch3 1c AE: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 27: Line 27:
like introspection, intercession, structural & behavioral --  
like introspection, intercession, structural & behavioral --  
Reflection at Different Stages of the Program Lifecycle
Reflection at Different Stages of the Program Lifecycle
Categorizing Reflection
We identify two main criteria to categorize reflective systems. These criteria are
when reflection takes place and what may be reflected. If we take what may be reflected
as a criterion, we can distinguish:
· Introspection: The system structure can be accessed but not modified. If we take
Java as an example, with its java.lang.reflect package, we can get information
about classes, objets, methods and fields at runtime [4].
· Structural Reflection: The system structure can be dynamically modified. An example
of this kind of reflection is the addition of object’s fields.
· Computational (Behavioral) Reflection: The system semantics (behavior) can be
modified. In the standard Java API v.1.3, the class java.lang.reflect.Proxy
has been added [5]; it can be used to modify the dispatching method mechanism,
being handled by a proxy object.
· A reflective programming language can be capable of changing itself, i.e. changing
its own lexical or syntactical specification; that is what we call Linguistic Reflection.
As an example, with OpenJava reflective language, the own language can be
enhanced to be adapted to specific design patterns [6].


==== Reflective languages ====
==== Reflective languages ====

Revision as of 01:04, 5 October 2010

Reflective Language Features vs. Reflective Packages

Definition

Reflection is the integral quality of a program to dynamically manipulate its own state. (i.e.,) the program can infer and modify itself by making use of its representation of its own self-accessible state(implementation, structure and all aspects). The program takes itself as its domain to be acted upon. It contains metadata and associated operations to manipulate the metadata in runtime. Programming languages that possess this quality are said to be reflective.


Consider the following simple example: public class HelloWorld {

  public void printName() {
     System.out.println(this.getClass().getName());
  }

} The line (new HelloWorld()).printName(); sends the string HelloWorld to standard out. Now let x be an instance of HelloWorld or one of its subclasses. The line x.printName(); sends the string naming the class to standard out.

This small example is more dramatic than it seems—it contains each of the steps previously mentioned. The printName method examines the object for its class (this.getClass()). In doing so, the decision of what to print is made by delegating to the object's class. The method acts on this decision by printing the returned name. Without being overridden, the printName method behaves differently for each subclass than it does for HelloWorld. The printName method is flexible; it adapts to the class that inherits it, causing the change in behavior.


Reflection is a valuable language feature to facilitate metaprogramming. A reflective system shall allow programmers to create new metalevel objects and selectively substitute them for an existing part of the running system. Those programs become their own meta language. Thus, reflection is writing programs that manipulate other programs or themselves.

Pros and cons of reflection

Reflective mechanisms

Types of reflection like introspection, intercession, structural & behavioral -- Reflection at Different Stages of the Program Lifecycle

Categorizing Reflection We identify two main criteria to categorize reflective systems. These criteria are when reflection takes place and what may be reflected. If we take what may be reflected as a criterion, we can distinguish: · Introspection: The system structure can be accessed but not modified. If we take Java as an example, with its java.lang.reflect package, we can get information about classes, objets, methods and fields at runtime [4]. · Structural Reflection: The system structure can be dynamically modified. An example of this kind of reflection is the addition of object’s fields. · Computational (Behavioral) Reflection: The system semantics (behavior) can be modified. In the standard Java API v.1.3, the class java.lang.reflect.Proxy has been added [5]; it can be used to modify the dispatching method mechanism, being handled by a proxy object. · A reflective programming language can be capable of changing itself, i.e. changing its own lexical or syntactical specification; that is what we call Linguistic Reflection. As an example, with OpenJava reflective language, the own language can be enhanced to be adapted to specific design patterns [6].


Reflective languages

Languages like LISP, CLOS, etc

Reflection in OO like JAVA

OO-languages (java ‘s reflex package, C#’s, C++’s, etc) Java is so well crafted and its reflection API so carefully constrained that security is controlled simply. By learning when to use reflection and when not to, you will avoid unnecessarily complex code that can often be the result of amateurish use of reflection. In addition, you will learn to evaluate the performance of your designs, thereby ensuring the resulting code satisfies its performance requirements.

This introduction describes reflection, but scarcely reveals its value in this limited space. This article and the next will cover:

Reflection basics Class fundamentals Using methods reflectively


In Java, reflection enables to discover information about the loaded classes: Fields, Methods and constructors Generics information Metadata annotations

It also enables to use these metaobjects to their instances in run time environment. E.g. Method.invoke(Object o, Object… args) With the Java reflection API, you can interrogate an object to get all sorts of information about its class.

References

http://www.springerlink.com/content/6l63883r9224q186/ http://www.cs.indiana.edu/~jsobel/rop.html http://publications.csail.mit.edu/lcs/pubs/pdf/MIT-LCS-TR-272.pdf http://www.di.uniovi.es/ooosws2001/papers/ortin_final.pdf http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/package-summary.html http://scg.unibe.ch/archive/papers/Duca06dMOOSEMODELS2006.pdf file:///C:/Users/Summer/Downloads/10.1.1.158.2012.pdf - good link .. http://www.ub.uni-konstanz.de/kops/volltexte/2002/803/pdf/DissertationEgbertAlthammer.pdf http://www.laputan.org/reflection/ooffrmla.html