CSC/ECE 517 Fall 2010/ch1 1c JF: Difference between revisions
No edit summary |
No edit summary |
||
Line 3: | Line 3: | ||
=Introduction: Reflection, Reflective languages and Reflective packages= | =Introduction: Reflection, Reflective languages and Reflective packages= | ||
Reflection is an aspect of a computer languages that allows "computations to observe and modify properties of their own behavior."[1] It was first theorized in | Reflection is an aspect of a computer languages that allows "computations to observe and modify properties of their own behavior."[1] It was first theorized in Brian Cantwell Smith's 1982 paper 'Procedural Reflection in Programming Langauges'[2]. | ||
A reflective language is a programming language that has been architected to allow reflection. This means the language was constructed to "'reason' effectively and consequentially about its own inference process."[2] | |||
Reflection provides languages with versatility [8] and flexibility [3]. Languages that are not reflective can incorporate some of the features of reflection by using reflective packages. For example, Java includes a reflective package called "java.lang.reflect." C++ also implements reflective features through reflective packages, one of which is the Run-Time Type Information (from Microsoft). | Reflection provides languages with versatility [8] and flexibility [3]. Languages that are not reflective can incorporate some of the features of reflection by using reflective packages. For example, Java includes a reflective package called "java.lang.reflect." C++ also implements reflective features through reflective packages, one of which is the Run-Time Type Information (from Microsoft). |
Revision as of 19:48, 17 September 2010
Reflective Language Features vs Reflective Packages
Introduction: Reflection, Reflective languages and Reflective packages
Reflection is an aspect of a computer languages that allows "computations to observe and modify properties of their own behavior."[1] It was first theorized in Brian Cantwell Smith's 1982 paper 'Procedural Reflection in Programming Langauges'[2].
A reflective language is a programming language that has been architected to allow reflection. This means the language was constructed to "'reason' effectively and consequentially about its own inference process."[2]
Reflection provides languages with versatility [8] and flexibility [3]. Languages that are not reflective can incorporate some of the features of reflection by using reflective packages. For example, Java includes a reflective package called "java.lang.reflect." C++ also implements reflective features through reflective packages, one of which is the Run-Time Type Information (from Microsoft).
Reflective Language Features vs Reflective Packages
The features of a reflective language can be broadly categorized as either introspection or intercession.
Introspection is the ability for a program to observe and therefore reason about its own state. Intercession is the ability for a program to modify its own execution state or alter its own interpretation or meaning.[7]
Introspection is what allows the program to "know" information about itself. This can include an object knowing what kind of object it is, and what methods it has. Intercession is what allows a program to change behavior based on that information. This can include picking which method is called at runtime, and calling methods that are added to objects at runtime.
While the behavior between a program written in a reflective language and a program written with a reflective package will be similar, how the features are implemented will differ. In a reflective language, you can access the information about an object or class from the object or class. In a language with a reflective package, functionality is provided through intermediary objects.
Introspective features
One of the most powerful features that reflection provides is the ability, given an object, to know what kind of object it is, and what methods it contains. In a reflective language, like Ruby, that is done by directly querying the object. For example, with the given class:
class Sample #empty class end samp = Sample.new
The call:
puts samp.class
will list "Sample" as the class and the call:
puts samp.methods
will list the methods that Sample has (which will include all the methods in Sample's superclass – Object). In a language with a reflective package, like Java, the package is not automatically included in the functionality. It must be imported with:
import java.lang.reflect.*;
Also, for any given object A, a separate class object B, is created and that B contains the information about A. For example, with the given class:
public class Sample { //Empty class }
The class information can be found by invoking: Sample samp = new Sample(); //Create an instance of Sample
Class sampClass = samp.getClass(); //Create a class object for instance of Sample System.out.println(sampClass.getName()); //Display class name
and the method list can be found with:
Method[] meth = sampClass.getMethods(); //Return array of method objects for Class object of Sample instance for (int i = 0; i < m.length; i++){ System.out.println(meth[i]); //Display methods }
As shown in the above examples, both Ruby and Java include similar introspective features of reflection. However, in Java, a Class object is created to access class information, and method information for the object is stored in method objects that can be accessed via the Class object. In Ruby, that information is accessed directly.
Intercessive features
Another important feature granted by reflection, is the ability for dynamic implementation of code. This can take many forms, including object creation and method calling at runtime. Again, in an inherently reflective language, these calls are made directly on the object. However, when a language has a reflective package to handle reflective features, the calls are directed through a separate object. For example, in Ruby, this class:
class Sample def printOne puts 1 end def printTwo puts 2 end def printThree puts 3 end end
defines three methods that print different numbers depending on which method is called. Without reflection, calls to these methods could not be changed at runtime. However, reflection allows the method calls to depend on string values that do not have to be initialized until runtime as seen in the following code:
samp = Sample.new #Create an instance of the Sample class option="printOne" samp.send(option) #printOne method is called option="printThree" samp.send(option) #printThree method is called
What is printed from this code depends on the value of "option", which might not have a value until runtime. In this code, the call:
samp.send(option)
is the same, but the "option" value has changed and that is what determines the method that is called. This same feature exists in languages with reflective packages, but again it is accessed indirectly. For example, in Java, a similar class:
public class Sample { public void printOne(){ System.out.println("1"); } public void printTwo(){ System.out.println("2"); } public void printThree(){ System.out.println("3"); } }
handles reflection by accessing the method through a Class object and Method object:
Sample samp = new Sample(); Class sampClass = samp.getClass(); String option = "printOne"; Method meth = sampClass.getMethod(option); meth.invoke(s, null); option = "printThree"; meth = sampClass.getMethod(option); meth.invoke(s, null);
The result of this code is the same as the Ruby code, but to achieve it a Class object was created to retrieve information about the original object, and a Method object was created to retrieve method information from the Class object.
References
[1] Reflection-Oriented Programming
[2] Procedural Reflection in Programming Languages
[3] Evolving a Reflective Language Lessons Learned from Implementing Traits
[4] Java Reflection Explained Simply
[6] Thomas, Dave with Chad Fowler and Andy Hunt. Programming Ruby. North Carolina: The Pragmatic Bookshelf, 2005.