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

changeset 46
7708bd6d800d
child 54
eaf608c64fec
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javap/BasicWriter.java	Tue Jun 03 13:26:47 2008 -0700
     1.3 @@ -0,0 +1,131 @@
     1.4 +/*
     1.5 + * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Sun designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Sun in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.26 + * have any questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javap;
    1.30 +
    1.31 +import java.io.PrintWriter;
    1.32 +
    1.33 +import com.sun.tools.classfile.AttributeException;
    1.34 +import com.sun.tools.classfile.ConstantPoolException;
    1.35 +import com.sun.tools.classfile.DescriptorException;
    1.36 +
    1.37 +/*
    1.38 + *  A writer similar to a PrintWriter but which does not hide exceptions.
    1.39 + *  The standard print calls are line-buffered; report calls write messages directly.
    1.40 + *
    1.41 + *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    1.42 + *  you write code that depends on this, you do so at your own risk.
    1.43 + *  This code and its internal interfaces are subject to change or
    1.44 + *  deletion without notice.</b>
    1.45 + */
    1.46 +public class BasicWriter {
    1.47 +    protected BasicWriter(Context context) {
    1.48 +        lineWriter = LineWriter.instance(context);
    1.49 +        out = context.get(PrintWriter.class);
    1.50 +    }
    1.51 +
    1.52 +    protected void print(String s) {
    1.53 +        lineWriter.print(s);
    1.54 +    }
    1.55 +
    1.56 +    protected void print(Object o) {
    1.57 +        lineWriter.print(o == null ? null : o.toString());
    1.58 +    }
    1.59 +
    1.60 +    protected void println() {
    1.61 +        lineWriter.println();
    1.62 +    }
    1.63 +
    1.64 +    protected void println(String s) {
    1.65 +        lineWriter.print(s);
    1.66 +        lineWriter.println();
    1.67 +    }
    1.68 +
    1.69 +    protected void println(Object o) {
    1.70 +        lineWriter.print(o == null ? null : o.toString());
    1.71 +        lineWriter.println();
    1.72 +    }
    1.73 +
    1.74 +    protected String report(AttributeException e) {
    1.75 +        out.println("Error: " + e.getMessage()); // i18n?
    1.76 +        return "???";
    1.77 +    }
    1.78 +
    1.79 +    protected String report(ConstantPoolException e) {
    1.80 +        out.println("Error: " + e.getMessage()); // i18n?
    1.81 +        return "???";
    1.82 +    }
    1.83 +
    1.84 +    protected String report(DescriptorException e) {
    1.85 +        out.println("Error: " + e.getMessage()); // i18n?
    1.86 +        return "???";
    1.87 +    }
    1.88 +
    1.89 +    protected String report(String msg) {
    1.90 +        out.println("Error: " + msg); // i18n?
    1.91 +        return "???";
    1.92 +    }
    1.93 +
    1.94 +    private LineWriter lineWriter;
    1.95 +    private PrintWriter out;
    1.96 +
    1.97 +    private static class LineWriter {
    1.98 +        static LineWriter instance(Context context) {
    1.99 +            LineWriter instance = context.get(LineWriter.class);
   1.100 +            if (instance == null)
   1.101 +                instance = new LineWriter(context);
   1.102 +            return instance;
   1.103 +        }
   1.104 +
   1.105 +        protected LineWriter(Context context) {
   1.106 +            context.put(LineWriter.class, this);
   1.107 +            out = context.get(PrintWriter.class);
   1.108 +            buffer = new StringBuilder();
   1.109 +        }
   1.110 +
   1.111 +        protected void print(String s) {
   1.112 +            if (s == null)
   1.113 +                s = "null";
   1.114 +            for (int i = 0; i < s.length(); i++) {
   1.115 +                char c = s.charAt(i);
   1.116 +                if (c == '\n') {
   1.117 +                    println();
   1.118 +                } else {
   1.119 +                    buffer.append(c);
   1.120 +                }
   1.121 +            }
   1.122 +
   1.123 +        }
   1.124 +
   1.125 +        protected void println() {
   1.126 +            out.println(buffer);
   1.127 +            buffer.setLength(0);
   1.128 +        }
   1.129 +
   1.130 +        private PrintWriter out;
   1.131 +        private StringBuilder buffer;
   1.132 +    }
   1.133 +}
   1.134 +

mercurial