KickJava   Java API By Example, From Geeks To Geeks.

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


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.CharOperation;
15 import org.eclipse.jdt.internal.compiler.CompilationResult;
16 import org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy;
17 import org.eclipse.jdt.internal.compiler.IProblemFactory;
18 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
19 import org.eclipse.jdt.internal.compiler.impl.ReferenceContext;
20 import org.eclipse.jdt.internal.compiler.util.Util;
21
22 /*
23  * Compiler error handler, responsible to determine whether
24  * a problem is actually a warning or an error; also will
25  * decide whether the compilation task can be processed further or not.
26  *
27  * Behavior : will request its current policy if need to stop on
28  * first error, and if should proceed (persist) with problems.
29  */

30
31 public class ProblemHandler {
32
33     public final static String JavaDoc[] NoArgument = CharOperation.NO_STRINGS;
34     
35     final public IErrorHandlingPolicy policy;
36     public final IProblemFactory problemFactory;
37     public final CompilerOptions options;
38 /*
39  * Problem handler can be supplied with a policy to specify
40  * its behavior in error handling. Also see static methods for
41  * built-in policies.
42  *
43  */

44 public ProblemHandler(IErrorHandlingPolicy policy, CompilerOptions options, IProblemFactory problemFactory) {
45     this.policy = policy;
46     this.problemFactory = problemFactory;
47     this.options = options;
48 }
49 /*
50  * Given the current configuration, answers which category the problem
51  * falls into:
52  * Error | Warning | Ignore
53  */

54 public int computeSeverity(int problemId){
55     
56     return ProblemSeverities.Error; // by default all problems are errors
57
}
58 public CategorizedProblem createProblem(
59     char[] fileName,
60     int problemId,
61     String JavaDoc[] problemArguments,
62     String JavaDoc[] messageArguments,
63     int severity,
64     int problemStartPosition,
65     int problemEndPosition,
66     int lineNumber,
67     int columnNumber) {
68
69     return this.problemFactory.createProblem(
70         fileName,
71         problemId,
72         problemArguments,
73         messageArguments,
74         severity,
75         problemStartPosition,
76         problemEndPosition,
77         lineNumber,
78         columnNumber);
79 }
80 public void handle(
81     int problemId,
82     String JavaDoc[] problemArguments,
83     String JavaDoc[] messageArguments,
84     int severity,
85     int problemStartPosition,
86     int problemEndPosition,
87     ReferenceContext referenceContext,
88     CompilationResult unitResult) {
89
90     if (severity == ProblemSeverities.Ignore)
91         return;
92
93     // if no reference context, we need to abort from the current compilation process
94
if (referenceContext == null) {
95         if ((severity & ProblemSeverities.Error) != 0) { // non reportable error is fatal
96
CategorizedProblem problem = this.createProblem(null, problemId, problemArguments, messageArguments, severity, 0, 0, 0, 0);
97             throw new AbortCompilation(null, problem);
98         } else {
99             return; // ignore non reportable warning
100
}
101     }
102
103     int[] lineEnds;
104     int lineNumber = problemStartPosition >= 0
105             ? Util.getLineNumber(problemStartPosition, lineEnds = unitResult.getLineSeparatorPositions(), 0, lineEnds.length-1)
106             : 0;
107     int columnNumber = problemStartPosition >= 0
108             ? Util.searchColumnNumber(unitResult.getLineSeparatorPositions(), lineNumber, problemStartPosition)
109             : 0;
110     CategorizedProblem problem =
111         this.createProblem(
112             unitResult.getFileName(),
113             problemId,
114             problemArguments,
115             messageArguments,
116             severity,
117             problemStartPosition,
118             problemEndPosition,
119             lineNumber,
120             columnNumber);
121
122     if (problem == null) return; // problem couldn't be created, ignore
123

124     switch (severity & ProblemSeverities.Error) {
125         case ProblemSeverities.Error :
126             this.record(problem, unitResult, referenceContext);
127             if ((severity & ProblemSeverities.Fatal) != 0) {
128                 referenceContext.tagAsHavingErrors();
129                 // should abort ?
130
int abortLevel;
131                 if ((abortLevel = this.policy.stopOnFirstError() ? ProblemSeverities.AbortCompilation : severity & ProblemSeverities.Abort) != 0) {
132                     referenceContext.abort(abortLevel, problem);
133                 }
134             }
135             break;
136         case ProblemSeverities.Warning :
137             this.record(problem, unitResult, referenceContext);
138             break;
139     }
140 }
141 /**
142  * Standard problem handling API, the actual severity (warning/error/ignore) is deducted
143  * from the problem ID and the current compiler options.
144  */

145 public void handle(
146     int problemId,
147     String JavaDoc[] problemArguments,
148     String JavaDoc[] messageArguments,
149     int problemStartPosition,
150     int problemEndPosition,
151     ReferenceContext referenceContext,
152     CompilationResult unitResult) {
153
154     this.handle(
155         problemId,
156         problemArguments,
157         messageArguments,
158         this.computeSeverity(problemId), // severity inferred using the ID
159
problemStartPosition,
160         problemEndPosition,
161         referenceContext,
162         unitResult);
163 }
164 public void record(CategorizedProblem problem, CompilationResult unitResult, ReferenceContext referenceContext) {
165     unitResult.record(problem, referenceContext);
166 }
167 }
168
Popular Tags