Gangnus
Gangnus

Reputation: 24464

Strange work or two-parameter form of messager.printMessage

According to Java.doc, Messager has 4 footprints of printMessage:

printMessage​(Diagnostic.Kind kind, CharSequence msg)
printMessage​(Diagnostic.Kind kind, CharSequence msg, Element e)
printMessage​(Diagnostic.Kind kind, CharSequence msg, Element e, AnnotationMirror a)
printMessage​(Diagnostic.Kind kind, CharSequence msg, Element e, AnnotationMirror a, AnnotationValue v)

The Interface of the annotation is:

@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
public @interface AddBuilder {
    public boolean lazyBuild() default false;
}

I want it to work only with non-abstract class and check for the abstractness in the Processor. The reason is: I want to know how can I make checks in the processor.


The second variant works:

public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    Set<? extends Element> classesForBuilder = roundEnv.getElementsAnnotatedWith(AddBuilder.class);
    for(Element classElement : classesForBuilder){
        if (classElement.getModifiers().contains(Modifier.ABSTRACT)  ) {
            messager.printMessage(Kind.ERROR, "AnnoBuilder cannot be applied to an abstract class.", classElement);
            return true;
        }
...

The error is seen on the annotated element and in Problems view

enter image description here

, but not seen in the Errors Log.


If I use the first form, expecting to find the error in Problems or Error Log:

public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    messager.printMessage(Kind.ERROR, "Some problem with annotations.");
    return true;

Upvotes: 1

Views: 47

Answers (0)

Related Questions