1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.problem;
13 import org.eclipse.jdt.core.compiler.IProblem;
14 import org.eclipse.jdt.internal.compiler.CompilationResult;
15 import org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy;
16 import org.eclipse.jdt.internal.compiler.IProblemFactory;
17 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
18 import org.eclipse.jdt.internal.compiler.impl.ReferenceContext;
21 * Compiler error handler, responsible to determine whether
22 * a problem is actually a warning or an error; also will
23 * decide whether the compilation task can be processed further or not.
25 * Behavior : will request its current policy if need to stop on
26 * first error, and if should proceed (persist) with problems.
29 public class ProblemHandler implements ProblemSeverities {
31 public final static String[] NoArgument = new String[0];
33 final public IErrorHandlingPolicy policy;
34 public final IProblemFactory problemFactory;
35 public final CompilerOptions options;
37 * Problem handler can be supplied with a policy to specify
38 * its behavior in error handling. Also see static methods for
42 public ProblemHandler(IErrorHandlingPolicy policy, CompilerOptions options, IProblemFactory problemFactory) {
44 this.problemFactory = problemFactory;
45 this.options = options;
48 * Given the current configuration, answers which category the problem
50 * Error | Warning | Ignore
52 public int computeSeverity(int problemId){
54 return Error; // by default all problems are errors
56 public IProblem createProblem(
59 String[] problemArguments,
60 String[] messageArguments,
62 int problemStartPosition,
63 int problemEndPosition,
66 return this.problemFactory.createProblem(
78 String[] problemArguments,
79 String[] messageArguments,
81 int problemStartPosition,
82 int problemEndPosition,
83 ReferenceContext referenceContext,
84 CompilationResult unitResult) {
86 if (severity == Ignore)
89 // if no reference context, we need to abort from the current compilation process
90 if (referenceContext == null) {
91 if ((severity & Error) != 0) { // non reportable error is fatal
92 IProblem problem = this.createProblem(null, problemId, problemArguments, messageArguments, severity, 0, 0, 0);
93 throw new AbortCompilation(null, problem);
95 return; // ignore non reportable warning
101 unitResult.getFileName(),
106 problemStartPosition,
108 problemStartPosition >= 0
109 ? searchLineNumber(unitResult.lineSeparatorPositions, problemStartPosition)
111 if (problem == null) return; // problem couldn't be created, ignore
113 switch (severity & Error) {
115 this.record(problem, unitResult, referenceContext);
116 referenceContext.tagAsHavingErrors();
121 (this.policy.stopOnFirstError() ? AbortCompilation : severity & Abort)) != 0) {
123 referenceContext.abort(abortLevel, problem);
127 this.record(problem, unitResult, referenceContext);
132 * Standard problem handling API, the actual severity (warning/error/ignore) is deducted
133 * from the problem ID and the current compiler options.
137 String[] problemArguments,
138 String[] messageArguments,
139 int problemStartPosition,
140 int problemEndPosition,
141 ReferenceContext referenceContext,
142 CompilationResult unitResult) {
148 this.computeSeverity(problemId), // severity inferred using the ID
149 problemStartPosition,
154 public void record(IProblem problem, CompilationResult unitResult, ReferenceContext referenceContext) {
155 unitResult.record(problem, referenceContext);
158 * Search the line number corresponding to a specific position
160 public static final int searchLineNumber(int[] startLineIndexes, int position) {
161 if (startLineIndexes == null)
163 int length = startLineIndexes.length;
166 int g = 0, d = length - 1;
170 if (position < startLineIndexes[m]) {
172 } else if (position > startLineIndexes[m]) {
178 if (position < startLineIndexes[m]) {