Skip to content

Commit e1aba3a

Browse files
author
zhourenjian
committed
Fixed bug that Boolean object is not correctly constructed.
Sebastian Gurin reporting: Boolean b = new Boolean(true); System.out.println(b.equals(true)); prints "true" in java and "false" in javascript. The sollution seems to be analog to Character's. And another small detail, Boolean class has not the getName() method... so Boolean.class.getName() translated js code will fail to execute...
1 parent 9960541 commit e1aba3a

File tree

1 file changed

+76
-16
lines changed
  • sources/net.sf.j2s.java.core/src/java/lang

1 file changed

+76
-16
lines changed
Lines changed: 76 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,80 @@
1-
/* http://j2s.sf.net/ */
1+
Clazz.load (["java.lang.Comparable", "java.io.Serializable"], "java.lang.Boolean", null, function () {
22
java.lang.Boolean = Boolean;
3-
Boolean.prototype.booleanValue=function(){
4-
return this.valueOf();
5-
}
3+
Boolean.__CLASS_NAME__ = "Boolean";
4+
Clazz.implementOf (Boolean, [java.io.Serializable, java.lang.Comparable]);
5+
Boolean.equals = Clazz.innerFunctions.equals;
6+
Boolean.getName = Clazz.innerFunctions.getName;
7+
Boolean.serialVersionUID = Boolean.prototype.serialVersionUID = -3665804199014368530;
68

7-
Boolean.prototype.parseBoolean = function(val){
8-
if(val == null || val.toLowerCase() == "false" || val == false){
9-
return false;
10-
}
11-
return true;
9+
Clazz.makeConstructor (Boolean,
10+
function (value) {
11+
this.valueOf = function () {
12+
return value;
13+
};
14+
}, "~B");
15+
Clazz.makeConstructor (Boolean,
16+
function (s) {
17+
this.valueOf = function () {
18+
return Boolean.toBoolean (s);
19+
};
20+
}, "~S");
21+
Boolean.parseBoolean = Clazz.defineMethod (Boolean, "parseBoolean",
22+
function (s) {
23+
return Boolean.toBoolean (s);
24+
}, "~S");
25+
Clazz.defineMethod (Boolean, "booleanValue",
26+
function () {
27+
return this.value;
28+
});
29+
Boolean.$valueOf = Clazz.defineMethod (Boolean, "$valueOf",
30+
function (b) {
31+
return (b ? Boolean.TRUE : Boolean.FALSE);
32+
}, "~B");
33+
Boolean.$valueOf = Clazz.defineMethod (Boolean, "$valueOf",
34+
function (s) {
35+
return Boolean.toBoolean (s) ? Boolean.TRUE : Boolean.FALSE;
36+
}, "~S");
37+
Boolean.toString = Clazz.defineMethod (Boolean, "toString",
38+
function (b) {
39+
return b ? "true" : "false";
40+
}, "~B");
41+
Clazz.defineMethod (Boolean, "toString",
42+
function () {
43+
return this.valueOf () ? "true" : "false";
44+
});
45+
Clazz.overrideMethod (Boolean, "hashCode",
46+
function () {
47+
return this.valueOf () ? 1231 : 1237;
48+
});
49+
Clazz.overrideMethod (Boolean, "equals",
50+
function (obj) {
51+
if (Clazz.instanceOf (obj, Boolean)) {
52+
return this.value == (obj).booleanValue ();
53+
}return false;
54+
}, "~O");
55+
Boolean.getBoolean = Clazz.defineMethod (Boolean, "getBoolean",
56+
function (name) {
57+
var result = false;
58+
try {
59+
result = Boolean.toBoolean (System.getProperty (name));
60+
} catch (e) {
61+
if (Clazz.instanceOf (e, IllegalArgumentException)) {
62+
} else if (Clazz.instanceOf (e, NullPointerException)) {
63+
} else {
64+
throw e;
1265
}
13-
14-
Boolean.$valueOf = Boolean.parseBoolean = Boolean.prototype.parseBoolean;
15-
16-
Boolean.TRUE = Boolean.prototype.TRUE = new Boolean(true);
17-
Boolean.FALSE = Boolean.prototype.FALSE = new Boolean(false);
66+
}
67+
return result;
68+
}, "~S");
69+
Clazz.overrideMethod (Boolean, "compareTo",
70+
function (b) {
71+
return (b.value == this.value ? 0 : (this.value ? 1 : -1));
72+
}, "Boolean");
73+
Boolean.toBoolean = Clazz.defineMethod (Boolean, "toBoolean",
74+
($fz = function (name) {
75+
return ((name != null) && name.equalsIgnoreCase ("true"));
76+
}, $fz.isPrivate = true, $fz), "~S");
77+
Boolean.TRUE = Boolean.prototype.TRUE = new Boolean (true);
78+
Boolean.FALSE = Boolean.prototype.FALSE = new Boolean (false);
1879
Boolean.TYPE = Boolean.prototype.TYPE = Boolean;
19-
20-
80+
});

0 commit comments

Comments
 (0)