Using generics and annotations to resolve implementation interface

Discussion in 'Java' started by Meidos, Jun 8, 2007.

  1. Meidos

    Meidos Guest

    I have an interface as follows:
    interface Handler<A extends Annotation> {
    handle(A annotation);
    }

    I have a single implementation for each type of supported annotation.
    The annotations are applied to methods in interfaces, and a dynamic
    proxy is used to implement the actual method. In order to determine
    which Handler to use for a method, I look at it's annotation, and pick
    the handler from a map:

    Map<Class<? extends Annotation>, Handler<?>> handlerMap;
    handlerMap.put(SomeAnnotation.class, new SomeAnnotationHandler());

    The problem is that when I get the Handler from the map, it will be a
    Handler of type Handler<? extends Annotation>, and I cannot call
    handle() on it because the type is unspecified. How can I solve this?
    Meidos, Jun 8, 2007
    #1
    1. Advertising

  2. Hey there,

    public class tester {

    Map<Class<? extends Annotation>, Handler<?>> handlerMap = new
    HashMap<Class<? extends Annotation>, Handler<?>>();
    SomeAnnotation some = new SomeAnnotation();

    public void deneme(){
    handlerMap.put(SomeAnnotation.class, new SomeAnnotationHandler());
    handlerMap.get(SomeAnnotation.class).handle(some);
    }
    }

    I tried to solve ur problem. I hope I am right. In the interface
    header you need to specify the generic type of methods.

    for example


    public interface Handler<A extends Annotation> {
    <A extends Annotation> void handle(A anotation);
    }

    Thanks
    The Billboard Hot 100, Jun 8, 2007
    #2
    1. Advertising

  3. Meidos

    Tom Hawtin Guest

    Meidos wrote:
    > interface Handler<A extends Annotation> {
    > handle(A annotation);
    > }


    > Map<Class<? extends Annotation>, Handler<?>> handlerMap;
    > handlerMap.put(SomeAnnotation.class, new SomeAnnotationHandler());
    >
    > The problem is that when I get the Handler from the map, it will be a
    > Handler of type Handler<? extends Annotation>, and I cannot call
    > handle() on it because the type is unspecified. How can I solve this?


    You are going to need an unchecked cast.

    class AnnotationHandlerMap {
    private final Map<
    Class<? extends Annotation>, Handler<? extends Annotation>
    > map = new java.util.HashMap<

    Class<? extends Annotation>, Handler<? extends Annotation>
    >();


    public <A extends Annotation> void put(
    Class<A> type, Handler<A> handler
    ) {
    map.put(type, handler);
    }

    @SuppressWarnings("unchecked")
    public <A extends Annotation> Handler<A> get(Class<A> type) {
    return (Handler<A>)map.get(type);
    }
    ...
    }

    (Dislaimer: Not tested or even compiled.)
    Tom Hawtin, Jun 8, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jrh001
    Replies:
    0
    Views:
    379
    jrh001
    Nov 25, 2004
  2. Juergen Berchtel
    Replies:
    1
    Views:
    5,987
    John C. Bollinger
    May 20, 2005
  3. Replies:
    1
    Views:
    10,139
  4. Stanimir Stamenkov
    Replies:
    4
    Views:
    1,268
    Stanimir Stamenkov
    Aug 12, 2008
  5. Soul
    Replies:
    0
    Views:
    514
Loading...

Share This Page