1 package net.sf.j2s.core;
3 import java.util.Arrays;
4 import java.util.Properties;
6 import org.eclipse.jdt.core.IJavaProject;
7 import org.eclipse.jdt.core.compiler.BuildContext;
8 import org.eclipse.jdt.core.compiler.ReconcileContext;
11 * New Java2Script compiler uses org.eclipse.jdt.core.compiler.CompilationParticipant instead of builder
13 * source: https://github.com/eclipse/org.aspectj.shadows/blob/master/org.eclipse.jdt.core/model/org/eclipse/jdt/core/compiler/CompilationParticipant.java
18 public class Java2ScriptCompilationParticipant extends org.eclipse.jdt.core.compiler.CompilationParticipant {
19 private BuildContext[] javaFiles;
20 private boolean isCleanBuild;
22 public Java2ScriptCompilationParticipant() {
23 System.out.println("CompilationParticipant started");
27 * Returns whether this participant is active for a given project.
29 * Default is to return <code>false</code>.
32 * For efficiency, participants that are not interested in the given project
33 * should return <code>false</code> for that project.
37 * the project to participate in
38 * @return whether this participant is active for a given project
40 public boolean isActive(IJavaProject project) {
41 boolean isj2s = Java2ScriptCompiler.isActive(project);
42 System.out.println("isActive " + isj2s + " " + project.getProject().getLocation());
47 * Notifies this participant that a build is about to start and provides it
48 * the opportunity to create missing source folders for generated source
49 * files. Additional source folders should be marked as optional so the
50 * project can be built when the folders do not exist. Only sent to
51 * participants interested in the project.
53 * Default is to return <code>READY_FOR_BUILD</code>.
56 * @see #buildFinished(IJavaProject project)
58 * the project about to build
59 * @return READY_FOR_BUILD or NEEDS_FULL_BUILD
61 public int aboutToBuild(IJavaProject project) {
62 System.out.println("aboutToBuild " + project.getProject().getLocation());
63 return READY_FOR_BUILD;
67 * Notifies this participant that a clean is about to start and provides it
68 * the opportunity to delete generated source files. Only sent to
69 * participants interested in the project.
72 * the project about to be cleaned
74 public void cleanStarting(IJavaProject project) {
75 System.out.println("cleanStarting " + project.getProject().getLocation());
80 * Notifies this participant that a compile operation is about to start and
81 * provides it the opportunity to generate source files based on the source
82 * files about to be compiled. When isBatchBuild is true, then files
83 * contains all source files in the project. Only sent to participants
84 * interested in the current build project.
87 * is an array of BuildContext
89 * identifies when the build is a batch build
91 public void buildStarting(BuildContext[] files, boolean isBatch) {
92 if (javaFiles != null) {
93 BuildContext[] concat = Arrays.copyOf(javaFiles, javaFiles.length + files.length);
94 System.arraycopy(files, 0, concat , javaFiles.length, files.length);
99 System.out.println("buildStarting " + files.length + " files, isBatch=" + isBatch);
103 * Notifies this participant that a build has finished for the project. This
104 * will be sent, even if buildStarting() was not sent when no source files
105 * needed to be compiled or the build failed. Only sent to participants
106 * interested in the project.
109 * the project about to build
112 public void buildFinished(IJavaProject project) {
113 if (javaFiles != null) {
114 Java2ScriptCompiler j2sCompiler = new Java2ScriptCompiler();
115 j2sCompiler.startBuild(isCleanBuild);
116 if (!j2sCompiler.initializeProject(project, true)) {
117 System.out.println(".j2s disabled");
120 System.out.println("building JavaScript " + project.getProject().getLocation());
121 for (int i = 0; i < javaFiles.length; i++) {
122 System.out.println("[" + (i+1) + "/" + javaFiles.length + "] transpiling " + javaFiles[i]);
123 // trying to keep the progess monitor running - didn't work
125 // Thread.currentThread().sleep(1);
126 // } catch (InterruptedException e) {
129 if (!j2sCompiler.compileToJavaScript(javaFiles[i].getFile())) {
130 System.out.println("Error processing " + javaFiles[i].getFile());
135 System.out.println("build finished " + project.getProject().getLocation());
137 isCleanBuild = false;
141 * Returns whether this participant is interested in only Annotations.
143 * Default is to return <code>false</code>.
146 * @return whether this participant is interested in only Annotations.
148 public boolean isAnnotationProcessor() {
153 * Notifies this participant that a compile operation has found source files
154 * using Annotations. Only sent to participants interested in the current
155 * build project that answer true to isAnnotationProcessor(). Each
156 * BuildContext was informed whether its source file currently
160 * is an array of BuildContext
162 public void processAnnotations(BuildContext[] files) {
167 * Notifies this participant that a reconcile operation is happening. The
168 * participant can act on this reconcile operation by using the given
169 * context. Other participant can then see the result of this participation
172 * Note that a participant should not modify the buffer of the working copy
173 * that is being reconciled.
176 * Default is to do nothing.
180 * the reconcile context to act on
182 public void reconcile(ReconcileContext context) {
183 // fired whenever a source file is changed -- before it is saved