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.IJavaProject;
19 import net.sourceforge.phpdt.core.IPackageFragment;
20 import net.sourceforge.phpdt.core.IProblemRequestor;
21 import net.sourceforge.phpdt.core.JavaCore;
22 import net.sourceforge.phpdt.core.JavaModelException;
23 import net.sourceforge.phpdt.core.compiler.CharOperation;
24 import net.sourceforge.phpdt.core.compiler.IProblem;
25 import net.sourceforge.phpdt.internal.compiler.CompilationResult;
26 import net.sourceforge.phpdt.internal.compiler.Compiler;
27 import net.sourceforge.phpdt.internal.compiler.DefaultErrorHandlingPolicies;
28 import net.sourceforge.phpdt.internal.compiler.ICompilerRequestor;
29 import net.sourceforge.phpdt.internal.compiler.IErrorHandlingPolicy;
30 import net.sourceforge.phpdt.internal.compiler.IProblemFactory;
31 import net.sourceforge.phpdt.internal.compiler.env.INameEnvironment;
32 import net.sourceforge.phpdt.internal.compiler.env.ISourceType;
33 import net.sourceforge.phpdt.internal.compiler.lookup.PackageBinding;
34 import net.sourceforge.phpdt.internal.compiler.parser.SourceTypeConverter;
35 import net.sourceforge.phpdt.internal.compiler.problem.AbortCompilation;
36 import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblemFactory;
37 import net.sourceforge.phpeclipse.internal.compiler.ast.CompilationUnitDeclaration;
39 import org.eclipse.core.runtime.IProgressMonitor;
42 * Responsible for resolving types inside a compilation unit being reconciled,
43 * reporting the discovered problems to a given IProblemRequestor.
45 public class CompilationUnitProblemFinder extends Compiler {
48 * Answer a new CompilationUnitVisitor using the given name environment and compiler options.
49 * The environment and options will be in effect for the lifetime of the compiler.
50 * When the compiler is run, compilation results are sent to the given requestor.
52 * @param environment org.eclipse.jdt.internal.compiler.api.env.INameEnvironment
53 * Environment used by the compiler in order to resolve type and package
54 * names. The name environment implements the actual connection of the compiler
55 * to the outside world (e.g. in batch mode the name environment is performing
56 * pure file accesses, reuse previous build state or connection to repositories).
57 * Note: the name environment is responsible for implementing the actual classpath
60 * @param policy org.eclipse.jdt.internal.compiler.api.problem.IErrorHandlingPolicy
61 * Configurable part for problem handling, allowing the compiler client to
62 * specify the rules for handling problems (stop on first error or accumulate
63 * them all) and at the same time perform some actions such as opening a dialog
64 * in UI when compiling interactively.
65 * @see org.eclipse.jdt.internal.compiler.api.problem.DefaultErrorHandlingPolicies
67 * @param settings The settings to use for the resolution.
69 * @param requestor org.eclipse.jdt.internal.compiler.api.ICompilerRequestor
70 * Component which will receive and persist all compilation results and is intended
71 * to consume them as they are produced. Typically, in a batch compiler, it is
72 * responsible for writing out the actual .class files to the file system.
73 * @see org.eclipse.jdt.internal.compiler.api.CompilationResult
75 * @param problemFactory org.eclipse.jdt.internal.compiler.api.problem.IProblemFactory
76 * Factory used inside the compiler to create problem descriptors. It allows the
77 * compiler client to supply its own representation of compilation problems in
78 * order to avoid object conversions. Note that the factory is not supposed
79 * to accumulate the created problems, the compiler will gather them all and hand
80 * them back as part of the compilation unit result.
82 protected CompilationUnitProblemFinder(
83 INameEnvironment environment,
84 IErrorHandlingPolicy policy,
86 ICompilerRequestor requestor,
87 IProblemFactory problemFactory) {
89 super(environment, policy, settings, requestor, problemFactory, true);
93 * Add additional source types
95 public void accept(ISourceType[] sourceTypes, PackageBinding packageBinding) {
96 // ensure to jump back to toplevel type for first one (could be a member)
97 // while (sourceTypes[0].getEnclosingType() != null)
98 // sourceTypes[0] = sourceTypes[0].getEnclosingType();
100 CompilationResult result =
101 new CompilationResult(sourceTypes[0].getFileName(), 1, 1, 10); //this.options.maxProblemsPerUnit);
103 // need to hold onto this
104 CompilationUnitDeclaration unit =
105 SourceTypeConverter.buildCompilationUnit(
106 sourceTypes,//sourceTypes[0] is always toplevel here
107 true, // need field and methods
108 true, // need member types
109 true, // need field initialization
110 lookupEnvironment.problemReporter,
114 this.lookupEnvironment.buildTypeBindings(unit);
115 this.lookupEnvironment.completeTypeBindings(unit, true);
120 * Low-level API performing the actual compilation
122 protected static IErrorHandlingPolicy getHandlingPolicy() {
123 return DefaultErrorHandlingPolicies.proceedWithAllProblems();
126 protected static INameEnvironment getNameEnvironment(ICompilationUnit sourceUnit)
127 throws JavaModelException {
128 return (SearchableEnvironment) ((JavaProject) sourceUnit.getJavaProject())
129 .getSearchableNameEnvironment();
133 * Answer the component to which will be handed back compilation results from the compiler
135 protected static ICompilerRequestor getRequestor() {
136 return new ICompilerRequestor() {
137 public void acceptResult(CompilationResult compilationResult) {
142 protected static IProblemFactory getProblemFactory(
143 final char[] fileName,
144 final IProblemRequestor problemRequestor,
145 final IProgressMonitor monitor) {
147 return new DefaultProblemFactory(Locale.getDefault()) {
148 public IProblem createProblem(
149 char[] originatingFileName,
151 String[] problemArguments,
152 String[] messageArguments,
158 if (monitor != null && monitor.isCanceled()){
159 throw new AbortCompilation(true, null); // silent abort
172 // only report local problems
173 if (CharOperation.equals(originatingFileName, fileName)){
174 if (JavaModelManager.VERBOSE){
175 System.out.println("PROBLEM FOUND while reconciling : "+problem.getMessage());//$NON-NLS-1$
177 problemRequestor.acceptProblem(problem);
179 if (monitor != null && monitor.isCanceled()){
180 throw new AbortCompilation(true, null); // silent abort
188 public static CompilationUnitDeclaration process(
189 ICompilationUnit unitElement,
190 IProblemRequestor problemRequestor,
191 IProgressMonitor monitor)
192 throws JavaModelException {
194 char[] fileName = unitElement.getElementName().toCharArray();
196 IJavaProject project = unitElement.getJavaProject();
197 CompilationUnitProblemFinder problemFinder =
198 new CompilationUnitProblemFinder(
199 getNameEnvironment(unitElement),
201 project.getOptions(true),
203 getProblemFactory(fileName, problemRequestor, monitor));
205 CompilationUnitDeclaration unit = null;
207 String encoding = project.getOption(JavaCore.CORE_ENCODING, true);
209 IPackageFragment packageFragment = (IPackageFragment)unitElement.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
210 char[][] expectedPackageName = null;
211 if (packageFragment != null){
212 expectedPackageName = CharOperation.splitOn('.', packageFragment.getElementName().toCharArray());
214 unit = problemFinder.resolve(
215 new BasicCompilationUnit(
216 unitElement.getSource().toCharArray(),
218 new String(fileName),
220 true, // verify methods
221 true); // analyze code
222 // true); // generate code
228 problemFinder.lookupEnvironment.reset();