21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.tools.javap;
27
28 import java.lang.reflect.AccessFlag;
29 import java.net.URI;
30 import java.text.DateFormat;
31 import java.util.Collection;
32 import java.util.Date;
33 import java.util.EnumSet;
34 import java.util.List;
35 import java.util.Set;
36
37 import java.lang.constant.ClassDesc;
38 import java.lang.reflect.Modifier;
39 import java.util.ArrayList;
40 import java.util.LinkedHashSet;
41 import java.lang.classfile.AccessFlags;
42 import java.lang.classfile.Attributes;
43 import java.lang.classfile.ClassModel;
44 import java.lang.classfile.ClassSignature;
45 import java.lang.classfile.ClassFile;
46 import static java.lang.classfile.ClassFile.*;
47 import java.lang.classfile.ClassHierarchyResolver;
48 import java.lang.classfile.constantpool.*;
49 import java.lang.classfile.FieldModel;
50 import java.lang.classfile.MethodModel;
51 import java.lang.classfile.MethodSignature;
52 import java.lang.classfile.Signature;
53 import java.lang.classfile.attribute.CodeAttribute;
54 import java.lang.classfile.attribute.SignatureAttribute;
55
56 /*
57 * The main javap class to write the contents of a class file as text.
58 *
59 * <p><b>This is NOT part of any supported API.
60 * If you write code that depends on this, you do so at your own risk.
134 println("Last modified " + df.format(lm));
135 }
136 } else if (size > 0) {
137 println("Size " + size + " bytes");
138 }
139 if (digestName != null && digest != null) {
140 StringBuilder sb = new StringBuilder();
141 for (byte b: digest)
142 sb.append(String.format("%02x", b));
143 println(digestName + " checksum " + sb);
144 }
145 }
146
147 cm.findAttribute(Attributes.sourceFile()).ifPresent(sfa ->
148 println("Compiled from \"" + sfa.sourceFile().stringValue() + "\""));
149
150 if (options.sysInfo || options.verbose) {
151 indent(-1);
152 }
153
154 writeModifiers(getClassModifiers(cm.flags()));
155
156 if ((classModel.flags().flagsMask() & ACC_MODULE) != 0) {
157 var attr = classModel.findAttribute(Attributes.module());
158 if (attr.isPresent()) {
159 var modAttr = attr.get();
160 if ((modAttr.moduleFlagsMask() & ACC_OPEN) != 0) {
161 print("open ");
162 }
163 print("module ");
164 print(() -> modAttr.moduleName().name().stringValue());
165 if (modAttr.moduleVersion().isPresent()) {
166 print("@");
167 print(() -> modAttr.moduleVersion().get().stringValue());
168 }
169 } else {
170 // fallback for malformed class files
171 print("class ");
172 print(() -> getJavaName(classModel.thisClass().asInternalName()));
173 }
174 } else {
780 case '\r': return "\\r";
781 case '\\': return "\\\\";
782 case '\'': return "\\'";
783 case '\"': return "\\\"";
784 default: return String.format("\\u%04x", (int) c);
785 }
786 }
787
788 private Set<String> getClassModifiers(AccessFlags flags) {
789 var flagSet = flagsReportUnknown(flags);
790 Set<AccessFlag> set;
791 if (flagSet.contains(AccessFlag.INTERFACE)) {
792 set = EnumSet.copyOf(flagSet);
793 set.remove(AccessFlag.ABSTRACT);
794 } else {
795 set = flagSet;
796 }
797 return getModifiers(set);
798 }
799
800 private static Set<String> getModifiers(Set<java.lang.reflect.AccessFlag> flags) {
801 Set<String> s = new LinkedHashSet<>();
802 for (var f : flags)
803 if (f.sourceModifier()) s.add(Modifier.toString(f.mask()));
804 return s;
805 }
806
807 private Set<String> getClassFlags(AccessFlags flags) {
808 return getFlags(flags.flagsMask(), flagsReportUnknown(flags));
809 }
810
811 private static Set<String> getFlags(int mask, Set<java.lang.reflect.AccessFlag> flags) {
812 Set<String> s = new LinkedHashSet<>();
813 for (var f: flags) {
814 s.add("ACC_" + f.name());
815 mask = mask & ~f.mask();
816 }
817 while (mask != 0) {
818 int bit = Integer.highestOneBit(mask);
819 s.add("0x" + Integer.toHexString(bit));
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.tools.javap;
27
28 import java.lang.reflect.AccessFlag;
29 import java.net.URI;
30 import java.text.DateFormat;
31 import java.util.Collection;
32 import java.util.Date;
33 import java.util.EnumSet;
34 import java.util.List;
35 import java.util.Set;
36
37 import java.lang.constant.ClassDesc;
38 import java.lang.reflect.Modifier;
39 import java.util.ArrayList;
40 import java.util.LinkedHashSet;
41
42 import com.sun.tools.javac.code.Source;
43 import java.lang.classfile.AccessFlags;
44 import java.lang.classfile.Attributes;
45 import java.lang.classfile.ClassModel;
46 import java.lang.classfile.ClassSignature;
47 import java.lang.classfile.ClassFile;
48 import static java.lang.classfile.ClassFile.*;
49 import java.lang.classfile.ClassHierarchyResolver;
50 import java.lang.classfile.constantpool.*;
51 import java.lang.classfile.FieldModel;
52 import java.lang.classfile.MethodModel;
53 import java.lang.classfile.MethodSignature;
54 import java.lang.classfile.Signature;
55 import java.lang.classfile.attribute.CodeAttribute;
56 import java.lang.classfile.attribute.SignatureAttribute;
57
58 /*
59 * The main javap class to write the contents of a class file as text.
60 *
61 * <p><b>This is NOT part of any supported API.
62 * If you write code that depends on this, you do so at your own risk.
136 println("Last modified " + df.format(lm));
137 }
138 } else if (size > 0) {
139 println("Size " + size + " bytes");
140 }
141 if (digestName != null && digest != null) {
142 StringBuilder sb = new StringBuilder();
143 for (byte b: digest)
144 sb.append(String.format("%02x", b));
145 println(digestName + " checksum " + sb);
146 }
147 }
148
149 cm.findAttribute(Attributes.sourceFile()).ifPresent(sfa ->
150 println("Compiled from \"" + sfa.sourceFile().stringValue() + "\""));
151
152 if (options.sysInfo || options.verbose) {
153 indent(-1);
154 }
155
156 writeModifiers(getClassModifiers(cm.flags(), classModel.majorVersion(), classModel.minorVersion()));
157
158 if ((classModel.flags().flagsMask() & ACC_MODULE) != 0) {
159 var attr = classModel.findAttribute(Attributes.module());
160 if (attr.isPresent()) {
161 var modAttr = attr.get();
162 if ((modAttr.moduleFlagsMask() & ACC_OPEN) != 0) {
163 print("open ");
164 }
165 print("module ");
166 print(() -> modAttr.moduleName().name().stringValue());
167 if (modAttr.moduleVersion().isPresent()) {
168 print("@");
169 print(() -> modAttr.moduleVersion().get().stringValue());
170 }
171 } else {
172 // fallback for malformed class files
173 print("class ");
174 print(() -> getJavaName(classModel.thisClass().asInternalName()));
175 }
176 } else {
782 case '\r': return "\\r";
783 case '\\': return "\\\\";
784 case '\'': return "\\'";
785 case '\"': return "\\\"";
786 default: return String.format("\\u%04x", (int) c);
787 }
788 }
789
790 private Set<String> getClassModifiers(AccessFlags flags) {
791 var flagSet = flagsReportUnknown(flags);
792 Set<AccessFlag> set;
793 if (flagSet.contains(AccessFlag.INTERFACE)) {
794 set = EnumSet.copyOf(flagSet);
795 set.remove(AccessFlag.ABSTRACT);
796 } else {
797 set = flagSet;
798 }
799 return getModifiers(set);
800 }
801
802 private Set<String> getClassModifiers(AccessFlags flags, int majorVersion, int minorVersion) {
803 boolean previewClassFile = minorVersion == ClassFile.PREVIEW_MINOR_VERSION;
804 Set<AccessFlag> flagSet = flagsReportUnknown(flags);
805 if (flagSet.contains(AccessFlag.INTERFACE)) {
806 flagSet = EnumSet.copyOf(flagSet);
807 flagSet.remove(AccessFlag.ABSTRACT);
808 } else if (Source.isSupported(Source.Feature.VALUE_CLASSES, majorVersion) && previewClassFile) {
809 Set<String> classModifers = getModifiers(flagSet);
810 classModifers.add("value");
811 return classModifers;
812 }
813 return getModifiers(flagSet);
814 }
815
816 private static Set<String> getModifiers(Set<java.lang.reflect.AccessFlag> flags) {
817 Set<String> s = new LinkedHashSet<>();
818 for (var f : flags)
819 if (f.sourceModifier()) s.add(Modifier.toString(f.mask()));
820 return s;
821 }
822
823 private Set<String> getClassFlags(AccessFlags flags) {
824 return getFlags(flags.flagsMask(), flagsReportUnknown(flags));
825 }
826
827 private static Set<String> getFlags(int mask, Set<java.lang.reflect.AccessFlag> flags) {
828 Set<String> s = new LinkedHashSet<>();
829 for (var f: flags) {
830 s.add("ACC_" + f.name());
831 mask = mask & ~f.mask();
832 }
833 while (mask != 0) {
834 int bit = Integer.highestOneBit(mask);
835 s.add("0x" + Integer.toHexString(bit));
|