2016-07-29 4 views
1

ベローに注釈を取得することができない私のカスタム注釈です: - :私はJavaでカスタム注釈を使用しようとしています。しかし

public class Table { 

    private long id; 

    @InputBox(width = 25 ,length = 25, placeholder = "" , title = "" , friendlyName = "" , name = "") 
    public String name; 

    @InputBox(length = 10,placeholder = "",title = "" , friendlyName = "" , name = "") 
    private int age;}} 

パーサ: - - ここでは

@Documented 
@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.FIELD) 
public @interface InputBox{ 

    int width() default 20; 
    int length() default 20; 
    String placeholder(); 
    String title(); 
    String friendlyName(); 
    String name(); 

} 

私は、アノテーションを使用していますここで私はクラス名を渡していますして取得します私は同じ安野にアクセスしていた場合

public JSONArray getFormMetaData(String className) throws InvocationTargetException, IllegalAccessException { 
     Class cl = null; 
     try{ 
      cl = Class.forName(className); 
     } catch(ClassNotFoundException e){ 
      e.printStackTrace(); 
     } 
     JSONArray jsonArray = new JSONArray(); 

     for(Field f: cl.getDeclaredFields()) 
     { 
      if(f.getDeclaredAnnotations().length >0){ 
       if(f.isAnnotationPresent(InputBox.class)){ 
        JSONObject obj = AnnotationProcessor.processInputBoxAnnotation(f); 
        obj.put("type","text"); 
        jsonArray.add(obj); 
       }}} 

- nullポインタ例外を与え、注釈やisAnnotationPresent方法を得ることができ、注釈の詳細ではなく、静的なメインメソッドの中で私は適切な結果を得ています。例: -

パブリッククラスメイン{

public static void main(String[] args) { 


    Table.class.getDeclaredFields()[1].getDeclaredAnnotations(); 

} 

}

しかし、私は、私はすべての注釈を取得していない午前APIを介して呼び出しの結果を取得しようとしていたとき。

+0

if条件にnullポインタが追加されますか? –

+0

はいf.isAnnotationPresent(InputBox.class)がNULLポインタ例外を指定しています。 –

+0

あなたの@InputBoxアノテーションのようなものは、ランタイムクラスパス上にありません。クラスパスにどのように追加しましたか? –

答えて

0

私はテストするあなたのテーブルクラスでmainメソッドを作成します。その作業の詳細は以下のコードを参照してください。

import java.lang.reflect.Field; 
import java.lang.reflect.InvocationTargetException; 

public class Table { 

    private long id; 

    @InputBox(width = 25, length = 25, placeholder = "", title = "", friendlyName = "", name = "") 
    public String name; 

    @InputBox(length = 10, placeholder = "", title = "", friendlyName = "", name = "") 
    private int age; 

    public void testAnnotation(String className) throws InvocationTargetException, IllegalAccessException { 
     Class cl = null; 
     try { 
      cl = Class.forName(className); 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } 

     for (Field f : cl.getDeclaredFields()) { 
      if (f.getDeclaredAnnotations().length > 0) { 
       if (f.isAnnotationPresent(InputBox.class)) { 
        System.out.println("annotationPresent"); 
       } 
      } 
     } 



    } 
    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException { 

     new Table().testAnnotation(Table.class.getName()); 
    } 
} 
+0

はい、静的メインメソッドから呼び出す場合は私が言及したように、everytingは正常に動作しています。しかし、サーバーに同じコードをデプロイしてapiを呼び出しているときに、 - if(f.isAnnotationPresent(InputBox.class))およびf.getDeclaredAnnotations()がnullを返す場合、nullポインタ例外が発生します。 –

関連する問題