views:

43

answers:

1

I'm having trouble with doubling up on my code for no reason other than my own lack of ability to do it more efficiently...

for (Method curr: all){
        if (curr.isAnnotationPresent(anno)){
            if (anno == Pre.class){
                for (String str : curr.getAnnotation(Pre.class).value()){
                    if (str.equals(method.getName()) && curr.getReturnType() == boolean.class && curr.getParameterTypes().length == 0){
                        toRun.add(curr);
                    }
                }
            } if (anno == Post.class) {
                for (String str : curr.getAnnotation(Post.class).value()){
                    if (str.equals(method.getName()) && curr.getReturnType() == boolean.class && curr.getParameterTypes().length == 0){
                        toRun.add(curr);
                    }
                }
            }
        }
    }

anno is a parameter - Class<? extends Annotation>, and Pre and Post are my annotations, both have a value() which is an array of strings.

Of course, this is all due to the fact that i let Eclipse auto fill code that i don't understand yet.

+2  A: 
Chris Smith
Because `curr.getAnnotation(anno).value()` can't compile. since it doesn't know what annotation is `anno` then it can't know that it has a `value()`. is I was to cast it using (Pre)(curr...) then it would work, but that doesn't really advance me anywhere.
Yonatan
Ah. Hmm. I guess you'd have to do something like this then: `final String[] values = anno == Pre.class? curr.getAnnotation(Pre.class).value() : curr.getAnnotation(Post.class).value();`, then iterate over that. Ugly, but can't see any other way as you can't do any kind of inheritance with annotations :/
Chris Smith