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

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 581
f2fdd52e4e87
child 2264
66245d9d84db
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

     1 /*
     2  * Copyright (c) 2007, 2008, Oracle and/or its affiliates. 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.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle 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 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  */
    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 supported API.
    39  *  If 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 void indent(int delta) {
    75         lineWriter.indent(delta);
    76     }
    78     protected void tab() {
    79         lineWriter.tab();
    80     }
    82     protected void setPendingNewline(boolean b) {
    83         lineWriter.pendingNewline = b;
    84     }
    86     protected String report(AttributeException e) {
    87         out.println("Error: " + e.getMessage()); // i18n?
    88         return "???";
    89     }
    91     protected String report(ConstantPoolException e) {
    92         out.println("Error: " + e.getMessage()); // i18n?
    93         return "???";
    94     }
    96     protected String report(DescriptorException e) {
    97         out.println("Error: " + e.getMessage()); // i18n?
    98         return "???";
    99     }
   101     protected String report(String msg) {
   102         out.println("Error: " + msg); // i18n?
   103         return "???";
   104     }
   106     protected String space(int w) {
   107         if (w < spaces.length && spaces[w] != null)
   108             return spaces[w];
   110         StringBuilder sb = new StringBuilder();
   111         for (int i = 0; i < w; i++)
   112             sb.append(" ");
   114         String s = sb.toString();
   115         if (w < spaces.length)
   116             spaces[w] = s;
   118         return s;
   119     }
   121     private String[] spaces = new String[80];
   123     private LineWriter lineWriter;
   124     private PrintWriter out;
   125     protected Messages messages;
   127     private static class LineWriter {
   128         static LineWriter instance(Context context) {
   129             LineWriter instance = context.get(LineWriter.class);
   130             if (instance == null)
   131                 instance = new LineWriter(context);
   132             return instance;
   133         }
   135         protected LineWriter(Context context) {
   136             context.put(LineWriter.class, this);
   137             Options options = Options.instance(context);
   138             indentWidth = options.indentWidth;
   139             tabColumn = options.tabColumn;
   140             out = context.get(PrintWriter.class);
   141             buffer = new StringBuilder();
   142         }
   144         protected void print(String s) {
   145             if (pendingNewline) {
   146                 println();
   147                 pendingNewline = false;
   148             }
   149             if (s == null)
   150                 s = "null";
   151             for (int i = 0; i < s.length(); i++) {
   152                 char c = s.charAt(i);
   153                 switch (c) {
   154                     case '\n':
   155                         println();
   156                         break;
   157                     default:
   158                         if (buffer.length() == 0)
   159                             indent();
   160                         buffer.append(c);
   161                 }
   162             }
   164         }
   166         protected void println() {
   167             out.println(buffer);
   168             buffer.setLength(0);
   169         }
   171         protected void indent(int delta) {
   172             indentCount += delta;
   173         }
   175         protected void tab() {
   176             if (buffer.length() == 0)
   177                 indent();
   178             space(indentCount * indentWidth + tabColumn - buffer.length());
   179         }
   181         private void indent() {
   182             space(indentCount * indentWidth);
   183         }
   185         private void space(int n) {
   186             for (int i = 0; i < n; i++)
   187                 buffer.append(' ');
   188         }
   190         private PrintWriter out;
   191         private StringBuilder buffer;
   192         private int indentCount;
   193         private int indentWidth;
   194         private int tabColumn;
   195         private boolean pendingNewline;
   196     }
   197 }

mercurial