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?
Annotation processing is a tool built into javac for scanning and processing annotations at compile time. It can create new source files; however, it can't modify existing ones. It's done in rounds. The first round starts when the compilation reaches the pre-compile phase.
The @interface element is used to declare an annotation. For example: @interface MyAnnotation{}
The annotation processing is done in multiple rounds. Each round starts with the compiler searching for the annotations in the source files and choosing the annotation processors suited for these annotations. Each annotation processor, in turn, is called on the corresponding sources.
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."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With