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

Tue, 25 May 2010 15:54:51 -0700

author
ohair
date
Tue, 25 May 2010 15:54:51 -0700
changeset 554
9d9f26857129
parent 415
49359d0e6a9c
child 581
f2fdd52e4e87
permissions
-rw-r--r--

6943119: Rebrand source copyright notices
Reviewed-by: darcy

     1 /*
     2  * Copyright (c) 1999, 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.javac.util;
    28 import java.io.IOException;
    29 import java.lang.ref.SoftReference;
    30 import java.nio.CharBuffer;
    31 import java.util.Map;
    32 import javax.tools.JavaFileObject;
    34 import com.sun.tools.javac.file.JavacFileManager;
    35 import com.sun.tools.javac.tree.JCTree;
    37 import static com.sun.tools.javac.util.LayoutCharacters.*;
    39 /**
    40  * A simple abstraction of a source file, as needed for use in a diagnostic message.
    41  * Provides access to the line and position in a line for any given character offset.
    42  *
    43  *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    44  *  you write code that depends on this, you do so at your own risk.
    45  *  This code and its internal interfaces are subject to change or
    46  *  deletion without notice.</b>
    47  */
    48 public class DiagnosticSource {
    50     /* constant DiagnosticSource to be used when sourcefile is missing */
    51     public static final DiagnosticSource NO_SOURCE = new DiagnosticSource() {
    52         @Override
    53         protected boolean findLine(int pos) {
    54             return false;
    55         }
    56     };
    58     public DiagnosticSource(JavaFileObject fo, AbstractLog log) {
    59         this.fileObject = fo;
    60         this.log = log;
    61     }
    63     private DiagnosticSource() {}
    65     /** Return the underlying file object handled by this
    66      *  DiagnosticSource object.
    67      */
    68     public JavaFileObject getFile() {
    69         return fileObject;
    70     }
    72     /** Return the one-based line number associated with a given pos
    73      * for the current source file.  Zero is returned if no line exists
    74      * for the given position.
    75      */
    76     public int getLineNumber(int pos) {
    77         try {
    78             if (findLine(pos)) {
    79                 return line;
    80             }
    81             return 0;
    82         } finally {
    83             buf = null;
    84         }
    85     }
    87     /** Return the one-based column number associated with a given pos
    88      * for the current source file.  Zero is returned if no column exists
    89      * for the given position.
    90      */
    91     public int getColumnNumber(int pos, boolean expandTabs) {
    92         try {
    93             if (findLine(pos)) {
    94                 int column = 0;
    95                 for (int bp = lineStart; bp < pos; bp++) {
    96                     if (bp >= bufLen) {
    97                         return 0;
    98                     }
    99                     if (buf[bp] == '\t' && expandTabs) {
   100                         column = (column / TabInc * TabInc) + TabInc;
   101                     } else {
   102                         column++;
   103                     }
   104                 }
   105                 return column + 1; // positions are one-based
   106             }
   107             return 0;
   108         } finally {
   109             buf = null;
   110         }
   111     }
   113     /** Return the content of the line containing a given pos.
   114      */
   115     public String getLine(int pos) {
   116         try {
   117             if (!findLine(pos))
   118                 return null;
   120             int lineEnd = lineStart;
   121             while (lineEnd < bufLen && buf[lineEnd] != CR && buf[lineEnd] != LF)
   122                 lineEnd++;
   123             if (lineEnd - lineStart == 0)
   124                 return null;
   125             return new String(buf, lineStart, lineEnd - lineStart);
   126         } finally {
   127             buf = null;
   128         }
   129     }
   131     public Map<JCTree, Integer> getEndPosTable() {
   132         return endPosTable;
   133     }
   135     public void setEndPosTable(Map<JCTree, Integer> t) {
   136         if (endPosTable != null && endPosTable != t)
   137             throw new IllegalStateException("endPosTable already set");
   138         endPosTable = t;
   139     }
   141     /** Find the line in the buffer that contains the current position
   142      * @param pos      Character offset into the buffer
   143      */
   144     protected boolean findLine(int pos) {
   145         if (pos == Position.NOPOS)
   146             return false;
   148         try {
   149             // try and recover buffer from soft reference cache
   150             if (buf == null && refBuf != null)
   151                 buf = refBuf.get();
   153             if (buf == null) {
   154                 buf = initBuf(fileObject);
   155                 lineStart = 0;
   156                 line = 1;
   157             } else if (lineStart > pos) { // messages don't come in order
   158                 lineStart = 0;
   159                 line = 1;
   160             }
   162             int bp = lineStart;
   163             while (bp < bufLen && bp < pos) {
   164                 switch (buf[bp++]) {
   165                 case CR:
   166                     if (bp < bufLen && buf[bp] == LF) bp++;
   167                     line++;
   168                     lineStart = bp;
   169                     break;
   170                 case LF:
   171                     line++;
   172                     lineStart = bp;
   173                     break;
   174                 }
   175             }
   176             return bp <= bufLen;
   177         } catch (IOException e) {
   178             log.directError("source.unavailable");
   179             buf = new char[0];
   180             return false;
   181         }
   182     }
   184     protected char[] initBuf(JavaFileObject fileObject) throws IOException {
   185         char[] buf;
   186         CharSequence cs = fileObject.getCharContent(true);
   187         if (cs instanceof CharBuffer) {
   188             CharBuffer cb = (CharBuffer) cs;
   189             buf = JavacFileManager.toArray(cb);
   190             bufLen = cb.limit();
   191         } else {
   192             buf = cs.toString().toCharArray();
   193             bufLen = buf.length;
   194         }
   195         refBuf = new SoftReference<char[]>(buf);
   196         return buf;
   197     }
   199     /** The underlying file object. */
   200     protected JavaFileObject fileObject;
   202     protected Map<JCTree, Integer> endPosTable;
   204     /** A soft reference to the content of the file object. */
   205     protected SoftReference<char[]> refBuf;
   207     /** A temporary hard reference to the content of the file object. */
   208     protected char[] buf;
   210     /** The length of the content. */
   211     protected int bufLen;
   213     /** The start of a line found by findLine. */
   214     protected int lineStart;
   216     /** The line number of a line found by findLine. */
   217     protected int line;
   219     /** A log for reporting errors, such as errors accessing the content. */
   220     protected AbstractLog log;
   221 }

mercurial