CSC/ECE 517 Fall 2010/ch1 1c JF

From Expertiza_Wiki
Revision as of 16:50, 8 September 2010 by Jmfoste2 (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Introduction

Reflection allows programs to access information about its objects and to modify program behavior based on that information. A reflective language allows direct access to that information. Another way of handling reflection in object-oriented languages to by creating a package that stores information about each class. In this structure, an object does not directly know what type it is or what methods it contains, but must indirectly access this information through another object.

Class Type and Method List

One of the most powerful functions 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

[5] Using Java Reflection