2016-07-29 17 views
1

Bellow zu bekommen, ist meine Gewohnheit Annotation: -Ich versuche, benutzerdefinierte Anmerkung in Java zu verwenden. Aber nicht in der Lage die Anmerkung in

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

} 

Hier habe ich die Anmerkung bin mit: -

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

Parser: - Hier ist classname bin vorbei die bekommen Anmerkung Details, aber nicht in der Lage die Anmerkung und isAnnotationPresent Methode, die zu null-Zeiger-Ausnahme zu erhalten -

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

Wenn ich die gleiche anno am Zugriff auf In einer statischen Hauptmethode erhalte ich korrekte Ergebnisse. Ex: -

public class Haupt {

public static void main(String[] args) { 


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

} 

}

Aber wenn ich Ergebnisse versuche durch api Aufruf zu bekommen ich keine Anmerkung bin immer.

+0

So erhalten Sie einen Nullpointer in der If-Bedingung? –

+0

Ja f.isAnnotationPresent (InputBox.class) gibt eine Nullzeiger-Ausnahme. –

+0

Scheint, dass Ihre Annotation @InputBox nicht im Klassenpfad der Laufzeit enthalten ist. Wie hast du es zum Klassenpfad hinzugefügt? –

Antwort

0

Ich mache nur Haupt-Methode in Ihrer Tabelle Klasse zu testen, ist die Arbeit in Ordnung, siehe unten Code.

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

Ja, wie ich schon erwähnt habe, wenn ich von statischer Hauptmethode aus anrufe, funktioniert alles gut. Aber wenn ich den gleichen Code auf dem Server bereitstellen und über API aufrufen, dann bekomme ich die Nullzeiger-Ausnahme in - if (f.isAnnotationPresent (InputBox.class)) und f.getDeclaredAnnotations() gibt null. –