Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 1.5.0.12和运行时的自定义注释_Java_Annotations_Runtime - Fatal编程技术网

Java 1.5.0.12和运行时的自定义注释

Java 1.5.0.12和运行时的自定义注释,java,annotations,runtime,Java,Annotations,Runtime,我在一个项目中需要使用上述特定的JAVA版本。我不想使用自定义注释并在运行时使用反射查询它的存在。所以我写了一个注释,一个要注释的类和一个测试类。问题是,注释不存在。当我使用其中一个内置注释时,一切都很好,注释就在那里。当我在Java1.6下尝试我的代码时,一切都很好 这个java版本中是否存在已知的bug,或者我是否需要添加更多内容 溴 马库斯 守则: // The annotation import java.lang.annotation.Retention; import static

我在一个项目中需要使用上述特定的JAVA版本。我不想使用自定义注释并在运行时使用反射查询它的存在。所以我写了一个注释,一个要注释的类和一个测试类。问题是,注释不存在。当我使用其中一个内置注释时,一切都很好,注释就在那里。当我在Java1.6下尝试我的代码时,一切都很好

这个java版本中是否存在已知的bug,或者我是否需要添加更多内容

溴 马库斯

守则:

// The annotation
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Retention(RUNTIME) 
public @interface GreetsTheWorld {
  public String value();
}

// The Annotated Class
@GreetsTheWorld("Hello, class!") 
public class HelloWorld {

  @GreetsTheWorld("Hello, field!") 
  public String greetingState;

  @GreetsTheWorld("Hello, constructor!") 
  public HelloWorld() {
  }

  @GreetsTheWorld("Hello, method!") 
  public void sayHi() {
  }
}

// The test
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class HelloWorldAnnotationTest {

  public static void main( String[] args ) throws Exception {
    //access the class annotation
    Class<HelloWorld> clazz = HelloWorld.class;
    System.out.println( clazz.getAnnotation( GreetsTheWorld.class ) );

    //access the constructor annotation
    Constructor<HelloWorld> constructor = clazz.getConstructor((Class[]) null);
    System.out.println(constructor.getAnnotation(GreetsTheWorld.class));

    //access the method annotation
    Method method = clazz.getMethod( "sayHi" );
    System.out.println(method.getAnnotation(GreetsTheWorld.class));

    //access the field annotation
    Field field = clazz.getField("greetingState");
    System.out.println(field.getAnnotation(GreetsTheWorld.class));
  }
}
//注释
导入java.lang.annotation.Retention;
导入静态java.lang.annotation.RetentionPolicy.RUNTIME;
@保留(运行时)
public@interface欢迎世界{
公共字符串值();
}
//注释类
@向世界致意(“同学们好!”)
公共类HelloWorld{
@欢迎世界(“你好,菲尔德!”)
公共字符串问候状态;
@欢迎世界(“你好,构造器!”)
公共HelloWorld(){
}
@欢迎世界(“你好,方法!”)
公共空间{
}
}
//测试
导入java.lang.reflect.Constructor;
导入java.lang.reflect.Field;
导入java.lang.reflect.Method;
公共类HelloWorldAnnotationTest{
公共静态void main(字符串[]args)引发异常{
//访问类注释
clazz类=HelloWorld.Class;
System.out.println(clazz.getAnnotation(GreetsTheWorld.class));
//访问构造函数注释
Constructor=clazz.getConstructor((Class[])null;
System.out.println(constructor.getAnnotation(GreetsTheWorld.class));
//访问方法注释
方法方法=clazz.getMethod(“sayHi”);
System.out.println(method.getAnnotation(GreetsTheWorld.class));
//访问字段注释
Field=clazz.getField(“greetingState”);
System.out.println(field.getAnnotation(GreetsTheWorld.class));
}
}

我终于找到了问题所在:一切都很好,工作正常。我遇到的一个问题是,我使用了公司的默认java设置,它们将编译器兼容性和源文件兼容性设置为1.5。但是类文件兼容性设置为1.2,并且此版本中没有注释。 启用特定于项目的设置并将类文件兼容性更改为1.5后,一切正常

谢谢你的帮助
马库斯

可能是oops的复制品,不,显然不是——对不起,我看是对的。我想你只得到了空值?不确定1.5(在1.6下对我有效),但也许你必须在注释中添加一个
@Target
说明符?是的,添加
@Target
会很好,但这不是必需的:
@Target
的JavaDoc说,如果目标元批注不在批注类型声明中,则声明的类型可用于任何程序元素