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

Fri, 19 Jun 2009 11:40:47 -0700

author
jjg
date
Fri, 19 Jun 2009 11:40:47 -0700
changeset 300
ed989c347b3c
parent 283
cd0630109de5
child 348
743f17b55b44
permissions
-rw-r--r--

6852856: javap changes to facilitate subclassing javap for variants
Reviewed-by: mcimadamore

     1 /*
     2  * Copyright 2007-2008 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    26 package com.sun.tools.javap;
    28 import java.io.PrintWriter;
    30 import com.sun.tools.classfile.AttributeException;
    31 import com.sun.tools.classfile.ConstantPoolException;
    32 import com.sun.tools.classfile.DescriptorException;
    34 /*
    35  *  A writer similar to a PrintWriter but which does not hide exceptions.
    36  *  The standard print calls are line-buffered; report calls write messages directly.
    37  *
    38  *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    39  *  you write code that depends on this, you do so at your own risk.
    40  *  This code and its internal interfaces are subject to change or
    41  *  deletion without notice.</b>
    42  */
    43 public class BasicWriter {
    44     protected BasicWriter(Context context) {
    45         lineWriter = LineWriter.instance(context);
    46         out = context.get(PrintWriter.class);
    47         messages = context.get(Messages.class);
    48         if (messages == null)
    49             throw new AssertionError();
    50     }
    52     protected void print(String s) {
    53         lineWriter.print(s);
    54     }
    56     protected void print(Object o) {
    57         lineWriter.print(o == null ? null : o.toString());
    58     }
    60     protected void println() {
    61         lineWriter.println();
    62     }
    64     protected void println(String s) {
    65         lineWriter.print(s);
    66         lineWriter.println();
    67     }
    69     protected void println(Object o) {
    70         lineWriter.print(o == null ? null : o.toString());
    71         lineWriter.println();
    72     }
    74     protected String report(AttributeException e) {
    75         out.println("Error: " + e.getMessage()); // i18n?
    76         return "???";
    77     }
    79     protected String report(ConstantPoolException e) {
    80         out.println("Error: " + e.getMessage()); // i18n?
    81         return "???";
    82     }
    84     protected String report(DescriptorException e) {
    85         out.println("Error: " + e.getMessage()); // i18n?
    86         return "???";
    87     }
    89     protected String report(String msg) {
    90         out.println("Error: " + msg); // i18n?
    91         return "???";
    92     }
    94     protected String space(int w) {
    95         if (w < spaces.length && spaces[w] != null)
    96             return spaces[w];
    98         StringBuilder sb = new StringBuilder();
    99         for (int i = 0; i < w; i++)
   100             sb.append(" ");
   102         String s = sb.toString();
   103         if (w < spaces.length)
   104             spaces[w] = s;
   106         return s;
   107     }
   109     private String[] spaces = new String[80];
   111     private LineWriter lineWriter;
   112     private PrintWriter out;
   113     protected Messages messages;
   115     private static class LineWriter {
   116         static LineWriter instance(Context context) {
   117             LineWriter instance = context.get(LineWriter.class);
   118             if (instance == null)
   119                 instance = new LineWriter(context);
   120             return instance;
   121         }
   123         protected LineWriter(Context context) {
   124             context.put(LineWriter.class, this);
   125             out = context.get(PrintWriter.class);
   126             buffer = new StringBuilder();
   127         }
   129         protected void print(String s) {
   130             if (s == null)
   131                 s = "null";
   132             for (int i = 0; i < s.length(); i++) {
   133                 char c = s.charAt(i);
   134                 if (c == '\n') {
   135                     println();
   136                 } else {
   137                     buffer.append(c);
   138                 }
   139             }
   141         }
   143         protected void println() {
   144             out.println(buffer);
   145             buffer.setLength(0);
   146         }
   148         private PrintWriter out;
   149         private StringBuilder buffer;
   150     }
   151 }

mercurial