Skip to content

Commit bcba0b3

Browse files
committed
EnumConstants moved to $clinit$, this$0 added
1 parent bc637b2 commit bcba0b3

File tree

8 files changed

+135
-53
lines changed

8 files changed

+135
-53
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1280,8 +1280,10 @@ protected static boolean isStatic(IBinding b) {
12801280
* necessary
12811281
* @return JavaScript for array creation
12821282
*/
1283-
private String clazzArray(ITypeBinding type, int dimFlag) {
1283+
protected String clazzArray(ITypeBinding type, int dimFlag) {
12841284
ITypeBinding ebinding = type.getElementType();
1285+
if (ebinding == null)
1286+
ebinding = type; // creating for Enum
12851287
String params = (ebinding.isPrimitive() ? getPrimitiveTYPE(ebinding.getName())
12861288
: getQualifiedStaticName(null, ebinding.getQualifiedName(), true, true, false))
12871289
+ (dimFlag == 0 ? "" : ", " + Math.abs(dimFlag) * type.getDimensions() * -1);

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
import org.eclipse.jdt.core.dom.SimpleName;
5454
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
5555
import org.eclipse.jdt.core.dom.SuperConstructorInvocation;
56-
import org.eclipse.jdt.core.dom.SuperFieldAccess;
5756
import org.eclipse.jdt.core.dom.SuperMethodInvocation;
5857
import org.eclipse.jdt.core.dom.SwitchCase;
5958
import org.eclipse.jdt.core.dom.SwitchStatement;
@@ -1256,6 +1255,10 @@ private boolean addClassOrInterface(ASTNode node, ITypeBinding binding, List<?>
12561255
haveDeclarations = true;
12571256
}
12581257
}
1258+
if (isEnum) {
1259+
addEnumConstants((EnumDeclaration) node);
1260+
haveDeclarations = true;
1261+
}
12591262
if (haveDeclarations || hasDependents)
12601263
buffer.append("}\r\n");
12611264
else
@@ -1342,7 +1345,8 @@ private boolean addClassOrInterface(ASTNode node, ITypeBinding binding, List<?>
13421345
if (isEnum) {
13431346
buffer.append(trailingBuffer.getAssertString());
13441347
addDefaultConstructor();
1345-
addEnumConstants(((EnumDeclaration) node).enumConstants());
1348+
buffer.append("var $vals=[];\r\n");
1349+
buffer.append("Clazz.newMeth(C$, 'values', function() { return $vals }, 1);\r\n");
13461350
} else {
13471351
buffer.append(trailingBuffer); // also writes the assert string
13481352
if (isAnonymous) {
@@ -1414,8 +1418,9 @@ private void addDefaultConstructor() {
14141418
*
14151419
* @param constants
14161420
*/
1417-
private void addEnumConstants(List<?> constants) {
1418-
buffer.append("var vals = [];\r\n");
1421+
private void addEnumConstants(EnumDeclaration e) {
1422+
List<?> constants = e.enumConstants();
1423+
buffer.append("$vals=Clazz.array(C$,[0]);\r\n");
14191424
for (int i = 0; i < constants.size(); i++) {
14201425
EnumConstantDeclaration enumConst = (EnumConstantDeclaration) constants.get(i);
14211426
IMethodBinding binding = enumConst.resolveConstructorBinding();
@@ -1427,7 +1432,7 @@ private void addEnumConstants(List<?> constants) {
14271432
anonName = getAnonymousName(anonDeclare.resolveBinding());
14281433
buffer.append("\r\n");
14291434
}
1430-
buffer.append("Clazz.newEnumConst(vals, ").append(getJ2SQualifiedName("C$.c$", null, binding, null, false))
1435+
buffer.append("Clazz.newEnumConst($vals, ").append(getJ2SQualifiedName("C$.c$", null, binding, null, false))
14311436
.append(", \"");
14321437
enumConst.getName().accept(this);
14331438
buffer.append("\", " + i);
@@ -1436,10 +1441,6 @@ private void addEnumConstants(List<?> constants) {
14361441
buffer.append(", ").append(anonName);
14371442
buffer.append(");\r\n");
14381443
}
1439-
buffer.append("Clazz.newMeth(C$, 'values', function() { return vals }, 1);\r\n");
1440-
// this next just ensures we have the valueOf() method in Enum if it
1441-
// is not already there.
1442-
buffer.append("Clazz.newMeth(Enum, 'valueOf$Class$S', function(cl, name) { return cl[name] }, 1);\r\n");
14431444
}
14441445

14451446
/**

sources/net.sf.j2s.java.core/build_core_applet.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@
180180
swingjs/api/js/JSInterface.js
181181
swingjs/jquery/JQueryUI.js
182182
swingjs/JSApp.js
183+
swingjs/JSApplet.js
183184
swingjs/JSAppletThread.js
184185
swingjs/JSAppletViewer.js
185186
swingjs/JSFontMetrics.js
@@ -223,7 +224,6 @@
223224
java/awt/peer/WindowPeer.js
224225
java/awt/Point.js
225226
java/awt/Window.js
226-
java/lang/Enum.js
227227
java/util/Random.js
228228
java/util/Vector.js
229229
javax/swing/AbstractListModel.js

sources/net.sf.j2s.java.core/src/java/lang/Enum.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,16 @@ public final Class<E> getDeclaringClass() {
208208
*/
209209
public static <T extends Enum<T>> T valueOf(Class<T> enumType,
210210
String name) {
211-
T result = enumType.enumConstantDirectory().get(name);
211+
212+
T result = null;
213+
/**
214+
* @j2sNative
215+
*
216+
* result = enumType.$clazz$[name];
217+
*/
218+
{
219+
result = enumType.enumConstantDirectory().get(name);
220+
}
212221
if (result != null)
213222
return result;
214223
if (name == null)
@@ -243,4 +252,5 @@ private void readObject(ObjectInputStream in) throws IOException,
243252
private void readObjectNoData() throws ObjectStreamException {
244253
throw new InvalidObjectException("can't deserialize enum");
245254
}
255+
246256
}

sources/net.sf.j2s.java.core/src/swingjs/JSGraphics2D.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,30 @@ public void fillRect(int x, int y, int width, int height) {
256256
ctx.fillRect(x, y, width, height);
257257
}
258258

259+
public void fill3DRect(int x, int y, int width, int height, boolean raised) {
260+
Paint p = getPaint();
261+
Color c = getColor();
262+
Color brighter = c.brighter();
263+
Color darker = c.darker();
264+
265+
if (!raised) {
266+
setColor(darker);
267+
} else if (p != c) {
268+
setColor(c);
269+
}
270+
fillRect(x + 1, y + 1, width - 2, height - 2);
271+
setColor(raised ? brighter : darker);
272+
// drawLine(x, y, x, y + height - 1);
273+
fillRect(x, y, 1, height);
274+
// drawLine(x + 1, y, x + width - 2, y);
275+
fillRect(x + 1, y, width - 2, 1);
276+
setColor(raised ? darker : brighter);
277+
// drawLine(x + 1, y + height - 1, x + width - 1, y + height - 1);
278+
fillRect(x + 1, y + height - 1, width - 1, 1);
279+
// drawLine(x + width - 1, y, x + width - 1, y + height - 2);
280+
fillRect(x + width - 1, y, 1, height - 1);
281+
setPaint(p);
282+
}
259283

260284
public void setFont(Font font) {
261285
// this equality check speeds mark/reset significantly

sources/net.sf.j2s.java.core/srcjs/js/j2sApplet.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// j2sApplet.js (based on JmolCore.js)
22

3+
// BH 12/22/2017 1:18:42 PM adds j2sargs for setting arguments
34
// BH 11/19/2017 3:55:04 AM adding support for swingjs2.js; adds static j2sHeadless=true;
45
// BH 10/4/2017 2:25:03 PM adds Clazz.loadClass("javajs.util.Base64")
56
// BH 7/18/2017 10:46:44 AM adds J2S._canClickFileReader, fixing J2S.getFileFromDialog for Chrome and Safari
@@ -24,7 +25,19 @@
2425

2526
if(typeof(jQuery)=="undefined") alert ("Note -- jQuery is required, but it's not defined.")
2627

27-
self.J2S || (J2S = {});
28+
self.J2S || (J2S = {
29+
getURIField: function(name, def) {
30+
try {
31+
var ref = document.location.href.toLowerCase();
32+
var i = ref.indexOf(name + "=");
33+
if (i >= 0)
34+
def = (document.location.href+"&").substring(i + name.length + 1).split("&")[0];
35+
} finally {
36+
return def;
37+
}
38+
}
39+
40+
});
2841

2942
if (!J2S._version)
3043
J2S = (function(document) {
@@ -45,6 +58,7 @@ J2S = (function(document) {
4558
monitorZIndex:z+99999 // way way front
4659
}
4760
};
61+
4862

4963
var j = {
5064

@@ -89,10 +103,8 @@ J2S = (function(document) {
89103
var ref = document.location.href.toLowerCase();
90104
j._debugCode = (ref.indexOf("j2sdebugcode") >= 0);
91105
j._debugCore = (ref.indexOf("j2sdebugcore") >= 0);
92-
var i = ref.indexOf("j2sdebugname=");
93-
j._debugName = (i >= 0 ? (document.location.href+"&").substring(i + 13).split("&")[0] : null);
94-
var i = ref.indexOf("j2slang=");
95-
j._lang = (i >= 0 ? (document.location.href+"&").substring(i + 8).split("&")[0] : null);
106+
j._debugName = J2S.getURIField("j2sdebugname", null);
107+
j._lang = J2S.getURIField("j2slang", null);
96108
j._httpProto = (ref.indexOf("https") == 0 ? "https://" : "http://");
97109
j._isFile = (ref.indexOf("file:") == 0);
98110
if (j._isFile) // ensure no attempt to read XML in local request:
@@ -1886,9 +1898,14 @@ J2S.Cache.put = function(filename, data) {
18861898

18871899
proto.__startAppletJS = function(applet) {
18881900
if (J2S._version.indexOf("$Date: ") == 0)
1889-
J2S._version = (J2S._version.substring(7) + " -").split(" -")[0] + " (J2S)"
1901+
J2S._version = (J2S._version.substring(7) + " -").split(" -")[0] + " (J2S)";
18901902
Clazz.load("java.lang.Class");
18911903
J2S._registerApplet(applet._id, applet);
1904+
if (!applet.__Info.args || applet.__Info.args == "?") {
1905+
var s = J2S.getURIField("j2sargs", null);
1906+
if (s !== null)
1907+
applet.__Info.args = decodeURIComponent(s);
1908+
}
18921909
try {
18931910
if (applet.__Info.main) {
18941911
try{

sources/net.sf.j2s.java.core/srcjs/js/j2sSwingJS.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -366,13 +366,14 @@ Clazz.new_ = function(c, args, cl) {
366366
return f;
367367
}
368368

369-
Clazz.newClass = function (prefix, name, clazz, clazzSuper, interfacez, type) {
369+
Clazz.newClass = function (prefix, name, clazz, clazzSuper, interfacez, type) {
370370
if (J2S._debugCore) {
371371
var qualifiedName = (prefix ? (prefix.__PKG_NAME__ || prefix.__CLASS_NAME__) + "." : "") + name;
372372
checkDeclared(qualifiedName, type);
373373
}
374374
clazz || (clazz = function () {Clazz.newInstance(this,arguments,0,clazz)});
375375
clazz.__NAME__ = name;
376+
prefix.__CLASS_NAME__ && (clazz.$this$0 = prefix.__CLASS_NAME__);
376377
clazz.$load$ = [clazzSuper, interfacez];
377378

378379
// get qualifed name, and for inner classes, the name to use to refer to this
@@ -435,14 +436,13 @@ Clazz.newInstance = function (objThis, args, isInner, clazz) {
435436
};
436437
}
437438

438-
clazz && clazz.$clinit$ && clazz.$clinit$();
439-
440439
objThis.__JSID__ = ++_jsid;
441440

442-
clazz && clazz.$init0$ && clazz.$init0$.apply(objThis);
443441

444442
if (!isInner) {
445-
if ((!args || args.length == 0) && objThis.c$) {
443+
clazz && clazz.$clinit$ && clazz.$clinit$();
444+
clazz && clazz.$init0$ && clazz.$init0$.apply(objThis);
445+
if ((!args || args.length == 0) && objThis.c$) {
446446
// allow for direct default call "new foo()" to run with its default constructor
447447
objThis.c$.apply(objThis);
448448
args && (args[2] = Clazz.inheritArgs)
@@ -485,13 +485,13 @@ Clazz.newInstance = function (objThis, args, isInner, clazz) {
485485
isNew = true;
486486
}
487487
// add all superclass references for outer object
488-
var clazz = getClazz(outerObj);
488+
var clazz1 = getClazz(outerObj);
489489
do {
490-
var key = getClassName(clazz, true);
490+
var key = getClassName(clazz1, true);
491491
if (!isNew && b[key])
492492
break;
493493
b[key] = outerObj;
494-
} while ((clazz = clazz.superclazz));
494+
} while ((clazz1 = clazz1.superclazz));
495495
}
496496
// add a flag to disallow any other same-class use of this map.
497497
b["$ " + innerName] = 1;
@@ -500,7 +500,9 @@ Clazz.newInstance = function (objThis, args, isInner, clazz) {
500500
outerObj.$b$ = b;
501501
// final objective: save this map for the inner object
502502
objThis.b$ = b;
503-
objThis.this$0 = outerObj;
503+
clazz.$this$0 && (objThis.this$0 = b[clazz.$this$0]);
504+
clazz.$clinit$ && clazz.$clinit$();
505+
clazz.$init0$ && clazz.$init0$.apply(objThis);
504506
};
505507

506508

@@ -1105,18 +1107,18 @@ var extendObject = function(clazz, exclude) {
11051107
//};
11061108

11071109
var excludeSuper = function(o) {
1108-
return o == "b$" || o == "this$0"
1110+
return o == "b$" || o == "$this$0"
11091111
|| o == "$init$"
11101112
|| o == "$init0$"
11111113
|| o == "$clinit$"
11121114
|| o == "$load$"
1113-
|| o == "c$"
11141115
|| o == "$Class$"
11151116
|| o == "prototype"
11161117
|| o == "__CLASS_NAME__"
11171118
|| o == "__CLASS_NAME$__"
11181119
|| o == "superclazz"
11191120
|| o == "implementz"
1121+
|| o.startsWith("c$")
11201122
}
11211123

11221124
var copyStatics = function(clazzSuper, clazzThis, andProto) {
@@ -1390,7 +1392,7 @@ var setSuperclass = function(clazzThis, clazzSuper){
13901392
if (clazzSuper == Number) {
13911393
clazzThis.prototype = new Number();
13921394
} else {
1393-
clazzThis.prototype = new clazzSuper (null, inheritArgs);
1395+
clazzThis.prototype = new clazzSuper ([inheritArgs]);
13941396
if (clazzSuper == Error) {
13951397
var pp = Throwable.prototype;
13961398
for (o in pp) {
@@ -2056,8 +2058,12 @@ Clazz.loadScript = function(file) {
20562058
s = data;
20572059
if (s.indexOf("missing ] after element list")>= 0)
20582060
s = "File not found";
2059-
doDebugger()
2060-
alert(s + " loading file " + file);
2061+
if (file.indexOf("/j2s/core/") >= 0) {
2062+
System.out.println(s + " loading " + file);
2063+
} else {
2064+
doDebugger()
2065+
alert(s + " loading file " + file);
2066+
}
20612067
}
20622068
}
20632069

0 commit comments

Comments
 (0)