CSC/ECE 517 Fall 2010/ch1 1c JF: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 98: Line 98:
  meth.invoke(s, null);
  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.
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.
Some of the more dynamic intercessive features that reflection gives a programming language are possible because of the dynamic nature of the language.  For example, in Ruby, a method can be added to an object that ''has already been instanced''.  Given a class "Sample", this code:
samp = Sample.new
def samp.anothermethod
  puts "This is a new method"
end
will create a method on the instance of Sample.  Reflection is what allows that method to be accessed so that the call:
samp.anothermethod
will display "This is a new method".  In Java, methods cannot be created on existing objects (because it is a statically typed language), so this kind of reflection is not possible.
=Conclusion=
Reflection offers many features that can assist in the creation of flexible and versatile programs.  These features are implemented differently depending on whether the language is reflective or whether reflective features are implemented with a reflective package.


= References =
= References =

Revision as of 20:54, 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 explained 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 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 from the object or class in question.

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.

Some of the more dynamic intercessive features that reflection gives a programming language are possible because of the dynamic nature of the language. For example, in Ruby, a method can be added to an object that has already been instanced. Given a class "Sample", this code:

samp = Sample.new
def samp.anothermethod
  puts "This is a new method"
end

will create a method on the instance of Sample. Reflection is what allows that method to be accessed so that the call:

samp.anothermethod

will display "This is a new method". In Java, methods cannot be created on existing objects (because it is a statically typed language), so this kind of reflection is not possible.

Conclusion

Reflection offers many features that can assist in the creation of flexible and versatile programs. These features are implemented differently depending on whether the language is reflective or whether reflective features are implemented with a reflective package.

References

[1] Sobel, J.M. and Daniel P. Friedman 1996 Reflection-Oriented Programming Indiana University, Bloomington, IN

[2] Smith, Brian Caldwell 1982 Procedural Reflection in Programming Languages MIT Press, Cambridge, MA

[3] Ducasse, Stéphane and Rmod Inria Lille Nord and Marcus Denker and Adrian Lienhard 2009 Evolving a Reflective Language Lessons Learned from Implementing Traits European Smalltalk User Group

[4] McHale, Ciaran 2008 Java Reflection Explained Simply

[5] Using Java Reflection

[6] Thomas, Dave with Chad Fowler and Andy Hunt. Programming Ruby. North Carolina: The Pragmatic Bookshelf, 2005.

[7] D. Bobrow, R. Gabriel, and J. White. CLOS in context—the shape of the design. In A. Paepcke, editor, Object-Oriented Programming: the CLOS perspective, pages 29–61. MIT Press, 1993.

[8] Anh Nguyen-tuong and Steve J. Chapin and Andrew S. Grimshaw and Charlie Viles. 1998 Using Reflection for Flexibility and Extensibility in a Metacomputing Environment Worley 12. ORNL Central Research Library 13. ORNL Laboratory Records INTERNAL DISTRIBUTION EXTERNAL DISTRIBUTION 13. Thomas A. Mazzola, Northrop Grumman Information Technologies, 6940 S. Kings Hwy, STE 210, Alexandria, VA 22310 14. Ronald Meris, Defense T