< prev index next >

src/jdk.jdeps/share/classes/com/sun/tools/javap/ClassWriter.java

Print this page

 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 {

789             case '\r': return "\\r";
790             case '\\': return "\\\\";
791             case '\'': return "\\'";
792             case '\"': return "\\\"";
793             default:   return String.format("\\u%04x", (int) c);
794         }
795     }
796 
797     private Set<String> getClassModifiers(AccessFlags flags) {
798         var flagSet = flagsReportUnknown(flags);
799         Set<AccessFlag> set;
800         if (flagSet.contains(AccessFlag.INTERFACE)) {
801             set = EnumSet.copyOf(flagSet);
802             set.remove(AccessFlag.ABSTRACT);
803         } else {
804             set = flagSet;
805         }
806         return getModifiers(set);
807     }
808 














809     private static Set<String> getModifiers(Set<java.lang.reflect.AccessFlag> flags) {
810         Set<String> s = new LinkedHashSet<>();
811         for (var f : flags)
812             if (f.sourceModifier()) s.add(Modifier.toString(f.mask()));
813         return s;
814     }
815 
816     private Set<String> getClassFlags(AccessFlags flags) {
817         return getFlags(flags.flagsMask(), flagsReportUnknown(flags));
818     }
819 
820     private static Set<String> getFlags(int mask, Set<java.lang.reflect.AccessFlag> flags) {
821         Set<String> s = new LinkedHashSet<>();
822         for (var f: flags) {
823             s.add("ACC_" + f.name());
824             mask = mask & ~f.mask();
825         }
826         while (mask != 0) {
827             int bit = Integer.highestOneBit(mask);
828             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 {

791             case '\r': return "\\r";
792             case '\\': return "\\\\";
793             case '\'': return "\\'";
794             case '\"': return "\\\"";
795             default:   return String.format("\\u%04x", (int) c);
796         }
797     }
798 
799     private Set<String> getClassModifiers(AccessFlags flags) {
800         var flagSet = flagsReportUnknown(flags);
801         Set<AccessFlag> set;
802         if (flagSet.contains(AccessFlag.INTERFACE)) {
803             set = EnumSet.copyOf(flagSet);
804             set.remove(AccessFlag.ABSTRACT);
805         } else {
806             set = flagSet;
807         }
808         return getModifiers(set);
809     }
810 
811     private Set<String> getClassModifiers(AccessFlags flags, int majorVersion, int minorVersion) {
812         boolean previewClassFile = minorVersion == ClassFile.PREVIEW_MINOR_VERSION;
813         Set<AccessFlag> flagSet = flagsReportUnknown(flags);
814         if (flagSet.contains(AccessFlag.INTERFACE)) {
815             flagSet = EnumSet.copyOf(flagSet);
816             flagSet.remove(AccessFlag.ABSTRACT);
817         } else if (Source.isSupported(Source.Feature.VALUE_CLASSES, majorVersion) && previewClassFile) {
818             Set<String> classModifers = getModifiers(flagSet);
819             classModifers.add("value");
820             return classModifers;
821         }
822         return getModifiers(flagSet);
823     }
824 
825     private static Set<String> getModifiers(Set<java.lang.reflect.AccessFlag> flags) {
826         Set<String> s = new LinkedHashSet<>();
827         for (var f : flags)
828             if (f.sourceModifier()) s.add(Modifier.toString(f.mask()));
829         return s;
830     }
831 
832     private Set<String> getClassFlags(AccessFlags flags) {
833         return getFlags(flags.flagsMask(), flagsReportUnknown(flags));
834     }
835 
836     private static Set<String> getFlags(int mask, Set<java.lang.reflect.AccessFlag> flags) {
837         Set<String> s = new LinkedHashSet<>();
838         for (var f: flags) {
839             s.add("ACC_" + f.name());
840             mask = mask & ~f.mask();
841         }
842         while (mask != 0) {
843             int bit = Integer.highestOneBit(mask);
844             s.add("0x" + Integer.toHexString(bit));
< prev index next >