【问题标题】:class.getAnnotation and getAnnotations doesn't work properlyclass.getAnnotation 和 getAnnotations 不能正常工作
【发布时间】:2015-04-08 08:56:06
【问题描述】:

像这样来自 gwt 的 SecureDispatchService 类:

@RemoteServiceRelativePath("dispatch")
public interface SecureDispatchService extends RemoteService {
    Result execute( String sessionId, Action<?> action ) throws DispatchException;
}

RemoteServiceRelativePath:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface RemoteServiceRelativePath {
  /**
   * The relative path for the {@link RemoteService} implementation.
   * 
   * @return relative path for the {@link RemoteService} implementation
   */
  String value();
}

测试代码很简单:

package com.name.sbts.wbts.sm;

import net.customware.gwt.dispatch.client.secure.SecureDispatchService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;

public class TestClass {

    public static void main(String[] args) {

        Class c = SecureDispatchService.class;
        System.out.println(c.getAnnotation(RemoteServiceRelativePath.class));
        System.out.println(c.getAnnotations().length);
    }
}

但结果是不想要的:

null
0

我在 Eclipse 中使用 JRE1.7 运行此代码

SecureDispatchService 在这个来自 google 的 jar 中:

gwt-dispatch-1.2.0.jar

我使用mvn eclipse:eclipse 来生成项目。 这个jar文件作为eclipse项目的引用库,真实路径在我的.m2/repostory中。

【问题讨论】:

  • 包含注解的类文件必须在正在运行的应用程序的类路径中。否则,JVM 将在加载类时静默删除注释,从而导致您看到的行为。也许这是你的问题? (如果是这样,我会将我的评论升级为答案。)
  • 也许是这个原因,但是你知道如何解决这个问题吗? SecureDispatchService 类由 google 提供,它位于 jar 文件中:gwt-dispatch-1.2.0.jar。我在eclipse中使用mvn eclipse:eclipse生成项目,直接在eclipse中运行应用。
  • 我也尝试将此jar文件添加到我的系统变量的CLASS_PATH,但似乎不起作用。
  • 你不应该使用mvn eclipse:eclipseeclipsify你的项目。而是使用 M2E 插件。此外,您的 POM 应该简单地将库作为依赖项。就是这样。 (永远不要操作 CLASS_PATH 环境变量。)
  • @Seelenvirtuose,感谢您的帮助。虽然它并没有真正解决这个问题。

标签: java eclipse gwt annotations gwt-rpc


【解决方案1】:

这是因为gwt-dispatch 项目是使用旧的gwt-user 依赖项(gwt 版本2.0.4)编译的。在这个版本的 gwt 中,RemoteServiceRelativePath 注释上没有@Retention(RetentionPolicy.RUNTIME),因此RemoteServiceRelativePath 注释在运行时不可用。

【讨论】:

  • 旧信息存储在哪里,我的意思是如果编译 gwt-dispatch-1.2.0.jar 而 RemoteServiceRelativePath 不使用@Retention(RetentionPolicy.RUNTIME),我机器上的 JVM 应该以某种方式知道这一点,并使用这个过时的数据,但在我的环境中,只有 gwt-user-2.7.0.jar
  • @JerryZhang:您正在检查SecureDispatchService 类,它没有关于RemoteServiceRelativePath 注释的信息,因为它是用旧版本编译的。
  • 是的,我明白了这个说法,但问题是:gwt-dispatch-1.2.0.jar 是否仍然保存gwt-user-2.0.4.jar 中的信息,因此jvm 可以查找remoteServiceRelativePath 的错误类详细信息。
  • @JerryZhang:是的,类包含有关其注释的信息。如果您在文本编辑器中打开 SecureDispatchService 类文件,那么您应该会看到类似 RuntimeInvisibleAnnotations 的内容。
  • 是的,我在txt打开的课上看到了。你有什么想法我可以绕过这个问题,重新编译一个本地版本的gwt-dispatch
猜你喜欢
  • 2012-07-11
  • 2012-09-24
  • 2013-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-17
  • 2021-07-16
  • 2017-01-28
相关资源
最近更新 更多