1 11 package org.eclipse.jdt.internal.core; 12 13 import java.util.Locale ; 14 import java.util.Map ; 15 16 import org.eclipse.jdt.core.ICompilationUnit; 17 import org.eclipse.jdt.core.IJavaElement; 18 import org.eclipse.jdt.core.JavaModelException; 19 import org.eclipse.jdt.core.compiler.IProblem; 20 import org.eclipse.jdt.internal.compiler.CompilationResult; 21 import org.eclipse.jdt.internal.compiler.Compiler; 22 import org.eclipse.jdt.internal.compiler.ASTVisitor; 23 import org.eclipse.jdt.internal.compiler.ICompilerRequestor; 24 import org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy; 25 import org.eclipse.jdt.internal.compiler.IProblemFactory; 26 import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; 27 import org.eclipse.jdt.internal.compiler.env.AccessRestriction; 28 import org.eclipse.jdt.internal.compiler.env.INameEnvironment; 29 import org.eclipse.jdt.internal.compiler.env.ISourceType; 30 import org.eclipse.jdt.internal.compiler.lookup.PackageBinding; 31 import org.eclipse.jdt.internal.compiler.parser.SourceTypeConverter; 32 import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory; 33 import org.eclipse.jdt.internal.core.util.Util; 34 35 public class CompilationUnitVisitor extends Compiler { 36 37 70 public CompilationUnitVisitor( 71 INameEnvironment environment, 72 IErrorHandlingPolicy policy, 73 Map settings, 74 ICompilerRequestor requestor, 75 IProblemFactory problemFactory) { 76 77 super(environment, policy, settings, requestor, problemFactory); 78 } 79 80 83 public void accept(ISourceType[] sourceTypes, PackageBinding packageBinding, AccessRestriction accessRestriction) { 84 CompilationResult result = 85 new CompilationResult(sourceTypes[0].getFileName(), 1, 1, this.options.maxProblemsPerUnit); 86 CompilationUnitDeclaration unit = 88 SourceTypeConverter.buildCompilationUnit( 89 sourceTypes, SourceTypeConverter.FIELD_AND_METHOD | SourceTypeConverter.MEMBER_TYPE, this.lookupEnvironment.problemReporter, 94 result); 95 96 if (unit != null) { 97 this.lookupEnvironment.buildTypeBindings(unit, accessRestriction); 98 this.lookupEnvironment.completeTypeBindings(unit, true); 99 } 100 } 101 102 105 protected static IErrorHandlingPolicy getHandlingPolicy() { 106 107 return new IErrorHandlingPolicy() { 109 public boolean stopOnFirstError() { 110 return false; 111 } 112 public boolean proceedOnErrors() { 113 return false; } 115 }; 116 } 117 118 121 protected static ICompilerRequestor getRequestor() { 122 return new ICompilerRequestor() { 123 public void acceptResult(CompilationResult compilationResult) { 124 } 126 }; 127 } 128 129 public static void visit( 130 ICompilationUnit unitElement, 131 ASTVisitor visitor) 132 throws JavaModelException { 133 134 JavaProject project = (JavaProject) unitElement.getJavaProject(); 135 CompilationUnitVisitor compilationUnitVisitor = 136 new CompilationUnitVisitor( 137 project.newSearchableNameEnvironment(unitElement.getOwner()), 138 getHandlingPolicy(), 139 project.getOptions(true), 140 getRequestor(), 141 getProblemFactory(visitor)); 142 143 CompilationUnitDeclaration unit = null; 144 try { 145 146 PackageFragment packageFragment = (PackageFragment)unitElement.getAncestor(IJavaElement.PACKAGE_FRAGMENT); 147 char[][] expectedPackageName = null; 148 if (packageFragment != null){ 149 expectedPackageName = Util.toCharArrays(packageFragment.names); 150 } 151 unit = 152 compilationUnitVisitor.resolve( 153 new BasicCompilationUnit( 154 unitElement.getSource().toCharArray(), 155 expectedPackageName, 156 unitElement.getElementName(), 157 unitElement), 158 true, false, false); if (unit != null) { 162 unit.traverse(visitor, unit.scope); 163 } 164 } finally { 165 if (unit != null) { 166 unit.cleanUp(); 167 } 168 } 169 } 170 171 protected static IProblemFactory getProblemFactory(final ASTVisitor visitor) { 172 173 return new DefaultProblemFactory(Locale.getDefault()) { 174 public IProblem createProblem( 175 char[] originatingFileName, 176 int problemId, 177 String [] problemArguments, 178 String [] messageArguments, 179 int severity, 180 int startPosition, 181 int endPosition, 182 int lineNumber) { 183 184 IProblem problem = 185 super.createProblem( 186 originatingFileName, 187 problemId, 188 problemArguments, 189 messageArguments, 190 severity, 191 startPosition, 192 endPosition, 193 lineNumber); 194 visitor.acceptProblem(problem); 195 return problem; 196 } 197 }; 198 } 199 } 200 | Popular Tags |