Skip to content

Commit 4367fc5

Browse files
committed
Adding source for 4.2 and dual transpiler working
1 parent b045499 commit 4367fc5

File tree

740 files changed

+72777
-94178
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

740 files changed

+72777
-94178
lines changed
629 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20231110160943
1+
20231111183955
Binary file not shown.
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20231110160943
1+
20231111183955

sources/net.sf.j2s.core/src/j2s/CorePlugin.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55

66
/**
77
* The main plugin class to be used in the desktop.
8+
*
9+
* Note that Eclipse must be started with the -clean flag if it is to register
10+
* the bundle version properly. So we use VERSION here instead
11+
*
12+
* This version should be also placed in MANIFEST.MF for the bundle version
13+
*
14+
* This wrapper class does nothing in and of itself. See the
15+
* Java2ScriptCompilationParticipant for that.
16+
*
817
*/
918
public class CorePlugin extends Plugin {
1019

@@ -16,11 +25,6 @@ public class CorePlugin extends Plugin {
1625
*
1726
* the actual "x.y.z" version is specified in plugin.xml.
1827
*
19-
* Note that Eclipse must be started with the -clean flag if it is to register
20-
* the bundle version properly. So we use VERSION here instead
21-
*
22-
* This version should be also placed in MANIFEST.MF for the bundle version
23-
*
2428
*/
2529
public static String VERSION = "5.0.1-v1";
2630

sources/net.sf.j2s.core/src/j2s/core/Java2ScriptCompilationParticipant.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package j2s.core;
22

33
import j2s.CorePlugin;
4+
5+
import java.io.File;
46
import java.util.ArrayList;
57
import java.util.Date;
68

79
import org.eclipse.core.resources.IFile;
10+
import org.eclipse.core.runtime.IPath;
811
import org.eclipse.jdt.core.IJavaProject;
912
import org.eclipse.jdt.core.compiler.BuildContext;
1013
import org.eclipse.jdt.core.compiler.ReconcileContext;
1114

1215
/**
13-
* New Java2Script compiler uses org.eclipse.jdt.core.compiler.CompilationParticipant instead of builder
16+
* The entry point from Eclipse once the Plugin is running.
1417
*
1518
* source: https://github.com/eclipse/org.aspectj.shadows/blob/master/org.eclipse.jdt.core/model/org/eclipse/jdt/core/compiler/CompilationParticipant.java
1619
*
@@ -30,6 +33,8 @@ public Java2ScriptCompilationParticipant() {
3033
}
3134

3235
/**
36+
* Question from Eclipse...is this one of your projects?
37+
*
3338
* Returns whether this participant is active for a given project.
3439
* <p>
3540
* Default is to return <code>false</code>.
@@ -45,18 +50,18 @@ public Java2ScriptCompilationParticipant() {
4550
*/
4651
@Override
4752
public boolean isActive(IJavaProject project) {
48-
if (project.getProject().getLocation() == null) {
53+
IPath path = project.getProject().getLocation();
54+
if (path == null) {
4955
// happens when comparing to team...show history item
5056
return false;
5157
}
52-
String j2sFileName = Java2ScriptCompiler.getJ2SConfigName(project);
53-
boolean isj2s = (j2sFileName != null);
58+
File f = Java2ScriptCompiler.checkJ2SDir(path.toOSString());
59+
boolean isj2s = (f != null);
5460
String loc = " " + project.getProject().getLocation() + " ";
55-
// notify only if changed
56-
String key = j2sFileName + "," + isj2s + "," + loc + ";";
61+
String key = f + "," + isj2s + "," + loc + ";";
5762
if (isActiveNotified.indexOf(key) < 0) {
58-
System.out.println("J2S isActive " + (isj2s ? j2sFileName : "false") + loc);
59-
isActiveNotified = isActiveNotified.replace(j2sFileName + (!isj2s) + loc, "");
63+
// notify only if changed
64+
System.out.println("J2S isActive " + (isj2s ? f : "false") + loc);
6065
isActiveNotified += key;
6166
}
6267
return isj2s;
@@ -130,18 +135,16 @@ public void buildStarting(BuildContext[] files, boolean isBatch) {
130135
*/
131136
@Override
132137
public void buildFinished(IJavaProject project) {
133-
String projectName = project.getProject().getName();
134-
138+
String projectName = project.getProject().getName();
135139
if (contexts != null && contexts.size() > 0) {
136-
Java2ScriptCompiler j2sCompiler = Java2ScriptCompiler.newCompiler(project);
140+
Java2ScriptCompiler j2sCompiler = Java2ScriptCompiler.newCompiler(project, contexts.get(0));
137141
if (j2sCompiler == null) {
138142
System.out.println("No .j2s or .j2sjmol file in project found for " + projectName);
139143
return;
140144
}
141145
System.out.println("J2S using transpiler " + j2sCompiler.getClass().getName());
142-
j2sCompiler.startBuild(isCleanBuild);
143-
if (!j2sCompiler.initializeProject(project)) {
144-
System.out.println("J2S .j2s disabled");
146+
if (!j2sCompiler.initializeProject(project, isCleanBuild)) {
147+
System.out.println("J2S project disabled");
145148
return;
146149
}
147150
boolean breakOnError = j2sCompiler.doBreakOnError();

sources/net.sf.j2s.core/src/j2s/core/Java2ScriptCompiler.java

Lines changed: 99 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
import java.util.Properties;
1616

1717
import org.eclipse.core.resources.IFile;
18+
import org.eclipse.core.resources.IProject;
1819
import org.eclipse.core.runtime.IPath;
20+
import org.eclipse.jdt.core.IClasspathEntry;
1921
import org.eclipse.jdt.core.IJavaProject;
2022
import org.eclipse.jdt.core.JavaModelException;
23+
import org.eclipse.jdt.core.compiler.BuildContext;
2124
import org.eclipse.jdt.core.dom.ASTParser;
2225

2326
import j2s.CorePlugin;
@@ -42,7 +45,6 @@ public abstract class Java2ScriptCompiler {
4245
private final static String J2S_COMPILER_STATUS_ENABLED = "enabled";
4346

4447
private final static String J2S_COMPILER_JAVA_VERSION = "j2s.compiler.java.version";
45-
private final static String J2S_COMPILER_JAVA_VERSION_DEFAULT = "8";
4648

4749
private final static String J2S_OUTPUT_PATH = "j2s.output.path";
4850
private final static String J2S_OUTPUT_PATH_DEFAULT = "bin";
@@ -74,10 +76,11 @@ public abstract class Java2ScriptCompiler {
7476
*
7577
* process the source file into JavaScript using the JDT abstract syntax tree
7678
* parser and visitor
79+
* @param isCleanBuild2
7780
*
7881
* @param javaSource
7982
*/
80-
abstract public boolean initializeProject(IJavaProject project);
83+
abstract public boolean initializeProject(IJavaProject project, boolean isCleanBuild2);
8184

8285
abstract public boolean compileToJavaScript(IFile javaSource, String trailer);
8386

@@ -134,34 +137,84 @@ public boolean doBreakOnError() {
134137
private final HashSet<String> copiedResourcePackages = new HashSet<String>();
135138

136139

140+
static File checkJ2SDir(String dir) {
141+
System.out.println("Checking for .j2s or .j2sjmol in " + dir);
142+
File f;
143+
return ((f = new File(dir, J2S_CONFIG_JMOL)).exists() ? f
144+
: (f = new File(dir, J2S_CONFIG_SWINGJS)).exists() ? f
145+
: null);
146+
}
147+
137148
/**
138-
* Check to see if this project is what we need
149+
* Entry point from compilation participant when Java build is complete and it is our turn.
150+
*
139151
* @param project
152+
* @param files
153+
* @return
154+
*/
155+
public static Java2ScriptCompiler newCompiler(IJavaProject project, BuildContext[] files) {
156+
if (files.length == 0)
157+
return null;
158+
String j2stype;
159+
File f = getJ2SConfigName(project, files[0]);
160+
return ( f == null ? null
161+
: J2S_CONFIG_JMOL.equals(j2stype = f.getName()) ?
162+
new Java2ScriptLegacyCompiler(f)
163+
: J2S_CONFIG_SWINGJS.equals(j2stype) ?
164+
new Java2ScriptSwingJSCompiler(f) : null);
165+
}
166+
167+
/**
168+
* Called by newCompiler only. Checks in the root of the classpath for this file
169+
* first, then in the project root directory.
170+
*
171+
* Check to see if this project is what we need for the FIRST file being
172+
* activated. Note that this means that if there are multiple classpath entries,
173+
* EACH should have this .j2smol file in it.
174+
*
175+
* @param project
176+
* @param files
177+
* @param retFile
140178
* @return ".j2s" or ".j2sjmol" or null
141179
*/
142-
public static String getJ2SConfigName(IJavaProject project) {
180+
private static File getJ2SConfigName(IJavaProject project, BuildContext files) {
181+
182+
File f = null;
143183
try {
144-
String dir = project.getProject().getLocation().toOSString();
145-
return (new File(dir, J2S_CONFIG_JMOL).exists() ? J2S_CONFIG_JMOL
146-
: new File(dir, J2S_CONFIG_SWINGJS).exists() ? J2S_CONFIG_SWINGJS
147-
: null);
148-
} catch (@SuppressWarnings("unused") Exception e) {
149-
return null;
184+
if (files == null)
185+
return null;
186+
String projectDir = project.getProject().getLocation().toOSString();
187+
IPath path = getFirstSourceClassPathEntry(project.getResolvedClasspath(true));
188+
String dir = new File(projectDir).getParent() + path; // is relative to workspace, I guess.
189+
System.out.println("checking entry for " + dir);
190+
f = checkJ2SDir(dir);
191+
if (f == null) {
192+
f = checkJ2SDir(projectDir);
193+
}
194+
} catch (@SuppressWarnings("unused") JavaModelException e1) {
195+
// no matter;
196+
} catch (Exception e) {
197+
e.printStackTrace();
150198
}
199+
return f;
151200
}
152201

153-
public static Java2ScriptCompiler newCompiler(IJavaProject project) {
154-
String j2stype = getJ2SConfigName(project);
155-
return ( J2S_CONFIG_JMOL.equals(j2stype) ?
156-
new Java2ScriptLegacyCompiler()
157-
: J2S_CONFIG_SWINGJS.equals(j2stype) ?
158-
new Java2ScriptSwingJSCompiler() : null);
202+
private static IPath getFirstSourceClassPathEntry(IClasspathEntry[] path) {
203+
for (int i = 0; i < path.length; i++) {
204+
IClasspathEntry e = path[i];
205+
if (e.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
206+
System.out.println(i + " " + e + "\n" + e.getPath());
207+
return e.getPath();
208+
}
209+
}
210+
return null;
159211
}
160212

161-
protected Java2ScriptCompiler(boolean isSwingJS, String j2sConfigFileName) {
213+
protected Java2ScriptCompiler(boolean isSwingJS, File f) {
162214
this.isSwingJS = isSwingJS;
163-
this.j2sConfigFileName = j2sConfigFileName;
164-
System.out.println("Java2ScriptCompiler " + this + " isSwingJS=" + isSwingJS + " " + j2sConfigFileName);
215+
activeJ2SFile = f;
216+
j2sConfigFileName = f.getName();
217+
System.out.println("Java2ScriptCompiler " + this + " isSwingJS=" + f + " " + j2sConfigFileName);
165218
// initialized only once for SwingJS and once for legacy version
166219
}
167220

@@ -183,9 +236,9 @@ protected void startBuild(boolean isClean) {
183236
* @param j2sFile
184237
* @param altLevel
185238
*/
186-
protected Properties initializeUsing(File j2sFile, int altLevel) {
187-
System.out.println("J2S using configuration file " + j2sFile);
188-
Properties props = new Properties();
239+
protected Properties getPropsForDir(String dir, String j2sConfigName, int altLevel) {
240+
File j2sFile = new File(dir, j2sConfigName);
241+
Properties props = new Properties();
189242
try (FileInputStream os = new FileInputStream(j2sFile)) {
190243
props.load(os);
191244
os.close();
@@ -204,38 +257,31 @@ protected Properties initializeUsing(File j2sFile, int altLevel) {
204257
* get all necessary .j2s params for a build
205258
*
206259
* @param project
260+
* @param isCleanBuild
207261
* @param jls4
208262
* @return true if this is a j2s project and is enabled
209263
*
210264
*/
211-
protected boolean initializeProject(IJavaProject project, int javaLanguageLevel) {
265+
protected boolean initializeProject(IJavaProject project, boolean isCleanBuild, int javaLanguageLevel) {
212266
this.project = project;
213-
if (!j2sConfigFileName.equals(getJ2SConfigName(project))) {
214-
// the file .j2s does not exist in the project directory -- skip this project
267+
projectFolder = project.getProject().getLocation().toOSString();
268+
startBuild(isCleanBuild);
269+
props = getPropsForDir(activeJ2SFile.getParent(), j2sConfigFileName, 0);
270+
System.out.println(this.getClass().getName() + " " + activeJ2SFile + " " + props);
271+
if (!isEnabled()) {
215272
return false;
216273
}
217-
projectFolder = project.getProject().getLocation().toOSString();
218-
File j2sFile = new File(projectFolder, j2sConfigFileName);
219-
props = initializeUsing(j2sFile, 0);
220-
if (props == null)
221-
props = new Properties();
222-
String status = getProperty(J2S_COMPILER_STATUS, J2S_COMPILER_STATUS_ENABLED);
223-
if (!J2S_COMPILER_STATUS_ENABLE.equalsIgnoreCase(status)
224-
&& !J2S_COMPILER_STATUS_ENABLED.equalsIgnoreCase(status)) {
225-
if (getFileContents(j2sFile).trim().length() == 0) {
226-
writeToFile(j2sFile, getDefaultJ2SFile());
227-
} else {
228-
// not enabled
229-
return false;
230-
}
274+
if (getFileContents(activeJ2SFile).trim().length() == 0) {
275+
writeToFile(activeJ2SFile, getDefaultJ2SFile());
231276
}
232277
int jslLevel = javaLanguageLevel;
233278
if (jslLevel == 8) {
234279
// SwingJS allows 8 or 11
235280
try {
236-
String ver = getProperty(J2S_COMPILER_JAVA_VERSION, J2S_COMPILER_JAVA_VERSION_DEFAULT);
281+
String ver = getProperty(J2S_COMPILER_JAVA_VERSION, "" + jslLevel);
237282
jslLevel = Integer.parseInt(ver);
238283
} catch (@SuppressWarnings("unused") Exception e) {
284+
System.out.println("j2s.compiler.java.version should be one of 4, 8, or 11");
239285
// ignore
240286
}
241287
}
@@ -260,14 +306,14 @@ protected boolean initializeProject(IJavaProject project, int javaLanguageLevel)
260306

261307
outputPath = getProperty(J2S_OUTPUT_PATH, null);
262308
if (outputPath == null) {
263-
outputPath = J2S_OUTPUT_PATH_DEFAULT; // bin
309+
outputPath = J2S_OUTPUT_PATH_DEFAULT; // bin
264310
try {
265311
IPath loc = project.getOutputLocation();
266312
outputPath = loc.toString().substring(loc.toString().lastIndexOf('/') + 1);
267313
} catch (JavaModelException e1) {
268314
// TODO Auto-generated catch block
269315
e1.printStackTrace();
270-
}
316+
}
271317
}
272318

273319
if (isDebugging) {
@@ -294,6 +340,12 @@ protected boolean initializeProject(IJavaProject project, int javaLanguageLevel)
294340
return true;
295341
}
296342

343+
private boolean isEnabled() {
344+
String status = getProperty(J2S_COMPILER_STATUS, J2S_COMPILER_STATUS_ENABLED);
345+
return (J2S_COMPILER_STATUS_ENABLE.equalsIgnoreCase(status)
346+
|| J2S_COMPILER_STATUS_ENABLED.equalsIgnoreCase(status));
347+
}
348+
297349
boolean excludeFile(IFile javaSource) {
298350
return excludeFile(javaSource.getFullPath().toString());
299351
}
@@ -311,12 +363,16 @@ private boolean excludeFile(String filePath) {
311363
}
312364

313365
protected String getProperty(String key, String def) {
366+
if (props == null) {
367+
System.out.println("getting " + key + " props is null");
368+
return null;
369+
}
314370
String val = props.getProperty(key);
315371
if (val == null)
316372
val = def;
317-
System.out.println(key + " = " + val);
318373
if (val != null && val.indexOf("<") == 0)
319374
val = null;
375+
System.out.println("getting " + key + " = " + val);
320376
return val;
321377
}
322378

sources/net.sf.j2s.core/src/j2s/jmol/Java2ScriptLegacyCompiler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ public class Java2ScriptLegacyCompiler extends Java2ScriptCompiler {
2626

2727
private int nResources;
2828

29-
public Java2ScriptLegacyCompiler() {
30-
super(false, J2S_CONFIG_JMOL);
29+
public Java2ScriptLegacyCompiler(File f) {
30+
super(false, f);
3131
}
3232

3333
@SuppressWarnings("deprecation")
34-
public boolean initializeProject(IJavaProject project) {
35-
if (!super.initializeProject(project, AST.JLS4)) {
34+
public boolean initializeProject(IJavaProject project, boolean isCleanBuild) {
35+
if (!super.initializeProject(project, isCleanBuild, AST.JLS4)) {
3636
return false;
3737
}
3838

0 commit comments

Comments
 (0)