0

I've created an annotation:

@Target(ElementType.FIELD )
@Retention( RetentionPolicy.RUNTIME )
public @interface Test
{
    Class something();
}

But when I call it with Integer.TYPE (for the return type of an int), it shows the error.

public class TestA
{
    @Test(Integer.TYPE)
    public int id;
}
Don Rhummy
  • 24,730
  • 42
  • 175
  • 330

2 Answers2

1

Class is not commensurate with the value Integer.TYPE

It is a compile-time error if the element type is not commensurate with the element value. An element type T is commensurate with an element value V if and only if one of the following is true:

  • [...]

  • If T is Class or an invocation of Class (§4.5), then V is a class literal (§15.8.2).

From the source code of Integer

public static final Class<Integer>  TYPE = (Class<Integer>) Class.getPrimitiveClass("int");

The expression Integer#TYPE is not a class literal. Integer.class or int.class would work, if that's what you're after.

Savior
  • 3,225
  • 4
  • 24
  • 48
1

Try using int.class instead of Integer.TYPE:

@Test(int.class)
FThompson
  • 28,352
  • 13
  • 60
  • 93