how to get parameter’s annotation in java?

getParameterAnnotations returns one array per parameter, using an empty array for any parameter which doesn’t have any annotations. For example:

import java.lang.annotation.*;
import java.lang.reflect.*;

@Retention(RetentionPolicy.RUNTIME)
@interface TestMapping {
}

public class Test {

    public void testMethod(String noAnnotation,
        @TestMapping String withAnnotation)
    {
    }

    public static void main(String[] args) throws Exception {
        Method method = Test.class.getDeclaredMethod
            ("testMethod", String.class, String.class);
        Annotation[][] annotations = method.getParameterAnnotations();
        for (Annotation[] ann : annotations) {
            System.out.printf("%d annotatations", ann.length);
            System.out.println();
        }
    }
}

This gives output:

0 annotatations
1 annotatations

That shows that the first parameter has no annotations, and the second parameter has one annotation. (The annotation itself would be in the second array, of course.)

That looks like exactly what you want, so I’m confused by your claim that getParameterAnnotations “only returns 3 annotations” – it will return an array of arrays. Perhaps you’re somehow flattening the returned array?

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)