CSC/ECE 517 Fall 2010/ch1 1c JF: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
== Introduction == | == Introduction == | ||
[http://en.wikipedia.org/wiki/Reflection_(computer_science) Reflection] allows a program to access information about its objects ([http://en.wikipedia.org/wiki/Type_introspection introspection]) and to modify program behavior based on that information (intercession [http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.153.6604 3]). Some languages, like Ruby and Objective-C, are inherently reflective, while other | [http://en.wikipedia.org/wiki/Reflection_(computer_science) Reflection] allows a program to access information about its objects ([http://en.wikipedia.org/wiki/Type_introspection introspection]) and to modify program behavior based on that information (intercession [http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.153.6604 3]). Some languages, like Ruby and Objective-C, are inherently reflective, while other languages, like C# and Java, implement reflective language by including a reflective package. | ||
In a reflective language, | In a reflective language, information about an object or class is accessed directly. In languages with reflective packages, teh information is accessed by creating another object that can access that information. | ||
There are many features that reflection gives in a language. Implementation for these features differs depending on whether it is an inherently reflective language, or a language with a reflective package. | |||
= Class Type and Method List = | = Class Type and Method List = | ||
One of the most powerful | 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 directly. For example: | ||
class Sample | class Sample | ||
Line 41: | Line 43: | ||
Reflective languages allow method calls to be made dynamically. Again, in an inherently reflective language, these calls are made directly on the object. However, with a reflective package, the calls are directed through an additional object layer. | Reflective languages allow method calls to be made dynamically. Again, in an inherently reflective language, these calls are made directly on the object. However, with a reflective package, the calls are directed through an additional object layer. | ||
For example, in Ruby, this code: | For example, in Ruby, this code: | ||
class Sample | class Sample | ||
def printOne | def printOne |
Revision as of 23:46, 8 September 2010
Introduction
Reflection allows a program to access information about its objects (introspection) and to modify program behavior based on that information (intercession 3). Some languages, like Ruby and Objective-C, are inherently reflective, while other languages, like C# and Java, implement reflective language by including a reflective package.
In a reflective language, information about an object or class is accessed directly. In languages with reflective packages, teh information is accessed by creating another object that can access that information.
There are many features that reflection gives in a language. Implementation for these features differs depending on whether it is an inherently reflective language, or a language with a reflective package.
Class Type and Method List
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 directly. For example:
class Sample #empty class end s = Sample.new puts s.class puts s.methods
will return "Sample" as the class and a list of the methods that Sample has (which will include all the methods in Sample's superclass – Object). However, in a language with a reflective package, like Java, a separate object must be created and that object contains the information. For example:
import java.lang.reflect.*; public class Sample { //Empty class } //Main in another class or same class public static void main(String args[]){ Sample s = new Sample(); Class sclass = s.getClass(); System.out.println(sclass.getName()); Method[] m = sclass.getMethods(); for (int i = 0; i < m.length; i++){ System.out.println(m[i]); } } }
will also return the class and methods for the "Sample" object, but it has to go through the "Class" object to get to that information.
Call Methods Dynamically
Reflective languages allow method calls to be made dynamically. Again, in an inherently reflective language, these calls are made directly on the object. However, with a reflective package, the calls are directed through an additional object layer. For example, in Ruby, this code:
class Sample def printOne puts 1 end def printTwo puts 2 end def printThree puts 3 end end s = Sample.new option="printOne" s.send(option) option="printThree" s.send(option)
will print "1" and then "3". The call:
s.send(option)
is the same, but the "option" value has changed and that is what determines the method that is called. This same behavior can happen in languages with dynamic packages. For example, in Java, this code:
import java.lang.reflect.*; public class Sample { public void printOne(){ System.out.println("1"); } public void printTwo(){ System.out.println("2"); } public void printThree(){ System.out.println("3"); } public static void main(String args[]){ Sample s = new Sample(); Class sampClass = s.getClass(); String option = "printOne"; try { Method m = sampClass.getMethod(option); m.invoke(s, null); } catch (NoSuchMethodException nsme){ nsme.printStackTrace(); } catch (IllegalAccessException iae){ iae.printStackTrace(); } catch (InvocationTargetException ite){ ite.printStackTrace(); } option = "printThree"; try { Method m = sampClass.getMethod(option); m.invoke(s, null); } catch (NoSuchMethodException nsme){ nsme.printStackTrace(); } catch (IllegalAccessException iae){ iae.printStackTrace(); } catch (InvocationTargetException ite){ ite.printStackTrace(); } } }
will result in the same behavior ("1" and then "3" are printed, with the only thing changing the "option" string), 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.