ベローに注釈を取得することができない私のカスタム注釈です: - :私は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を介して呼び出しの結果を取得しようとしていたとき。
if条件にnullポインタが追加されますか? –
はいf.isAnnotationPresent(InputBox.class)がNULLポインタ例外を指定しています。 –
あなたの@InputBoxアノテーションのようなものは、ランタイムクラスパス上にありません。クラスパスにどのように追加しましたか? –