Java Report Archives


Limitations of reflective method lookup

Reflection is a powerful and intriguing feature of Java. Using the facilities of the class <FONT FACE="Courier">java.lang.Class</FONT> and the Reflection API (<FONT FACE="Courier">package java.lang.reflect</FONT>), programmers can gather information about classes in the JVM at runtime, including references to methods and constructors. After obtaining these references in the form of <FONT FACE="Courier">Method</FONT> and <FONT FACE="Courier">Constructor</FONT> objects, users can invoke them with parameters (via the <FONT FACE="Courier">invoke()</FONT> method of <FONT FACE="Courier">Method</FONT> and the <FONT FACE="Courier">newInstance()</FONT> method of <FONT FACE="Courier">Constructor</FONT>) and manipulate the results.<P> The addition of reflection to the Java API has enabled such potent technologies as JavaBeans, serialization, RMI, and dynamic proxies. However, reflection has limitations that might surprise users. The purpose of this article is to share these limitations with readers, and to offer a solution for dealing with them.