问题描述
我在一个项目中,我需要使用上述特定的Java版本。我wan't使用自定义的注释,并使用反射查询它的presence运行期间。所以我写了一个注解,类注释和测试类。 IST的问题,注释是不存在的。当我使用内置的注解之一,一切都很好,注释是存在的。当我尝试JAVA在我的code 1.6一切都好...
有没有在这个Java版本的已知错误,还是我需要添加更多的东西?
BR
马库斯
在code:
//注释
进口java.lang.annotation.Retention;
引入静态java.lang.annotation.RetentionPolicy.RUNTIME;@Retention(运行时)
公共@interface GreetsTheWorld {
公共字符串值();
}//被注解类
@GreetsTheWorld(你好,类!)
公共类的HelloWorld { @GreetsTheWorld(你好,场!)
公共字符串greetingState; @GreetsTheWorld(你好,构造!)
公开的HelloWorld(){
} @GreetsTheWorld(你好,法!)
公共无效的sayHi(){
}
}// 考试
进口java.lang.reflect.Constructor中;
进口java.lang.reflect.Field中;
进口的java.lang.reflect.Method;公共类HelloWorldAnnotationTest { 公共静态无效的主要(字串[] args)抛出异常{
//访问类注解
类<&的HelloWorld GT; clazz所= HelloWorld.class的;
的System.out.println(clazz.getAnnotation(GreetsTheWorld.class)); //访问构造注解
构造函数和LT;的HelloWorld>构造= clazz.getConstructor((类[])NULL);
的System.out.println(constructor.getAnnotation(GreetsTheWorld.class)); //访问方法的注释
方法方法= clazz.getMethod(sayHi的);
的System.out.println(method.getAnnotation(GreetsTheWorld.class)); //接入领域注解
场场= clazz.getField(greetingState);
的System.out.println(field.getAnnotation(GreetsTheWorld.class));
}
}
我终于发现是什么问题:一切都很好和作品。有一个问题我是,我使用了默认的Java设置,由我公司,可以设置编译器一致性测试和源文件兼容性1.5。但类文件兼容性设定为1.2,并有在该版本没有注释。
使项目特定的设置和改变类文件的兼容性到1.5之后,一切工作正常。
您的帮助谢谢
马库斯
I'm in a project where I need to use the above specific JAVA Version. And I wan't to use a custom annotation and query it's presence during RUNTIME using reflection. So I wrote an annotation, a class to annotate and a test class. The problem ist that, the annotation is not there. When I use one of the built in Annotations, everything is fine, the annotation is there. When I try my code under JAVA 1.6 everything is fine...
Is there a known bug in this java version or do I need to add something more?
BRMarkus
The code:
// 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));
}
}
I finally found what was the problem: Everything is fine and works. The one problem I had was, that I used the default java settings from my company, and they set Compiler Compliance and Source File compatibility to 1.5. But the class file compatibility was set to 1.2, and there were no annotations in this version.After enabling the project specific settings and changing the class file compatibility to 1.5, everything works fine.
Thanks for your helpMarkus
这篇关于Java的1.5.0.12和定制注解在运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!