Skip to content

Commit 5eefbf1

Browse files
hansonrhansonr
authored andcommitted
updated Differences.txt explaining how SwingJS is different from Java
1 parent b40f114 commit 5eefbf1

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

sources/net.sf.j2s.java.core/doc/Differences.txt

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,75 @@ they must allow full exit and re-entry of Thread.run(), not the typical while/sl
6666
The key is to create a state-based run() that can be exited and re-entered in JavaScript.
6767

6868

69+
Static fields
70+
-------------
71+
72+
Final static primitive "constant" fields (String, boolean, int, etc.) such as
73+
74+
static final int TEST = 3;
75+
static final String MY_STRING = "my " + "string";
76+
77+
are converted to their primitive form automatically by the Eclipse Java compiler
78+
and do not appear in the JavaScript by their names.
79+
80+
Other static fields are properties of their class and can be used as expected.
81+
82+
Note, however, that SwingJS runs all "Java" code on a page in a common "jvm"
83+
(like older versions of Java). So, like the older Java schema, the JavaScript
84+
equivalents of both applets and applications will share all of their static
85+
fields and methods. This includes java.lang.System.
86+
87+
Basically, SwingJS implementations of Java run in a browser page-based sandbox
88+
instead of an applet-specific one.
89+
90+
In general, this is no problem. But if we are to implement pages with
91+
multiple applets present, we must be sure to only have static references
92+
that are "final" or specifically meant to be shared in a JavaScript
93+
environment only (since they will not be shared in Java).
94+
95+
A simple solution, if static non-constant references are needed, is to attach the
96+
field to Thread.currentThread.threadGroup(), which is an applet-specific reference.
97+
Be sure, if you do this, that you use explicit setters and getters:
98+
99+
For example,
100+
101+
private static String myvar;
102+
103+
...
104+
105+
public void setMyVar(String x) {
106+
ThreadGroup g = Thread.currentThread().threadGroup();
107+
/**
108+
* @j2sNative g._myvar = x;
109+
*
110+
*/
111+
{
112+
myvar = x;
113+
}
114+
}
115+
116+
public String getMyVar() {
117+
ThreadGroup g = Thread.currentThread().threadGroup();
118+
/**
119+
* @j2sNative return g._myvar || null;
120+
*
121+
*/
122+
{
123+
return myvar;
124+
}
125+
}
126+
127+
in Java will get and set x the same in JavaScript and in Java.
128+
129+
130+
A convenient way to do this in general is to supply a singleton class with
131+
explicitly private-only constructors and then refer to it in Java and in JavaScript
132+
instead of using static field, referring to myclass.getIntance().xxx instead of
133+
myclass.xxx in Java (and JavaScript).
134+
135+
This was done extensively in the Jalview project. See jalview.bin.Instance.
136+
137+
69138
Modal Dialogs
70139
-------------
71140

0 commit comments

Comments
 (0)