只扫描测试包:使用abstractprocessor的java注解验证

kzmpq1sx  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(161)
<plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-compiler-plugin</artifactId>
               <version>3.8.1</version>
               <configuration>
                   <source>1.8</source>
                   <target>1.8</target>
               </configuration>
           </plugin>

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Function {
    String name();
}

@AutoService(Processor.class)
public class FunctionProcessor extends AbstractProcessor {

        @Override
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
             for (Element element : roundEnv.getElementsAnnotatedWith(RuleFunction.class)) {
          // code to validate duplicate function name 
    }
}

例子:

@Function(name = "First")
public class FirstAnnotation {
}

@Function(name = "First")
class SecondAnnotation {
}

我想抛出编译错误,以防任何人创建具有重复名称的类。roundenvironment不扫描主包,但扫描测试包。有人能帮我吗

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题