You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Ola Berg <ol...@arkitema.se> on 2002/06/20 10:08:16 UTC

Re:[reflect] method lookup helper

Craig wrote:

>In addition, many of the signatures above seem like *very* low value-add
>over the standard Java APIs -- the most complicated things (looking up the
>right Method object by scanning all the superclasses and implemented
>interfaces/superinterfaces is the important part. �

That\'s my view on reflect too: make Method lookup easy (and caching) in order to faciliate introspect.

I have done some coding testing my ideas, and they basically goes like this:

MethodPredicate is an abstract class implementing Predicate. Besides evaluate(), MethodPredicate contains a couple of factory methods, so that you build predicates like this:

//public void setXxx( Type foo);
public static final MethodPredicate SETTERS_PREDICATE =
    MethodPredicate.IS_MEMBER.
    and( MethodPredicate.IS_PUBLIC).
    and( MethodPredicate.IS_VOID).
    and( MethodPredicate.nameStartsWith( \"set\") ).
    and( MethodPredicate.withNumParameters( 1))
;

//public static void main( String [] args)
public static final MethodPredicate MAIN_PREDICATE =
    MethodPredicate.IS_PUBLIC.
    and( MethodPredicate.IS_STATIC).
    and( MethodPredicate.IS_VOID).
    and( MethodPredicate.nameEquals( \"main\")).
    and(
        MethodPredicate.withExactParameterTypes(
            new Class[]{ String[].class}
        )
    )
;


MethodCategory is a class defining a category of methods. You instantiate it with one MethodPredicate (defining the set of methods), and one transformation that transforms a Method to an identifier.

For beans-like introspection the transformation transforms \"xxxPropertyName\" into \"propertyName\". Once instantiated, the MethodCategory can find all the matching methods in any object, and if any particular class isn\'t looked up, a reflection lookup takes place and the methods are stored.

Small subclasses of MethodPredicate are Getters, Setters, IndexedSetters, Putters and Adders, just to show how easy support for a new category of methods can be added.

Now looking at a cute hierarchy to deal not only with Methods but Constructors, Fields, Members, and maybe classes and packages too.

/O


--------------------
ola.berg@arkitema.se
0733 - 99 99 17

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>