mercredi 28 septembre 2016

Java reflection pass a method as a parameter of another method?

I can get a list of methods that are annotated no problem...

Method[] m = clazz.getDeclaredMethods();

Now I would like to pass the method[x] to a function. For instance...

router.get("/").handler(RoutingContext handler)

I would like to pass it to the handler as a method reference.

In java 8 we can just do router.get("/").handler(this::myMethod)

Updated example:

public void myFunction() throws Exception {

    Router routes = Router.router(...);

    Handler<RoutingContext> handler = this::myHandler;
    routes.route("/").handler(handler);
    routes.route("/someOtherRoute").handler(this::anotherHandler);

}

public void myHandler(final RoutingContext rcs) {
    rcs.doSomething();
}

I would like to annotate the function myHandler so I can find it reflectively and add it to the "Router". So with reflection I can get a list of methods that have been annotated no problem and then for each one add it to my router...

So say I have some "web" anotations...

@GET
public void myHandler(final RoutingContext rcs) {
    rcs.doSomething();
}

@POST
public void anotherHandler(final RoutingContext rcs) {
    rcs.doSomething();
}

I can list these methods using reflection. Cool no problem. But then I would like to pass them as method references to router.handler() as shown in the above example...

If you haven't guessed it it for a web framework and no I wont release it to the wild not like we need another one. It's for learning purposes lol.





Aucun commentaire:

Enregistrer un commentaire