Why does @FunctionalInterface have a RUNTIME retention?

前端 未结 3 1342
误落风尘
误落风尘 2020-12-11 15:43

Said in Javadoc:

If a type is annotated with this annotation type, compilers are required to generate an error message unless ...

3条回答
  •  情书的邮戳
    2020-12-11 15:56

    @FunctionalInterface is for runtime reflection, compile check, and java runtime process probably.

    javap is used to de-compile and compare two interfaces, one with @FunctionalInterface, the other none.

    Just extra two lines byte code in @FunctionalInterface tagged interface:

    Constant pool:
       #7 = ...   RuntimeVisibleAnnotations
       #8 = ...   Ljava/lang/FunctionalInterface;
    

    And both implementation/lambda express are same at byte code level.

    Except for interface reflection:

    X.class.getAnnotation(FunctionalInterface.class) == null?;
    

提交回复
热议问题