我有一个带有简单java类的jar,如下所示
package org.debraj.pkgcmp.dummy;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import com.google.common.annotations.VisibleForTesting;
public class PackageAnnotation {
@VisibleForTesting
public void dummyMethod() {
}
}
用javap反编译类后,注释信息没有显示出来。
dmanna-m01:dummy dmanna$ javap PackageAnnotation.class
Compiled from "PackageAnnotation.java"
public class org.debraj.pkgcmp.dummy.PackageAnnotation {
public org.debraj.pkgcmp.dummy.PackageAnnotation();
public void dummyMethod();
}
即使@VisibleForTesting
是RetentionPolicy.CLASS
类型的注释,有人可以告诉我为什么反编译类中缺少注释信息吗?
dmanna-m01:dummy dmanna$ java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
番石榴版-23.5-jre
我已经看过这个question但是接受的答案在这里似乎没有用。根据Java doc
RetentionPolicy.class - 将在类中记录注释 文件由编译器生成,但在运行时不需要由VM保留。
因此,javap应该能够显示RetentionPolicy.CLASS
的注释信息。
修改
dmanna-m01:dummy dmanna$ javap -c PackageAnnotation.class
Compiled from "PackageAnnotation.java"
public class org.debraj.pkgcmp.dummy.PackageAnnotation {
public org.debraj.pkgcmp.dummy.PackageAnnotation();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public void dummyMethod();
Code:
0: return
}
javap -v
dmanna-m01:dummy dmanna$ javap -v PackageAnnotation.class
Classfile /Users/dmanna/workspaces/java/eclipse/pkgcmp/target/classes/org/debraj/pkgcmp/dummy/PackageAnnotation.class
Last modified Feb 14, 2018; size 489 bytes
MD5 checksum 06949d127e5f631f77060be1b1f09b60
Compiled from "PackageAnnotation.java"
public class org.debraj.pkgcmp.dummy.PackageAnnotation
minor version: 0
major version: 49
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Class #2 // org/debraj/pkgcmp/dummy/PackageAnnotation
#2 = Utf8 org/debraj/pkgcmp/dummy/PackageAnnotation
#3 = Class #4 // java/lang/Object
#4 = Utf8 java/lang/Object
#5 = Utf8 <init>
#6 = Utf8 ()V
#7 = Utf8 Code
#8 = Methodref #3.#9 // java/lang/Object."<init>":()V
#9 = NameAndType #5:#6 // "<init>":()V
#10 = Utf8 LineNumberTable
#11 = Utf8 LocalVariableTable
#12 = Utf8 this
#13 = Utf8 Lorg/debraj/pkgcmp/dummy/PackageAnnotation;
#14 = Utf8 dummyMethod
#15 = Utf8 RuntimeInvisibleAnnotations
#16 = Utf8 Lcom/google/common/annotations/VisibleForTesting;
#17 = Utf8 SourceFile
#18 = Utf8 PackageAnnotation.java
{
public org.debraj.pkgcmp.dummy.PackageAnnotation();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #8 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 5: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Lorg/debraj/pkgcmp/dummy/PackageAnnotation;
public void dummyMethod();
descriptor: ()V
flags: ACC_PUBLIC
RuntimeInvisibleAnnotations:
0: #16()
Code:
stack=0, locals=1, args_size=1
0: return
LineNumberTable:
line 9: 0
LocalVariableTable:
Start Length Slot Name Signature
0 1 0 this Lorg/debraj/pkgcmp/dummy/PackageAnnotation;
}
SourceFile: "PackageAnnotation.java"
编辑2
如果我使用某些RUNTIME保留政策,我仍然没有在javap
中看到注释(例如 - Subscribe)
package org.debraj.pkgcmp.dummy;
import com.google.common.eventbus.Subscribe;
public class PackageAnnotation {
@Subscribe
public void dummyMethod() {
}
}
javap
dmanna-m01:dummy dmanna$ javap PackageAnnotation.class
Compiled from "PackageAnnotation.java"
public class org.debraj.pkgcmp.dummy.PackageAnnotation {
public org.debraj.pkgcmp.dummy.PackageAnnotation();
public void dummyMethod();
}
javap -c
dmanna-m01:dummy dmanna$ javap -c PackageAnnotation.class
Compiled from "PackageAnnotation.java"
public class org.debraj.pkgcmp.dummy.PackageAnnotation {
public org.debraj.pkgcmp.dummy.PackageAnnotation();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public void dummyMethod();
Code:
0: return
}
javap -v
dmanna-m01:dummy dmanna$ javap -v PackageAnnotation.class
Classfile /Users/dmanna/workspaces/java/eclipse/pkgcmp/target/classes/org/debraj/pkgcmp/dummy/PackageAnnotation.class
Last modified Feb 14, 2018; size 476 bytes
MD5 checksum fd8b8e678ed771fa19193b73feae1acb
Compiled from "PackageAnnotation.java"
public class org.debraj.pkgcmp.dummy.PackageAnnotation
minor version: 0
major version: 49
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #3.#16 // java/lang/Object."<init>":()V
#2 = Class #17 // org/debraj/pkgcmp/dummy/PackageAnnotation
#3 = Class #18 // java/lang/Object
#4 = Utf8 <init>
#5 = Utf8 ()V
#6 = Utf8 Code
#7 = Utf8 LineNumberTable
#8 = Utf8 LocalVariableTable
#9 = Utf8 this
#10 = Utf8 Lorg/debraj/pkgcmp/dummy/PackageAnnotation;
#11 = Utf8 dummyMethod
#12 = Utf8 RuntimeVisibleAnnotations
#13 = Utf8 Lcom/google/common/eventbus/Subscribe;
#14 = Utf8 SourceFile
#15 = Utf8 PackageAnnotation.java
#16 = NameAndType #4:#5 // "<init>":()V
#17 = Utf8 org/debraj/pkgcmp/dummy/PackageAnnotation
#18 = Utf8 java/lang/Object
{
public org.debraj.pkgcmp.dummy.PackageAnnotation();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 5: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Lorg/debraj/pkgcmp/dummy/PackageAnnotation;
public void dummyMethod();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=0, locals=1, args_size=1
0: return
LineNumberTable:
line 9: 0
LocalVariableTable:
Start Length Slot Name Signature
0 1 0 this Lorg/debraj/pkgcmp/dummy/PackageAnnotation;
RuntimeVisibleAnnotations:
0: #13()
}
SourceFile: "PackageAnnotation.java"