1 11 package org.eclipse.jdt.internal.core; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 15 16 import org.eclipse.core.runtime.IProgressMonitor; 17 import org.eclipse.core.runtime.OperationCanceledException; 18 import org.eclipse.jdt.core.*; 19 import org.eclipse.jdt.core.compiler.CategorizedProblem; 20 import org.eclipse.jdt.internal.compiler.*; 21 import org.eclipse.jdt.internal.compiler.Compiler; 22 import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; 23 import org.eclipse.jdt.internal.compiler.env.AccessRestriction; 24 import org.eclipse.jdt.internal.compiler.env.INameEnvironment; 25 import org.eclipse.jdt.internal.compiler.env.ISourceType; 26 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; 27 import org.eclipse.jdt.internal.compiler.lookup.PackageBinding; 28 import org.eclipse.jdt.internal.compiler.parser.Parser; 29 import org.eclipse.jdt.internal.compiler.parser.SourceTypeConverter; 30 import org.eclipse.jdt.internal.core.util.CommentRecorderParser; 31 import org.eclipse.jdt.internal.core.util.Util; 32 33 37 public class CompilationUnitProblemFinder extends Compiler { 38 39 74 protected CompilationUnitProblemFinder( 75 INameEnvironment environment, 76 IErrorHandlingPolicy policy, 77 CompilerOptions compilerOptions, 78 ICompilerRequestor requestor, 79 IProblemFactory problemFactory) { 80 81 super(environment, 82 policy, 83 compilerOptions, 84 requestor, 85 problemFactory 86 ); 87 } 88 89 92 public void accept(ISourceType[] sourceTypes, PackageBinding packageBinding, AccessRestriction accessRestriction) { 93 97 CompilationResult result = 98 new CompilationResult(sourceTypes[0].getFileName(), 1, 1, this.options.maxProblemsPerUnit); 99 100 CompilationUnitDeclaration unit = 102 SourceTypeConverter.buildCompilationUnit( 103 sourceTypes, SourceTypeConverter.FIELD_AND_METHOD | SourceTypeConverter.MEMBER_TYPE | SourceTypeConverter.FIELD_INITIALIZATION, this.lookupEnvironment.problemReporter, 108 result); 109 110 if (unit != null) { 111 this.lookupEnvironment.buildTypeBindings(unit, accessRestriction); 112 this.lookupEnvironment.completeTypeBindings(unit); 113 } 114 } 115 116 protected static CompilerOptions getCompilerOptions(Map settings, boolean creatingAST, boolean statementsRecovery) { 117 CompilerOptions compilerOptions = new CompilerOptions(settings); 118 compilerOptions.performMethodsFullRecovery = statementsRecovery; 119 compilerOptions.performStatementsRecovery = statementsRecovery; 120 compilerOptions.parseLiteralExpressionsAsConstants = !creatingAST; 121 compilerOptions.storeAnnotations = creatingAST; 122 return compilerOptions; 123 } 124 125 128 protected static IErrorHandlingPolicy getHandlingPolicy() { 129 return DefaultErrorHandlingPolicies.proceedWithAllProblems(); 130 } 131 132 135 protected static ICompilerRequestor getRequestor() { 136 return new ICompilerRequestor() { 137 public void acceptResult(CompilationResult compilationResult) { 138 } 140 }; 141 } 142 143 public static CompilationUnitDeclaration process( 144 CompilationUnitDeclaration unit, 145 ICompilationUnit unitElement, 146 char[] contents, 147 Parser parser, 148 WorkingCopyOwner workingCopyOwner, 149 HashMap problems, 150 boolean creatingAST, 151 int reconcileFlags, 152 IProgressMonitor monitor) 153 throws JavaModelException { 154 155 JavaProject project = (JavaProject) unitElement.getJavaProject(); 156 CancelableNameEnvironment environment = null; 157 CancelableProblemFactory problemFactory = null; 158 CompilationUnitProblemFinder problemFinder = null; 159 try { 160 environment = new CancelableNameEnvironment(project, workingCopyOwner, monitor); 161 problemFactory = new CancelableProblemFactory(monitor); 162 problemFinder = new CompilationUnitProblemFinder( 163 environment, 164 getHandlingPolicy(), 165 getCompilerOptions(project.getOptions(true), creatingAST, ((reconcileFlags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0)), 166 getRequestor(), 167 problemFactory); 168 if (parser != null) { 169 problemFinder.parser = parser; 170 } 171 PackageFragment packageFragment = (PackageFragment)unitElement.getAncestor(IJavaElement.PACKAGE_FRAGMENT); 172 char[][] expectedPackageName = null; 173 if (packageFragment != null){ 174 expectedPackageName = Util.toCharArrays(packageFragment.names); 175 } 176 if (unit == null) { 177 unit = problemFinder.resolve( 178 new BasicCompilationUnit( 179 contents, 180 expectedPackageName, 181 unitElement.getPath().toString(), 182 unitElement), 183 true, true, true); } else { 187 problemFinder.resolve( 188 unit, 189 null, true, true, true); } 194 CompilationResult unitResult = unit.compilationResult; 195 CategorizedProblem[] unitProblems = unitResult.getProblems(); 196 int length = unitProblems == null ? 0 : unitProblems.length; 197 if (length > 0) { 198 CategorizedProblem[] categorizedProblems = new CategorizedProblem[length]; 199 System.arraycopy(unitProblems, 0, categorizedProblems, 0, length); 200 problems.put(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER, categorizedProblems); 201 } 202 unitProblems = unitResult.getTasks(); 203 length = unitProblems == null ? 0 : unitProblems.length; 204 if (length > 0) { 205 CategorizedProblem[] categorizedProblems = new CategorizedProblem[length]; 206 System.arraycopy(unitProblems, 0, categorizedProblems, 0, length); 207 problems.put(IJavaModelMarker.TASK_MARKER, categorizedProblems); 208 } 209 if (NameLookup.VERBOSE) { 210 System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " + environment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms"); System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " + environment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms"); } 213 return unit; 214 } catch (OperationCanceledException e) { 215 throw e; 216 } catch(RuntimeException e) { 217 String lineDelimiter = unitElement.findRecommendedLineSeparator(); 219 StringBuffer message = new StringBuffer ("Exception occurred during problem detection:"); message.append(lineDelimiter); 221 message.append("----------------------------------- SOURCE BEGIN -------------------------------------"); message.append(lineDelimiter); 223 message.append(contents); 224 message.append(lineDelimiter); 225 message.append("----------------------------------- SOURCE END -------------------------------------"); Util.log(e, message.toString()); 227 throw new JavaModelException(e, IJavaModelStatusConstants.COMPILER_FAILURE); 228 } finally { 229 if (environment != null) 230 environment.monitor = null; if (problemFactory != null) 232 problemFactory.monitor = null; if (problemFinder != null && !creatingAST) 235 problemFinder.lookupEnvironment.reset(); 236 } 237 } 238 239 public static CompilationUnitDeclaration process( 240 ICompilationUnit unitElement, 241 char[] contents, 242 WorkingCopyOwner workingCopyOwner, 243 HashMap problems, 244 boolean creatingAST, 245 int reconcileFlags, 246 IProgressMonitor monitor) 247 throws JavaModelException { 248 249 return process(null, unitElement, contents, null, workingCopyOwner, problems, creatingAST, reconcileFlags, monitor); 250 } 251 252 256 public void initializeParser() { 257 this.parser = new CommentRecorderParser(this.problemReporter, this.options.parseLiteralExpressionsAsConstants); 258 } 259 } 260 261 | Popular Tags |