KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > problem > DefaultProblem


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.compiler.problem;
12
13 import org.eclipse.jdt.core.compiler.CategorizedProblem;
14 import org.eclipse.jdt.core.compiler.IProblem;
15 import org.eclipse.jdt.internal.compiler.util.Messages;
16 import org.eclipse.jdt.internal.compiler.util.Util;
17
18 public class DefaultProblem extends CategorizedProblem {
19     private char[] fileName;
20     private int id;
21     private int startPosition, endPosition, line, column;
22     private int severity;
23     private String JavaDoc[] arguments;
24     private String JavaDoc message;
25     
26     // cannot directly point to IJavaModelMarker constants from within batch compiler
27
private static final String JavaDoc MARKER_TYPE_PROBLEM = "org.eclipse.jdt.core.problem"; //$NON-NLS-1$
28
private static final String JavaDoc MARKER_TYPE_TASK = "org.eclipse.jdt.core.task"; //$NON-NLS-1$
29

30     public static final Object JavaDoc[] EMPTY_VALUES = {};
31     
32 public DefaultProblem(
33     char[] originatingFileName,
34     String JavaDoc message,
35     int id,
36     String JavaDoc[] stringArguments,
37     int severity,
38     int startPosition,
39     int endPosition,
40     int line,
41     int column) {
42
43     this.fileName = originatingFileName;
44     this.message = message;
45     this.id = id;
46     this.arguments = stringArguments;
47     this.severity = severity;
48     this.startPosition = startPosition;
49     this.endPosition = endPosition;
50     this.line = line;
51     this.column = column;
52 }
53
54 public String JavaDoc errorReportSource(char[] unitSource) {
55     //extra from the source the innacurate token
56
//and "highlight" it using some underneath ^^^^^
57
//put some context around too.
58

59     //this code assumes that the font used in the console is fixed size
60

61     //sanity .....
62
if ((this.startPosition > this.endPosition)
63         || ((this.startPosition < 0) && (this.endPosition < 0))
64         || unitSource.length == 0)
65         return Messages.problem_noSourceInformation;
66
67     StringBuffer JavaDoc errorBuffer = new StringBuffer JavaDoc();
68     errorBuffer.append(' ').append(Messages.bind(Messages.problem_atLine, String.valueOf(this.line)));
69     errorBuffer.append(Util.LINE_SEPARATOR);
70     errorBuffer.append('\t');
71     
72     char c;
73     final char SPACE = '\u0020';
74     final char MARK = '^';
75     final char TAB = '\t';
76     //the next code tries to underline the token.....
77
//it assumes (for a good display) that token source does not
78
//contain any \r \n. This is false on statements !
79
//(the code still works but the display is not optimal !)
80

81     // expand to line limits
82
int length = unitSource.length, begin, end;
83     for (begin = this.startPosition >= length ? length - 1 : this.startPosition; begin > 0; begin--) {
84         if ((c = unitSource[begin - 1]) == '\n' || c == '\r') break;
85     }
86     for (end = this.endPosition >= length ? length - 1 : this.endPosition ; end+1 < length; end++) {
87         if ((c = unitSource[end + 1]) == '\r' || c == '\n') break;
88     }
89     
90     // trim left and right spaces/tabs
91
while ((c = unitSource[begin]) == ' ' || c == '\t') begin++;
92     //while ((c = unitSource[end]) == ' ' || c == '\t') end--; TODO (philippe) should also trim right, but all tests are to be updated
93

94     // copy source
95
errorBuffer.append(unitSource, begin, end-begin+1);
96     errorBuffer.append(Util.LINE_SEPARATOR).append("\t"); //$NON-NLS-1$
97

98     // compute underline
99
for (int i = begin; i <this.startPosition; i++) {
100         errorBuffer.append((unitSource[i] == TAB) ? TAB : SPACE);
101     }
102     for (int i = this.startPosition; i <= (this.endPosition >= length ? length - 1 : this.endPosition); i++) {
103         errorBuffer.append(MARK);
104     }
105     return errorBuffer.toString();
106 }
107 /**
108  * Answer back the original arguments recorded into the problem.
109  * @return java.lang.String[]
110  */

111 public String JavaDoc[] getArguments() {
112     return this.arguments;
113 }
114 /**
115  * @see org.eclipse.jdt.core.compiler.CategorizedProblem#getCategoryID()
116  */

117 public int getCategoryID() {
118     return ProblemReporter.getProblemCategory(this.severity, this.id);
119 }
120
121 /**
122  * Answer the type of problem.
123  * @see org.eclipse.jdt.core.compiler.IProblem#getID()
124  * @return int
125  */

126 public int getID() {
127     return this.id;
128 }
129
130 /**
131  * Answers a readable name for the category which this problem belongs to,
132  * or null if none could be found.
133  * FOR TESTING PURPOSE
134  * @return java.lang.String
135  */

136 public String JavaDoc getInternalCategoryMessage() {
137     switch(getCategoryID()) {
138         case CAT_UNSPECIFIED:
139             return "unspecified"; //$NON-NLS-1$
140
case CAT_BUILDPATH:
141             return "buildpath"; //$NON-NLS-1$
142
case CAT_SYNTAX:
143             return "syntax"; //$NON-NLS-1$
144
case CAT_IMPORT:
145             return "import"; //$NON-NLS-1$
146
case CAT_TYPE:
147             return "type"; //$NON-NLS-1$
148
case CAT_MEMBER:
149             return "member"; //$NON-NLS-1$
150
case CAT_INTERNAL:
151             return "internal"; //$NON-NLS-1$
152
case CAT_JAVADOC:
153             return "javadoc"; //$NON-NLS-1$
154
case CAT_CODE_STYLE:
155             return "code style"; //$NON-NLS-1$
156
case CAT_POTENTIAL_PROGRAMMING_PROBLEM:
157             return "potential programming problem"; //$NON-NLS-1$
158
case CAT_NAME_SHADOWING_CONFLICT:
159             return "name shadowing conflict"; //$NON-NLS-1$
160
case CAT_DEPRECATION:
161             return "deprecation"; //$NON-NLS-1$
162
case CAT_UNNECESSARY_CODE:
163             return "unnecessary code"; //$NON-NLS-1$
164
case CAT_UNCHECKED_RAW:
165             return "unchecked/raw"; //$NON-NLS-1$
166
case CAT_NLS:
167             return "nls"; //$NON-NLS-1$
168
case CAT_RESTRICTION:
169             return "restriction"; //$NON-NLS-1$
170
}
171     return null;
172 }
173
174 /**
175  * Returns the marker type associated to this problem.
176  * @see org.eclipse.jdt.core.compiler.CategorizedProblem#getMarkerType()
177  */

178 public String JavaDoc getMarkerType() {
179     return this.id == IProblem.Task
180         ? MARKER_TYPE_TASK
181         : MARKER_TYPE_PROBLEM;
182 }
183
184 /**
185  * Answer a localized, human-readable message string which describes the problem.
186  * @return java.lang.String
187  */

188 public String JavaDoc getMessage() {
189     return this.message;
190 }
191
192 /**
193  * Answer the file name in which the problem was found.
194  * @return char[]
195  */

196 public char[] getOriginatingFileName() {
197     return this.fileName;
198 }
199
200 /**
201  * Answer the end position of the problem (inclusive), or -1 if unknown.
202  * @return int
203  */

204 public int getSourceEnd() {
205     return this.endPosition;
206 }
207 /**
208  * Answer the line number in source where the problem begins.
209  * @return int
210  */

211 public int getSourceColumnNumber() {
212     return this.column;
213 }
214 /**
215  * Answer the line number in source where the problem begins.
216  * @return int
217  */

218 public int getSourceLineNumber() {
219     return this.line;
220 }
221 /**
222  * Answer the start position of the problem (inclusive), or -1 if unknown.
223  * @return int
224  */

225 public int getSourceStart() {
226     return this.startPosition;
227 }
228
229 /*
230  * Helper method: checks the severity to see if the Error bit is set.
231  * @return boolean
232  */

233 public boolean isError() {
234     return (this.severity & ProblemSeverities.Error) != 0;
235 }
236
237 /*
238  * Helper method: checks the severity to see if the Error bit is not set.
239  * @return boolean
240  */

241 public boolean isWarning() {
242     return (this.severity & ProblemSeverities.Error) == 0;
243 }
244
245 public void setOriginatingFileName(char[] fileName) {
246     this.fileName = fileName;
247 }
248
249 /**
250  * Set the end position of the problem (inclusive), or -1 if unknown.
251  *
252  * Used for shifting problem positions.
253  * @param sourceEnd the new value of the sourceEnd of the receiver
254  */

255 public void setSourceEnd(int sourceEnd) {
256     this.endPosition = sourceEnd;
257 }
258
259 /**
260  * Set the line number in source where the problem begins.
261  * @param lineNumber the new value of the line number of the receiver
262  */

263 public void setSourceLineNumber(int lineNumber) {
264
265     this.line = lineNumber;
266 }
267
268 /**
269  * Set the start position of the problem (inclusive), or -1 if unknown.
270  *
271  * Used for shifting problem positions.
272  * @param sourceStart the new value of the source start position of the receiver
273  */

274 public void setSourceStart(int sourceStart) {
275     this.startPosition = sourceStart;
276 }
277
278 public String JavaDoc toString() {
279     String JavaDoc s = "Pb(" + (this.id & IProblem.IgnoreCategoriesMask) + ") "; //$NON-NLS-1$ //$NON-NLS-2$
280
if (this.message != null) {
281         s += this.message;
282     } else {
283         if (this.arguments != null)
284             for (int i = 0; i < this.arguments.length; i++)
285                 s += " " + this.arguments[i]; //$NON-NLS-1$
286
}
287     return s;
288 }
289 }
290
Popular Tags