4

I am making a Test annotation, and I want to be able for the user to specify an expected return value.

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Test {
    Object value() default null;
    Class<? extends Throwable> exception() default NoException.class;
    int timeout() default 0;
}

As I know, you cannot specify Object as the return type in annotation methods.

I am wondering if there is a return type which allows primitives AND strings to be returned.

If not, is it possible to create? Thank you.

2

1 Answer 1

7

In short no. The oracle docs explain this

Return types are restricted to primitives, String, Class, enums, annotations, and arrays of the preceding types. Methods can have default values

See here for the Oracle documents on this.

If you could pass an object what would be the syntax? The items that are passed have to be constants which is why there is the restriction

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

1 Comment

Question clearly specifies that he wants "primitives AND strings". So no class instances (other than Strings). I agree that it should be possible somehow. Too bad..

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.