2011-11-02 9 views
23

私は新しいJavaプログラマーです。以下は私のコードです:javaでパラメータの注釈を取得する方法は?

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) 

私はすべてのファイルの注釈を取得したいと思います。いくつかのフィールドには注釈がついており、いくつかのフィールドは注釈されていない

私はmethod.getParameterAnnotations()の使用方法を知っていますが、3つの注釈を返すだけです。

私はそれらの対応方法がわかりません。

私は、次のような結果を期待:任意の注釈を持っていない任意のパラメータのための空の配列を使用して、パラメータごとに配列を返すgetParameterAnnotations

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") 

答えて

35

を。例えば:

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(); 
     } 
    } 
} 

これは、出力を与える:

0 annotatations 
1 annotatations 

最初のパラメータは何の注釈を持っていない、2番目のパラメータ一つの注釈を有することを示しています。

getParameterAnnotations「3つの注釈しか返しません」というあなたの主張には混乱しています。アレイ。おそらく、返された配列を何とか平坦化しているのでしょうか?

関連する問題