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.core;
13 import java.util.Locale;
16 import net.sourceforge.phpdt.core.ICompilationUnit;
17 import net.sourceforge.phpdt.core.IJavaElement;
18 import net.sourceforge.phpdt.core.IJavaModelStatusConstants;
19 import net.sourceforge.phpdt.core.IJavaProject;
20 import net.sourceforge.phpdt.core.IPackageFragment;
21 import net.sourceforge.phpdt.core.IProblemRequestor;
22 import net.sourceforge.phpdt.core.JavaCore;
23 import net.sourceforge.phpdt.core.JavaModelException;
24 import net.sourceforge.phpdt.core.WorkingCopyOwner;
25 import net.sourceforge.phpdt.core.compiler.CharOperation;
26 import net.sourceforge.phpdt.core.compiler.IProblem;
27 import net.sourceforge.phpdt.internal.compiler.CompilationResult;
28 import net.sourceforge.phpdt.internal.compiler.Compiler;
29 import net.sourceforge.phpdt.internal.compiler.DefaultErrorHandlingPolicies;
30 import net.sourceforge.phpdt.internal.compiler.ICompilerRequestor;
31 import net.sourceforge.phpdt.internal.compiler.IErrorHandlingPolicy;
32 import net.sourceforge.phpdt.internal.compiler.IProblemFactory;
33 import net.sourceforge.phpdt.internal.compiler.env.INameEnvironment;
34 import net.sourceforge.phpdt.internal.compiler.env.ISourceType;
35 import net.sourceforge.phpdt.internal.compiler.lookup.PackageBinding;
36 import net.sourceforge.phpdt.internal.compiler.parser.SourceTypeConverter;
37 import net.sourceforge.phpdt.internal.compiler.parser.UnitParser;
38 import net.sourceforge.phpdt.internal.compiler.problem.AbortCompilation;
39 import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblemFactory;
40 import net.sourceforge.phpdt.internal.core.util.Util;
41 import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration;
43 import org.eclipse.core.runtime.IProgressMonitor;
46 * Responsible for resolving types inside a compilation unit being reconciled,
47 * reporting the discovered problems to a given IProblemRequestor.
49 public class CompilationUnitProblemFinder extends Compiler {
52 * Answer a new CompilationUnitVisitor using the given name environment and compiler options.
53 * The environment and options will be in effect for the lifetime of the compiler.
54 * When the compiler is run, compilation results are sent to the given requestor.
56 * @param environment org.eclipse.jdt.internal.compiler.api.env.INameEnvironment
57 * Environment used by the compiler in order to resolve type and package
58 * names. The name environment implements the actual connection of the compiler
59 * to the outside world (e.g. in batch mode the name environment is performing
60 * pure file accesses, reuse previous build state or connection to repositories).
61 * Note: the name environment is responsible for implementing the actual classpath
64 * @param policy org.eclipse.jdt.internal.compiler.api.problem.IErrorHandlingPolicy
65 * Configurable part for problem handling, allowing the compiler client to
66 * specify the rules for handling problems (stop on first error or accumulate
67 * them all) and at the same time perform some actions such as opening a dialog
68 * in UI when compiling interactively.
69 * @see org.eclipse.jdt.internal.compiler.api.problem.DefaultErrorHandlingPolicies
71 * @param settings The settings to use for the resolution.
73 * @param requestor org.eclipse.jdt.internal.compiler.api.ICompilerRequestor
74 * Component which will receive and persist all compilation results and is intended
75 * to consume them as they are produced. Typically, in a batch compiler, it is
76 * responsible for writing out the actual .class files to the file system.
77 * @see org.eclipse.jdt.internal.compiler.api.CompilationResult
79 * @param problemFactory org.eclipse.jdt.internal.compiler.api.problem.IProblemFactory
80 * Factory used inside the compiler to create problem descriptors. It allows the
81 * compiler client to supply its own representation of compilation problems in
82 * order to avoid object conversions. Note that the factory is not supposed
83 * to accumulate the created problems, the compiler will gather them all and hand
84 * them back as part of the compilation unit result.
86 protected CompilationUnitProblemFinder(
87 INameEnvironment environment,
88 IErrorHandlingPolicy policy,
90 ICompilerRequestor requestor,
91 IProblemFactory problemFactory) {
93 super(environment, policy, settings, requestor, problemFactory, true);
97 * Add additional source types
99 public void accept(ISourceType[] sourceTypes, PackageBinding packageBinding) {
100 // ensure to jump back to toplevel type for first one (could be a member)
101 // while (sourceTypes[0].getEnclosingType() != null)
102 // sourceTypes[0] = sourceTypes[0].getEnclosingType();
104 CompilationResult result =
105 new CompilationResult(sourceTypes[0].getFileName(), 1, 1, 10); //this.options.maxProblemsPerUnit);
107 // need to hold onto this
108 CompilationUnitDeclaration unit =
109 SourceTypeConverter.buildCompilationUnit(
110 sourceTypes,//sourceTypes[0] is always toplevel here
111 true, // need field and methods
112 true, // need member types
113 true, // need field initialization
114 lookupEnvironment.problemReporter,
118 this.lookupEnvironment.buildTypeBindings(unit);
119 this.lookupEnvironment.completeTypeBindings(unit, true);
124 * Low-level API performing the actual compilation
126 protected static IErrorHandlingPolicy getHandlingPolicy() {
127 return DefaultErrorHandlingPolicies.proceedWithAllProblems();
130 protected static INameEnvironment getNameEnvironment(ICompilationUnit sourceUnit)
131 throws JavaModelException {
132 return (SearchableEnvironment) ((JavaProject) sourceUnit.getJavaProject())
133 .getSearchableNameEnvironment();
137 * Answer the component to which will be handed back compilation results from the compiler
139 protected static ICompilerRequestor getRequestor() {
140 return new ICompilerRequestor() {
141 public void acceptResult(CompilationResult compilationResult) {
146 protected static IProblemFactory getProblemFactory(
147 final char[] fileName,
148 final IProblemRequestor problemRequestor,
149 final IProgressMonitor monitor) {
151 return new DefaultProblemFactory(Locale.getDefault()) {
152 public IProblem createProblem(
153 char[] originatingFileName,
155 String[] problemArguments,
156 String[] messageArguments,
162 if (monitor != null && monitor.isCanceled()){
163 throw new AbortCompilation(true, null); // silent abort
176 // only report local problems
177 if (CharOperation.equals(originatingFileName, fileName)){
178 if (JavaModelManager.VERBOSE){
179 System.out.println("PROBLEM FOUND while reconciling : "+problem.getMessage());//$NON-NLS-1$
181 problemRequestor.acceptProblem(problem);
183 if (monitor != null && monitor.isCanceled()){
184 throw new AbortCompilation(true, null); // silent abort
192 public static CompilationUnitDeclaration process(
193 CompilationUnitDeclaration unit,
194 ICompilationUnit unitElement,
197 WorkingCopyOwner workingCopyOwner,
198 IProblemRequestor problemRequestor,
199 IProblemFactory problemFactory,
201 IProgressMonitor monitor)
202 throws JavaModelException {
204 char[] fileName = unitElement.getElementName().toCharArray();
206 JavaProject project = (JavaProject) unitElement.getJavaProject();
207 CompilationUnitProblemFinder problemFinder =
208 new CompilationUnitProblemFinder(
209 project.newSearchableNameEnvironment(workingCopyOwner),
211 project.getOptions(true),
214 if (parser != null) {
215 problemFinder.parser = parser;
220 IPackageFragment packageFragment = (IPackageFragment)unitElement.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
221 char[][] expectedPackageName = null;
222 if (packageFragment != null){
223 expectedPackageName = CharOperation.splitOn('.', packageFragment.getElementName().toCharArray());
226 unit = problemFinder.resolve(
227 new BasicCompilationUnit(
230 new String(fileName),
232 true, // verify methods
233 true); //, // analyze code
234 //true); // generate code
236 problemFinder.resolve(
238 null, // no need for source
239 true, // verify methods
240 true); //, // analyze code
241 // true); // generate code
243 reportProblems(unit, problemRequestor, monitor);
245 } catch(RuntimeException e) {
246 // avoid breaking other tools due to internal compiler failure (40334)
247 Util.log(e, "Exception occurred during problem detection: "); //$NON-NLS-1$
248 throw new JavaModelException(e, IJavaModelStatusConstants.COMPILER_FAILURE);
250 if (cleanupCU && unit != null) {
253 problemFinder.lookupEnvironment.reset();
256 public static CompilationUnitDeclaration process(
257 ICompilationUnit unitElement,
259 WorkingCopyOwner workingCopyOwner,
260 IProblemRequestor problemRequestor,
262 IProgressMonitor monitor)
263 throws JavaModelException {
265 return process(null/*no CompilationUnitDeclaration*/, unitElement, contents, null/*use default Parser*/, workingCopyOwner, problemRequestor, new DefaultProblemFactory(), cleanupCU, monitor);
267 public static CompilationUnitDeclaration process(
268 ICompilationUnit unitElement,
269 IProblemRequestor problemRequestor,
270 IProgressMonitor monitor)
271 throws JavaModelException {
273 char[] fileName = unitElement.getElementName().toCharArray();
275 IJavaProject project = unitElement.getJavaProject();
276 CompilationUnitProblemFinder problemFinder =
277 new CompilationUnitProblemFinder(
278 getNameEnvironment(unitElement),
280 project.getOptions(true),
282 getProblemFactory(fileName, problemRequestor, monitor));
284 CompilationUnitDeclaration unit = null;
286 String encoding = project.getOption(JavaCore.CORE_ENCODING, true);
288 IPackageFragment packageFragment = (IPackageFragment)unitElement.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
289 char[][] expectedPackageName = null;
290 if (packageFragment != null){
291 expectedPackageName = CharOperation.splitOn('.', packageFragment.getElementName().toCharArray());
293 unit = problemFinder.resolve(
294 new BasicCompilationUnit(
295 unitElement.getSource().toCharArray(),
297 new String(fileName),
299 true, // verify methods
300 true); // analyze code
301 // true); // generate code
307 problemFinder.lookupEnvironment.reset();
310 private static void reportProblems(CompilationUnitDeclaration unit, IProblemRequestor problemRequestor, IProgressMonitor monitor) {
311 CompilationResult unitResult = unit.compilationResult;
312 IProblem[] problems = unitResult.getAllProblems();
313 for (int i = 0, problemLength = problems == null ? 0 : problems.length; i < problemLength; i++) {
314 if (JavaModelManager.VERBOSE){
315 System.out.println("PROBLEM FOUND while reconciling : "+problems[i].getMessage());//$NON-NLS-1$
317 if (monitor != null && monitor.isCanceled()) break;
319 problemRequestor.acceptProblem(problems[i]);