Skip to content

Instantly share code, notes, and snippets.

Last active Jul 20, 2018
What would you like to do?
Code snapshot for stackoverflow question 51358016
public @interface Anno {
String value();
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.TypeElement;
public class HelloWorldProcessor extends AbstractProcessor {
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (!roundEnv.processingOver()) {
try {
FileObject resource = processingEnv.getFiler().createSourceFile("HelloWorldMessage");
PrintWriter ps = new PrintWriter(resource.openWriter());
ps.println("public interface HelloWorldMessage { String HELLO_WORLD = \"Hello World\";}");
} catch (IOException e) {
// Ignore, it may throw resource already exist exception which is not matter
return false;
public class UseHelloWorld {
public static final String HW = HelloWorldMessage.HELLO_WORLD;
@Anno(UseHelloWorld.HW) // This line fails
//@Anno(HelloWorldMessage.HELLO_WORLD) // This line works
public void bean() {
Copy link

XDean commented Jul 19, 2018

File structure


Step to reproduce

  • javac
  • javac -processor HelloWorldProcessor

You can try to comment in UseHelloWorld#4 and comment out UseHelloWorld#5, it will work fine. (Don't forget clean all generated file)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment