cd1009384e152a5ebff1b3b293f555208435e0ca
[phpeclipse.git] /
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
12
13 import java.util.Locale;
14 import java.util.Map;
15
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;
42
43 import org.eclipse.core.runtime.IProgressMonitor;
44
45 /**
46  * Responsible for resolving types inside a compilation unit being reconciled,
47  * reporting the discovered problems to a given IProblemRequestor.
48  */
49 public class CompilationUnitProblemFinder extends Compiler {
50
51         /**
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.
55          *
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
62          *            rules.
63          *
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
70          * 
71          *      @param settings The settings to use for the resolution.
72          *      
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
78          *
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.
85          */
86         protected CompilationUnitProblemFinder(
87                 INameEnvironment environment,
88                 IErrorHandlingPolicy policy,
89                 Map settings,
90                 ICompilerRequestor requestor,
91                 IProblemFactory problemFactory) {
92
93                 super(environment, policy, settings, requestor, problemFactory, true);
94         }
95
96         /**
97          * Add additional source types
98          */
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();
103
104                 CompilationResult result =
105                         new CompilationResult(sourceTypes[0].getFileName(), 1, 1, 10); //this.options.maxProblemsPerUnit);
106
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,
115                                 result);
116
117                 if (unit != null) {
118                         this.lookupEnvironment.buildTypeBindings(unit);
119                         this.lookupEnvironment.completeTypeBindings(unit, true);
120                 }
121         }
122
123         /*
124          *  Low-level API performing the actual compilation
125          */
126         protected static IErrorHandlingPolicy getHandlingPolicy() {
127                 return DefaultErrorHandlingPolicies.proceedWithAllProblems();
128         }
129
130         protected static INameEnvironment getNameEnvironment(ICompilationUnit sourceUnit)
131                 throws JavaModelException {
132                 return (SearchableEnvironment) ((JavaProject) sourceUnit.getJavaProject())
133                         .getSearchableNameEnvironment();
134         }
135
136         /*
137          * Answer the component to which will be handed back compilation results from the compiler
138          */
139         protected static ICompilerRequestor getRequestor() {
140                 return new ICompilerRequestor() {
141                         public void acceptResult(CompilationResult compilationResult) {
142                         }
143                 };
144         }
145
146         protected static IProblemFactory getProblemFactory(
147                 final char[] fileName, 
148                 final IProblemRequestor problemRequestor,
149                 final IProgressMonitor monitor) {
150
151                 return new DefaultProblemFactory(Locale.getDefault()) {
152                         public IProblem createProblem(
153                                 char[] originatingFileName,
154                                 int problemId,
155                                 String[] problemArguments,
156                                 String[] messageArguments,
157                                 int severity,
158                                 int startPosition,
159                                 int endPosition,
160                                 int lineNumber) {
161
162                                 if (monitor != null && monitor.isCanceled()){
163                                         throw new AbortCompilation(true, null); // silent abort
164                                 }
165                                 
166                                 IProblem problem =
167                                         super.createProblem(
168                                                 originatingFileName,
169                                                 problemId,
170                                                 problemArguments,
171                                                 messageArguments,
172                                                 severity,
173                                                 startPosition,
174                                                 endPosition,
175                                                 lineNumber);
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$
180                                         }
181                                         problemRequestor.acceptProblem(problem);
182                                 }
183                                 if (monitor != null && monitor.isCanceled()){
184                                         throw new AbortCompilation(true, null); // silent abort
185                                 }
186
187                                 return problem;
188                         }
189                 };
190         }
191
192         public static CompilationUnitDeclaration process(
193                         CompilationUnitDeclaration unit,
194                         ICompilationUnit unitElement, 
195                         char[] contents,
196                         UnitParser parser,
197                         WorkingCopyOwner workingCopyOwner,
198                         IProblemRequestor problemRequestor,
199                         IProblemFactory problemFactory,
200                         boolean cleanupCU,
201                         IProgressMonitor monitor)
202                         throws JavaModelException {
203
204                         char[] fileName = unitElement.getElementName().toCharArray();
205                         
206                         JavaProject project = (JavaProject) unitElement.getJavaProject();
207                         CompilationUnitProblemFinder problemFinder =
208                                 new CompilationUnitProblemFinder(
209                                         project.newSearchableNameEnvironment(workingCopyOwner),
210                                         getHandlingPolicy(),
211                                         project.getOptions(true),
212                                         getRequestor(),
213                                         problemFactory);
214                         if (parser != null) {
215                                 problemFinder.parser = parser;
216                         }
217
218                         try {
219                                 
220                                 IPackageFragment packageFragment = (IPackageFragment)unitElement.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
221                                 char[][] expectedPackageName = null;
222                                 if (packageFragment != null){
223                                         expectedPackageName = CharOperation.splitOn('.', packageFragment.getElementName().toCharArray());
224                                 }
225                                 if (unit == null) {
226                                         unit = problemFinder.resolve(
227                                                 new BasicCompilationUnit(
228                                                         contents,
229                                                         expectedPackageName,
230                                                         new String(fileName),
231                                                         unitElement),
232                                                 true, // verify methods
233                                                 true); //, // analyze code
234                                                 //true); // generate code
235                                 } else {
236                                         problemFinder.resolve(
237                                                 unit,
238                                                 null, // no need for source
239                                                 true, // verify methods
240                                                 true); //, // analyze code
241                                         //      true); // generate code
242                                 }
243                                 reportProblems(unit, problemRequestor, monitor);
244                                 return unit;
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);
249                         } finally {
250                                 if (cleanupCU && unit != null) {
251                                         unit.cleanUp();
252                                 }
253                                 problemFinder.lookupEnvironment.reset();                        
254                         }
255                 }
256         public static CompilationUnitDeclaration process(
257                         ICompilationUnit unitElement, 
258                         char[] contents,
259                         WorkingCopyOwner workingCopyOwner,
260                         IProblemRequestor problemRequestor,
261                         boolean cleanupCU,
262                         IProgressMonitor monitor)
263                         throws JavaModelException {
264                                 
265                         return process(null/*no CompilationUnitDeclaration*/, unitElement, contents, null/*use default Parser*/, workingCopyOwner, problemRequestor, new DefaultProblemFactory(), cleanupCU, monitor);
266                 }
267         public static CompilationUnitDeclaration process(
268                 ICompilationUnit unitElement, 
269                 IProblemRequestor problemRequestor,
270                 IProgressMonitor monitor)
271                 throws JavaModelException {
272
273                 char[] fileName = unitElement.getElementName().toCharArray();
274                 
275                 IJavaProject project = unitElement.getJavaProject();
276                 CompilationUnitProblemFinder problemFinder =
277                         new CompilationUnitProblemFinder(
278                                 getNameEnvironment(unitElement),
279                                 getHandlingPolicy(),
280                                 project.getOptions(true),
281                                 getRequestor(),
282                                 getProblemFactory(fileName, problemRequestor, monitor));
283
284                 CompilationUnitDeclaration unit = null;
285                 try {
286                         String encoding = project.getOption(JavaCore.CORE_ENCODING, true);
287                         
288                         IPackageFragment packageFragment = (IPackageFragment)unitElement.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
289                         char[][] expectedPackageName = null;
290                         if (packageFragment != null){
291                                 expectedPackageName = CharOperation.splitOn('.', packageFragment.getElementName().toCharArray());
292                         }
293                         unit = problemFinder.resolve(
294                                         new BasicCompilationUnit(
295                                                 unitElement.getSource().toCharArray(),
296                                                 expectedPackageName,
297                                                 new String(fileName),
298                                                 encoding),
299                                         true, // verify methods
300                                         true); // analyze code
301 //                                      true); // generate code
302                         return unit;
303                 } finally {
304                         if (unit != null) {
305                                 unit.cleanUp();
306                         }
307                         problemFinder.lookupEnvironment.reset();                        
308                 }
309         }
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$
316                         }
317                         if (monitor != null && monitor.isCanceled()) break;
318                         problemRequestor.acceptProblem(problems[i]);                            
319                 }
320         }
321 }       
322