classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: finding method efficiently


From: Tom Tromey
Subject: Re: finding method efficiently
Date: 27 Sep 2004 23:46:57 -0600
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50

>>>>> "Robert" == Robert Schuster <address@hidden> writes:

Robert> Now when evaluating the XML data I have to search the method "put"
Robert> which accepts the two arguments. Having
Robert> only String and JFrame as argument types means that the call to
Robert>      HashMap.class.getMethod("put", new Class[] { String.class,
Robert>      JFrame.class });
Robert> will fail. My current naive approach to find HashMap.put(Object,
Robert> Object) is to generate a long list of a superclasses and
Robert> interfaces of all arguments and then calling getMethod("put", ..) with
Robert> every combination.

You could use Class.getMethods to get an array of all methods in a
class, then search for the method that allows this call, by using
isAssignableFrom on the arguments.  That's about all I can think of.
This is more efficient than using getMethod since you don't need to
generate all combinations of the method argument types.  getMethods
will look at the inheritance tree for you, so you only need to call it
once.

Tom




reply via email to

[Prev in Thread] Current Thread [Next in Thread]