Added "Task Tags" functionality (TODO,...)
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / CompilationUnitProblemFinder.java
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.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;
38
39 import org.eclipse.core.runtime.IProgressMonitor;
40
41 /**
42  * Responsible for resolving types inside a compilation unit being reconciled,
43  * reporting the discovered problems to a given IProblemRequestor.
44  */
45 public class CompilationUnitProblemFinder extends Compiler {
46
47         /**
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.
51          *
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
58          *            rules.
59          *
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
66          * 
67          *      @param settings The settings to use for the resolution.
68          *      
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
74          *
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.
81          */
82         protected CompilationUnitProblemFinder(
83                 INameEnvironment environment,
84                 IErrorHandlingPolicy policy,
85                 Map settings,
86                 ICompilerRequestor requestor,
87                 IProblemFactory problemFactory) {
88
89                 super(environment, policy, settings, requestor, problemFactory, true);
90         }
91
92         /**
93          * Add additional source types
94          */
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();
99
100                 CompilationResult result =
101                         new CompilationResult(sourceTypes[0].getFileName(), 1, 1, 10); //this.options.maxProblemsPerUnit);
102
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,
111                                 result);
112
113                 if (unit != null) {
114                         this.lookupEnvironment.buildTypeBindings(unit);
115                         this.lookupEnvironment.completeTypeBindings(unit, true);
116                 }
117         }
118
119         /*
120          *  Low-level API performing the actual compilation
121          */
122         protected static IErrorHandlingPolicy getHandlingPolicy() {
123                 return DefaultErrorHandlingPolicies.proceedWithAllProblems();
124         }
125
126         protected static INameEnvironment getNameEnvironment(ICompilationUnit sourceUnit)
127                 throws JavaModelException {
128                 return (SearchableEnvironment) ((JavaProject) sourceUnit.getJavaProject())
129                         .getSearchableNameEnvironment();
130         }
131
132         /*
133          * Answer the component to which will be handed back compilation results from the compiler
134          */
135         protected static ICompilerRequestor getRequestor() {
136                 return new ICompilerRequestor() {
137                         public void acceptResult(CompilationResult compilationResult) {
138                         }
139                 };
140         }
141
142         protected static IProblemFactory getProblemFactory(
143                 final char[] fileName, 
144                 final IProblemRequestor problemRequestor,
145                 final IProgressMonitor monitor) {
146
147                 return new DefaultProblemFactory(Locale.getDefault()) {
148                         public IProblem createProblem(
149                                 char[] originatingFileName,
150                                 int problemId,
151                                 String[] problemArguments,
152                                 String[] messageArguments,
153                                 int severity,
154                                 int startPosition,
155                                 int endPosition,
156                                 int lineNumber) {
157
158                                 if (monitor != null && monitor.isCanceled()){
159                                         throw new AbortCompilation(true, null); // silent abort
160                                 }
161                                 
162                                 IProblem problem =
163                                         super.createProblem(
164                                                 originatingFileName,
165                                                 problemId,
166                                                 problemArguments,
167                                                 messageArguments,
168                                                 severity,
169                                                 startPosition,
170                                                 endPosition,
171                                                 lineNumber);
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$
176                                         }
177                                         problemRequestor.acceptProblem(problem);
178                                 }
179                                 if (monitor != null && monitor.isCanceled()){
180                                         throw new AbortCompilation(true, null); // silent abort
181                                 }
182
183                                 return problem;
184                         }
185                 };
186         }
187
188         public static CompilationUnitDeclaration process(
189                 ICompilationUnit unitElement, 
190                 IProblemRequestor problemRequestor,
191                 IProgressMonitor monitor)
192                 throws JavaModelException {
193
194                 char[] fileName = unitElement.getElementName().toCharArray();
195                 
196                 IJavaProject project = unitElement.getJavaProject();
197                 CompilationUnitProblemFinder problemFinder =
198                         new CompilationUnitProblemFinder(
199                                 getNameEnvironment(unitElement),
200                                 getHandlingPolicy(),
201                                 project.getOptions(true),
202                                 getRequestor(),
203                                 getProblemFactory(fileName, problemRequestor, monitor));
204
205                 CompilationUnitDeclaration unit = null;
206                 try {
207                         String encoding = project.getOption(JavaCore.CORE_ENCODING, true);
208                         
209                         IPackageFragment packageFragment = (IPackageFragment)unitElement.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
210                         char[][] expectedPackageName = null;
211                         if (packageFragment != null){
212                                 expectedPackageName = CharOperation.splitOn('.', packageFragment.getElementName().toCharArray());
213                         }
214                         unit = problemFinder.resolve(
215                                         new BasicCompilationUnit(
216                                                 unitElement.getSource().toCharArray(),
217                                                 expectedPackageName,
218                                                 new String(fileName),
219                                                 encoding),
220                                         true, // verify methods
221                                         true); // analyze code
222 //                                      true); // generate code
223                         return unit;
224                 } finally {
225                         if (unit != null) {
226                                 unit.cleanUp();
227                         }
228                         problemFinder.lookupEnvironment.reset();                        
229                 }
230         }
231 }       
232