2

I want to have an Integer Array as input in annotation interface, something like this.

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface CheckAccess {

    AccessType accessType() default AccessType.ALL;
    Integer[] permissions();

}

so that while providing the input, I can use some constant having integer values. Like this:-

@CheckAccess(permissions={CAN_READ, CAN_WRITE})

CAN_READ = 1;
CAN_WRITE=2;

How can I achieve this? Because when I set Integer[] it throws compilation error but works fine for String[].

8
  • Which compilation error? Commented Apr 2, 2018 at 10:22
  • Error:(15, 12) java: invalid type for element {0} of annotation type Commented Apr 2, 2018 at 10:23
  • You should use an enum. Commented Apr 2, 2018 at 10:25
  • 1
    Is there no way it could accept Integer[]? I mean it is a proper class not a primitive. Commented Apr 2, 2018 at 10:25
  • 2
    @DaveRanjan I think switching to int[] is the simplest solution. Commented Apr 2, 2018 at 10:31

1 Answer 1

3

The Java compiler (at least the one in my Eclipse) gives the following error message:

Invalid type Integer[] for the annotation attribute permissions;
only primitive type, String, Class, annotation, enumeration are permitted or one-dimensional arrays thereof

Therefore you need to replace Integer[] by int[], like this:

int[] permissions() default 0;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks it solved my problem, but question is Integer is also a Class so it should be allowed right? I mean if String[] is what makes Integer[] so different?
You misunderstood what is meant by Class here. It is an annotation attribute of type Class, lwith value like for example String.class or Object.class.

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.