src/share/classes/com/sun/tools/javac/util/DiagnosticSource.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/util/DiagnosticSource.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,220 @@
     1.4 +/*
     1.5 + * Copyright (c) 1999, 2012, Oracle and/or its affiliates. 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.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javac.util;
    1.30 +
    1.31 +import java.io.IOException;
    1.32 +import java.lang.ref.SoftReference;
    1.33 +import java.nio.CharBuffer;
    1.34 +import javax.tools.JavaFileObject;
    1.35 +
    1.36 +import com.sun.tools.javac.file.JavacFileManager;
    1.37 +import com.sun.tools.javac.tree.EndPosTable;
    1.38 +
    1.39 +import static com.sun.tools.javac.util.LayoutCharacters.*;
    1.40 +
    1.41 +/**
    1.42 + * A simple abstraction of a source file, as needed for use in a diagnostic message.
    1.43 + * Provides access to the line and position in a line for any given character offset.
    1.44 + *
    1.45 + *  <p><b>This is NOT part of any supported API.
    1.46 + *  If you write code that depends on this, you do so at your own risk.
    1.47 + *  This code and its internal interfaces are subject to change or
    1.48 + *  deletion without notice.</b>
    1.49 + */
    1.50 +public class DiagnosticSource {
    1.51 +
    1.52 +    /* constant DiagnosticSource to be used when sourcefile is missing */
    1.53 +    public static final DiagnosticSource NO_SOURCE = new DiagnosticSource() {
    1.54 +        @Override
    1.55 +        protected boolean findLine(int pos) {
    1.56 +            return false;
    1.57 +        }
    1.58 +    };
    1.59 +
    1.60 +    public DiagnosticSource(JavaFileObject fo, AbstractLog log) {
    1.61 +        this.fileObject = fo;
    1.62 +        this.log = log;
    1.63 +    }
    1.64 +
    1.65 +    private DiagnosticSource() {}
    1.66 +
    1.67 +    /** Return the underlying file object handled by this
    1.68 +     *  DiagnosticSource object.
    1.69 +     */
    1.70 +    public JavaFileObject getFile() {
    1.71 +        return fileObject;
    1.72 +    }
    1.73 +
    1.74 +    /** Return the one-based line number associated with a given pos
    1.75 +     * for the current source file.  Zero is returned if no line exists
    1.76 +     * for the given position.
    1.77 +     */
    1.78 +    public int getLineNumber(int pos) {
    1.79 +        try {
    1.80 +            if (findLine(pos)) {
    1.81 +                return line;
    1.82 +            }
    1.83 +            return 0;
    1.84 +        } finally {
    1.85 +            buf = null;
    1.86 +        }
    1.87 +    }
    1.88 +
    1.89 +    /** Return the one-based column number associated with a given pos
    1.90 +     * for the current source file.  Zero is returned if no column exists
    1.91 +     * for the given position.
    1.92 +     */
    1.93 +    public int getColumnNumber(int pos, boolean expandTabs) {
    1.94 +        try {
    1.95 +            if (findLine(pos)) {
    1.96 +                int column = 0;
    1.97 +                for (int bp = lineStart; bp < pos; bp++) {
    1.98 +                    if (bp >= bufLen) {
    1.99 +                        return 0;
   1.100 +                    }
   1.101 +                    if (buf[bp] == '\t' && expandTabs) {
   1.102 +                        column = (column / TabInc * TabInc) + TabInc;
   1.103 +                    } else {
   1.104 +                        column++;
   1.105 +                    }
   1.106 +                }
   1.107 +                return column + 1; // positions are one-based
   1.108 +            }
   1.109 +            return 0;
   1.110 +        } finally {
   1.111 +            buf = null;
   1.112 +        }
   1.113 +    }
   1.114 +
   1.115 +    /** Return the content of the line containing a given pos.
   1.116 +     */
   1.117 +    public String getLine(int pos) {
   1.118 +        try {
   1.119 +            if (!findLine(pos))
   1.120 +                return null;
   1.121 +
   1.122 +            int lineEnd = lineStart;
   1.123 +            while (lineEnd < bufLen && buf[lineEnd] != CR && buf[lineEnd] != LF)
   1.124 +                lineEnd++;
   1.125 +            if (lineEnd - lineStart == 0)
   1.126 +                return null;
   1.127 +            return new String(buf, lineStart, lineEnd - lineStart);
   1.128 +        } finally {
   1.129 +            buf = null;
   1.130 +        }
   1.131 +    }
   1.132 +
   1.133 +    public EndPosTable getEndPosTable() {
   1.134 +        return endPosTable;
   1.135 +    }
   1.136 +
   1.137 +    public void setEndPosTable(EndPosTable t) {
   1.138 +        if (endPosTable != null && endPosTable != t)
   1.139 +            throw new IllegalStateException("endPosTable already set");
   1.140 +        endPosTable = t;
   1.141 +    }
   1.142 +
   1.143 +    /** Find the line in the buffer that contains the current position
   1.144 +     * @param pos      Character offset into the buffer
   1.145 +     */
   1.146 +    protected boolean findLine(int pos) {
   1.147 +        if (pos == Position.NOPOS)
   1.148 +            return false;
   1.149 +
   1.150 +        try {
   1.151 +            // try and recover buffer from soft reference cache
   1.152 +            if (buf == null && refBuf != null)
   1.153 +                buf = refBuf.get();
   1.154 +
   1.155 +            if (buf == null) {
   1.156 +                buf = initBuf(fileObject);
   1.157 +                lineStart = 0;
   1.158 +                line = 1;
   1.159 +            } else if (lineStart > pos) { // messages don't come in order
   1.160 +                lineStart = 0;
   1.161 +                line = 1;
   1.162 +            }
   1.163 +
   1.164 +            int bp = lineStart;
   1.165 +            while (bp < bufLen && bp < pos) {
   1.166 +                switch (buf[bp++]) {
   1.167 +                case CR:
   1.168 +                    if (bp < bufLen && buf[bp] == LF) bp++;
   1.169 +                    line++;
   1.170 +                    lineStart = bp;
   1.171 +                    break;
   1.172 +                case LF:
   1.173 +                    line++;
   1.174 +                    lineStart = bp;
   1.175 +                    break;
   1.176 +                }
   1.177 +            }
   1.178 +            return bp <= bufLen;
   1.179 +        } catch (IOException e) {
   1.180 +            log.directError("source.unavailable");
   1.181 +            buf = new char[0];
   1.182 +            return false;
   1.183 +        }
   1.184 +    }
   1.185 +
   1.186 +    protected char[] initBuf(JavaFileObject fileObject) throws IOException {
   1.187 +        char[] buf;
   1.188 +        CharSequence cs = fileObject.getCharContent(true);
   1.189 +        if (cs instanceof CharBuffer) {
   1.190 +            CharBuffer cb = (CharBuffer) cs;
   1.191 +            buf = JavacFileManager.toArray(cb);
   1.192 +            bufLen = cb.limit();
   1.193 +        } else {
   1.194 +            buf = cs.toString().toCharArray();
   1.195 +            bufLen = buf.length;
   1.196 +        }
   1.197 +        refBuf = new SoftReference<char[]>(buf);
   1.198 +        return buf;
   1.199 +    }
   1.200 +
   1.201 +    /** The underlying file object. */
   1.202 +    protected JavaFileObject fileObject;
   1.203 +
   1.204 +    protected EndPosTable endPosTable;
   1.205 +
   1.206 +    /** A soft reference to the content of the file object. */
   1.207 +    protected SoftReference<char[]> refBuf;
   1.208 +
   1.209 +    /** A temporary hard reference to the content of the file object. */
   1.210 +    protected char[] buf;
   1.211 +
   1.212 +    /** The length of the content. */
   1.213 +    protected int bufLen;
   1.214 +
   1.215 +    /** The start of a line found by findLine. */
   1.216 +    protected int lineStart;
   1.217 +
   1.218 +    /** The line number of a line found by findLine. */
   1.219 +    protected int line;
   1.220 +
   1.221 +    /** A log for reporting errors, such as errors accessing the content. */
   1.222 +    protected AbstractLog log;
   1.223 +}

mercurial