CSC/ECE 517 Fall 2009/wiki2pthelm

From Expertiza_Wiki
Revision as of 17:28, 7 October 2009 by Discostu (talk | contribs)
Jump to navigation Jump to search

Reflection-Oriented Programming

Reflection-Oriented Programming (ROP) is defined as a programming model design with the intent to analyze and improve it's structure, performance or other implementation aspects during runtime, as opposed to compile time. Two key concepts of ROP are meta-information, which is the state of the program at execution time, and metaprogramming, which is the idea that a language can act as it's own reflection. In other words, a language can reflect (modify) itself. More precisely, we define reflection-oriented programming to be a programming style that uses any means available (reflection or otherwise) to extend the meta-information in order to avoid situations in which a local program's requirements leads to non-local rewriting of said program [4].


Reflection vs. ROP

Reflection is the process by which a computer program can observe and modify it's own structure and behavior[1]. It allows an executing program to examine or "introspect" upon itself, and manipulate internal properties of the program[2]. Java, Ruby and .NET are among the most popular reflective languages, however there are many others[3]. Although reflection is the primary concept behind ROP, it does not necessarily preclude the existence of ROP. ROP is a concept of design; as with most designs, it is not necessarily dependent on the implementation. Computational programming can also be used, and does not assume the presence of source code at runtime, something that is essential to the concept of reflection[4].


Meta-Information

Meta-Information, or metadata, is the state of the program at execution time. Put another way, Meta-information is information about information. For example, a if a document is considered to be information, its title, location, and subject are examples of meta-information[5].


Metaprogramming

Metaprogramming introduces the concept of "programs that write programs". Reflection is one type of metaprogramming, and therefore can be considered as an option when attempting to design based on ROP. Metaprograms are written in Metalanguage[6].


Examples


Simple Reflection

As an example, first let us review what reflection looks like as an implementation. To see how reflection works, consider this simple example:

  import java.lang.reflect.*;

  public class DumpMethods {
     public static void main(String args[])
     {
        try {
           Class c = Class.forName(args[0]);
           Method m[] = c.getDeclaredMethods();
           for (int i = 0; i < m.length; i++)
           System.out.println(m[i].toString());
        }
        catch (Throwable e) {
           System.err.println(e);
        }
     }
  }


For an invocation of:

 java DumpMethods java.util.Stack

the output is:

 public java.lang.Object java.util.Stack.push(
   java.lang.Object)
  public synchronized 
    java.lang.Object java.util.Stack.pop()
  public synchronized
     java.lang.Object java.util.Stack.peek()
  public boolean java.util.Stack.empty()
  public synchronized 
    int java.util.Stack.search(java.lang.Object)


That is, the method names of class java.util.Stack are listed, along with their fully qualified parameter and return types.

This program loads the specified class using class.forName, and then calls getDeclaredMethods to retrieve the list of methods defined in the class. java.lang.reflect.Method is a class representing a single class method[2].


ROP

ROP is more than just simple reflection (although technically, it can be reflection alone), it is also analyzation or improvement at runtime without prior knowledge of source. A good example of this concept is Measuring Computational Expense. To implement such behavior, we need only these definitions:


 define reflective <runtime> (<simple-reflective>)
 slot ticks, init-value: 0;
 end;
 define method atomic (v, outer :: <runtime>)
 update(outer, ticks: add1(outer.ticks))
 end;
 define method compound-initial (outer :: <runtime>)
 update(outer, ticks: add1(outer.ticks))
 end;


It is convenient to have default behaviors for atomic, compound-initial, and compound-rest that simply "thread" meta-information through the computations. The inherited definition of compound-rest suffices. Now suppose we want to evaluate some expression E without counting its cost. We could use the reify and reflect operations like this:


 let r = reify(<runtime>);
 let result = E;
 reflect(<runtime>, r, result);


Here we capture the expense meta-information before evaluating E and save it as r. After evaluating E, we restore the old meta-information. The result from evaluating E is returned. Of course, any time we want to know the cumulative expense for the program, we can examine reify(<runtime>).ticks[4].


References

[1] Reflection (Computer Science), Wikipedia: http://en.wikipedia.org/wiki/Reflection_%28computer_science%29
[2] Using Java Reflection, Sun: http://java.sun.com/developer/technicalArticles/ALT/Reflection/
[3] List of reflective programming languages and platforms, Wikipedia: http://en.wikipedia.org/wiki/List_of_reflective_programming_languages_and_platforms
[4] An Introduction to Reflection-Oriented Programming, Sobel & Friedman: http://www2.parc.com/csl/groups/sda/projects/reflection96/docs/sobel/sobel.pdf
[5] What is Metainformation?: http://searchoracle.techtarget.com/sDefinition/0,,sid41_gci900705,00.html [6] Metaprogramming, Wikipedia: http://en.wikipedia.org/wiki/Metaprogramming