Question

Is there a way to add processors to the compiler without making annotations?

Basically, I would like for the build to fail if a user did not implement an interface correctly (ie. postconditions are not fulfilled). At compile time, I would like to check if a class implements an interface, and if it does, run some code to check if the implementation is correct.

For example, I would like to ensure that classes that implement getErrorMoniker() return a string in camelCase.

public interface MyError {
  public String getErrorMoniker();
}

public class MyErrorImplemented1 {
  @Override
  public String getErrorMoniker() { return "goodErrorMoniker"; }
}

public class MyErrorImplemented2 {
  @Override
  public String getErrorMoniker() {
    return "BADERRORMONIKER"; // I would like a compile error here
  }
}

Any suggestions would be appreciated.

Was it helpful?

Solution

A processor annotated with @SupportedAnnotationTypes("*") should in theory be able to processs all source files as it also applies to an empty set of annotations. From the documentation:

If there are no annotation types present, annotation processing still occurs but only universal processors which support processing "*" can claim the (empty) set of annotation types.

Although you goal to check for return values probably won't work, since this happens compile time, not runtime.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top