3

I'm writing an annotation processor that needs to access elements that only exist within an anonymous class. Specifically, accessing a method within an enum anonymous class.

enum Foo {
    BAR {
        @MyAnnotation
        void doSomething() { [...] }
    };

    @MyAnnotation
    void doSomethingElse() { [...] }
}

When calling roundEnv.getElementsAnnotatedWith(MyAnnotation.class) within the annotation process I do not get an element representing the doSomething method. I do however, get an element representing the doSomethingElse method so it seems to be specific to anonymous classes. I've observed the same for field annotations as well within an anonymous enum class.

What am I missing? Is this sort of processing just not supported?

1 Answer 1

3

No, this isn't supported.

In Java, the only information the compiler has to tell it what methods are available on an object is the compile-time type of that object. If a method isn't available on the known type of the object, it's not available as far as the compiler is concerned.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.