Java – comments do not work

I'm working on an annotation that forces a class to be immutable Here is the processor code:

@SupportedAnnotationTypes("archipel.immutability.IsImmutable")
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class IsImmutableProcessor extends AbstractProcessor {

    @Override
    public boolean process(Set<? extends TypeElement> annotations,RoundEnvironment roundEnv) {
        for (TypeElement type : annotations) {
            processMustBeImmutable(roundEnv,type);
        }
        return true;
    }

    private void processMustBeImmutable(RoundEnvironment env,TypeElement type) {
        for (Element element : env.getElementsAnnotatedWith(type)) {
            processClass(element);
        }
    }

    private void processClass(Element element) {
        boolean isFinal=false;

        for(Modifier modifier : element.getModifiers()) {
            if (modifier.equals(Modifier.FINAL)) {
                isFinal=true;
                break;
            }
        }

        if (!isFinal) {
            processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,"Class "+element+" is not immutable because it is not final");
        } else {
            for (Element subElement : element.getEnclosedElements()) {
                if (subElement.getKind()==ElementKind.FIELD) {
                    isFinal=false;

                    for(Modifier modifier : subElement.getModifiers()) {
                        if (modifier.equals(Modifier.FINAL)) {
                            isFinal=true;
                            break;
                        }
                    }
                    if (!isFinal) {
                        processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,"Field "+element+" is not immutable because it is not final");
                    } else {
                        Element superElement = subElement.getEnclosingElement();
                        // TODO
                    }
                }
            }
        }
    }

}

Of course, the annotation itself is insignificant:

@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.soURCE)
public @interface IsImmutable {
}

I compiled it with the ant script:

<project name="immutability" basedir="." default="main">

    <property name="lib.dir" value="lib"/>

    <property name="src.dir" value="src"/>

    <property name="build.dir" value="build"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="Meta.dir" value="${build.dir}/Meta-INF"/>
    <property name="jar.dir" value="${build.dir}/jar"/>

    <property name="processor-package"
        value="archipel.immutability" />

    <property name="processor" value="${processor-package}.IsImmutableProcessor"/>

    <path id="classpath">
        <fileset dir="${lib.dir}" includes="**/*.jar"/>
        <fileset dir="${classes.dir}"/>
    </path>

    <target name="clean">
    <delete dir="${build.dir}"/>
    </target>

    <target name="compile" description="Compiles the code.">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" />
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
        <jar destfile="${jar.dir}/${ant.project.name}.jar">
            <fileset dir="${classes.dir}"/>
            <service type="javax.annotation.processing.Processor" provider="archipel.immutability.IsImmutableProcessor"/>
        </jar>
    </target>

    <target name="main" depends="clean,jar"/>

</project>

The problem is that something must be missing, because when I try to use the result annotation provided by the generated jar file, as shown below, there is no response:

@IsImmutable
public class Immut {

    private int toto;

    public int getToto() {
        return toto;
    }

    public void setToto(int toto) {
        this.toto = toto;
    }

    public final static void main(String args[]) {
        Immut truc = new Immut();
        truc.setToto(5);
        truc.setToto(6);
    }

}

Obviously, this class is not final, and it should signal an error in eclipse But this is not the case

Any ideas?

Editor: I use build The jar file built from XML seems to be correct: it contains class files and meta inf / services / javax annotation. processing. Processor file containing archipel immutability. IsImmutableProcessor. I imported this jar file into my test project. When I used annotations in my immune class (this is just a rough test), there was no response

Solution

By default, annotation processing is disabled in eclipse To enable it, you need to open the project properties and then

If you need more information, please see: getting started with AP in eclipse

Edit:

Look at this tutorial It explains in detail how to set up eclipse to use a custom annotation processor

Important: use this method when printing errors: javax annotation. processing. Messager. Printmessage (kind, charsequence, element) replaces: javax annotation. processing. Messager. printMessage(Kind,CharSequence).

Messages from the first are visible in the problems view and most source related views, while messages from the second are visible only in the error log view

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>