commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ola Berg <ola.b...@arkitema.se>
Subject Re:[reflect] method lookup helper
Date Thu, 20 Jun 2002 08:08:16 GMT
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:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message