ant编辑java忽略注释_java – 注释不起作用

我正在研究一个注释,它旨在强制一个类是不可变的.这里是处理器的代码:

@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脚本编译它:

value="archipel.immutability" />

问题是,必须缺少某些东西,因为当我尝试使用由生成的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中发出错误信号.但事实并非如此.

任何的想法?

编辑:我使用build.xml构建的jar文件似乎是正确的:它包含类文件,还包含Meta-INF / services / javax.annotation.processing.Processor文件,其中包含archipel.immutability.IsImmutableProcessor.我在我的测试项目中导入了这个jar文件,当我在我的Immut类中使用注释时(这只是一个粗略的测试),没有任何反应.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值