The Artima Developer Community
Sponsored Link

Java Buzz Forum
No inheritance with reflection

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Chris Winters

Posts: 931
Nickname: cwinters
Registered: Jul, 2003

Daytime: Java hacker; nighttime: Perl hacker; sleeptime: some of both.
No inheritance with reflection Posted: Aug 31, 2004 11:52 PM
Reply to this message Reply

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.
Latest Java Buzz Posts
Latest Java Buzz Posts by Chris Winters
Latest Posts From cwinters.com

Advertisement

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.

Read: No inheritance with reflection

Topic: SpringFramework.com: Spring goes professional Previous Topic   Next Topic Topic: Groovestry Update

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use