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

发布于 2024-10-12 13:31:43 字数 1625 浏览 4 评论 0原文

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

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

BR 马库斯

代码:

// 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'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?

BR
Markus

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

憧憬巴黎街头的黎明 2024-10-19 13:31:43

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

感谢您的帮助
马库斯

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 help
Markus

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文