You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AjCompiler.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /********************************************************************
  2. * Copyright (c) 2007 Contributors. All rights reserved.
  3. * This program and the accompanying materials are made available
  4. * under the terms of the Eclipse Public License v 2.0
  5. * which accompanies this distribution and is available at
  6. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  7. *
  8. * Contributors: IBM Corporation - initial API and implementation
  9. * Helen Hawkins - initial version (bug 148190)
  10. *******************************************************************/
  11. package org.aspectj.ajde.core;
  12. import java.io.File;
  13. import org.aspectj.ajde.core.internal.AjdeCoreBuildManager;
  14. import org.aspectj.ajdt.internal.core.builder.AjState;
  15. import org.aspectj.ajdt.internal.core.builder.IncrementalStateManager;
  16. import org.aspectj.asm.AsmManager;
  17. import org.aspectj.bridge.IMessage;
  18. import org.aspectj.bridge.Message;
  19. import org.aspectj.org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
  20. /**
  21. * The class to be used by tools to drive a build. An AjCompiler is created with a unique id (for example the absolute pathname of a
  22. * project or .lst file) along with implementations of ICompilerConfiguration, IBuildProgressMonitor and IBuildMessageHandler. Tools
  23. * then call build() or buildFresh() on this AjCompiler.
  24. *
  25. * <p>
  26. * An AjCompiler is associated with one id, therefore a new one needs to be created for a new id (project, .lst file etc.). It is
  27. * the responsibility of the tools to manage the lifecycle of the AjCompiler's.
  28. */
  29. public class AjCompiler {
  30. private final String compilerId;
  31. private final ICompilerConfiguration compilerConfig;
  32. private final IBuildProgressMonitor monitor;
  33. private final IBuildMessageHandler handler;
  34. private final AjdeCoreBuildManager buildManager;
  35. /**
  36. * Creates a new AjCompiler for the given id, ICompilerConfiguration, IBuildProgressMonitor and IBuildMessageHandler. None of
  37. * the arguments can be null.
  38. *
  39. * @param compilerId - Unique String used to identify this AjCompiler
  40. * @param compilerConfig - ICompilerConfiguration implementation
  41. * @param buildProgressMonitor - IBuildProgressMonitor implementation
  42. * @param buildMessageHandler - IBuildMessageHandler implementation
  43. */
  44. public AjCompiler(String compilerId, ICompilerConfiguration compilerConfig, IBuildProgressMonitor buildProgressMonitor,
  45. IBuildMessageHandler buildMessageHandler) {
  46. this.compilerConfig = compilerConfig;
  47. this.monitor = buildProgressMonitor;
  48. this.handler = buildMessageHandler;
  49. this.compilerId = compilerId;
  50. this.buildManager = new AjdeCoreBuildManager(this);
  51. }
  52. /**
  53. * @return the id for this AjCompiler
  54. */
  55. public String getId() {
  56. return compilerId;
  57. }
  58. /**
  59. * @return the ICompilerConfiguration associated with this AjCompiler
  60. */
  61. public ICompilerConfiguration getCompilerConfiguration() {
  62. return compilerConfig;
  63. }
  64. /**
  65. * @return the IBuildProgressMonitor associated with this AjCompiler
  66. */
  67. public IBuildProgressMonitor getBuildProgressMonitor() {
  68. return monitor;
  69. }
  70. /**
  71. * @return the IBuildMessageHandler associated with this AjCompiler
  72. */
  73. public IBuildMessageHandler getMessageHandler() {
  74. return handler;
  75. }
  76. /**
  77. * Perform an incremental build if possible, otherwise it will default to a full build.
  78. */
  79. public void build() {
  80. if (hasValidId()) {
  81. buildManager.performBuild(false);
  82. }
  83. }
  84. /**
  85. * Perform a full build.
  86. */
  87. public void buildFresh() {
  88. if (hasValidId()) {
  89. buildManager.performBuild(true);
  90. }
  91. }
  92. /**
  93. * Clear the incremental state associated with this AjCompiler from the IncrementalStateManager. This is necessary until AjState
  94. * is reworked and there's an AjState associated with an AjCompiler rather than requiring a map of them. If the environment is
  95. * not cleaned up then jar locks may be kept.
  96. */
  97. public void clearLastState() {
  98. IncrementalStateManager.removeIncrementalStateInformationFor(compilerId);
  99. buildManager.cleanupEnvironment();
  100. }
  101. public boolean addDependencies(File file, String[] typeNameDependencies) {
  102. AjState state = IncrementalStateManager.retrieveStateFor(compilerId);
  103. return state.recordDependencies(file, typeNameDependencies);
  104. }
  105. /**
  106. * @return true if the underlying version of the compiler is compatible with Java 6, returns false otherwise.
  107. */
  108. public boolean isJava6Compatible() {
  109. return CompilerOptions.versionToJdkLevel(JavaOptions.VERSION_16) != 0;
  110. }
  111. /**
  112. * Ensures that the id associated with this compiler is non-null. If it is null then sends an ABORT message to the
  113. * messageHandler.
  114. */
  115. private boolean hasValidId() {
  116. if (compilerId == null) {
  117. Message msg = new Message("compiler didn't have an id associated with it", IMessage.ABORT, null, null);
  118. handler.handleMessage(msg);
  119. return false;
  120. }
  121. return true;
  122. }
  123. /**
  124. * Set a CustomMungerFactory to the compiler's weaver
  125. *
  126. * The type of factory should be org.aspectj.weaver.CustomMungerFactory but due to dependency problem of project ajde.core, it
  127. * is Object for now.
  128. *
  129. * @param factory
  130. */
  131. public void setCustomMungerFactory(Object factory) {
  132. buildManager.setCustomMungerFactory(factory);
  133. }
  134. /**
  135. * @return the CustomMungerFactory from the compiler's weaver
  136. *
  137. * The return type should be org.aspectj.weaver.CustomMungerFactory but due to dependency problem of project ajde.core,
  138. * it is Object for now.
  139. */
  140. public Object getCustomMungerFactory() {
  141. return buildManager.getCustomMungerFactory();
  142. }
  143. public AsmManager getModel() {
  144. return buildManager.getStructureModel();
  145. }
  146. public AjdeCoreBuildManager getBuildManager() {
  147. return buildManager;
  148. }
  149. }