class.getAnnotation和getAnnotations无法正常工作

Jer*_*ang 5 java eclipse gwt annotations gwt-rpc

SecureDispatchService这样从GWT:

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

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();
}
Run Code Online (Sandbox Code Playgroud)

测试代码非常简单:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

但结果不是这样的:

null
0
Run Code Online (Sandbox Code Playgroud)

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

SecureDispatchService 来自google的这个jar:

gwt-dispatch-1.2.0.jar
Run Code Online (Sandbox Code Playgroud)

我曾经mvn eclipse:eclipse生成过这个项目.这个jar文件是eclipse项目的引用库,它的真实路径在我的.m2/repostory中.

Ale*_*r M 2

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