Annotation attribute must be a class literal? Why? Constants should be fine too

后端 未结 5 831
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-13 18:55

Can someone explain why String and Class annotation parameters are expected differently? Why does the compiler require literals for Classes, wherby accepting constants for Strin

5条回答
  •  庸人自扰
    2021-02-13 19:27

    Look this:

    public static final Class TEST_EXCEPTION = RuntimeException.class;
    

    The class should be a Throwable that the compiler is able to check. So, try this:

    public static final Class TEST_EXCEPTION = RuntimeException.class;
    

提交回复
热议问题