PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/aspectj-1.6.9/aspectjtools1.6.9/org/aspectj/org/eclipse/jdt/internal/core/CompilationUnitProblemFinder.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 260 lines | 173 code | 18 blank | 69 comment | 22 complexity | ae36bf54acd6490310214e23d3f78a1a MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2007 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.aspectj.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.aspectj.org.eclipse.jdt.core.*;
  17. import org.aspectj.org.eclipse.jdt.core.compiler.CategorizedProblem;
  18. import org.aspectj.org.eclipse.jdt.internal.compiler.*;
  19. import org.aspectj.org.eclipse.jdt.internal.compiler.Compiler;
  20. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
  21. import org.aspectj.org.eclipse.jdt.internal.compiler.env.AccessRestriction;
  22. import org.aspectj.org.eclipse.jdt.internal.compiler.env.INameEnvironment;
  23. import org.aspectj.org.eclipse.jdt.internal.compiler.env.ISourceType;
  24. import org.aspectj.org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
  25. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.PackageBinding;
  26. import org.aspectj.org.eclipse.jdt.internal.compiler.parser.Parser;
  27. import org.aspectj.org.eclipse.jdt.internal.compiler.parser.SourceTypeConverter;
  28. import org.aspectj.org.eclipse.jdt.internal.core.util.CommentRecorderParser;
  29. import org.aspectj.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.aspectj.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.aspectj.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.aspectj.org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies
  54. *
  55. * @param compilerOptions The compiler options to use for the resolution.
  56. *
  57. * @param requestor org.aspectj.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.aspectj.org.eclipse.jdt.internal.compiler.CompilationResult
  62. *
  63. * @param problemFactory org.aspectj.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. // need to hold onto this
  93. CompilationUnitDeclaration unit =
  94. SourceTypeConverter.buildCompilationUnit(
  95. sourceTypes,//sourceTypes[0] is always toplevel here
  96. SourceTypeConverter.FIELD_AND_METHOD // need field and methods
  97. | SourceTypeConverter.MEMBER_TYPE // need member types
  98. | SourceTypeConverter.FIELD_INITIALIZATION, // need field initialization
  99. this.lookupEnvironment.problemReporter,
  100. result);
  101. if (unit != null) {
  102. this.lookupEnvironment.buildTypeBindings(unit, accessRestriction);
  103. this.lookupEnvironment.completeTypeBindings(unit);
  104. }
  105. }
  106. protected static CompilerOptions getCompilerOptions(Map settings, boolean creatingAST, boolean statementsRecovery) {
  107. CompilerOptions compilerOptions = new CompilerOptions(settings);
  108. compilerOptions.performMethodsFullRecovery = statementsRecovery;
  109. compilerOptions.performStatementsRecovery = statementsRecovery;
  110. compilerOptions.parseLiteralExpressionsAsConstants = !creatingAST; /*parse literal expressions as constants only if not creating a DOM AST*/
  111. compilerOptions.storeAnnotations = creatingAST; /*store annotations in the bindings if creating a DOM AST*/
  112. return compilerOptions;
  113. }
  114. /*
  115. * Low-level API performing the actual compilation
  116. */
  117. protected static IErrorHandlingPolicy getHandlingPolicy() {
  118. return DefaultErrorHandlingPolicies.proceedWithAllProblems();
  119. }
  120. /*
  121. * Answer the component to which will be handed back compilation results from the compiler
  122. */
  123. protected static ICompilerRequestor getRequestor() {
  124. return new ICompilerRequestor() {
  125. public void acceptResult(CompilationResult compilationResult) {
  126. // default requestor doesn't handle compilation results back
  127. }
  128. };
  129. }
  130. public static CompilationUnitDeclaration process(
  131. CompilationUnitDeclaration unit,
  132. ICompilationUnit unitElement,
  133. char[] contents,
  134. Parser parser,
  135. WorkingCopyOwner workingCopyOwner,
  136. HashMap problems,
  137. boolean creatingAST,
  138. int reconcileFlags,
  139. IProgressMonitor monitor)
  140. throws JavaModelException {
  141. JavaProject project = (JavaProject) unitElement.getJavaProject();
  142. CancelableNameEnvironment environment = null;
  143. CancelableProblemFactory problemFactory = null;
  144. CompilationUnitProblemFinder problemFinder = null;
  145. try {
  146. environment = new CancelableNameEnvironment(project, workingCopyOwner, monitor);
  147. problemFactory = new CancelableProblemFactory(monitor);
  148. problemFinder = new CompilationUnitProblemFinder(
  149. environment,
  150. getHandlingPolicy(),
  151. getCompilerOptions(project.getOptions(true), creatingAST, ((reconcileFlags & ICompilationUnit.ENABLE_STATEMENTS_RECOVERY) != 0)),
  152. getRequestor(),
  153. problemFactory);
  154. if (parser != null) {
  155. problemFinder.parser = parser;
  156. }
  157. PackageFragment packageFragment = (PackageFragment)unitElement.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
  158. char[][] expectedPackageName = null;
  159. if (packageFragment != null){
  160. expectedPackageName = Util.toCharArrays(packageFragment.names);
  161. }
  162. if (unit == null) {
  163. unit = problemFinder.resolve(
  164. new BasicCompilationUnit(
  165. contents,
  166. expectedPackageName,
  167. unitElement.getPath().toString(),
  168. unitElement),
  169. true, // verify methods
  170. true, // analyze code
  171. true); // generate code
  172. } else {
  173. problemFinder.resolve(
  174. unit,
  175. null, // no need for source
  176. true, // verify methods
  177. true, // analyze code
  178. true); // generate code
  179. }
  180. CompilationResult unitResult = unit.compilationResult;
  181. CategorizedProblem[] unitProblems = unitResult.getProblems();
  182. int length = unitProblems == null ? 0 : unitProblems.length;
  183. if (length > 0) {
  184. CategorizedProblem[] categorizedProblems = new CategorizedProblem[length];
  185. System.arraycopy(unitProblems, 0, categorizedProblems, 0, length);
  186. problems.put(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER, categorizedProblems);
  187. }
  188. unitProblems = unitResult.getTasks();
  189. length = unitProblems == null ? 0 : unitProblems.length;
  190. if (length > 0) {
  191. CategorizedProblem[] categorizedProblems = new CategorizedProblem[length];
  192. System.arraycopy(unitProblems, 0, categorizedProblems, 0, length);
  193. problems.put(IJavaModelMarker.TASK_MARKER, categorizedProblems);
  194. }
  195. if (NameLookup.VERBOSE) {
  196. System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInSourcePackage: " + environment.nameLookup.timeSpentInSeekTypesInSourcePackage + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
  197. System.out.println(Thread.currentThread() + " TIME SPENT in NameLoopkup#seekTypesInBinaryPackage: " + environment.nameLookup.timeSpentInSeekTypesInBinaryPackage + "ms"); //$NON-NLS-1$ //$NON-NLS-2$
  198. }
  199. return unit;
  200. } catch (OperationCanceledException e) {
  201. throw e;
  202. } catch(RuntimeException e) {
  203. // avoid breaking other tools due to internal compiler failure (40334)
  204. String lineDelimiter = unitElement.findRecommendedLineSeparator();
  205. StringBuffer message = new StringBuffer("Exception occurred during problem detection:"); //$NON-NLS-1$
  206. message.append(lineDelimiter);
  207. message.append("----------------------------------- SOURCE BEGIN -------------------------------------"); //$NON-NLS-1$
  208. message.append(lineDelimiter);
  209. message.append(contents);
  210. message.append(lineDelimiter);
  211. message.append("----------------------------------- SOURCE END -------------------------------------"); //$NON-NLS-1$
  212. Util.log(e, message.toString());
  213. throw new JavaModelException(e, IJavaModelStatusConstants.COMPILER_FAILURE);
  214. } finally {
  215. if (environment != null)
  216. environment.monitor = null; // don't hold a reference to this external object
  217. if (problemFactory != null)
  218. problemFactory.monitor = null; // don't hold a reference to this external object
  219. // NB: unit.cleanUp() is done by caller
  220. if (problemFinder != null && !creatingAST)
  221. problemFinder.lookupEnvironment.reset();
  222. }
  223. }
  224. public static CompilationUnitDeclaration process(
  225. ICompilationUnit unitElement,
  226. char[] contents,
  227. WorkingCopyOwner workingCopyOwner,
  228. HashMap problems,
  229. boolean creatingAST,
  230. int reconcileFlags,
  231. IProgressMonitor monitor)
  232. throws JavaModelException {
  233. return process(null/*no CompilationUnitDeclaration*/, unitElement, contents, null/*use default Parser*/, workingCopyOwner, problems, creatingAST, reconcileFlags, monitor);
  234. }
  235. /* (non-Javadoc)
  236. * Fix for bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=60689.
  237. * @see org.aspectj.org.eclipse.jdt.internal.compiler.Compiler#initializeParser()
  238. */
  239. public void initializeParser() {
  240. this.parser = new CommentRecorderParser(this.problemReporter, this.options.parseLiteralExpressionsAsConstants);
  241. }
  242. }