PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/eclipse_SDK-3.7.1/plugins/org.eclipse.jdt.core.source_3.7.1.v_B76_R37x/org/eclipse/jdt/internal/core/CompilationUnitProblemFinder.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 279 lines | 185 code | 20 blank | 74 comment | 22 complexity | 5d484dd540f69e897e1567617e967cc4 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2010 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * IBM Corporation - initial API and implementation
  10. *******************************************************************************/
  11. package org.eclipse.jdt.internal.core;
  12. import java.util.HashMap;
  13. import java.util.Map;
  14. import org.eclipse.core.runtime.IProgressMonitor;
  15. import org.eclipse.core.runtime.OperationCanceledException;
  16. import org.eclipse.jdt.core.*;
  17. import org.eclipse.jdt.core.compiler.CategorizedProblem;
  18. import org.eclipse.jdt.internal.compiler.*;
  19. import org.eclipse.jdt.internal.compiler.Compiler;
  20. import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
  21. import org.eclipse.jdt.internal.compiler.env.AccessRestriction;
  22. import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
  23. import org.eclipse.jdt.internal.compiler.env.ISourceType;
  24. import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
  25. import org.eclipse.jdt.internal.compiler.lookup.PackageBinding;
  26. import org.eclipse.jdt.internal.compiler.parser.SourceTypeConverter;
  27. import org.eclipse.jdt.internal.compiler.problem.AbortCompilation;
  28. import org.eclipse.jdt.internal.core.util.CommentRecorderParser;
  29. import org.eclipse.jdt.internal.core.util.Util;
  30. /**
  31. * Responsible for resolving types inside a compilation unit being reconciled,
  32. * reporting the discovered problems to a given IProblemRequestor.
  33. */
  34. public class CompilationUnitProblemFinder extends Compiler {
  35. /**
  36. * Answer a new CompilationUnitVisitor using the given name environment and compiler options.
  37. * The environment and options will be in effect for the lifetime of the compiler.
  38. * When the compiler is run, compilation results are sent to the given requestor.
  39. *
  40. * @param environment org.eclipse.jdt.internal.compiler.api.env.INameEnvironment
  41. * Environment used by the compiler in order to resolve type and package
  42. * names. The name environment implements the actual connection of the compiler
  43. * to the outside world (e.g. in batch mode the name environment is performing
  44. * pure file accesses, reuse previous build state or connection to repositories).
  45. * Note: the name environment is responsible for implementing the actual classpath
  46. * rules.
  47. *
  48. * @param policy org.eclipse.jdt.internal.compiler.api.problem.IErrorHandlingPolicy
  49. * Configurable part for problem handling, allowing the compiler client to
  50. * specify the rules for handling problems (stop on first error or accumulate
  51. * them all) and at the same time perform some actions such as opening a dialog
  52. * in UI when compiling interactively.
  53. * @see org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies
  54. *
  55. * @param compilerOptions The compiler options to use for the resolution.
  56. *
  57. * @param requestor org.eclipse.jdt.internal.compiler.api.ICompilerRequestor
  58. * Component which will receive and persist all compilation results and is intended
  59. * to consume them as they are produced. Typically, in a batch compiler, it is
  60. * responsible for writing out the actual .class files to the file system.
  61. * @see org.eclipse.jdt.internal.compiler.CompilationResult
  62. *
  63. * @param problemFactory org.eclipse.jdt.internal.compiler.api.problem.IProblemFactory
  64. * Factory used inside the compiler to create problem descriptors. It allows the
  65. * compiler client to supply its own representation of compilation problems in
  66. * order to avoid object conversions. Note that the factory is not supposed
  67. * to accumulate the created problems, the compiler will gather them all and hand
  68. * them back as part of the compilation unit result.
  69. */
  70. protected CompilationUnitProblemFinder(
  71. INameEnvironment environment,
  72. IErrorHandlingPolicy policy,
  73. CompilerOptions compilerOptions,
  74. ICompilerRequestor requestor,
  75. IProblemFactory problemFactory) {
  76. super(environment,
  77. policy,
  78. compilerOptions,
  79. requestor,
  80. problemFactory
  81. );
  82. }
  83. /**
  84. * Add additional source types
  85. */
  86. public void accept(ISourceType[] sourceTypes, PackageBinding packageBinding, AccessRestriction accessRestriction) {
  87. // ensure to jump back to toplevel type for first one (could be a member)
  88. // while (sourceTypes[0].getEnclosingType() != null)
  89. // sourceTypes[0] = sourceTypes[0].getEnclosingType();
  90. CompilationResult result =
  91. new CompilationResult(sourceTypes[0].getFileName(), 1, 1, this.options.maxProblemsPerUnit);
  92. // https://bugs.eclipse.org/bugs/show_bug.cgi?id=305259, build the compilation unit in its own sand box.
  93. final long savedComplianceLevel = this.options.complianceLevel;
  94. final long savedSourceLevel = this.options.sourceLevel;
  95. try {
  96. IJavaProject project = ((SourceTypeElementInfo) sourceTypes[0]).getHandle().getJavaProject();
  97. this.options.complianceLevel = CompilerOptions.versionToJdkLevel(project.getOption(JavaCore.COMPILER_COMPLIANCE, true));
  98. this.options.sourceLevel = CompilerOptions.versionToJdkLevel(project.getOption(JavaCore.COMPILER_SOURCE, true));
  99. // need to hold onto this
  100. CompilationUnitDeclaration unit =
  101. SourceTypeConverter.buildCompilationUnit(
  102. sourceTypes,//sourceTypes[0] is always toplevel here
  103. SourceTypeConverter.FIELD_AND_METHOD // need field and methods
  104. | SourceTypeConverter.MEMBER_TYPE // need member types
  105. | SourceTypeConverter.FIELD_INITIALIZATION, // need field initialization
  106. this.lookupEnvironment.problemReporter,
  107. result);
  108. if (unit != null) {
  109. this.lookupEnvironment.buildTypeBindings(unit, accessRestriction);
  110. this.lookupEnvironment.completeTypeBindings(unit);
  111. }
  112. } finally {
  113. this.options.complianceLevel = savedComplianceLevel;
  114. this.options.sourceLevel = savedSourceLevel;
  115. }
  116. }
  117. protected static CompilerOptions getCompilerOptions(Map settings, boolean creatingAST, boolean statementsRecovery) {
  118. CompilerOptions compilerOptions = new CompilerOptions(settings);
  119. compilerOptions.performMethodsFullRecovery = statementsRecovery;
  120. compilerOptions.performStatementsRecovery = statementsRecovery;
  121. compilerOptions.parseLiteralExpressionsAsConstants = !creatingAST; /*parse literal expressions as constants only if not creating a DOM AST*/
  122. compilerOptions.storeAnnotations = creatingAST; /*store annotations in the bindings if creating a DOM AST*/
  123. return compilerOptions;
  124. }
  125. /*
  126. * Low-level API performing the actual compilation
  127. */
  128. protected static IErrorHandlingPolicy getHandlingPolicy() {
  129. return DefaultErrorHandlingPolicies.proceedWithAllProblems();
  130. }
  131. /*
  132. * Answer the component to which will be handed back compilation results from the compiler
  133. */
  134. protected static ICompilerRequestor getRequestor() {
  135. return new ICompilerRequestor() {
  136. public void acceptResult(CompilationResult compilationResult) {
  137. // default requestor doesn't handle compilation results back
  138. }
  139. };
  140. }
  141. /*
  142. * Can return null if the process was aborted or canceled
  143. */
  144. public static CompilationUnitDeclaration process(
  145. CompilationUnit unitElement,
  146. SourceElementParser parser,
  147. WorkingCopyOwner workingCopyOwner,
  148. HashMap problems,
  149. boolean creatingAST,
  150. int reconcileFlags,
  151. IProgressMonitor monitor)
  152. throws JavaModelException {
  153. JavaProject project = (JavaProject) unitElement.getJavaProject();
  154. CancelableNameEnvironment environment = null;
  155. CancelableProblemFactory problemFactory = null;
  156. CompilationUnitProblemFinder problemFinder = null;
  157. CompilationUnitDeclaration unit = null;
  158. try {
  159. environment = new CancelableNameEnvironment(project, workingCopyOwner, monitor);
  160. problemFactory = new CancelableProblemFactory(monitor);
  161. CompilerOptions compilerOptions = getCompilerOptions(project.getOptions(true), creatingAST, ((reconcileFlags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0));
  162. boolean ignoreMethodBodies = (reconcileFlags & ICompilationUnit.IGNORE_METHOD_BODIES) != 0;
  163. compilerOptions.ignoreMethodBodies = ignoreMethodBodies;
  164. problemFinder = new CompilationUnitProblemFinder(
  165. environment,
  166. getHandlingPolicy(),
  167. compilerOptions,
  168. getRequestor(),
  169. problemFactory);
  170. boolean analyzeAndGenerateCode = true;
  171. if (ignoreMethodBodies) {
  172. analyzeAndGenerateCode = false;
  173. }
  174. try {
  175. if (parser != null) {
  176. problemFinder.parser = parser;
  177. unit = parser.parseCompilationUnit(unitElement, true/*full parse*/, monitor);
  178. problemFinder.resolve(
  179. unit,
  180. unitElement,
  181. true, // verify methods
  182. analyzeAndGenerateCode, // analyze code
  183. analyzeAndGenerateCode); // generate code
  184. } else {
  185. unit =
  186. problemFinder.resolve(
  187. unitElement,
  188. true, // verify methods
  189. analyzeAndGenerateCode, // analyze code
  190. analyzeAndGenerateCode); // generate code
  191. }
  192. } catch (AbortCompilation e) {
  193. problemFinder.handleInternalException(e, unit);
  194. }
  195. if (unit != null) {
  196. CompilationResult unitResult = unit.compilationResult;
  197. CategorizedProblem[] unitProblems = unitResult.getProblems();
  198. int length = unitProblems == null ? 0 : unitProblems.length;
  199. if (length > 0) {
  200. CategorizedProblem[] categorizedProblems = new CategorizedProblem[length];
  201. System.arraycopy(unitProblems, 0, categorizedProblems, 0, length);
  202. problems.put(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER, categorizedProblems);
  203. }
  204. unitProblems = unitResult.getTasks();
  205. length = unitProblems == null ? 0 : unitProblems.length;
  206. if (length > 0) {
  207. CategorizedProblem[] categorizedProblems = new CategorizedProblem[length];
  208. System.arraycopy(unitProblems, 0, categorizedProblems, 0, length);
  209. problems.put(IJavaModelMarker.TASK_MARKER, categorizedProblems);
  210. }
  211. if (NameLookup.VERBOSE) {
  212. System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " + environment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
  213. System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " + environment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
  214. }
  215. }
  216. } catch (OperationCanceledException e) {
  217. // catch this exception so as to not enter the catch(RuntimeException e) below
  218. throw e;
  219. } catch(RuntimeException e) {
  220. // avoid breaking other tools due to internal compiler failure (40334)
  221. String lineDelimiter = unitElement.findRecommendedLineSeparator();
  222. StringBuffer message = new StringBuffer("Exception occurred during problem detection:"); //$NON-NLS-1$
  223. message.append(lineDelimiter);
  224. message.append("----------------------------------- SOURCE BEGIN -------------------------------------"); //$NON-NLS-1$
  225. message.append(lineDelimiter);
  226. message.append(unitElement.getSource());
  227. message.append(lineDelimiter);
  228. message.append("----------------------------------- SOURCE END -------------------------------------"); //$NON-NLS-1$
  229. Util.log(e, message.toString());
  230. throw new JavaModelException(e, IJavaModelStatusConstants.COMPILER_FAILURE);
  231. } finally {
  232. if (environment != null)
  233. environment.setMonitor(null); // don't hold a reference to this external object
  234. if (problemFactory != null)
  235. problemFactory.monitor = null; // don't hold a reference to this external object
  236. // NB: unit.cleanUp() is done by caller
  237. if (problemFinder != null && !creatingAST)
  238. problemFinder.lookupEnvironment.reset();
  239. }
  240. return unit;
  241. }
  242. public static CompilationUnitDeclaration process(
  243. CompilationUnit unitElement,
  244. WorkingCopyOwner workingCopyOwner,
  245. HashMap problems,
  246. boolean creatingAST,
  247. int reconcileFlags,
  248. IProgressMonitor monitor)
  249. throws JavaModelException {
  250. return process(unitElement, null/*use default Parser*/, workingCopyOwner, problems, creatingAST, reconcileFlags, monitor);
  251. }
  252. /* (non-Javadoc)
  253. * Fix for bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=60689.
  254. * @see org.eclipse.jdt.internal.compiler.Compiler#initializeParser()
  255. */
  256. public void initializeParser() {
  257. this.parser = new CommentRecorderParser(this.problemReporter, this.options.parseLiteralExpressionsAsConstants);
  258. }
  259. }