001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.model.compiler;
038:
039: import java.io.File;
040: import java.io.Serializable;
041:
042: /** A class to represent compilerr errors and warnings. Having this class allows DrJava
043: * to make the errors as legible as possible.
044: * @version $Id: CompilerError.java 4255 2007-08-28 19:17:37Z mgricken $
045: */
046: public class CompilerError implements Comparable, Serializable {
047: private File _file;
048:
049: /** zero-based line number. */
050: private final int _lineNumber;
051:
052: /** zero-based column number. */
053: private final int _startColumn;
054: private final String _message;
055: private final boolean _isWarning;
056:
057: /** This boolean is true when the CompilerError does not have a location (lineNumber is -1). */
058: private boolean _noLocation;
059:
060: /** Constructor.
061: * @param file the file where the error occurred
062: * @param lineNumber the line number of the error
063: * @param startColumn the starting column of the error
064: * @param message the error message
065: */
066: public CompilerError(File file, int lineNumber, int startColumn,
067: String message, boolean isWarning) {
068: _file = file;
069: _lineNumber = lineNumber;
070: _startColumn = startColumn;
071: _message = message;
072: _isWarning = isWarning;
073: if (lineNumber < 0)
074: _noLocation = true;
075: }
076:
077: /** Constructor for an CompilerError with an associated file but no location in the source */
078: public CompilerError(File file, String message, boolean isWarning) {
079: this (file, -1, -1, message, isWarning);
080: }
081:
082: /** Constructor for CompilerErrors without files.
083: * @param message the error message
084: */
085: public CompilerError(String message, boolean isWarning) {
086: this (null, message, isWarning);
087: }
088:
089: /** This function returns true if and only if the given error has no location */
090: public boolean hasNoLocation() {
091: return _noLocation;
092: }
093:
094: /** Gets a String representation of the error. Abstract.
095: * @return the error as a String
096: */
097: public String toString() {
098: return this .getClass().toString() + "(file=" + fileName()
099: + ", line=" + _lineNumber + ", col=" + _startColumn
100: + ", msg=" + _message + ")";
101: }
102:
103: /** Gets the file.
104: * @return the file with errors.
105: */
106: public File file() {
107: return _file;
108: }
109:
110: /** Gets the full name of the file.
111: * @return the file name.
112: */
113: public String fileName() {
114: if (_file == null)
115: return "";
116: return _file.getAbsolutePath();
117: }
118:
119: /** Gets the zero-based line number of the error. NOTE: javac/javadoc produces zero-based line numbers internally,
120: * but prints one-based line numbers to the command line.
121: * @return the zero-based line number
122: */
123: public int lineNumber() {
124: return _lineNumber;
125: }
126:
127: /** Gets the column where the error begins.
128: * @return the starting column
129: */
130: public int startColumn() {
131: return _startColumn;
132: }
133:
134: /** Gets the error message.
135: * @return the error message.
136: */
137: public String message() {
138: return _message;
139: }
140:
141: /** This function returns a message telling the file this error is from appropriate to display to a user, indicating
142: * if there is no file associated with this error.
143: */
144: public String getFileMessage() {
145: if (_file == null)
146: return "(no associated file)";
147: return fileName();
148: }
149:
150: /** This function returns a message telling the line this error is from appropriate to display to a user, indicating
151: * if there is no file associated with this error. This is adjusted to show one-based numbers,
152: * since internally we store a zero-based index.
153: */
154: public String getLineMessage() {
155: if (_file == null || this ._lineNumber < 0)
156: return "(no source location)";
157: return "" + (_lineNumber + 1);
158: }
159:
160: /** Determines if the error is a warning.
161: * @return true if the error is a warning.
162: */
163: public boolean isWarning() {
164: return _isWarning;
165: }
166:
167: /** Compares by file, then by line, then by column. Errors without files are considered equal, but less than any
168: * errors with files. Warnings are considered greater than errors when they are otherwise equal.
169: */
170: public int compareTo(Object o) {
171: CompilerError other = (CompilerError) o;
172:
173: // Determine if I have a file
174: if (_file != null) {
175: // "this" has a file
176: if (other.file() != null) {
177: // "this" and other have files; compare them
178: int fileComp = _file.compareTo(other.file());
179: if (fileComp != 0)
180: return fileComp;
181: // This and other have equal files; compare positions
182: return compareByPosition(other);
183: } else
184: return 1; // Other has no file so "this" is bigger
185: }
186: // "this" has no file
187: if (other.file() != null)
188: return -1; // Other has a file so "this" is smaller
189: // Neither error has a file
190: boolean otherWarning = other.isWarning();
191: return compareErrorWarning(other);
192: }
193:
194: /** Compares this error's postion with other error's, based first on line number, then by column. */
195: private int compareByPosition(CompilerError other) {
196: // Compare by line unless lines are equal
197: int byLine = _lineNumber - other.lineNumber();
198: if (byLine != 0)
199: return byLine;
200:
201: int byCol = _startColumn - other.startColumn();
202: if (byCol != 0)
203: return byCol;
204: return compareErrorWarning(other);
205: }
206:
207: /** Compare otherwise equal errors. */
208: private int compareErrorWarning(CompilerError other) {
209: return (isWarning() ? (other.isWarning() ? 0 : 1) : (other
210: .isWarning() ? -1 : 0));
211: }
212: }
|