< prev index next >

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

Print this page

 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 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.io.PrintWriter;
 29 import java.lang.classfile.AccessFlags;
 30 import java.lang.reflect.AccessFlag;
 31 import java.lang.reflect.ClassFileFormatVersion;
 32 import java.lang.reflect.Modifier;
 33 import java.util.EnumMap;
 34 import java.util.Map;
 35 import java.util.Set;
 36 import java.util.function.Supplier;
 37 


 38 /*
 39  *  A writer similar to a PrintWriter but which does not hide exceptions.
 40  *  The standard print calls are line-buffered; report calls write messages directly.
 41  *
 42  *  <p><b>This is NOT part of any supported API.
 43  *  If you write code that depends on this, you do so at your own risk.
 44  *  This code and its internal interfaces are subject to change or
 45  *  deletion without notice.</b>
 46  */
 47 public class BasicWriter {
 48 
 49     protected BasicWriter(Context context) {
 50         lineWriter = LineWriter.instance(context);
 51         out = context.get(PrintWriter.class);
 52         messages = context.get(Messages.class);
 53         if (messages == null)
 54             throw new AssertionError();
 55     }
 56 
 57     protected Set<AccessFlag> flagsReportUnknown(AccessFlags flags, ClassFileFormatVersion cffv) {
 58         return maskToAccessFlagsReportUnknown(flags.flagsMask(), flags.location(), cffv);
 59     }
 60 
 61     protected Set<AccessFlag> maskToAccessFlagsReportUnknown(int mask, AccessFlag.Location location, ClassFileFormatVersion cffv) {









 62         try {
 63             return AccessFlag.maskToAccessFlags(mask, location, cffv);
 64         } catch (IllegalArgumentException ex) {
 65             mask &= location.flagsMask(cffv);
 66             report("Access Flags: " + ex.getMessage());
 67             return AccessFlag.maskToAccessFlags(mask, location, cffv);
 68         }
 69     }
 70 
 71     protected void print(String s) {
 72         lineWriter.print(s);
 73     }
 74 
 75     protected void print(Object o) {
 76         lineWriter.print(o == null ? null : o.toString());
 77     }
 78 
 79     protected void print(Supplier<Object> safeguardedCode) {
 80         try {
 81             print(safeguardedCode.get());

 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 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.io.PrintWriter;
 29 import java.lang.classfile.AccessFlags;
 30 import java.lang.reflect.AccessFlag;
 31 import java.lang.reflect.ClassFileFormatVersion;



 32 import java.util.Set;
 33 import java.util.function.Supplier;
 34 
 35 import jdk.internal.reflect.PreviewAccessFlags;
 36 
 37 /*
 38  *  A writer similar to a PrintWriter but which does not hide exceptions.
 39  *  The standard print calls are line-buffered; report calls write messages directly.
 40  *
 41  *  <p><b>This is NOT part of any supported API.
 42  *  If you write code that depends on this, you do so at your own risk.
 43  *  This code and its internal interfaces are subject to change or
 44  *  deletion without notice.</b>
 45  */
 46 public class BasicWriter {
 47 
 48     protected BasicWriter(Context context) {
 49         lineWriter = LineWriter.instance(context);
 50         out = context.get(PrintWriter.class);
 51         messages = context.get(Messages.class);
 52         if (messages == null)
 53             throw new AssertionError();
 54     }
 55 
 56     protected Set<AccessFlag> flagsReportUnknown(AccessFlags flags, ClassFileFormatVersion cffv) {
 57         return maskToAccessFlagsReportUnknown(flags.flagsMask(), flags.location(), cffv);
 58     }
 59 
 60     protected Set<AccessFlag> maskToAccessFlagsReportUnknown(int mask, AccessFlag.Location location, ClassFileFormatVersion cffv) {
 61         if (cffv == null) {
 62             try {
 63                 return PreviewAccessFlags.maskToAccessFlags(mask, location);
 64             } catch (IllegalArgumentException ex) {
 65                 mask &= PreviewAccessFlags.flagsMask(location);
 66                 report("Access Flags: " + ex.getMessage());
 67                 return PreviewAccessFlags.maskToAccessFlags(mask, location);
 68             }
 69         }
 70         try {
 71             return AccessFlag.maskToAccessFlags(mask, location, cffv);
 72         } catch (IllegalArgumentException ex) {
 73             mask &= location.flagsMask(cffv);
 74             report("Access Flags: " + ex.getMessage());
 75             return AccessFlag.maskToAccessFlags(mask, location, cffv);
 76         }
 77     }
 78 
 79     protected void print(String s) {
 80         lineWriter.print(s);
 81     }
 82 
 83     protected void print(Object o) {
 84         lineWriter.print(o == null ? null : o.toString());
 85     }
 86 
 87     protected void print(Supplier<Object> safeguardedCode) {
 88         try {
 89             print(safeguardedCode.get());
< prev index next >