0

Every annotation processor I've made seems to have this problem. For example, a @Constant annotation:

package annotations;

@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.FIELD)
public @interface Constant {

}

The processor:

package processor;

@SupportedAnnotationTypes("annotations.Constant")
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public final class ConstantProcessor extends AbstractProcessor {

    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        for(Element element : roundEnv.getElementsAnnotatedWith(Constant.class)) {
           Set<Modifier> modifiers = element.getModifiers();

           if(!modifiers.contains(Modifier.PUBLIC) || !modifiers.contains(Modifier.STATIC) || !modifiers.contains(Modifier.FINAL)) {
               processingEnv.getMessager().printMessage(Kind.ERROR, "A constant must be public, static and final", element);
           }
        }

       return false;
    }
}

This will raise a compiler error if a field annotated with @Constant isn't public static final.

The problem is, the error won't appear until I save the file. Same with the error going away. If I fix the problem, the error stays until I save the file.

I'm using Eclipse Luna with Java 8u31. Is there any way to prevent this?

John Ericksen
  • 10,995
  • 4
  • 45
  • 75
Vince
  • 14,470
  • 7
  • 39
  • 84
  • What you've provided won't compile - the method is `getMessager`, and you don't return a boolean. – Makoto Apr 13 '15 at 03:44
  • @Makoto Feel free to edit. Wrote this from my phone, had to hop off the comp in the middle of my question /: – Vince Apr 13 '15 at 03:45
  • What's so hard about saving the file? ctrl+S is literally sitting in your left hand. – josephus Apr 13 '15 at 03:46
  • @josephus It's about consistency. Every other compiler error appears instantly (if you import a type, you dont have to save for the error to go away) – Vince Apr 13 '15 at 03:47

2 Answers2

3

The compiler does not run until the file is saved. That is why the compiler error cannot be determined to be fixed (until the file is saved). No, there is nothing to fix (that is the way it is designed to work).

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • 1
    And this applies to nearly any error that isn't a trivial syntax issue caught by the AST. – chrylis -cautiouslyoptimistic- Apr 13 '15 at 03:45
  • What about "type not resolved" errors? After importing, the errors go away without needing to save – Vince Apr 13 '15 at 03:48
  • @VinceEmigh That would be namespaces being imported, also eclipse has its' own compiler. – Elliott Frisch Apr 13 '15 at 03:50
  • What do you mean "that would be namespaces being imported"? I'm wondering how it's able to remove the compile error as soon as the type has been imported. I guess `@Override` is a better example; the error appears instantly if the method isn't actually overriding anything. And I know eclipse has it's own compiler, I'm just not understanding why we don't have the ability to stay consistent with other compile errors. Designing it that way doesn't make much sense. Thanks for the quick response, by the way. – Vince Apr 13 '15 at 03:55
  • 1
    The compiler (and language) *always* uses a fully qualified class-names, imports are processed very early and the compiler sees `java.lang.String` (not `String` and even `java.lang.*` is implicitly imported). As for the annotation based processing, that happens much later in the compilation. Other early things are replacing unicode escaped characters. That's why `String str = "\u0022";` causes a compiler error. – Elliott Frisch Apr 13 '15 at 03:58
  • Importing was a bad example on my part. Isn't `@Override` processed by an annotation processor? Why does `@Override` have the ability to display errors instantly? Is my processor not used in the same way as the one checking `@Override`? – Vince Apr 13 '15 at 04:40
  • @VinceEmigh It is true that there are [Predefined Annotations](https://docs.oracle.com/javase/tutorial/java/annotations/predefined.html), but try modifying the interface of a method from the super class (that has a sub-class with an `@Override`)... notice that the sub-class is error free **until** you save the super class. – Elliott Frisch Apr 14 '15 at 02:08
0

I don't know how to make the error go away without saving (not sure that's possible), but I guess the next best thing is saving your file automatically.

Take a look at these plugins. You can also tweak how often they save.

josephus
  • 8,284
  • 1
  • 37
  • 57