注释处理器,生成一个编译器错误 [英] Annotation Processor, generating a compiler error

查看:275
本文介绍了注释处理器,生成一个编译器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义的注释,例如,可以确保一个字段或方法既公共最后,并会产生一个编译时错误,如果字段或方法是不是两个公共最后,如在这些实施例

I'm trying to create a custom annotation that, for example, ensures that a field or method is both public and final, and would generate a compile time error if the field or method is not both public and final, as in these examples:

// Compiles
@PublicFinal
public final int var = 2;

// Compiles
@PublicFinal
public final void myMethod {}

// Compile time error
@PublicFinal
private final int fail = 2;

到目前为止,我做了两个自定义注释界面:

So far, I've made both the custom annotation interface:

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Documented
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.METHOD, ElementType.FIELD})
public @interface PublicFinal { }

处理器

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import java.util.Set;

@SupportedAnnotationTypes("PublicFinal")
public class PubicFinalProcessor extends AbstractProcessor
{
    @Override
    public boolean process(
            Set<? extends TypeElement> annotations,
            RoundEnvironment roundEnv)
    {
        for (TypeElement typeElement : annotations)
        {
            Set<Modifier> modifiers = typeElement.getModifiers();

            if (!modifiers.contains(Modifier.FINAL)
                    || !modifiers.contains(Modifier.PUBLIC))
            {
                // Compile time error.
                // TODO How do I raise an error?
            }
        }

        // All PublicFinal annotations are handled by this Processor.
        return true;
    }
}

作为暗示由 TODO ,我不知道如何生成编译时错误。在<一个href=\"http://docs.oracle.com/javase/6/docs/api/javax/annotation/processing/Processor.html\">documentation处理器使得它清楚,我不应该抛出一个异常,

As hinted by the TODO, I do not know how to generate the compile time error. The documentation of Processor makes it clear that I should not be throwing an exception,

如果处理器抛出一个未捕获的异常,该工具可能停止其他活动的注释处理器。

If a processor throws an uncaught exception, the tool may cease other active annotation processors.

它接着描述当一个错误情况发生了,会发生什么,但是现在的如何的引发错误的条件。

It goes on to describe what happens when an error condition is raised, but now how to raise an error condition.

问:我怎么提高的错误条件这样就产生一个编译时错误

Question: how do I raise an error condition such that it generates a compile time error?

推荐答案

您可能希望 processingEnv.getMessager()。printMessage(Kind.ERROR,方法是不公开的和最后的元素)

<一个href=\"http://docs.oracle.com/javase/6/docs/api/javax/annotation/processing/Messager.html\">Messager: 打印一条消息,一个错误一种会引发一个错误。

Messager: "Printing a message with an error kind will raise an error."

这篇关于注释处理器,生成一个编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆