|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| DJError.java | 89.3% | 89.1% | 88.2% | 89% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK | |
| 2 | * | |
| 3 | * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu) | |
| 4 | * All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * * Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * * Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the | |
| 14 | * names of its contributors may be used to endorse or promote products | |
| 15 | * derived from this software without specific prior written permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
| 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 28 | * | |
| 29 | * This software is Open Source Initiative approved Open Source Software. | |
| 30 | * Open Source Initative Approved is a trademark of the Open Source Initiative. | |
| 31 | * | |
| 32 | * This file is part of DrJava. Download the current version of this project | |
| 33 | * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ | |
| 34 | * | |
| 35 | * END_COPYRIGHT_BLOCK*/ | |
| 36 | ||
| 37 | package edu.rice.cs.drjava.model; | |
| 38 | ||
| 39 | import java.io.File; | |
| 40 | import java.io.Serializable; | |
| 41 | ||
| 42 | import edu.rice.cs.util.FileOps; | |
| 43 | import edu.rice.cs.util.UnexpectedException; | |
| 44 | ||
| 45 | ||
| 46 | /** A class to represent source errors and warnings generated by the compiler, JUnit, etc. This class enables DrJava | |
| 47 | * to highlight the error text. | |
| 48 | * @version $Id: DJError.java 5439 2011-08-11 17:13:04Z rcartwright $ | |
| 49 | */ | |
| 50 | public class DJError implements Comparable<DJError>, Serializable { | |
| 51 | private volatile File _file; | |
| 52 | ||
| 53 | /** zero-based line number. */ | |
| 54 | private volatile int _lineNumber; | |
| 55 | ||
| 56 | /** zero-based column number. */ | |
| 57 | private final int _startColumn; | |
| 58 | private final String _message; | |
| 59 | private final boolean _isWarning; | |
| 60 | ||
| 61 | /** This boolean is true when the DJError does not have a location (lineNumber is -1). */ | |
| 62 | private volatile boolean _noLocation; | |
| 63 | ||
| 64 | /** Constructor. | |
| 65 | * @param file the file where the error occurred | |
| 66 | * @param lineNumber the line number of the error | |
| 67 | * @param startColumn the starting column of the error | |
| 68 | * @param message the error message | |
| 69 | */ | |
| 70 | 3109 | public DJError(File file, int lineNumber, int startColumn, String message, boolean isWarning) { |
| 71 | // System.err.println("instance of DJError (or subclass) constructed; file = " + file + "; message = " + message); | |
| 72 | // need to precisely match the CompilerError message, otherwise a file name containing | |
| 73 | // "CompilerError" may trigger an UnexpectedException (see bug 2872797) | |
| 74 | 3109 | if (message != null && message.startsWith("Compile exception: sun.tools.java.CompilerError")) |
| 75 | 0 | throw new UnexpectedException(message); |
| 76 | 3109 | _file = file; |
| 77 | 3109 | _lineNumber = lineNumber; |
| 78 | 3109 | _startColumn = startColumn; |
| 79 | 3109 | _message = message; |
| 80 | 3109 | _isWarning = isWarning; |
| 81 | 3034 | if (lineNumber < 0) _noLocation = true; |
| 82 | } | |
| 83 | ||
| 84 | /** Constructor for an DJError with an associated file but no location in the source */ | |
| 85 | 3034 | public DJError(File file, String message, boolean isWarning) { this(file, -1, -1, message, isWarning); } |
| 86 | ||
| 87 | /** Constructor for CompilerErrors without files. | |
| 88 | * @param message the error message | |
| 89 | */ | |
| 90 | 2989 | public DJError(String message, boolean isWarning) { this(null, message, isWarning); } |
| 91 | ||
| 92 | /** This function returns true if and only if the given error has no location */ | |
| 93 | 3080 | public boolean hasNoLocation() { return _noLocation; } |
| 94 | ||
| 95 | /** Gets a String representation of the error. Abstract. | |
| 96 | * @return the error as a String | |
| 97 | */ | |
| 98 | 0 | public String toString() { |
| 99 | 0 | return this.getClass().toString() + "(file=" + fileName() + ", line=" + _lineNumber + ", col=" + _startColumn + |
| 100 | ", msg=" + _message + ")"; | |
| 101 | } | |
| 102 | ||
| 103 | /** Gets the file. | |
| 104 | * @return the file with errors. | |
| 105 | */ | |
| 106 | 1064 | public File file() { return _file; } |
| 107 | ||
| 108 | /** Gets the full name of the file. | |
| 109 | * @return the file name. | |
| 110 | */ | |
| 111 | 3110 | public String fileName() { |
| 112 | 2989 | if (_file == null) return ""; |
| 113 | 121 | return _file.getAbsolutePath(); |
| 114 | } | |
| 115 | ||
| 116 | /** Gets the zero-based line number of the error. NOTE: javac/javadoc produces zero-based line numbers internally, | |
| 117 | * but prints one-based line numbers to the command line. | |
| 118 | * @return the zero-based line number | |
| 119 | */ | |
| 120 | 377 | public int lineNumber() { return _lineNumber; } |
| 121 | ||
| 122 | /** Sets the line number. | |
| 123 | * @param ln line number | |
| 124 | */ | |
| 125 | 0 | public void setLineNumber(int ln) { _lineNumber = ln; } |
| 126 | ||
| 127 | /** Gets the column where the error begins. | |
| 128 | * @return the starting column | |
| 129 | */ | |
| 130 | 107 | public int startColumn() { return _startColumn; } |
| 131 | ||
| 132 | /** Gets the error message. | |
| 133 | * @return the error message. | |
| 134 | */ | |
| 135 | 3 | public String message() { return _message; } |
| 136 | ||
| 137 | /** This function returns a message telling the file this error is from appropriate to display to a user, indicating | |
| 138 | * if there is no file associated with this error. | |
| 139 | */ | |
| 140 | 1 | public String getFileMessage() { |
| 141 | 0 | if (_file == null || _file == FileOps.NULL_FILE) return "(no associated file)"; |
| 142 | 1 | return fileName(); |
| 143 | } | |
| 144 | ||
| 145 | /** This function returns a message telling the line this error is from appropriate to display to a user, indicating | |
| 146 | * if there is no file associated with this error. This is adjusted to show one-based numbers, | |
| 147 | * since internally we store a zero-based index. | |
| 148 | */ | |
| 149 | 1 | public String getLineMessage() { |
| 150 | 0 | if (_file == null || _file == FileOps.NULL_FILE || this._lineNumber < 0) return "(no source location)"; |
| 151 | 1 | return "" + (_lineNumber + 1); |
| 152 | } | |
| 153 | ||
| 154 | /** Determines if the error is a warning. | |
| 155 | * @return true if the error is a warning. | |
| 156 | */ | |
| 157 | 3203 | public boolean isWarning() { return _isWarning; } |
| 158 | ||
| 159 | /** Compares by file, then by line, then by column. Errors without files are considered equal, but less than any | |
| 160 | * errors with files. Warnings are considered greater than errors when they are otherwise equal. | |
| 161 | */ | |
| 162 | 269 | public int compareTo(DJError other) { |
| 163 | ||
| 164 | // Determine if I have a file | |
| 165 | 269 | if (_file != null) { |
| 166 | // "this" has a file | |
| 167 | 263 | if (other.file() != null) { |
| 168 | // "this" and other have files; compare them | |
| 169 | 261 | int fileComp = _file.compareTo(other.file()); |
| 170 | 165 | if (fileComp != 0) return fileComp; |
| 171 | // This and other have equal files; compare positions | |
| 172 | 96 | return compareByPosition(other); |
| 173 | } | |
| 174 | 2 | else return 1; // Other has no file so "this" is bigger |
| 175 | } | |
| 176 | // "this" has no file | |
| 177 | 2 | if (other.file() != null) return -1; // Other has a file so "this" is smaller |
| 178 | // Neither error has a file | |
| 179 | // boolean otherWarning = other.isWarning(); | |
| 180 | 4 | return compareErrorWarning(other); |
| 181 | } | |
| 182 | ||
| 183 | /** Compares this error's postion with other error's, based first on line number, then by column. */ | |
| 184 | 96 | private int compareByPosition(DJError other) { |
| 185 | // Compare by line unless lines are equal | |
| 186 | 96 | int byLine = _lineNumber - other.lineNumber(); |
| 187 | 63 | if (byLine != 0) return byLine; |
| 188 | ||
| 189 | 33 | int byCol = _startColumn - other.startColumn(); |
| 190 | 3 | if (byCol != 0) return byCol; |
| 191 | 30 | return compareErrorWarning(other); |
| 192 | } | |
| 193 | ||
| 194 | /** Compare otherwise equal errors. */ | |
| 195 | 34 | private int compareErrorWarning(DJError other) { |
| 196 | 34 | return (isWarning()? (other.isWarning()? 0 : 1) : (other.isWarning()? -1 : 0)); |
| 197 | } | |
| 198 | } |
|
||||||||||