【问题标题】:Methods for getting annotation metadata in JavaJava中获取注解元数据的方法
【发布时间】:2012-01-16 05:01:59
【问题描述】:

我正在为 GWT 开发 JSR-303 验证框架。你们中的一些人可能已经听说过它,即使它是一个小项目。 Here is gwt-validation.

在过去 (v1.0) 中,它为每个类使用一个标记接口,并且每个类都有单独生成的元数据。这很糟糕,因为它不是 JSR-303 标准的一部分,我们继续下一个想法。

在 2.0 版中,它在运行时使用Reflections 扫描类路径。这很棒。缺点是它似乎无法在容器化环境或有特殊限制的环境中工作。

这可能是我的错,看下面的代码:

    //this little snippet goes through the classpath urls and ommits jars that are on the forbidden list.
    //this is intended to remove jars from the classpath that we know are not ones that will contain patterns
    Set<URL> classPathUrls = ClasspathHelper.forJavaClassPath();
    Set<URL> useableUrls = new HashSet<URL>();
    for(URL url : classPathUrls) {
        boolean use = true;
        for(String jar : this.doNotScanJarsInThisList) {
            if(url.toString().contains(jar)) {
                use = false;
                break;
            }
        }
        if(use) {
            useableUrls.add(url);
        }
        use = false;
    }       

    ConfigurationBuilder builder = new ConfigurationBuilder()
                                    .setUrls(useableUrls)
                                    .setScanners(   new TypeAnnotationsScanner(), 
                                                    new FieldAnnotationsScanner(), 
                                                    new MethodAnnotationsScanner(), 
                                                    new SubTypesScanner()
                                    )
                                    .useParallelExecutor()
                                    ;

    this.reflections = new Reflections(builder);

我正在使用过滤器删除我知道无法在其中包含我感兴趣的注释的 jar。正如我所提到的,这会极大地提高速度(尤其是在大型类路径上) 但我基于此的 ClasspathHelper.forJavaClassPath() 可能不是进入 container 环境的最佳方式。 (例如 Tomcat、JBoss)

有没有更好的方法,或者至少有一种方法可以在容器环境中工作,并且仍然让我的用户过滤掉他们不想要的类?

我已经研究了一些 Hibernate Validation 项目(JSR-303 的参考实现),他们似乎至少(至少部分地)使用了 Java 6 中的注释处理。这不能就是故事的全部,因为直到 JDK6 和 Hibernate Validator 与 JDK5 兼容才出现。 (见:hibernate documentation

因此,与往常一样,故事还有更多内容。

我已经阅读了这些主题,供参考:

  • 关于 Scannotation,它几乎已被 Reflections 所取代。
  • 这个one,但它使用 File,我不确定它在 GAE(Google App Engine)或 Tomcat 中的含义。
  • 另一个that goes over 我已经谈过很多事情了。

这些线程只提供了这么多帮助。

我也读过about the annotation processing framework,但我一定遗漏了一些东西。它似乎做了我想做的事,但它似乎只在 compile time 工作,我知道这不是 Hibernate Validator 所做的。 (谁能解释它是如何进行扫描的?它适用于 GAE,这意味着它不能使用任何 IO 包。)

此外,这段代码会比我上面的代码更好吗?

Set<URL> classPathUrls = ClasspathHelper.forClassLoader(Thread.currentThread().getContextClassLoader());

能否正确地将类加载器放入 Tomcat 或 JBoss 容器中?似乎扫描了一组较小的类并且仍然可以完成。

那么,无论如何,谁能帮我指出正确的方向?还是我只是坚持我所拥有的?

【问题讨论】:

    标签: java validation annotations metadata bean-validation


    【解决方案1】:

    你可以看看 Spring 的注解支持。 Spring 可以扫描文件中的注释(使用 asm IIRC),并在容器内外工作。

    这可能并不容易,因为它通过了 Spring 的 Resource 抽象,但它应该可以重用(或提取)相关代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-30
      • 1970-01-01
      • 2010-11-02
      • 1970-01-01
      • 2023-03-04
      • 2017-11-20
      • 2021-08-13
      相关资源
      最近更新 更多