Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to view annotation of java classfile via command line?

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. :(

like image 636
ares Avatar asked Oct 14 '10 12:10

ares


People also ask

Is a command that disassembles a class file?

The javap command disassembles one or more class files. Its output depends on the options used. If no options are used, javap prints out the package, protected, and public fields and methods of the classes passed to it.

How do I view a .class file?

A simple way to see what String literals are used in a ". class" file is to use the javap utility in your JDK installation to dump the file using the "-v" option. Then grep for text that looks like <String "..."> where ... is the String you are looking for.

How do you check if a class has an annotation?

The isAnnotation() method is used to check whether a class object is an annotation. The isAnnotation() method has no parameters and returns a boolean value. If the return value is true , then the class object is an annotation. If the return value is false , then the class object is not an annotation.


1 Answers

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 
like image 165
scalapeno Avatar answered Sep 22 '22 15:09

scalapeno