繁体   English   中英

Eclipse 中的注释处理器,Ant process() 未运行

[英]Annotation Processor in Eclipse with Ant process() not running

这是最后的问题。 我已经在互联网上搜索了可能有什么问题,但似乎没有任何效果。 我创建了一个应该生成资源文件的自定义注释,类如下:

@Target(METHOD)
@Retention(CLASS)
@Repeatable(Schedules.class)
public @interface Scheduled {

    /**
     * Specifies one or more minutes with in an hour. 
     */
    int[] minute() default {};
    
    /**
     * Specifies one or more hours within a day.
     */
    int[] hour() default {};
    
    /**
     * Specifies one or more days in a month
     */
    int[] date() default {};
    /**
     * Specifies one or more months within a year.
     */
    int[] month() default {};
    
    /**
     * Specifies one or more days within a week.
     */
    int[] dayOfWeek() default {};

}

@Target(METHOD)
@Retention(CLASS)
public @interface Schedules {

    Scheduled[] value();
}

注解处理器的结构如下:

@SupportedAnnotationTypes({"files.application.Scheduled"})
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class ScheduledProcessor extends AbstractProcessor {
    
    @Override
    public synchronized void init(ProcessingEnvironment env) {
        super.init(env);
    }
    
    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
       ...
    }
    
    public void buildFile(ArrayList<String> scheduledList) {
       ...
    }
}

注释的测试类:

public class AnnotationTest {
    private int age;
    private String name;
    
    @Scheduled
    public void setAge(int age) {
        this.age = age;
    }
    
    public int getAge() {
        return this.age;
    }
    
    @Scheduled(month= {7,8})
    @Scheduled(date= {5,6})
    public void setName(String name) {
        this.name = name;
    }
    
    public String getName() {
        return this.name;
    }
}

我们使用 ANT 任务通过代码完成许多任务,其中之一是compile

    <target name="-compile" depends="-pre-compile,-classpath-compile">
        <mkdir dir="${build.bin}"/>
        <javac
            destdir="${build.bin}" 
            encoding="UTF-8"
            includeAntRuntime="false" 
            debug="true" 
            debuglevel="${build.javac.debuglevel}"
            source="${build.javac.source}"
            target="${build.javac.target}"
        >
            <src path="${build.source}" />
            <src path="${build.gen}" />
            <classpath refid="classpath.compile" />
            
            <compilerarg line="-processor files.application.ScheduledProcessor"/>
            <compilerarg line="-s ${build.source}" />
        </javac>

我目前的项目结构是:

core
    src
        annotsProject
                 Scheduled.java
                 Schedules.java
                 ScheduledProcessor.java
                 AnnotationTest.java
        META-INF
             services
                  javax.annotation.processing.Processor
    anttasks.xml

当我运行编译 ANT 任务时, init(ProcessingEvironment env)被成功调用,但process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)永远不会被调用。 我尝试创建处理器的 .jar 文件和 META-INF 文件夹并将其添加到项目构建路径中,但没有成功。 我尝试从命令行使用javac ,但仍然没有。 也许我误解了什么时候应该调用process()方法,或者我的文件夹结构是错误的? 任何帮助将不胜感激!

发布问题后我想通了。 我搬到AnnotationTest.java到自己的项目, testProj ,并增加了一个依赖于testProjcore的的ivy.xml我们使用。 我编译了core ,它成功完成,但没有显示有关注释处理器的任何信息。 然后我用单独的ANT命令构建了它的工件。 然后我编译了testProj ,它确实在注释处理器中显示了我的printMessage() ,我在process()中有。 我检查了我想要创建的文件的输出位置,它就在那里! 我认为使用注释处理器的.jar方法是正确的方向,但我可能把它放在了错误的位置,因为我们的项目是如何设置的。 我们使用的 artifacts ANT命令为我构建了.jar并将其放在正确的位置,以便testProj可以看到它。 发布我的答案,以防它对任何人有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM