Path: blob/master/src/jdk.jdeps/share/classes/com/sun/tools/javap/BasicWriter.java
58461 views
/*1* Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.javap;2627import java.io.PrintWriter;2829import com.sun.tools.classfile.AttributeException;30import com.sun.tools.classfile.ConstantPoolException;31import com.sun.tools.classfile.DescriptorException;3233/*34* A writer similar to a PrintWriter but which does not hide exceptions.35* The standard print calls are line-buffered; report calls write messages directly.36*37* <p><b>This is NOT part of any supported API.38* If you write code that depends on this, you do so at your own risk.39* This code and its internal interfaces are subject to change or40* deletion without notice.</b>41*/42public class BasicWriter {43protected BasicWriter(Context context) {44lineWriter = LineWriter.instance(context);45out = context.get(PrintWriter.class);46messages = context.get(Messages.class);47if (messages == null)48throw new AssertionError();49}5051protected void print(String s) {52lineWriter.print(s);53}5455protected void print(Object o) {56lineWriter.print(o == null ? null : o.toString());57}5859protected void println() {60lineWriter.println();61}6263protected void println(String s) {64lineWriter.print(s);65lineWriter.println();66}6768protected void println(Object o) {69lineWriter.print(o == null ? null : o.toString());70lineWriter.println();71}7273protected void indent(int delta) {74lineWriter.indent(delta);75}7677protected void tab() {78lineWriter.tab();79}8081protected void setPendingNewline(boolean b) {82lineWriter.pendingNewline = b;83}8485protected String report(AttributeException e) {86out.println("Error: " + e.getMessage()); // i18n?87return "???";88}8990protected String report(ConstantPoolException e) {91out.println("Error: " + e.getMessage()); // i18n?92return "???";93}9495protected String report(DescriptorException e) {96out.println("Error: " + e.getMessage()); // i18n?97return "???";98}99100protected String report(String msg) {101out.println("Error: " + msg); // i18n?102return "???";103}104105protected String space(int w) {106if (w < spaces.length && spaces[w] != null)107return spaces[w];108109StringBuilder sb = new StringBuilder();110for (int i = 0; i < w; i++)111sb.append(" ");112113String s = sb.toString();114if (w < spaces.length)115spaces[w] = s;116117return s;118}119120private String[] spaces = new String[80];121122private LineWriter lineWriter;123private PrintWriter out;124protected Messages messages;125126private static class LineWriter {127static LineWriter instance(Context context) {128LineWriter instance = context.get(LineWriter.class);129if (instance == null)130instance = new LineWriter(context);131return instance;132}133134protected LineWriter(Context context) {135context.put(LineWriter.class, this);136Options options = Options.instance(context);137indentWidth = options.indentWidth;138tabColumn = options.tabColumn;139out = context.get(PrintWriter.class);140buffer = new StringBuilder();141}142143protected void print(String s) {144if (pendingNewline) {145println();146pendingNewline = false;147}148if (s == null)149s = "null";150for (int i = 0; i < s.length(); i++) {151char c = s.charAt(i);152switch (c) {153case ' ':154pendingSpaces++;155break;156157case '\n':158println();159break;160161default:162if (buffer.length() == 0)163indent();164if (pendingSpaces > 0) {165for (int sp = 0; sp < pendingSpaces; sp++)166buffer.append(' ');167pendingSpaces = 0;168}169buffer.append(c);170}171}172173}174175protected void println() {176// ignore/discard pending spaces177pendingSpaces = 0;178out.println(buffer);179buffer.setLength(0);180}181182protected void indent(int delta) {183indentCount += delta;184}185186protected void tab() {187int col = indentCount * indentWidth + tabColumn;188pendingSpaces += (col <= buffer.length() ? 1 : col - buffer.length());189}190191private void indent() {192pendingSpaces += (indentCount * indentWidth);193}194195private final PrintWriter out;196private final StringBuilder buffer;197private int indentCount;198private final int indentWidth;199private final int tabColumn;200private boolean pendingNewline;201private int pendingSpaces;202}203}204205206207