CSC/ECE 517 Summer 2008/wiki1 3 jb: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
This wiki will explore how <b>reflection</b> is implemented with both Ruby and Java, with the goal of showing which language's implementation makes it easier to write, easier to understand, and more efficient.
This wiki will explore how <b>reflection</b> is implemented with both Java and Ruby, with the goal of showing which language's implementation makes it easier to write, easier to understand, and more efficient.


==Basics==
==Basics==

Revision as of 22:20, 1 June 2008

This wiki will explore how reflection is implemented with both Java and Ruby, with the goal of showing which language's implementation makes it easier to write, easier to understand, and more efficient.

Basics

Reflection refers to the ability of a program to peer inside itself and observe the components that it is comprised of. Reflection, as supported in Object Oriented languages such Java and Ruby, provides facilities to query about the methods and attributes of a specified class, and to execute methods that are discovered at runtime. These two features of reflection in Java and Ruby will explored in detail below.

Reflection in Java

import java.lang.reflect.*;

class MountainBike
{
	boolean diskBrakes() { return true; }
	boolean knobbyTires() { return true; }

	public static void main (String argv[])
	{
		Class c = MountainBike.class;
		Method methods[] = c.getDeclaredMethods();
		for (int i=0; i<methods.length; i++)
		{
			System.out.println(methods[i].getName());
		}
	}
}

Reflection in Ruby

class MountainBike
  def diskBrakes 
      return true
  end
  def knobbyTires
      return true
    end
end

mb =  MountainBike.new
puts MountainBike.public_instance_methods(false)

Conclusions

Java Reflection Links

Pros and cons of reflection, written for Java but pretty generic
Using Java reflection
Javadoc for the java.lang.Class class
Javadoc for the java.lang.reflect package


Ruby Reflection Links

Good overview of reflection in Ruby