How to check if a parameter of the current method has an annotation and retrieve that parameter value in Java?

前端 未结 3 1997
逝去的感伤
逝去的感伤 2021-02-08 06:50

Consider this code:

public example(String s, int i, @Foo Bar bar) {
  /* ... */
}

I want to check if the method has an annotation @Foo

相关标签:
3条回答
  • 2021-02-08 07:06

    If you're looking for annotations on the method, you probably want method.getAnnotations() or method.getDeclaredAnnotations().

    The method.getParameterAnnotations() call gives you annotations on the method's formal parameters, not on the method itself.

    Looking back at the question title, I suspect you are looking for annotations on the parameters, which I didn't read in the content of the question. If that's the case, your code looks fine.

    See Method Javadoc and AnnotatedElement Javadoc.

    0 讨论(0)
  • 2021-02-08 07:13

    The outer for loop

    for (Annotation[] annotations : paramAnnotations) {
       ...
    }
    

    should use an explicit counter, otherwise you don't know what parameter you are processing right now

    final Annotation[][] paramAnnotations = method.getParameterAnnotations();
    final Class[] paramTypes = method.getParameterTypes();
    for (int i = 0; i < paramAnnotations.length; i++) {
        for (Annotation a: paramAnnotations[i]) {
            if (a instanceof Foo) {
                System.out.println(String.format("parameter %d with type %s is annotated with @Foo", i, paramTypes[i]);
            }
        }
    }
    

    Also make sure your annotation type is annotated with @Retention(RetentionPolicy.RUNTIME)

    From your question it is not entirely clear what you are trying to do. We agree on the difference of formal parameters vs. actual arguments:

    void foo(int x) { }
    
    { foo(3); }
    

    where x is a parameter and 3 is an argument?

    It is not possible to get the arguments of methods via reflection. If it is possible at all, you would have to use the sun.unsafe package. I can't tell you much about that though.

    0 讨论(0)
  • 2021-02-08 07:17

    getParameterAnnotations returns an array with the length equals to the amount of method parameters. Each element in that array contains an array of annotations on that parameter.
    So getParameterAnnotations()[2][0] contains the first ([0]) annotation of the third ([2]) parameter.

    If you only need to check if at least one parameter contains an annotation of a specific type, the method could look like this:

    private boolean isAnyParameterAnnotated(Method method, Class<?> annotationType) {
        final Annotation[][] paramAnnotations = method.getParameterAnnotations();    
        for (Annotation[] annotations : paramAnnotations) {
            for (Annotation an : annotations) {
                if(an.annotationType().equals(annotationType)) {
                    return true;
                }
            }
        }
        return false;
    }
    
    0 讨论(0)
提交回复
热议问题