How to view annotation of java classfile via command line?

后端 未结 2 368
无人及你
无人及你 2020-12-14 16:41

Is there a command line tool, preferrably in the JDK, that either prints all annotations in a classfile or takes a specific annotation as argument to print?

If so, i

相关标签:
2条回答
  • 2020-12-14 17:26

    Correct me if im wrong, but I thought, that annotations are stripped from the classes by the compiler, unless you use the Annotation @Retention(RetentionPolicy.RUNTIME) on the Annotations itself, the information will not be preserved in the class file. If the annotations are preserved in the classfile, you can use javap (part of the jdk) to see those:

    javap my.package.MyClass
    

    Update: This seems to need JDK7, JDK6's javap doesnt print the annotations, but you can use the following tools from the University of Washington's website to extract the Annotation Information:

    Annotation-utilities

    0 讨论(0)
  • 2020-12-14 17:40

    Using the -verbose or -v flag with javap version 1.7 or later will show the retained annotations.

    Example:

    javap -p -v ./services/target/windup-web-services/WEB-INF/classes/org/jboss/windup/web/services/model/RegisteredApplication.class
    
     #50 = Utf8   Ljavax/persistence/Column;
     #64 = Utf8   Lorg/jboss/windup/web/services/validators/NotBlankConstraint;
     #67 = Utf8   Ljavax/validation/constraints/Size;
    ...
    private java.lang.String title;
      descriptor: Ljava/lang/String;
      flags: ACC_PRIVATE
      RuntimeVisibleAnnotations:
        0: #50(#62=I#63)
        1: #64(#65=s#66)
        2: #67(#68=I#69,#70=I#63,#65=s#71
    
    0 讨论(0)
提交回复
热议问题