0

I am new to annotation processor, I wonder if it is possible to get the the return value of a method marked with annotations during annotation processing. For example, I have annotation

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Parameters {}

and I have a class marked with this annotation:

@Parameters
public static Collection<Object[]> data() {
    return Arrays.asList(new Object[][] {
            { 0, 0, 0 }, { 1, 1, 2 }
    });
}

is it possible I get new Object[][] { { 0, 0, 0 }, { 1, 1, 2 } } using annotation processing tool(apt)?

1 Answer 1

0

Yes, you can invoke your method with reflection by filtering on the list of methods available in your class and annotated with your custom annotation. Your code will look something like this:

Object[][] objectArray; // should be initialize
Class<?> clazz = object.getClass();
for (Method method : clazz.getDeclaredMethods()) {
    if (method.isAnnotationPresent(Parameters.class)) {
        objectArray = method.invoke(object);
    }
}

return objectArray;

Where object is an instance of your class containing the your method.

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

2 Comments

is it possible to use annotation processor tool(apt)?

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.