29

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, is there an equivalent command that can be run on a jar file for a specific class contained within?

I've googled this for a while and had no luck. :(

Amanda S
  • 3,266
  • 4
  • 33
  • 45
ares
  • 709
  • 6
  • 13

2 Answers2

35

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
Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
scalapeno
  • 833
  • 1
  • 6
  • 13
13

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

mernst
  • 7,437
  • 30
  • 45
fasseg
  • 17,504
  • 8
  • 62
  • 73
  • Thanks for this. I was playing with the tools in the JDK/bin directory and found this one. This doesn't seem to display the annotation though but I can see it through a text editor: "RuntimeVisibleAnnotations^A^@!L/VersionID;^A^@^Evalue^A^@^P$Revision: 1.5 $" – ares Oct 14 '10 at 13:30
  • I should add this annotation has @Retention(RetentionPolicy.RUNTIME) as you specified. – ares Oct 14 '10 at 13:31
  • 1
    Does one need to run `javap` with any additional flags for the annotations to show up? Not seeing them when I run `javap java.lang.String`. – Amanda S Aug 22 '11 at 18:04
  • @amanda: check the update in the answer, did you run javap with jdk < 7? – fasseg Aug 23 '11 at 09:00
  • 6
    The `-v` option was required for me (in addition to jdk 7.) – phs Dec 02 '13 at 07:05
  • @fasseg Can you let me know what if the annotation is of type `RetentionPolicy.CLASS` ? As per the javadoc Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time. It says the annotation are to be recorded in the class file. But on decompiling using javap I am not seeing the annotation. For my testing I was just using `@VisibleForTesting` annotation from Guava. – tuk Feb 13 '18 at 16:45