Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get parameter's annotation in java?

I'm a new Java programmer. Following is my code:

    public void testSimple1(String lotteryName,                         int useFrequence,                         Date validityBegin,                         Date validityEnd,                         LotteryPasswdEnum lotteryPasswd,                         LotteryExamineEnum lotteryExamine,                         LotteryCarriageEnum lotteryCarriage,                         @TestMapping(key = "id", csvFile = "lottyScope.csv") xxxxxxxx lotteryScope,                         @TestMapping(key = "id", csvFile = "lotteryUseCondition.csv") xxxxxxxx lotteryUseCondition,                         @TestMapping(key = "id", csvFile = "lotteryFee.csv") xxxxxxxx lotteryFee) 

I want to get all filed's annotations. Some fields are annotated and some ain't.

I know how to use method.getParameterAnnotations() function, but it just returns three annotations.

I don't know how to correspond them.

I expect the following result:

lotteryName - none useFrequence- none validityBegin -none validityEnd -none lotteryPasswd -none lotteryExamine-none lotteryCarriage-none lotteryScope - @TestMapping(key = "id", csvFile = "lottyScope.csv") lotteryUseCondition - @TestMapping(key = "id", csvFile = "lotteryUseCondition.csv") lotteryFee - @TestMapping(key = "id", csvFile = "lotteryFee.csv") 
like image 574
fred Avatar asked Nov 02 '11 07:11

fred


People also ask

How do you find the parameters of annotations?

getAnnotation(Annotation. class). param1() to get the param1 value. Here you have named your annotation interface as Annotation.

What is @interface annotation in Java?

@interface is used to create your own (custom) Java annotations. Annotations are defined in their own file, just like a Java class or interface. Here is custom Java annotation example: @interface MyAnnotation { String value(); String name(); int age(); String[] newNames(); }

What is @target annotation in Java?

If an @Target meta-annotation is present, the compiler will enforce the usage restrictions indicated by ElementType enum constants, in line with JLS 9.7. 4. For example, this @Target meta-annotation indicates that the declared type is itself a meta-annotation type.


1 Answers

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?

like image 60
Jon Skeet Avatar answered Sep 21 '22 11:09

Jon Skeet