This post originated from an RSS feed registered with Java Buzz
by Chris Winters.
Original Post: No inheritance with reflection
Feed Title: cwinters.com
Feed URL: http://www.cwinters.com/search/registrar.php?domain=jroller.com®istrar=sedopark
Feed Description: Chris Winters on Java, programming and technology, usually in that order.
This is probably something painfully obvious to people more familiar with reflection, but you cannot use find a method through reflection by passing a superclass for an argument. For example, if you method has Collection as the first argument and you pass in an ArrayList class, the method will not be found. Here's an example:
import java.lang.reflect.*;
import java.util.*;
public class ReflectMethodTest {
public void aGeneric( Collection fooItems ) {}
public void lessGeneric( List fooItems ) {}
public void specific( ArrayList fooItems ) {}
public static void main( String[] args ) throws Exception {
ArrayList foo = new ArrayList();
tryMethod( "aGeneric", new Class[] { foo.getClass() } );
tryMethod( "lessGeneric", new Class[] { foo.getClass() } );
tryMethod( "specific", new Class[] { foo.getClass() } );
}
private static void tryMethod( String methodName, Class[] args ) {
try {
ReflectMethodTest.class.getMethod( methodName, args );
p( methodName + "() with arg of ArrayList: FOUND" );
}
catch ( NoSuchMethodException e ) {
p( methodName + "() with arg of ArrayList: NOT FOUND" );
}
}
private static void p( String msg ) {
System.out.println( msg );
}
}
This will print out:
shazam:~/work/tmp cwinters$ java ReflectMethodTest
aGeneric() with arg of ArrayList: NOT FOUND
lessGeneric() with arg of ArrayList: NOT FOUND
specific() with arg of ArrayList: FOUND
This is kind of surprising as I thought reflection would find the best match rather than just the specific match. Live and learn.