3

我已经写了注释

    @Retention(RetentionPolicy.SOURCE)
public @interface Encrypt {

}

及其处理器...

@SupportedAnnotationTypes("it.trecube.annotation.Encrypt")
@SupportedSourceVersion(SourceVersion.RELEASE_7)
public class EncryptProcessor extends AbstractProcessor{

    public EncryptProcessor(){
        super();
    }


    @Override
    public boolean process(Set<? extends TypeElement> annotations,
            RoundEnvironment roundEnv) {
        String className = null;
        String packageName = null;
        String fqClassName = null;
        for (Element elem : roundEnv.getElementsAnnotatedWith(Encrypt.class)) {
            if (elem.getKind() == ElementKind.CLASS) {
                //              Encrypt encrypt = elem.getAnnotation(Encrypt.class);
                //              String message = "annotation found in " + elem.getSimpleName();
                //              processingEnv.getMessager().printMessage(Kind.NOTE, message);
                TypeElement classElement = (TypeElement) elem;
                PackageElement packageElement = (PackageElement) classElement.getEnclosingElement();

                processingEnv.getMessager().printMessage(
                        Diagnostic.Kind.NOTE,
                        "annotated class: @Encrypt" , elem);

                className = classElement.getSimpleName().toString();
                packageName = packageElement.getQualifiedName().toString();
                fqClassName = classElement.getQualifiedName().toString();

                if (fqClassName != null) {
                    processingEnv.getMessager().printMessage(
                            Diagnostic.Kind.NOTE,
                            "fqClassName: "+fqClassName , elem);
                    Properties props = new Properties();
                    URL url = this.getClass().getClassLoader().getResource("velocity.properties");
                    try {
                        props.load(url.openStream());
                    } catch (IOException e) {
                        processingEnv.getMessager().printMessage(
                                Diagnostic.Kind.ERROR,
                                "annotated class: " + classElement.getQualifiedName()+"->\n"+
                                        e.getMessage(), elem);
                        e.printStackTrace();
                        return true;
                    }
                    VelocityEngine ve = new VelocityEngine(props);
                    ve.init();

                    VelocityContext vc = new VelocityContext();

                    vc.put("className", className);
                    vc.put("packageName", packageName);


                    Template vt = ve.getTemplate("encrypt.vm");
                    File file = new File("src/main/java/"+fqClassName.replace(".", "/")+"_Encrypt.aj");
                    try {
                    BufferedWriter bw = new BufferedWriter(new FileWriter(file));

                        processingEnv.getMessager().printMessage(
                                Diagnostic.Kind.NOTE,
                                "creating source file: " + file.getAbsolutePath());


                        processingEnv.getMessager().printMessage(
                                Diagnostic.Kind.NOTE,
                                "applying velocity template: " + vt.getName());

                        vt.merge(vc, bw);

                        bw.close();
                    } catch (IOException e) {
                        processingEnv.getMessager().printMessage(
                                Diagnostic.Kind.ERROR,
                                "applying velocity error: " + vt.getName()+"->\n"+e.getMessage());
                        e.printStackTrace();
                    }
                }
            }
        }
        return true; // no further processing of this annotation type
    }

我已经在一个 Maven 客户端项目中测试了所有内容并且所有的工作,但是当我尝试将它用于现有的 Spring(完整的 Maven 构建)项目时不起作用......有人可以帮助我吗?肿瘤坏死因子

4

2 回答 2

2

您是否在META-INF/services/javax.annotation.processing.Processor拥有注释处理器的项目中创建了文件?此文件应包含处理器的完全限定名称。此外,使用 maven,您可以通过配置指定在构建中使用哪些注释处理器maven-compiler-plugin

<plugin>
   <artifactId>maven-compiler-plugin</artifactId>
   <configuration>
        <annotationProcessors>
            <annotationProcessor>com.mycompany.MyAnnotationProcessor</annotationProcessor>
        </annotationProcessors>
    </configuration>
</plugin>

此外,将注释处理器与您使用的项目放在同一个项目中可能会导致 maven 出现一些问题,因此如果您还没有这样做,我建议您将注释处理器和注释提取到单独的 maven 项目中。

于 2013-03-29T16:11:12.653 回答
1

Solved adding this...

            <plugin>
                <groupId>org.bsc.maven</groupId>
                <artifactId>maven-processor-plugin</artifactId>
                <executions>
                    <execution>
                        <id>process</id>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <phase>generate-sources</phase>
                    </execution>
                </executions>
            </plugin>
于 2013-03-29T16:39:02.790 回答