Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

java - Annotation Processor, generating a compiler error

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

and Processor:

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You probably want processingEnv.getMessager().printMessage(Kind.ERROR, "method wasn't public and final", element).

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...