Skip to content

Commit df0959d

Browse files
authored
Merge pull request #229 from BobHanson/master
monolithic update from BobHanson/master
2 parents c6c042b + b84c33d commit df0959d

Some content is hidden

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

74 files changed

+9465
-7731
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,31 @@ within Eclipse, with immediate JavaScript testing and debugging in a browser bas
1919
in Java. Using the java2script/SwingJS Eclipse plug-in, both Java .class files and their equivalent .js files are created simultaneously.
2020

2121

22-
java2script/SwingJS includes an Eclipse plug-in (technically a "drop-in"), net.sf.j2s.core.zip
22+
java2script/SwingJS includes an Eclipse plug-in (technically a "drop-in"), net.sf.j2s.cor
23+
24+
e.zip
2325
(see https://github.com/BobHanson/java2script/blob/master/sources/net.sf.j2s.core/dist/swingjs),
2426
along with a JavaScript version of the Java Virtual Machine (SwingJS, https://github.com/BobHanson/java2script/blob/master/sources/net.sf.j2s.java.core/SwingJS-site.zip)
2527
to allow the rapid and
2628
automated production of browser-ready JavaScript versions of Java applications and applets.
2729

2830

31+
# Working Examples
32+
33+
Examples of SwingJS applied to allow single-source Java+JavaScript development include:
34+
35+
Jmol-SwingJS https://github.com/BobHanson/Jmol-SwingJS
36+
37+
JalView-JS https://www.jalview.org/jalview-js
38+
39+
MathePrisma https://www.matheprisma.de/ (over 600 applets)
40+
41+
Open Source Physics (OSP) https://github.com/OpenSourcePhysics/osp/tree/swingJS
42+
43+
TrackerJS https://physlets.org/tracker/trackerJS
44+
45+
46+
2947
# QuickStart
3048

3149
See https://github.com/BobHanson/java2script/tree/master/sources/net.sf.j2s.core/dist and the README file in that directory.
-119 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20231128000108
1+
20240114215407
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20231128000108
1+
20240114215407

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

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,12 @@ public boolean visit(PackageDeclaration node) {
8484

8585
public boolean visit(Block node) {
8686
blockLevel++;
87-
if (buffer != null) {
88-
buffer.append("{\r\n");
89-
}
9087
ASTNode parent = node.getParent();
9188
if (parent instanceof MethodDeclaration) {
9289
MethodDeclaration method = (MethodDeclaration) parent;
9390
Javadoc javadoc = method.getJavadoc();
94-
/*
95-
* if comment contains "@j2sNative", then output the given native JavaScript
96-
* codes directly.
97-
*/
91+
// if comment contains "@j2sNative", then output the given native JavaScript
92+
// codes directly.
9893
if (!processJ2STags(javadoc, node, true)) {
9994
return false;
10095
}
@@ -118,10 +113,10 @@ public boolean visit(Block node) {
118113
superclass = superclass.getSuperclass();
119114
}
120115
if (containsSuperPrivateMethod) {
121-
buffer.append("var $private = Clazz.checkPrivateMethod (arguments);\r\n");
122-
buffer.append("if ($private != null) {\r\n");
123-
buffer.append("return $private.apply (this, arguments);\r\n");
124-
buffer.append("}\r\n");
116+
buffer.append("var $private = Clazz.checkPrivateMethod (arguments);\n");
117+
buffer.append("if ($private != null) {\n");
118+
buffer.append("return $private.apply (this, arguments);\n");
119+
buffer.append("}\n");
125120
}
126121
}
127122
}
@@ -244,8 +239,24 @@ protected boolean processJ2STags(Javadoc javadoc, Block node, boolean superVisit
244239
if ("@j2sNative".equals(tagEl.getTagName())) {
245240
if (superVisit)
246241
super.visit(node);
247-
if (buffer != null)
248-
writeJavaScript(tagEl);
242+
if (buffer != null) {
243+
List<?> fragments = tagEl.fragments();
244+
boolean isFirstLine = true;
245+
StringBuffer buf = new StringBuffer();
246+
for (Iterator<?> iterator = fragments.iterator(); iterator
247+
.hasNext();) {
248+
TextElement commentEl = (TextElement) iterator.next();
249+
String text = commentEl.getText().trim();
250+
if (isFirstLine) {
251+
if (text.length() == 0) {
252+
continue;
253+
}
254+
}
255+
buf.append(text);
256+
buf.append("\n");
257+
}
258+
buffer.append(fixCommentBlock(buf.toString()));
259+
}
249260
return false;
250261
}
251262
}
@@ -254,25 +265,6 @@ protected boolean processJ2STags(Javadoc javadoc, Block node, boolean superVisit
254265
return true;
255266
}
256267

257-
private void writeJavaScript(TagElement tagEl) {
258-
List<?> fragments = tagEl.fragments();
259-
boolean isFirstLine = true;
260-
StringBuffer buf = new StringBuffer();
261-
for (Iterator<?> iterator = fragments.iterator(); iterator
262-
.hasNext();) {
263-
TextElement commentEl = (TextElement) iterator.next();
264-
String text = commentEl.getText().trim();
265-
if (isFirstLine) {
266-
if (text.length() == 0) {
267-
continue;
268-
}
269-
}
270-
buf.append(text);
271-
buf.append("\r\n");
272-
}
273-
buffer.append(fixCommentBlock(buf.toString()));
274-
}
275-
276268
private String fixCommentBlock(String text) {
277269
if (text == null || text.length() == 0) {
278270
return text;
@@ -284,16 +276,19 @@ private String fixCommentBlock(String text) {
284276

285277
/**
286278
* Write JavaScript source from @j2sNative and @J2SIgnore
279+
*
280+
* @return true if JavaScript was written
287281
*/
288-
protected boolean writeJ2SSources(BodyDeclaration node, String tagName, String prefix, String suffix, boolean both) {
289-
boolean existed = false;
282+
protected boolean writeJ2STags(BodyDeclaration node, boolean needScope) {
283+
String prefix = (needScope ? "{\n" : "");
284+
String suffix = (needScope ? "\n}" : "");
290285
Javadoc javadoc = node.getJavadoc();
291286
if (javadoc != null) {
292287
List<?> tags = javadoc.tags();
293288
if (tags.size() != 0) {
294289
for (Iterator<?> iter = tags.iterator(); iter.hasNext();) {
295290
TagElement tagEl = (TagElement) iter.next();
296-
if (tagName.equals(tagEl.getTagName())) {
291+
if ("@j2sNative".equals(tagEl.getTagName())) {
297292
List<?> fragments = tagEl.fragments();
298293
StringBuffer buf = new StringBuffer();
299294
boolean isFirstLine = true;
@@ -306,19 +301,16 @@ protected boolean writeJ2SSources(BodyDeclaration node, String tagName, String p
306301
}
307302
}
308303
buf.append(text);
309-
buf.append("\r\n");
304+
buf.append("\n");
310305
}
311306
String sources = buf.toString().trim();
312307
sources = sources.replaceAll("(\\/)-\\*|\\*-(\\/)", "$1*$2").replaceAll("<@>", "@");
313308
buffer.append(prefix + sources + suffix);
314-
existed = true;
309+
return true;
315310
}
316311
}
317312
}
318313
}
319-
if (existed && !both) {
320-
return existed;
321-
}
322314
List<?> modifiers = node.modifiers();
323315
for (Iterator<?> iter = modifiers.iterator(); iter.hasNext();) {
324316
Object obj = iter.next();
@@ -327,9 +319,7 @@ protected boolean writeJ2SSources(BodyDeclaration node, String tagName, String p
327319
String qName = annotation.getTypeName().getFullyQualifiedName();
328320
int index = qName.indexOf("J2S");
329321
if (index != -1) {
330-
String annName = qName.substring(index);
331-
annName = annName.replaceFirst("J2S", "@j2s");
332-
if (annName.startsWith(tagName)) {
322+
if (qName.substring(index).startsWith("J2SNative")) {
333323
StringBuffer buf = new StringBuffer();
334324
IAnnotationBinding annotationBinding = annotation.resolveAnnotationBinding();
335325
if (annotationBinding != null) {
@@ -342,23 +332,23 @@ protected boolean writeJ2SSources(BodyDeclaration node, String tagName, String p
342332
Object[] lines = (Object[]) value;
343333
for (int j = 0; j < lines.length; j++) {
344334
buf.append(lines[j]);
345-
buf.append("\r\n");
335+
buf.append("\n");
346336
}
347337
} else if (value instanceof String) {
348338
buf.append(value);
349-
buf.append("\r\n");
339+
buf.append("\n");
350340
}
351341
}
352342
}
353343
}
354344
}
355345
buffer.append(prefix + buf.toString().trim() + suffix);
356-
existed = true;
346+
return true;
357347
}
358348
}
359349
}
360350
}
361-
return existed;
351+
return false;
362352
}
363353

364354
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,8 @@ public boolean visit(TypeDeclaration node) {
330330
allClassBindings.add(resolveBinding);
331331
}
332332
readJ2sImportTags(node);
333-
334333
processImports(node);
335334
processJ2SRequireImport(node);
336-
//visitForOptionals(node);
337335
return super.visit(node);
338336
}
339337

@@ -345,7 +343,6 @@ public boolean visit(EnumDeclaration node) {
345343
allClassBindings.add(resolveBinding);
346344
}
347345
readJ2sImportTags(node);
348-
349346
imports.add("java.lang.Enum");
350347
processImports(node);
351348
processJ2SRequireImport(node);
@@ -508,8 +505,8 @@ public boolean visit(MethodInvocation node) {
508505

509506
public boolean visit(Initializer node) {
510507
if (J2SUtil.getJ2STag(node, "@j2sIgnore") != null) {
511-
return false;
512-
}
508+
return false;
509+
}
513510
return super.visit(node);
514511
}
515512

@@ -842,7 +839,6 @@ private void processImports(AbstractTypeDeclaration node) {
842839
if (mustAddDependency(qualifiedName, node, false)) {
843840
imports.add(qn);
844841
}
845-
//musts.add(superBinding.getQualifiedName());
846842
}
847843
}
848844
List<?> superInterfaces = null;

0 commit comments

Comments
 (0)