1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 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 net.sourceforge.phpdt.internal.compiler.problem;
13 import net.sourceforge.phpdt.core.compiler.IProblem;
14 import net.sourceforge.phpdt.internal.compiler.CompilationResult;
15 import net.sourceforge.phpdt.internal.compiler.IErrorHandlingPolicy;
16 import net.sourceforge.phpdt.internal.compiler.IProblemFactory;
17 import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
18 import net.sourceforge.phpdt.internal.compiler.impl.ReferenceContext;
21 * Compiler error handler, responsible to determine whether a problem is
22 * actually a warning or an error; also will decide whether the compilation task
23 * can be processed further or not.
25 * Behavior : will request its current policy if need to stop on first error,
26 * 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;
35 public final IProblemFactory problemFactory;
37 public final CompilerOptions options;
40 * Problem handler can be supplied with a policy to specify its behavior in
41 * error handling. Also see static methods for built-in policies.
44 public ProblemHandler(IErrorHandlingPolicy policy, CompilerOptions options,
45 IProblemFactory problemFactory) {
47 this.problemFactory = problemFactory;
48 this.options = options;
52 * Given the current configuration, answers which category the problem falls
53 * into: Error | Warning | Ignore
55 public int computeSeverity(int problemId) {
56 return Error; // by default all problems are errors
59 public IProblem createProblem(char[] fileName, int problemId,
60 String[] problemArguments, String[] messageArguments, int severity,
61 int problemStartPosition, int problemEndPosition, int lineNumber,
62 ReferenceContext referenceContext, CompilationResult unitResult) {
64 return problemFactory.createProblem(fileName, problemId,
65 problemArguments, messageArguments, severity,
66 problemStartPosition, problemEndPosition, lineNumber);
69 public void handle(int problemId, String[] problemArguments,
70 String[] messageArguments, int severity, int problemStartPosition,
71 int problemEndPosition, ReferenceContext referenceContext,
72 CompilationResult unitResult) {
74 if (severity == Ignore)
77 // if no reference context, we need to abort from the current
78 // compilation process
79 if (referenceContext == null) {
80 if ((severity & Error) != 0) { // non reportable error is fatal
81 throw new AbortCompilation(problemId, problemArguments,
84 return; // ignore non reportable warning
88 IProblem problem = this
89 .createProblem(unitResult.getFileName(), problemId,
90 problemArguments, messageArguments, severity,
91 problemStartPosition, problemEndPosition,
92 problemStartPosition >= 0 ? searchLineNumber(
93 unitResult.lineSeparatorPositions,
94 problemStartPosition) : 0, referenceContext,
97 return; // problem couldn't be created, ignore
99 switch (severity & Error) {
101 this.record(problem, unitResult, referenceContext);
102 referenceContext.tagAsHavingErrors();
106 if ((abortLevel = (policy.stopOnFirstError() ? AbortCompilation
107 : severity & Abort)) != 0) {
109 referenceContext.abort(abortLevel);
113 this.record(problem, unitResult, referenceContext);
119 * Standard problem handling API, the actual severity (warning/error/ignore)
120 * is deducted from the problem ID and the current compiler options.
122 public void handle(int problemId, String[] problemArguments,
123 String[] messageArguments, int problemStartPosition,
124 int problemEndPosition, ReferenceContext referenceContext,
125 CompilationResult unitResult) {
127 this.handle(problemId, problemArguments, messageArguments,
128 this.computeSeverity(problemId), // severity inferred using
130 problemStartPosition, problemEndPosition, referenceContext,
134 public void record(IProblem problem, CompilationResult unitResult,
135 ReferenceContext referenceContext) {
136 unitResult.record(problem, referenceContext);
140 * Search the line number corresponding to a specific position
142 * @param methodBinding
143 * net.sourceforge.phpdt.internal.compiler.nameloopkup.SyntheticAccessMethodBinding
145 public static final int searchLineNumber(int[] startLineIndexes,
147 if (startLineIndexes == null)
149 int length = startLineIndexes.length;
152 int g = 0, d = length - 1;
156 if (position < startLineIndexes[m]) {
158 } else if (position > startLineIndexes[m]) {
164 if (position < startLineIndexes[m]) {