Skip to content

Commit f02e289

Browse files
hansonrhansonr
authored andcommitted
Java2Script 3.2.1 adds Java 8 functionality:
the "::" operator lambda expressions java.util.function.* java.util.stream.* default methods in interfaces
1 parent b16b89c commit f02e289

File tree

98 files changed

+31996
-76
lines changed

Some content is hidden

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

98 files changed

+31996
-76
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.ltk.core.refactoring.enable.project.refactoring.history=false

sources/net.sf.j2s.core/README.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The seed Eclipse project site/ directory for all SwingJS projects can be found a
66

77
https://github.com/BobHanson/java2script/blob/master/sources/net.sf.j2s.java.core/SwingJS-site.zip?raw=true
88

9+
this file is old. See dist/dropins/README.txt
910

1011
Known issues: 2/2018
1112

sources/net.sf.j2s.core/dist/README.txt

Lines changed: 86 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,32 @@ SwingJS distribution
22

33
https://github.com/BobHanson/java2script/blob/master/sources/net.sf.j2s.core/dist/dropins/README.txt
44

5-
7/15/2018 Bob Hanson hansonr@stolaf.edu
5+
7/20/2018 Bob Hanson hansonr@stolaf.edu
6+
7+
The base Java version is Java 6. However, many of the functionalities of Java 7 and Java 8 are included.
8+
This includes
9+
10+
the "::" operator
11+
lambda expressions
12+
java.util.function.*
13+
java.util.stream.*
14+
default methods in interfaces
15+
16+
All projects compiled under 3.1.1 need to be recompiled using the Java2Script 3.2.1 transpiler, as
17+
described below. Note that if you use /** @j2sNative */ calls to interface methods that are singlets,
18+
you no longer need to add qualifications (such as $O) to them. It should be no problem to leave them
19+
qualified, though, as the transpiler creates qualified and unqualified aliases.
20+
21+
There are situations where this use of unqualified method names can run into problems. For example, in
22+
java.util.stream.ReferencePipeline, there are three different declarations of the functional interface
23+
method "accept". They were just for erro reporting and have been removed.
24+
My guess that this is a rarity, but I am not sure.
25+
26+
27+
SwingJS has been successfully tested in Eclipse version Neon-Photon on Mac and Windows platforms.
28+
(No reason to believe it would not also work for Linux; just haven't tried that recently.)
29+
Java 8 is the target Java version for transpilation. Please report any missing classes or strange
30+
errors.
631

732
net.sf.j2s.core_3.2.1.jar replaces net.sf.j2s.core_3.1.1.jar
833

@@ -11,7 +36,7 @@ Most importantly, the requirement that the .project file be changed to
1136
indicate a customized Java builder is dropped in version 3.2.1, which just uses
1237
the standard Eclipse java builder, org.eclipse.jdt.core.javabuilder.
1338

14-
Thus, to get started, all you need are:
39+
For v. 3.2.1, to get started with SwingJS, all you need are:
1540

1641
1) the latest transpiler from
1742

@@ -31,8 +56,6 @@ or your project will no longer compile.
3156

3257
Comments below have been adjusted for these differences.
3358

34-
SwingJS has been successfully tested in Eclipse version Neon-Photon on Mac and Windows platforms.
35-
(No reason to believe it would not also work for Linux; just haven't tried that recently.)
3659

3760
INSTALLATION INSTRUCTIONS
3861

@@ -77,6 +100,10 @@ On Mac systems, the Eclipse directory is generally
77100
2. Restart Eclipse and check for the presence of the plug-in at
78101
help...about Eclipse...installation details...Plug-ins...(wait several seconds for tabulation)
79102

103+
Search for "j2s" to find j2s.sourceforge.net Java2Script Core
104+
105+
If that is not there, you don't have net.sf.j2s.core.jar in the proper directory.
106+
80107
Note relating to updating to 3.2.1 from 3.1.1 version of Java2Script:
81108

82109
If the version on this readout does not match the version that is
@@ -88,10 +115,6 @@ On Mac systems, the Eclipse directory is generally
88115
Eclipse once with the -clean flag.
89116

90117

91-
search for "j2s" to find j2s.sourceforge.net Java2Script Core
92-
93-
If that is not there, you don't have net.sf.j2s.core.jar in the proper directory.
94-
95118

96119
----------------------------------
97120
Creating a new J2S/SwingJS project
@@ -101,7 +124,7 @@ Create an Eclipse Java project for your work, if you have not done so already.
101124
If your source code is not all already in src/, navigate to the project...properties...
102125
Java Build Path...source and add all the source directories you need.
103126

104-
Note that your project must not include any Jar file based dependencies.
127+
Note that your project must not include any Jar file-based dependencies.
105128
All source code must be available. (Source code from decompiling .class files will work.)
106129

107130

@@ -124,36 +147,32 @@ Simply download and unzip that file into your project, creating a top-level site
124147
Enabling the Java2Script/SwingJS transpiler
125148
-------------------------------------------
126149

127-
1. Create in your Eclipse project the file:
150+
1. Create in your Eclipse project the empty file:
128151

129152
.j2s
130153

131-
containing simply:
154+
The next time you build the project, the transpiler will see that and
155+
add to it:
132156

133157
j2s.compiler.status=enable
158+
j2s.site.directory=site
134159

135-
2. Edit the .project file to indicate that the j2s transpiler is to be used
136-
rather than the standard Java compiler by changing the buildSpec buildCommand from
137-
138-
org.eclipse.jdt.core.javabuilder
139-
140-
to
141-
142-
net.sf.j2s.core.java2scriptbuilder
143-
160+
along with several other commented-out options. If you want, you can
161+
add these two yourself before you run the transpiler.
144162

145163
--------------------
146164
Building the project
147165
--------------------
148166

149167
Build your project as you normally would. Java class files will be created as usual in the bin/ directory.
150168
JavaScript equivalents of these files will be created in the site/swingjs/j2s directory. You might have to
151-
do a project refresh to see these site files.
169+
do a project refresh to see these site files. If you open one in Eclipse, notice that each time you build
170+
the project (for example by saving a changed Java file with Project...Save Automatically set), Eclipse
171+
will prompt you to reload this file with changes.
152172

153173
Do take a look at the .js files created. You will notice that they are all the methods and fields of your
154-
Java project *except* final static constants. SwingJS does not recreate those by name; it just uses them.
155-
(The only thing this should affect is that java.lang.reflect.Field does not indicate these names.)
156-
174+
Java project *except* final static constants. SwingJS does not recreate final static constants by name;
175+
It just uses them. This means that the java.lang.reflect.Field methods will not indicate these names.
157176

158177
----------------------------------------------
159178
Testing the JavaScript version of your project
@@ -163,7 +182,34 @@ The J2S transpiler will automatically set up for you in site/ a sample HTML page
163182
that subclasses JApplet or contains a public void main(String[] args) method. You will want to
164183
associate those files with an external HTML browser. We recommend Firefox.
165184

166-
Since you will be running AJAX locally within these browsers, you may need to enable local
185+
Do not change these files, as they will be recreated each time the transpiler runs. If you want
186+
a different configuration -- a different width or height, or some additional "applet" parameters,
187+
make a copy of this file and change within it the Info block:
188+
189+
Info = {
190+
code: _CODE_,
191+
main: _MAIN_,
192+
width: 850,
193+
height: 550,
194+
readyFunction: null,
195+
serverURL: 'https://chemapps.stolaf.edu/jmol/jsmol/php/jsmol.php',
196+
j2sPath: 'swingjs/j2s',
197+
console:'sysoutdiv',
198+
allowjavascript: true
199+
}
200+
201+
These Info key/value pairs are equivalent to Java applet parameters. Use Info.args for the main(args[])
202+
parameters. For instance:
203+
204+
args:["test","true"],
205+
206+
The serverURL reference allows for reading files in AJAX from servers that are not configured with
207+
208+
access-control-origin: *
209+
210+
You can try removing that if you want to.
211+
212+
Since you will be running AJAX locally within your browser, you may need to enable local
167213
file reading in your browser. Instructions for doing that can be found at
168214
http://wiki.jmol.org/index.php/Troubleshooting/Local_Files
169215

@@ -176,9 +222,13 @@ If you find you are missing a Java class, please contact me (Bob Hanson) at hans
176222
You can try adding these yourself by **temporarily** adding one or more of the Java classes found
177223
at http://grepcode.com to the proper package in your project. For example, java/awt.
178224

179-
If you do that, be sure to use the OpenJDK version. Most of the code in the SwingJS project started with
180-
Java 6-b14 or 6-b27. Build your project, then delete these Java files, because you do not necessarily
181-
want your Java code using that version, just JavaScript.
225+
If you do that, be sure to use the OpenJDK version. For example:
226+
227+
http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes
228+
229+
Most of the code in the SwingJS project started with
230+
Java 6-b14 or 6-b27. Build your project, then delete these Java files, because, should this file be added
231+
to an updated verison of SwingJS, you should probably use the one provided, not your own. Your choice.
182232

183233

184234
----------------
@@ -198,15 +248,20 @@ working site is https://chemapps.stolaf.edu/swingjs/physlets
198248

199249
Phet applets have not been put on GitHub yet.
200250

251+
Note that these sites use an older v. 3.1.1 transpiler and runtime.
201252

253+
Feb. 17, 2018
202254

203-
As of Feb. 17, 2018, we are actively converting a variety of functioning Java applets.
255+
We are actively converting a variety of functioning Java applets.
204256

205257
The physlets Animator, Doppler, and Optics are working.
206258

207-
As of Jan. 2, 2018, all known Java-to-JavaScript issues have been dealt with.
259+
Jan. 2, 2018
260+
208261
Efficient Google Closure Compiler compression is in place.
209262

210-
As of Dec. 17, 2017, SwingJS is fully operational in its "version 2" format,
263+
Dec. 17, 2017
264+
265+
SwingJS is fully operational in its "version 2" format (still v. 3.1.1),
211266
which includes fully qualified method, array, and number typing.
212267

104 Bytes
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2018-07-19_0557 AM
104 Bytes
Binary file not shown.

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/Java2ScriptVisitor.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ public boolean visit(ClassInstanceCreation node) {
395395
AnonymousClassDeclaration anonDeclare = node.getAnonymousClassDeclaration();
396396
if (anonDeclare != null) {
397397
String anonClassName = getAnonymousName(binding);
398-
processNewAnnonInstance(node, anonDeclare, binding, anonClassName, NOT_LAMBDA);
398+
processNewAnonInstance(node, anonDeclare, binding, anonClassName, NOT_LAMBDA);
399399
return false;
400400
}
401401

@@ -465,7 +465,7 @@ public boolean visit(ClassInstanceCreation node) {
465465
* @param anonClassName
466466
* @param isLambda
467467
*/
468-
private void processNewAnnonInstance(ASTNode node, ASTNode anonDeclare, ITypeBinding binding, String anonClassName,
468+
private void processNewAnonInstance(ASTNode node, ASTNode anonDeclare, ITypeBinding binding, String anonClassName,
469469
int lambdaType) {
470470
boolean isLambda = (lambdaType != NOT_LAMBDA);
471471
VariableAdapter variableAdapter = (VariableAdapter) getAdaptable(VariableAdapter.class);
@@ -2989,7 +2989,7 @@ private String getQualifiedSimpleName(SimpleName node) {
29892989
}
29902990
if (binding == null) {
29912991
String name = getShortenedQualifiedName(NameMapper.getJ2SName(node));
2992-
return NameMapper.getJ2SValidFieldName$Qualifier(name, true);
2992+
return NameMapper.get$QualifiedJ2SFieldName(name, true);
29932993
}
29942994
if (binding instanceof IVariableBinding)
29952995
return simpleNameInVarBinding(node, leadingChar, (IVariableBinding) binding);
@@ -2998,7 +2998,7 @@ private String getQualifiedSimpleName(SimpleName node) {
29982998

29992999
ITypeBinding typeBinding = node.resolveTypeBinding();
30003000
// >>Math<<.max
3001-
return NameMapper.getJ2SValidFieldName$Qualifier(typeBinding == null ? node.getFullyQualifiedName()
3001+
return NameMapper.get$QualifiedJ2SFieldName(typeBinding == null ? node.getFullyQualifiedName()
30023002
: assureQualifiedNameAllowP$(typeBinding.getQualifiedName()), true);
30033003
}
30043004

@@ -3049,7 +3049,7 @@ private String simpleNameInMethodBinding(SimpleName node, boolean isQualified, I
30493049
}
30503050
}
30513051
if (checkNameViolation)
3052-
ret += NameMapper.getJ2SValidFieldName$Qualifier(name, false);
3052+
ret += NameMapper.get$QualifiedJ2SFieldName(name, false);
30533053
}
30543054
return ret + name;
30553055
}
@@ -3147,14 +3147,14 @@ public boolean visit(SuperFieldAccess node) {
31473147
for (int i = 0; i < declaredFields.length; i++) {
31483148
String superFieldName = NameMapper.getJ2SVariableName(declaredFields[i]);
31493149
if (fieldName.equals(superFieldName)) {
3150-
buffer.append(NameMapper.getJ2SValidFieldName$Qualifier(fieldName, false));
3150+
buffer.append(NameMapper.get$QualifiedJ2SFieldName(fieldName, false));
31513151
buffer.append(NameMapper.getJ2SFieldName$Appended(classBinding.getSuperclass(), fieldName));
31523152
return false;
31533153
}
31543154
}
31553155
}
31563156
}
3157-
buffer.append(NameMapper.getJ2SValidFieldName$Qualifier(fieldName, true));
3157+
buffer.append(NameMapper.get$QualifiedJ2SFieldName(fieldName, true));
31583158
return false;
31593159
}
31603160

@@ -4368,7 +4368,7 @@ private static void addGenericClassMethod(String classKey, String methodName, St
43684368
private String getMethodNameOrArrayForDeclaration(IMethodBinding mBinding, boolean isConstructor,
43694369
boolean addUnqualified, boolean isLambda) {
43704370
String nodeName = mBinding.getName();
4371-
String methodName = (isConstructor ? "c$" : NameMapper.getJ2SMethodName(mBinding));
4371+
String methodName = (isConstructor ? "c$" : NameMapper.get$QualifiedJ2SMethodName(mBinding));
43724372
if (methodName.equals("subSequence$I$I")) {
43734373
// for StringBuffer and StringBuilder to be like String
43744374
return "['subSequence','subSequence$I$I']";
@@ -5332,7 +5332,7 @@ public static boolean checkJ2SInheritedMethodNameCollision(ITypeBinding binding,
53325332
if (binding != null) {
53335333
IMethodBinding[] methods = binding.getDeclaredMethods();
53345334
for (int i = 0; i < methods.length; i++)
5335-
if (name.equals(getJ2SMethodName(methods[i])))
5335+
if (name.equals(get$QualifiedJ2SMethodName(methods[i])))
53365336
return true;
53375337
ITypeBinding superclass = binding.getSuperclass();
53385338
if (checkJ2SInheritedMethodNameCollision(superclass, name))
@@ -5347,7 +5347,7 @@ public static boolean checkJ2SInheritedMethodNameCollision(ITypeBinding binding,
53475347
}
53485348

53495349
protected static String getJ2SCheckedFieldName(ITypeBinding classBinding, String fieldName) {
5350-
String name = getJ2SValidFieldName$Qualifier(fieldName, false);
5350+
String name = get$QualifiedJ2SFieldName(fieldName, false);
53515351
if (classBinding != null) {
53525352
if (checkJ2SInheritedMethodNameCollision(classBinding, fieldName)) {
53535353
name += "$";
@@ -5366,7 +5366,7 @@ public static String getJ2SName(SimpleName node) {
53665366
return getJ2SVariableName((IVariableBinding) binding);
53675367
}
53685368
if (binding instanceof IMethodBinding) {
5369-
return getJ2SMethodName((IMethodBinding) binding);
5369+
return get$QualifiedJ2SMethodName((IMethodBinding) binding);
53705370
}
53715371
String nameID = node.getIdentifier();
53725372
return nameID;
@@ -5388,7 +5388,7 @@ public static String getJ2SName(SimpleName node) {
53885388
* @param addName
53895389
* @return
53905390
*/
5391-
protected static String getJ2SValidFieldName$Qualifier(String name, boolean addName) {
5391+
protected static String get$QualifiedJ2SFieldName(String name, boolean addName) {
53925392
boolean isViolation = checkJ2SKeywordViolation(name);
53935393
return (isViolation ? "$" : "") + (addName ? name : "");
53945394
}
@@ -5431,7 +5431,7 @@ protected static String getJ2SVariableName(IVariableBinding binding) {
54315431
* @param binding
54325432
* @return
54335433
*/
5434-
static String getJ2SMethodName(IMethodBinding binding) {
5434+
static String get$QualifiedJ2SMethodName(IMethodBinding binding) {
54355435
String name = binding.getName();
54365436
boolean isViolation = (checkJ2SKeywordViolation(name)
54375437
&& !binding.getDeclaringClass().getQualifiedName().startsWith("swingjs.api"));
@@ -5517,7 +5517,7 @@ public static boolean isJ2SInheritedFieldName(ITypeBinding binding, String name)
55175517
"c$", "apply", "arguments", "bind", "call", "caller", "watch", "unwatch", "valueOf", "isPrototypeOf",
55185518
"isGenerator", "prototype" };
55195519

5520-
public static boolean checkJ2SKeywordViolation(String name) {
5520+
static boolean checkJ2SKeywordViolation(String name) {
55215521
for (int i = 0; i < keywords.length; i++) {
55225522
if (keywords[i].equals(name)) {
55235523
return true;
@@ -5735,7 +5735,7 @@ public boolean visit(ExpressionMethodReference node) {
57355735
node.getExpression().accept(this);
57365736
buffer.append(";");
57375737
ITypeBinding binding = node.resolveTypeBinding();
5738-
processNewAnnonInstance(node, null, binding, null, LAMBDA_METHOD);
5738+
processNewAnonInstance(node, null, binding, null, LAMBDA_METHOD);
57395739
addAnonymousFunctionWrapper(false);
57405740
return false;
57415741
}
@@ -5748,7 +5748,7 @@ public boolean visit(ExpressionMethodReference node) {
57485748
public boolean visit(CreationReference node) {
57495749
// see http://www.baeldung.com/java-8-double-colon-operator
57505750
ITypeBinding binding = node.resolveTypeBinding();
5751-
processNewAnnonInstance(node, null, binding, null, LAMBDA_CREATION);
5751+
processNewAnonInstance(node, null, binding, null, LAMBDA_CREATION);
57525752
return false;
57535753
}
57545754

@@ -5761,7 +5761,7 @@ public boolean visit(LambdaExpression node) {
57615761
// new runnable() { public xxx singleMethod() { Body })
57625762

57635763
ITypeBinding binding = node.resolveTypeBinding();
5764-
processNewAnnonInstance(node, null, binding, null, LAMBDA_EXPRESSION);
5764+
processNewAnonInstance(node, null, binding, null, LAMBDA_EXPRESSION);
57655765
return false;
57665766
}
57675767

0 commit comments

Comments
 (0)