簡體   English   中英

是否可以在處理器的類路徑上掃描注釋?

[英]Is it possible to scan for annotations on the classpath in a processor?

我想編寫一個注釋處理器來掃描類路徑上的注釋。

這個想法是這樣的:

  • 主庫
    • 處理器實現,它從依賴項 A 和依賴項 B 中查找@Foo注釋,並基於它們生成 class。
  • 依賴項 A
    • 取決於基地
    • 聲明@Foo(someParam=Bar.class) public class A {...}
  • 依賴 B
    • 取決於基地
    • 聲明注解@Foo(someParam=Baz.class) public class B {...}
  • 根據
    • 聲明public @interface Foo{...}

這可能嗎? 有沒有更好的方法呢?

是的,這實際上是您使用注釋處理器的最基本和最簡單的模式。

任何給定的注釋處理器都會注冊它感興趣的注釋。您可以選擇說"*" ,在這種情況下,您的注釋處理器將被傳遞給每個生成的 class model(即 object 代表飛行中的 class;在解析之后,但是在它被寫入磁盤之前)。 更常見的是,您選擇一個或幾個注釋,在這種情況下,您只會收到使用以下注釋之一注釋的那些模型的通知:

基地項目

MyAnno.java

package com.foo;

@Target(ElementType.TYPE) //[1]
@Retention(RetentionPolicy.SOURCE)
public @interface MyAnno {}

MyProcessor.java

@SupportedAnnotationTypes("com.foo.MyAnno") // [2]
class MyProcessor extends AbstractProcessor {

  @Override public boolean process(Set<? extends TypeElement> annos, RoundEnvironment round) {

    for (TypeElement annoType : annos) {
      Set<? extends Element> annotatedElems = round.getElementsAnnotatedWith(annoType);
      for (Element elem : annotatedElems) hello(elem);
    }
    return false;
  }

  private void hello(Element elem) {
    // 'elem' can represent packages, classes, methods, fields, etc.
    // because you constrainted the anno to only be allowed on types,
    // it'd have to be a type (class or interface), so we can cast..

    TypeElement typeElem = (TypeElement) elem;
    // do whatever you want with it, here.
}

[1] 這表示此注解只能放在類型上。 因此,例如,不在方法上(嘗試編寫@MyAnno public String foo() {}將是一個編譯器錯誤,表明不能將@MyAnno放在方法上)。

[2] 注解處理器存在先有雞還是先有蛋的問題:您仍在編譯代碼,這意味着表示代碼的 class 文件可能還不存在,編譯器也不知道什么方法甚至在其中可用。 因此,您無法使用通常的反射庫。 相反,您會獲得模型/元素類型,例如TypeElement ,其中很多是“字符串類型”,您在其中以字符串形式引用事物,例如此處。 您不寫@SupportedAnnotationTypes(MyAnno.class)這一事實是 AP 設計的有意部分。

真的,只需按照您的花園品種“我如何編寫注釋處理器”教程進行操作即可。 他們應該涵蓋這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM