我正在尝试在java中使用自定义注释。但无法在中获取注释
下面是我的自定义注释:-我正在尝试在java中使用自定义注释。但无法在中获取注释,java,annotations,Java,Annotations,下面是我的自定义注释:- @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface InputBox{ int width() default 20; int length() default 20; String placeholder(); String title(); String friendlyName();
@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 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:-这里我传递className以获取注释细节,但无法获取注释,并且isAnnotationPresent方法给出null指针异常-
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);
}}}
当我在静态main方法中访问同一注释时,我得到了正确的结果。例如:
公共班机{
public static void main(String[] args) {
Table.class.getDeclaredFields()[1].getDeclaredAnnotations();
}
}
但是,当我试图通过api获取调用结果时,我没有得到任何注释。我只是在表类中创建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());
}
}
我只是在你们的表类中做了主要的方法来测试,其工作很好,请参考下面的代码
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());
}
}
那么您在if条件下得到了一个空指针?是的,f.isAnnotationPresent(InputBox.class)给出了空指针异常。您的@InputBox注释似乎不在运行时类路径上。您是如何将其添加到类路径的?您是否粘贴了实际的代码?您使用的是哪个版本的服务器?服务器在哪个版本中使用哪个jre?因此,在if条件下会得到一个空指针?是的,f.isAnnotationPresent(InputBox.class)给出了空指针异常。您的@InputBox注释似乎不在运行时类路径上。您是如何将其添加到类路径的?您是否粘贴了实际的代码?您使用的是哪个版本的服务器?服务器使用的是哪个版本的jre?是的,正如我提到的,如果我从静态主方法调用,那么一切都正常。但当我在服务器上部署相同的代码并通过api调用时,在--if(f.isAnnotationPresent(InputBox.class))和f.getDeclaredAnnotations()中获取null指针异常时,会给出null。是的,正如我提到的,如果我从静态主方法调用,则每次都可以正常工作。但当我在服务器上部署相同的代码并通过api调用时,在--if(f.isAnnotationPresent(InputBox.class))和f.getDeclaredAnnotations()中获取null指针异常时,会给出null。