langtools/src/share/classes/com/sun/tools/javac/util/DiagnosticSource.java
author jjg
Thu, 10 Jun 2010 16:08:01 -0700
changeset 5847 1908176fd6e3
parent 5520 86e4b9a9da40
child 11055 ec1418effa77
permissions -rw-r--r--
6944312: Potential rebranding issues in openjdk/langtools repository sources Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3995
diff changeset
     2
 * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
     4
 *
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3995
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3995
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    10
 *
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    15
 * accompanied this code).
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    16
 *
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3995
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3995
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3995
diff changeset
    23
 * questions.
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    24
 */
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    25
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    26
package com.sun.tools.javac.util;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    27
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    28
import java.io.IOException;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    29
import java.lang.ref.SoftReference;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    30
import java.nio.CharBuffer;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    31
import java.util.Map;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    32
import javax.tools.JavaFileObject;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    33
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    34
import com.sun.tools.javac.file.JavacFileManager;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    35
import com.sun.tools.javac.tree.JCTree;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    36
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    37
import static com.sun.tools.javac.util.LayoutCharacters.*;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    38
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    39
/**
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    40
 * A simple abstraction of a source file, as needed for use in a diagnostic message.
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    41
 * Provides access to the line and position in a line for any given character offset.
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    42
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    43
 *  <p><b>This is NOT part of any supported API.
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    44
 *  If you write code that depends on this, you do so at your own risk.
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    45
 *  This code and its internal interfaces are subject to change or
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    46
 *  deletion without notice.</b>
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    47
 */
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    48
public class DiagnosticSource {
3144
202fa249dc34 6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents: 1109
diff changeset
    49
202fa249dc34 6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents: 1109
diff changeset
    50
    /* constant DiagnosticSource to be used when sourcefile is missing */
202fa249dc34 6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents: 1109
diff changeset
    51
    public static final DiagnosticSource NO_SOURCE = new DiagnosticSource() {
202fa249dc34 6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents: 1109
diff changeset
    52
        @Override
202fa249dc34 6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents: 1109
diff changeset
    53
        protected boolean findLine(int pos) {
202fa249dc34 6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents: 1109
diff changeset
    54
            return false;
202fa249dc34 6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents: 1109
diff changeset
    55
        }
202fa249dc34 6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents: 1109
diff changeset
    56
    };
202fa249dc34 6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents: 1109
diff changeset
    57
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    58
    public DiagnosticSource(JavaFileObject fo, AbstractLog log) {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    59
        this.fileObject = fo;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    60
        this.log = log;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    61
    }
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    62
3144
202fa249dc34 6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents: 1109
diff changeset
    63
    private DiagnosticSource() {}
202fa249dc34 6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents: 1109
diff changeset
    64
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    65
    /** Return the underlying file object handled by this
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    66
     *  DiagnosticSource object.
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    67
     */
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    68
    public JavaFileObject getFile() {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    69
        return fileObject;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    70
    }
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    71
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    72
    /** Return the one-based line number associated with a given pos
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    73
     * for the current source file.  Zero is returned if no line exists
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    74
     * for the given position.
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    75
     */
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    76
    public int getLineNumber(int pos) {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    77
        try {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    78
            if (findLine(pos)) {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    79
                return line;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    80
            }
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    81
            return 0;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    82
        } finally {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    83
            buf = null;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    84
        }
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    85
    }
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    86
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    87
    /** Return the one-based column number associated with a given pos
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    88
     * for the current source file.  Zero is returned if no column exists
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    89
     * for the given position.
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    90
     */
1109
853d8c191eac 6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents: 867
diff changeset
    91
    public int getColumnNumber(int pos, boolean expandTabs) {
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    92
        try {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    93
            if (findLine(pos)) {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    94
                int column = 0;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    95
                for (int bp = lineStart; bp < pos; bp++) {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    96
                    if (bp >= bufLen) {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    97
                        return 0;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
    98
                    }
1109
853d8c191eac 6733837: Recent work on javac diagnostic affected javac output
mcimadamore
parents: 867
diff changeset
    99
                    if (buf[bp] == '\t' && expandTabs) {
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   100
                        column = (column / TabInc * TabInc) + TabInc;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   101
                    } else {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   102
                        column++;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   103
                    }
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   104
                }
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   105
                return column + 1; // positions are one-based
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   106
            }
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   107
            return 0;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   108
        } finally {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   109
            buf = null;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   110
        }
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   111
    }
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   112
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   113
    /** Return the content of the line containing a given pos.
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   114
     */
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   115
    public String getLine(int pos) {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   116
        try {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   117
            if (!findLine(pos))
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   118
                return null;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   119
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   120
            int lineEnd = lineStart;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   121
            while (lineEnd < bufLen && buf[lineEnd] != CR && buf[lineEnd] != LF)
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   122
                lineEnd++;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   123
            if (lineEnd - lineStart == 0)
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   124
                return null;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   125
            return new String(buf, lineStart, lineEnd - lineStart);
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   126
        } finally {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   127
            buf = null;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   128
        }
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   129
    }
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   130
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   131
    public Map<JCTree, Integer> getEndPosTable() {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   132
        return endPosTable;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   133
    }
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   134
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   135
    public void setEndPosTable(Map<JCTree, Integer> t) {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   136
        if (endPosTable != null && endPosTable != t)
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   137
            throw new IllegalStateException("endPosTable already set");
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   138
        endPosTable = t;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   139
    }
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   140
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   141
    /** Find the line in the buffer that contains the current position
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   142
     * @param pos      Character offset into the buffer
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   143
     */
3144
202fa249dc34 6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
mcimadamore
parents: 1109
diff changeset
   144
    protected boolean findLine(int pos) {
867
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   145
        if (pos == Position.NOPOS)
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   146
            return false;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   147
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   148
        try {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   149
            // try and recover buffer from soft reference cache
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   150
            if (buf == null && refBuf != null)
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   151
                buf = refBuf.get();
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   152
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   153
            if (buf == null) {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   154
                buf = initBuf(fileObject);
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   155
                lineStart = 0;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   156
                line = 1;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   157
            } else if (lineStart > pos) { // messages don't come in order
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   158
                lineStart = 0;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   159
                line = 1;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   160
            }
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   161
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   162
            int bp = lineStart;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   163
            while (bp < bufLen && bp < pos) {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   164
                switch (buf[bp++]) {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   165
                case CR:
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   166
                    if (bp < bufLen && buf[bp] == LF) bp++;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   167
                    line++;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   168
                    lineStart = bp;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   169
                    break;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   170
                case LF:
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   171
                    line++;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   172
                    lineStart = bp;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   173
                    break;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   174
                }
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   175
            }
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   176
            return bp <= bufLen;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   177
        } catch (IOException e) {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   178
            log.directError("source.unavailable");
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   179
            buf = new char[0];
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   180
            return false;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   181
        }
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   182
    }
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   183
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   184
    protected char[] initBuf(JavaFileObject fileObject) throws IOException {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   185
        char[] buf;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   186
        CharSequence cs = fileObject.getCharContent(true);
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   187
        if (cs instanceof CharBuffer) {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   188
            CharBuffer cb = (CharBuffer) cs;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   189
            buf = JavacFileManager.toArray(cb);
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   190
            bufLen = cb.limit();
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   191
        } else {
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   192
            buf = cs.toString().toCharArray();
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   193
            bufLen = buf.length;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   194
        }
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   195
        refBuf = new SoftReference<char[]>(buf);
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   196
        return buf;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   197
    }
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   198
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   199
    /** The underlying file object. */
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   200
    protected JavaFileObject fileObject;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   201
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   202
    protected Map<JCTree, Integer> endPosTable;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   203
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   204
    /** A soft reference to the content of the file object. */
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   205
    protected SoftReference<char[]> refBuf;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   206
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   207
    /** A temporary hard reference to the content of the file object. */
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   208
    protected char[] buf;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   209
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   210
    /** The length of the content. */
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   211
    protected int bufLen;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   212
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   213
    /** The start of a line found by findLine. */
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   214
    protected int lineStart;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   215
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   216
    /** The line number of a line found by findLine. */
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   217
    protected int line;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   218
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   219
    /** A log for reporting errors, such as errors accessing the content. */
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   220
    protected AbstractLog log;
1dff24b5f407 6724071: refactor Log into a front end and back end
jjg
parents:
diff changeset
   221
}