1 32 33 package edu.rice.cs.drjava.model.compiler; 34 35 import java.io.File ; 36 import java.io.Serializable ; 37 38 42 public class CompilerError implements Comparable , Serializable { 43 private File _file; 44 45 46 private final int _lineNumber; 47 48 49 private final int _startColumn; 50 private final String _message; 51 private final boolean _isWarning; 52 53 54 private boolean _noLocation; 55 56 62 public CompilerError(File file, int lineNumber, int startColumn, String 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 72 public CompilerError(File file, String message, boolean isWarning) { this(file, -1, -1, message, isWarning); } 73 74 77 public CompilerError(String message, boolean isWarning) { this(null, message, isWarning); } 78 79 80 public boolean hasNoLocation() { return _noLocation; } 81 82 85 public String toString() { 86 return this.getClass().toString() + "(file=" + fileName() + ", line=" + _lineNumber + ", col=" + _startColumn + 87 ", msg=" + _message + ")"; 88 } 89 90 93 public File file() { return _file; } 94 95 98 public String fileName() { 99 if (_file == null) return ""; 100 return _file.getAbsolutePath(); 101 } 102 103 107 public int lineNumber() { return _lineNumber; } 108 109 112 public int startColumn() { return _startColumn; } 113 114 117 public String message() { return _message; } 118 119 122 public String getFileMessage() { 123 if (_file == null) return "(no associated file)"; 124 return fileName(); 125 } 126 127 131 public String getLineMessage() { 132 if (_file == null || this._lineNumber < 0) return "(no source location)"; 133 return "" + (_lineNumber + 1); 134 } 135 136 139 public boolean isWarning() { return _isWarning; } 140 141 144 public int compareTo(Object o) { 145 CompilerError other = (CompilerError) o; 146 147 if (_file != null) { 149 if (other.file() != null) { 151 int fileComp = _file.compareTo(other.file()); 153 if (fileComp != 0) return fileComp; 154 return compareByPosition(other); 156 } 157 else return 1; } 159 if (other.file() != null) return -1; boolean otherWarning = other.isWarning(); 163 return compareErrorWarning(other); 164 } 165 166 167 private int compareByPosition(CompilerError other) { 168 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 178 private int compareErrorWarning(CompilerError other) { 179 return (isWarning()? (other.isWarning()? 0 : 1) : (other.isWarning()? -1 : 0)); 180 } 181 } | Popular Tags |