注释不起作用

问题描述 投票:6回答:3

我正在研究一个注释,该注释的目的是使类必须是不变的。这里是处理器的代码:

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

}

注解本身是微不足道的,当然:

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

并且我用Ant脚本进行编译:

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

问题是,必须丢失一些东西,因为当我尝试使用结果jar文件提供的结果注释时,如下所示,什么都没有发生:

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

}

很明显,此类不是最终的,该类应该在Eclipse中发出错误信号。但是不是。

任何想法?

Edit:用build.xml构建的jar文件似乎是正确的:它包含类文件,还包含META-INF/services/javax.annotation.processing.Processor文件,其中包含archipel.immutability.IsImmutableProcessor。我将这个jar文件导入我的测试项目中,当我在Immut类中使用批注(这只是一个粗略的测试)时,什么也没发生。

java ant annotations
3个回答
10
投票

默认情况下,在Eclipse中禁用注释处理。为了启用它,您需要打开项目属性,然后1. Java编译器->注释处理:启用注释处理2. Java编译器->注释处理->工厂路径:添加带有工厂的jar

如果需要更多信息,请查看:getting started with AP in eclipse

编辑:

看看这个tutorial。它详细说明了如何设置eclipse以使用自定义注释处理器。

重要:打印错误时,请使用以下方法:javax.annotation.processing.Messager.printMessage(Kind, CharSequence, Element)代替:javax.annotation.processing.Messager.printMessage(Kind, CharSequence).

来自第一个消息的消息在“问题”视图和大多数与源相关的视图中可见,而来自第二个消息的消息仅在ErrorLog视图中可见。


0
投票

可能值得使用cglib使您的对象不可变。因为假设您在类中有一个集合,并且集合字段是最终的。您也可以使用此系列的吸气剂。这是否意味着不能在类之外修改集合?不完全是。仅当此集合使用Collections.unmodifiedCollection方法包装时。或者,如果您使用的是番石榴不可变集合http://code.google.com/p/guava-libraries/wiki/ImmutableCollectionsExplained

重点是-不要试图仅检查字段是否为最终值-不能处理所有情况。使用cglib代理代替<< [spring and hibernate确实


0
投票
我已经尝试了上面写的每件事,但没有成功enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.