简体   繁体   中英

Annotation Processor in Eclipse with Ant process() not running

this is a last resort question. I have searched all over the internet for what could possibly be wrong, but nothing seems to work. I have created a custom annotation that should generate a resource file, the classes are as follows:

@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 {};

}

and

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

    Scheduled[] value();
}

The annotation processor is structured as follows:

@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) {
       ...
    }
}

Test class for the annotation:

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;
    }
}

We use ANT tasks to accomplish many tasks with the code, one being 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>

My current project structure is:

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

When I run the compile ANT task, the init(ProcessingEvironment env) is successfully called, but process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) never gets called. I have tried creating a .jar file of the processor and the META-INF folder and adding it to the project build path, but no go. I have tried using javac from the command line, and still nothing. Maybe I am misunderstanding when the process() method should be called or maybe my folder structure is wrong? Any help would be greatly appreciated!

I figured it out after posting the question. I moved AnnotationTest.java to its own project, testProj , and added a dependency for testProj to core 's ivy.xml that we use. I compiled core , which finished successfully, but did not display any information about the annotation processor. I then built its artifacts with a separate ANT command. Then I compiled testProj , it did display my printMessage() , that I have in my process() , in the annotation processor. I checked the output location for the file I wanted created, and it was there! I think that .jar method of using the annotation processor was the right direction, but I might have put it in the wrong location for how our projects are set up. The artifacts ANT command we use, built the .jar for me and put it in the proper location so that testProj would see it. Posting my answer in case it helps anyone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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