Skip to content

Commit 043b835

Browse files
author
zhourenjian
committed
1. Highly integrate Java2Script's incremental build with Java's. That is to say, compiling one *.java to *.class will now also compile to *.js at the same time.
2. Now Java's javabuilder is replaced with java2scriptbuilder 3. Fix bugs of migrating from Eclipse 3.2 4. Wizards now add Java's classpath variables correctly.
1 parent 6dfc9e3 commit 043b835

20 files changed

+554
-452
lines changed

plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
id="java2scriptbuilder"
5757
name="Java2Script Builder">
5858
<builder>
59-
<run class="net.sf.j2s.core.Java2ScriptProjectBuilder">
59+
<run class="net.sf.j2s.core.builder.Java2ScriptBuilder">
6060
</run>
6161
</builder>
6262
</extension>

src/net/sf/j2s/core/Java2ScriptProject.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
package net.sf.j2s.core;
1313

1414
import org.eclipse.core.resources.IProject;
15+
import org.eclipse.core.runtime.CoreException;
16+
import org.eclipse.jdt.internal.core.ExternalJavaProject;
1517

1618
/**
1719
* @author zhou renjian
@@ -20,10 +22,21 @@
2022
*/
2123
public class Java2ScriptProject {
2224

23-
public static boolean hasJava2ScriptNature(IProject project) {
24-
Java2ScriptProjectNature pn = new Java2ScriptProjectNature();
25-
pn.setProject(project);
26-
return pn.hasNature();
25+
/**
26+
* Returns true if the given project is accessible and it has
27+
* a java nature, otherwise false.
28+
* @param project IProject
29+
* @return boolean
30+
*/
31+
public static boolean hasJava2ScriptNature(IProject project) {
32+
try {
33+
return project.hasNature("net.sf.j2s.java2scriptnature");
34+
} catch (CoreException e) {
35+
if (ExternalJavaProject.EXTERNAL_PROJECT_NAME.equals(project.getName()))
36+
return true;
37+
// project does not exist or is not open
38+
}
39+
return false;
2740
}
2841

2942
}

src/net/sf/j2s/core/Java2ScriptProjectNature.java

Lines changed: 96 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.eclipse.core.resources.IProjectDescription;
1717
import org.eclipse.core.resources.IProjectNature;
1818
import org.eclipse.core.runtime.CoreException;
19+
import org.eclipse.jdt.core.JavaCore;
1920

2021
/**
2122
* @author zhou renjian
@@ -30,12 +31,14 @@ public class Java2ScriptProjectNature implements IProjectNature {
3031
*/
3132
public void configure() throws CoreException {
3233
addToBuildSpec("net.sf.j2s.core.java2scriptbuilder");
34+
removeFromBuildSpec(JavaCore.BUILDER_ID);
3335
}
3436
/* (non-Javadoc)
3537
* @see org.eclipse.core.resources.IProjectNature#deconfigure()
3638
*/
3739
public void deconfigure() throws CoreException {
3840
removeFromBuildSpec("net.sf.j2s.core.java2scriptbuilder");
41+
addToBuildSpec(JavaCore.BUILDER_ID);
3942
}
4043
/* (non-Javadoc)
4144
* @see org.eclipse.core.resources.IProjectNature#getProject()
@@ -53,19 +56,31 @@ public void setProject(IProject project) {
5356
public boolean hasNature() {
5457
try {
5558
IProjectDescription description = this.project.getDescription();
56-
int javaCommandIndex = getJavaCommandIndex(description.getBuildSpec());
59+
int javaCommandIndex = getJava2ScriptCommandIndex(description.getBuildSpec());
5760
return (javaCommandIndex != -1);
5861
} catch (CoreException e) {
5962
e.printStackTrace();
6063
return false;
6164
}
6265
}
63-
66+
6467
/**
6568
* Adds a builder to the build spec for the given project.
6669
*/
6770
public void addToBuildSpec(String builderID) throws CoreException {
68-
//this.project.addToBuildSpec(builderID);
71+
if ("net.sf.j2s.core.java2scriptbuilder".equals(builderID)) {
72+
IProjectDescription description = this.project.getDescription();
73+
int javaCommandIndex = getJava2ScriptCommandIndex(description.getBuildSpec());
74+
75+
if (javaCommandIndex == -1) {
76+
77+
// Add a Java command to the build spec
78+
ICommand command = description.newCommand();
79+
command.setBuilderName(builderID);
80+
setJava2ScriptCommand(description, command);
81+
}
82+
return;
83+
}
6984

7085
IProjectDescription description = this.project.getDescription();
7186
int javaCommandIndex = getJavaCommandIndex(description.getBuildSpec());
@@ -77,13 +92,13 @@ public void addToBuildSpec(String builderID) throws CoreException {
7792
command.setBuilderName(builderID);
7893
setJavaCommand(description, command);
7994
}
80-
}
95+
}
8196

8297
/**
8398
* Update the Java command in the build spec (replace existing one if present,
8499
* add one first if none).
85100
*/
86-
private void setJavaCommand(
101+
private void setJava2ScriptCommand(
87102
IProjectDescription description,
88103
ICommand newCommand)
89104
throws CoreException {
@@ -95,8 +110,36 @@ private void setJavaCommand(
95110
if (oldJavaCommandIndex == -1) {
96111
// Add a Java build spec before other builders (1FWJK7I)
97112
newCommands = new ICommand[oldBuildSpec.length + 1];
98-
System.arraycopy(oldBuildSpec, 0, newCommands, 0, oldBuildSpec.length);
99-
newCommands[oldBuildSpec.length] = newCommand;
113+
System.arraycopy(oldBuildSpec, 0, newCommands, 1, oldBuildSpec.length);
114+
newCommands[0] = newCommand;
115+
} else {
116+
oldBuildSpec[oldJavaCommandIndex] = newCommand;
117+
newCommands = oldBuildSpec;
118+
}
119+
120+
// Commit the spec change into the project
121+
description.setBuildSpec(newCommands);
122+
this.project.setDescription(description, null);
123+
}
124+
125+
/**
126+
* Update the Java command in the build spec (replace existing one if present,
127+
* add one first if none).
128+
*/
129+
private void setJavaCommand(
130+
IProjectDescription description,
131+
ICommand newCommand)
132+
throws CoreException {
133+
134+
ICommand[] oldBuildSpec = description.getBuildSpec();
135+
int oldJavaCommandIndex = getJava2ScriptCommandIndex(oldBuildSpec);
136+
ICommand[] newCommands;
137+
138+
if (oldJavaCommandIndex == -1) {
139+
// Add a Java build spec before other builders (1FWJK7I)
140+
newCommands = new ICommand[oldBuildSpec.length + 1];
141+
System.arraycopy(oldBuildSpec, 0, newCommands, 1, oldBuildSpec.length);
142+
newCommands[0] = newCommand;
100143
} else {
101144
oldBuildSpec[oldJavaCommandIndex] = newCommand;
102145
newCommands = oldBuildSpec;
@@ -111,7 +154,21 @@ private void setJavaCommand(
111154
* Find the specific Java command amongst the given build spec
112155
* and return its index or -1 if not found.
113156
*/
114-
private int getJavaCommandIndex(ICommand[] buildSpec) {
157+
private static int getJavaCommandIndex(ICommand[] buildSpec) {
158+
159+
for (int i = 0; i < buildSpec.length; ++i) {
160+
if (buildSpec[i].getBuilderName().equals(JavaCore.BUILDER_ID)) {
161+
return i;
162+
}
163+
}
164+
return -1;
165+
}
166+
167+
/**
168+
* Find the specific Java2Script command amongst the given build spec
169+
* and return its index or -1 if not found.
170+
*/
171+
private static int getJava2ScriptCommandIndex(ICommand[] buildSpec) {
115172

116173
for (int i = 0; i < buildSpec.length; ++i) {
117174
if (buildSpec[i].getBuilderName().equals("net.sf.j2s.core.java2scriptbuilder")) {
@@ -139,4 +196,35 @@ public void removeFromBuildSpec(String builderID) throws CoreException {
139196
}
140197
}
141198
}
199+
200+
public static boolean hasJavaBuilder(IProject project) {
201+
try {
202+
IProjectDescription description = project.getDescription();
203+
int javaCommandIndex = getJavaCommandIndex(description.getBuildSpec());
204+
return javaCommandIndex != -1;
205+
} catch (CoreException e) {
206+
e.printStackTrace();
207+
}
208+
return false;
209+
}
210+
211+
public static boolean removeJavaBuilder(IProject project) {
212+
try {
213+
IProjectDescription description = project.getDescription();
214+
ICommand[] commands = description.getBuildSpec();
215+
for (int i = 0; i < commands.length; ++i) {
216+
if (commands[i].getBuilderName().equals(JavaCore.BUILDER_ID)) {
217+
ICommand[] newCommands = new ICommand[commands.length - 1];
218+
System.arraycopy(commands, 0, newCommands, 0, i);
219+
System.arraycopy(commands, i + 1, newCommands, i, commands.length - i - 1);
220+
description.setBuildSpec(newCommands);
221+
project.setDescription(description, null);
222+
return true;
223+
}
224+
}
225+
} catch (CoreException e) {
226+
e.printStackTrace();
227+
}
228+
return false;
229+
}
142230
}

0 commit comments

Comments
 (0)