CSC/ECE 517 Summer 2008/wiki1 3 jb: Difference between revisions
No edit summary |
No edit summary |
||
Line 8: | Line 8: | ||
<pre> | <pre> | ||
import java.lang.reflect.*; | import java.lang.reflect.*; | ||
class DiscBrakes { } | |||
class MountainBike | class MountainBike | ||
{ | { | ||
boolean | private DiscBrakes m_discBrakes = null; | ||
public boolean hasDiscBrakes() { return m_discBrakes != null; } | |||
public void setDiscBrakes(DiscBrakes discBrakes) { m_discBrakes = discBrakes; } | |||
public static void main (String argv[]) | public static void main (String argv[]) | ||
{ | { | ||
Class c = MountainBike.class; | Class c = MountainBike.class; | ||
Method | |||
MountainBike mb = new MountainBike(); | |||
DiscBrakes db = new DiscBrakes(); | |||
Method meth = null; | |||
System.out.println(mb.hasDiscBrakes()); | |||
try | |||
{ | |||
meth = c.getMethod("setDiscBrakes", DiscBrakes.class); | |||
} | |||
catch (NoSuchMethodException e) { } | |||
try | |||
{ | { | ||
meth.invoke(mb, db); | |||
} | } | ||
catch (IllegalAccessException e) { } | |||
catch (InvocationTargetException e) { } | |||
System.out.println(mb.hasDiscBrakes()); | |||
} | } | ||
} | } | ||
Line 28: | Line 48: | ||
==Reflection in Ruby== | ==Reflection in Ruby== | ||
<pre> | <pre> | ||
class DiscBrakes | |||
end | |||
class MountainBike | class MountainBike | ||
def | @m_discBrakes = nil | ||
return | def hasDiscBrakes | ||
return @m_discBrakes != nil | |||
end | |||
def setDiscBrakes(discBrakes) | |||
@m_discBrakes = discBrakes | |||
end | end | ||
end | end | ||
mb = MountainBike.new | mb = MountainBike.new | ||
puts | |||
puts mb.hasDiscBrakes | |||
db = DiscBrakes.new | |||
setter = mb.method("setDiscBrakes") | |||
setter.call(db) | |||
puts mb.hasDiscBrakes | |||
</pre> | </pre> | ||
==Conclusions== | ==Conclusions== | ||
==Java Reflection Links== | ==Java Reflection Links== |
Revision as of 23: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 DiscBrakes { } class MountainBike { private DiscBrakes m_discBrakes = null; public boolean hasDiscBrakes() { return m_discBrakes != null; } public void setDiscBrakes(DiscBrakes discBrakes) { m_discBrakes = discBrakes; } public static void main (String argv[]) { Class c = MountainBike.class; MountainBike mb = new MountainBike(); DiscBrakes db = new DiscBrakes(); Method meth = null; System.out.println(mb.hasDiscBrakes()); try { meth = c.getMethod("setDiscBrakes", DiscBrakes.class); } catch (NoSuchMethodException e) { } try { meth.invoke(mb, db); } catch (IllegalAccessException e) { } catch (InvocationTargetException e) { } System.out.println(mb.hasDiscBrakes()); } }
Reflection in Ruby
class DiscBrakes end class MountainBike @m_discBrakes = nil def hasDiscBrakes return @m_discBrakes != nil end def setDiscBrakes(discBrakes) @m_discBrakes = discBrakes end end mb = MountainBike.new puts mb.hasDiscBrakes db = DiscBrakes.new setter = mb.method("setDiscBrakes") setter.call(db) puts mb.hasDiscBrakes
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