class.getAnnotation和getAnnotations不能正常工作

时间:2015-04-08 08:56:06

标签: java eclipse gwt annotations gwt-rpc

来自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:

gwt-dispatch-1.2.0.jar

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

1 个答案:

答案 0 :(得分:2)

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

相关问题