KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > compiler > CompilerError


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  * END_COPYRIGHT_BLOCK*/

32
33 package edu.rice.cs.drjava.model.compiler;
34
35 import java.io.File JavaDoc;
36 import java.io.Serializable JavaDoc;
37
38 /** A class to represent compilerr errors and warnings. Having this class allows DrJava
39   * to make the errors as legible as possible.
40   * @version $Id: CompilerError.java 3903 2006-07-05 20:03:06Z rcartwright $
41   */

42 public class CompilerError implements Comparable JavaDoc, Serializable JavaDoc {
43   private File JavaDoc _file;
44   
45   /** zero-based line number. */
46   private final int _lineNumber;
47   
48   /** zero-based column number. */
49   private final int _startColumn;
50   private final String JavaDoc _message;
51   private final boolean _isWarning;
52   
53   /** This boolean is true when the CompilerError does not have a location (lineNumber is -1). */
54   private boolean _noLocation;
55   
56   /** Constructor.
57     * @param file the file where the error occurred
58     * @param lineNumber the line number of the error
59     * @param startColumn the starting column of the error
60     * @param message the error message
61     */

62   public CompilerError(File JavaDoc file, int lineNumber, int startColumn, String JavaDoc message, boolean isWarning) {
63     _file = file;
64     _lineNumber = lineNumber;
65     _startColumn = startColumn;
66     _message = message;
67     _isWarning = isWarning;
68     if (lineNumber < 0) _noLocation = true;
69   }
70     
71   /** Constructor for an CompilerError with an associated file but no location in the source */
72   public CompilerError(File JavaDoc file, String JavaDoc message, boolean isWarning) { this(file, -1, -1, message, isWarning); }
73   
74   /** Constructor for CompilerErrors without files.
75    * @param message the error message
76    */

77   public CompilerError(String JavaDoc message, boolean isWarning) { this(null, message, isWarning); }
78   
79   /** This function returns true if and only if the given error has no location */
80   public boolean hasNoLocation() { return _noLocation; }
81   
82   /** Gets a String representation of the error. Abstract.
83     * @return the error as a String
84     */

85   public String JavaDoc toString() {
86     return this.getClass().toString() + "(file=" + fileName() + ", line=" + _lineNumber + ", col=" + _startColumn +
87       ", msg=" + _message + ")";
88   }
89   
90   /** Gets the file.
91     * @return the file with errors.
92     */

93   public File JavaDoc file() { return _file; }
94   
95   /** Gets the full name of the file.
96     * @return the file name.
97     */

98   public String JavaDoc fileName() {
99     if (_file == null) return "";
100     return _file.getAbsolutePath();
101   }
102   
103   /** Gets the zero-based line number of the error. NOTE: javac/javadoc produces zero-based line numbers internally,
104     * but prints one-based line numbers to the command line.
105     * @return the zero-based line number
106     */

107   public int lineNumber() { return _lineNumber; }
108   
109   /** Gets the column where the error begins.
110     * @return the starting column
111     */

112   public int startColumn() { return _startColumn; }
113   
114   /** Gets the error message.
115     * @return the error message.
116     */

117   public String JavaDoc message() { return _message; }
118   
119   /** This function returns a message telling the file this error is from appropriate to display to a user, indicating
120     * if there is no file associated with this error.
121     */

122   public String JavaDoc getFileMessage() {
123     if (_file == null) return "(no associated file)";
124     return fileName();
125   }
126   
127   /** This function returns a message telling the line this error is from appropriate to display to a user, indicating
128     * if there is no file associated with this error. This is adjusted to show one-based numbers,
129     * since internally we store a zero-based index.
130     */

131   public String JavaDoc getLineMessage() {
132     if (_file == null || this._lineNumber < 0) return "(no source location)";
133     return "" + (_lineNumber + 1);
134   }
135   
136   /** Determines if the error is a warning.
137     * @return true if the error is a warning.
138     */

139   public boolean isWarning() { return _isWarning; }
140   
141   /** Compares by file, then by line, then by column. Errors without files are considered equal, but less than any
142     * errors with files. Warnings are considered greater than errors when they are otherwise equal.
143     */

144   public int compareTo(Object JavaDoc o) {
145     CompilerError other = (CompilerError) o;
146     
147     // Determine if I have a file
148
if (_file != null) {
149       // "this" has a file
150
if (other.file() != null) {
151          // "this" and other have files; compare them
152
int fileComp = _file.compareTo(other.file());
153         if (fileComp != 0) return fileComp;
154         // This and other have equal files; compare positions
155
return compareByPosition(other);
156       }
157       else return 1; // Other has no file so "this" is bigger
158
}
159     // "this" has no file
160
if (other.file() != null) return -1; // Other has a file so "this" is smaller
161
// Neither error has a file
162
boolean otherWarning = other.isWarning();
163     return compareErrorWarning(other);
164   }
165   
166   /** Compares this error's postion with other error's, based first on line number, then by column. */
167   private int compareByPosition(CompilerError other) {
168     // Compare by line unless lines are equal
169
int byLine = _lineNumber - other.lineNumber();
170     if (byLine != 0) return byLine;
171     
172     int byCol = _startColumn - other.startColumn();
173     if (byCol != 0) return byCol;
174     return compareErrorWarning(other);
175   }
176   
177   /** Compare otherwise equal errors. */
178   private int compareErrorWarning(CompilerError other) {
179     return (isWarning()? (other.isWarning()? 0 : 1) : (other.isWarning()? -1 : 0));
180   }
181 }
Popular Tags