Skip to content

Commit 8cbc456

Browse files
author
zhourenjian
committed
Add window["j2s.object.native"]=true; support for unpolluted native JavaScript object, so Java2Script library can co-exist with other JavaScript libraries, like YUI, jQuery, ...
1. new Object(); in Java will be compiled into JavaScript as "new JavaObject();" 2. If there is no window["j2s.object.native"] = true; line, JavaObject === Object, and everything is the same as before. 3. In native Object mode, Function, DOM class, Object's prototype are not modified. But String, Array, Number, Boolean's prototype are modified. More information, please visit http://groups.google.com/group/java2script/browse_thread/thread/3d6deb9c3c0a0cda
1 parent 44b7d7c commit 8cbc456

7 files changed

Lines changed: 126 additions & 118 deletions

File tree

sources/net.sf.j2s.java.core/src/java/lang/Boolean.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
Clazz.load (["java.lang.Comparable", "java.io.Serializable"], "java.lang.Boolean", null, function () {
22
java.lang.Boolean = Boolean;
3+
if (Clazz.supportsNativeObject) {
4+
for (var i = 0; i < Clazz.extendedObjectMethods.length; i++) {
5+
var p = Clazz.extendedObjectMethods[i];
6+
Boolean.prototype[p] = JavaObject.prototype[p];
7+
}
8+
}
39
Boolean.__CLASS_NAME__ = "Boolean";
410
Clazz.implementOf (Boolean, [java.io.Serializable, java.lang.Comparable]);
511
Boolean.equals = Clazz.innerFunctions.equals;

sources/net.sf.j2s.java.core/src/java/lang/Class.js

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,74 @@ Class = Clazz = function () {
4545
NullObject = function () {
4646
};
4747

48+
JavaObject = Object;
49+
50+
/* protected */
51+
Clazz.supportsNativeObject = window["j2s.object.native"];
52+
53+
if (Clazz.supportsNativeObject) {
54+
JavaObject = function () {};
55+
}
56+
57+
JavaObject.prototype.equals = function (obj) {
58+
return this == obj;
59+
};
60+
61+
JavaObject.prototype.hashCode = function () {
62+
try {
63+
return this.toString ().hashCode ();
64+
} catch (e) {
65+
var str = ":";
66+
for (var s in this) {
67+
str += s + ":"
68+
}
69+
return str.hashCode ();
70+
}
71+
};
72+
73+
JavaObject.prototype.getClass = function () {
74+
return Clazz.getClass (this);
75+
};
76+
77+
JavaObject.prototype.clone = function () {
78+
var o = new this.constructor ();
79+
for (var i in this) {
80+
o[i] = this[i];
81+
}
82+
return o;
83+
};
84+
85+
/*
86+
* Methods for thread in Object
87+
*/
88+
JavaObject.prototype.finalize = function () {};
89+
JavaObject.prototype.notify = function () {};
90+
JavaObject.prototype.notifyAll = function () {};
91+
JavaObject.prototype.wait = function () {};
92+
93+
JavaObject.prototype.to$tring = Object.prototype.toString;
94+
JavaObject.prototype.toString = function () {
95+
if (this.__CLASS_NAME__ != null) {
96+
return "[" + this.__CLASS_NAME__ + " object]";
97+
} else {
98+
return this.to$tring ();
99+
}
100+
};
101+
102+
if (Clazz.supportsNativeObject) {
103+
/* protected */
104+
Clazz.extendedObjectMethods = [
105+
"equals", "hashCode", "getClass", "clone", "finalize", "notify", "notifyAll", "wait", "to$tring", "toString"
106+
];
107+
108+
for (var i = 0; i < Clazz.extendedObjectMethods.length; i++) {
109+
var p = Clazz.extendedObjectMethods[i];
110+
Array.prototype[p] = JavaObject.prototype[p];
111+
}
112+
JavaObject.__CLASS_NAME__ = "Object";
113+
JavaObject["getClass"] = function () { return JavaObject; };
114+
}
115+
48116
/**
49117
* Try to fix bug on Safari
50118
*/
@@ -154,7 +222,7 @@ Clazz.getClass = function (clazzHost) {
154222
* null is always treated as Object.
155223
* But what about "undefined"?
156224
*/
157-
return Object;
225+
return JavaObject;
158226
}
159227
if (typeof clazzHost == "function") {
160228
return clazzHost;
@@ -172,7 +240,7 @@ Clazz.getClass = function (clazzHost) {
172240
if (obj.__CLASS_NAME__ != null) {
173241
clazzName = obj.__CLASS_NAME__;
174242
} else if (obj.constructor == null) {
175-
return Object; // Is it safe?
243+
return JavaObject; // Is it safe?
176244
} else {
177245
return obj.constructor;
178246
}
@@ -416,7 +484,7 @@ Clazz.getInheritedLevel = function (clazzTarget, clazzBase) {
416484

417485
zzalc = zzalc.superClazz;
418486
if (zzalc == null) {
419-
if (clazzBase === Object) {
487+
if (clazzBase === Object || clazzBase === JavaObject) {
420488
/*
421489
* getInheritedLevel(String, CharSequence) == 1
422490
* getInheritedLevel(String, Object) == 1.5
@@ -1337,7 +1405,7 @@ Clazz.evalType = function (typeStr, isQualified) {
13371405
} else if (typeStr == "number") {
13381406
return Number;
13391407
} else if (typeStr == "object") {
1340-
return Object;
1408+
return JavaObject;
13411409
} else if (typeStr == "string") {
13421410
return String;
13431411
} else if (typeStr == "boolean") {
@@ -1524,7 +1592,7 @@ Clazz.innerFunctions = {
15241592
if (java.io.InputStream != null) {
15251593
is = new java.io.InputStream ();
15261594
} else {
1527-
is = new Object ();
1595+
is = new JavaObject ();
15281596
is.__CLASS_NAME__ = "java.io.InputStream";
15291597
is.close = function () {};
15301598
}
@@ -1657,6 +1725,12 @@ Clazz.decorateFunction = function (clazzFun, prefix, name) {
16571725
prefix[name] = clazzFun;
16581726
}
16591727
clazzFun.__CLASS_NAME__ = qName;
1728+
if (Clazz.supportsNativeObject) {
1729+
for (var i = 0; i < Clazz.extendedObjectMethods.length; i++) {
1730+
var p = Clazz.extendedObjectMethods[i];
1731+
clazzFun.prototype[p] = JavaObject.prototype[p];
1732+
}
1733+
}
16601734
clazzFun.prototype.__CLASS_NAME__ = qName;
16611735
/*
16621736
clazzFun.equals = Clazz.innerFunctions.equals;
@@ -1759,6 +1833,12 @@ Clazz.declareAnonymous = function (prefix, name, clazzParent, interfacez,
17591833
Clazz.decorateAsType = function (clazzFun, qClazzName, clazzParent,
17601834
interfacez, parentClazzInstance, inheritClazzFuns) {
17611835
clazzFun.__CLASS_NAME__ = qClazzName;
1836+
if (Clazz.supportsNativeObject) {
1837+
for (var i = 0; i < Clazz.extendedObjectMethods.length; i++) {
1838+
var p = Clazz.extendedObjectMethods[i];
1839+
clazzFun.prototype[p] = JavaObject.prototype[p];
1840+
}
1841+
}
17621842
//if (qClazzName != "String" && qClazzName != "Object"
17631843
// && qClazzName != "Number" && qClazzName != "Date") {
17641844
clazzFun.prototype.__CLASS_NAME__ = qClazzName;

sources/net.sf.j2s.java.core/src/java/lang/ClassExt.js

Lines changed: 15 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -632,74 +632,13 @@ Clazz.load = function (musts, clazz, optionals, declaration) {
632632
* TODO: make sure that invading Object prototype does not affect other
633633
* existed library, such as Dojo, YUI, Prototype, ...
634634
*/
635-
java.lang.Object = Object;
635+
java.lang.Object = JavaObject;
636636

637-
//Clazz.decorateAsType (java.lang.Object, "Object");
638-
//Object.__CLASS_NAME__ = "Object";
639-
640-
Object.getName = Clazz.innerFunctions.getName;
641-
642-
Object.prototype.equals = function (obj) {
643-
return this == obj;
644-
};
645-
646-
Object.prototype.hashCode = function () {
647-
try {
648-
return this.toString ().hashCode ();
649-
} catch (e) {
650-
var str = ":";
651-
for (var s in this) {
652-
str += s + ":"
653-
}
654-
return str.hashCode ();
655-
}
656-
};
657-
658-
Object.prototype.getClass = function () {
659-
return Clazz.getClass (this);
660-
};
661-
662-
Object.prototype.clone = function () {
663-
var o = new this.constructor ();
664-
for (var i in this) {
665-
o[i] = this[i];
666-
}
667-
return o;
668-
//return this;
669-
};
670-
671-
/*
672-
* Methods for thread in Object
673-
*/
674-
Object.prototype.finalize = function () {};
675-
Object.prototype.notify = function () {};
676-
Object.prototype.notifyAll = function () {};
677-
Object.prototype.wait = function () {};
678-
679-
Object.prototype.to$tring = Object.prototype.toString;
680-
Object.prototype.toString = function () {
681-
if (this.__CLASS_NAME__ != null) {
682-
return "[" + this.__CLASS_NAME__ + " object]";
683-
} else {
684-
return this.to$tring ();
685-
}
686-
};
637+
JavaObject.getName = Clazz.innerFunctions.getName;
687638

688639
w$ = window; // Short for browser's window object
689640
d$ = document; // Short for browser's document object
690641
System = {
691-
out : {
692-
__CLASS_NAME__ : "java.io.PrintStream",
693-
print : function () {},
694-
printf : function () {},
695-
println : function () {}
696-
},
697-
err : {
698-
__CLASS_NAME__ : "java.io.PrintStream",
699-
print : function () {},
700-
printf : function () {},
701-
println : function () {}
702-
},
703642
currentTimeMillis : function () {
704643
return new Date ().getTime ();
705644
},
@@ -744,6 +683,18 @@ System = {
744683
}
745684
}
746685
};
686+
System.out = new JavaObject ();
687+
System.out.__CLASS_NAME__ = "java.io.PrintStream";
688+
System.out.print = function () {};
689+
System.out.printf = function () {};
690+
System.out.println = function () {};
691+
692+
System.err = new JavaObject ();
693+
System.err.__CLASS_NAME__ = "java.io.PrintStream";
694+
System.err.print = function () {};
695+
System.err.printf = function () {};
696+
System.err.println = function () {};
697+
747698
popup = assert = log = error = window.alert;
748699

749700
Thread = function () {};
@@ -953,7 +904,7 @@ Clazz.innerFunctions.newInstance = function () {
953904
{
954905
var inF = Clazz.innerFunctionNames;
955906
for (var i = 0; i < inF.length; i++) {
956-
Object[inF[i]] = Clazz.innerFunctions[inF[i]];
907+
JavaObject[inF[i]] = Clazz.innerFunctions[inF[i]];
957908
Array[inF[i]] = Clazz.innerFunctions[inF[i]];
958909
}
959910
Array["isArray"] = function () {

sources/net.sf.j2s.java.core/src/java/lang/Console.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ window.assert = function () {
445445
* java.lang.System may be overriden later
446446
*/
447447
/* public */
448-
System = new Object ();
448+
System = new JavaObject ();
449449

450450
System.currentTimeMillis = function () {
451451
return new Date ().getTime ();
@@ -492,9 +492,8 @@ window.assert = function () {
492492
};
493493

494494
/* public */
495-
System.out = {
496-
__CLASS_NAME__ : "java.io.PrintStream"
497-
}; //new Object ();
495+
System.out = new JavaObject ();
496+
System.out.__CLASS_NAME__ = "java.io.PrintStream";
498497

499498
/* public */
500499
System.out.print = function (s) {
@@ -514,9 +513,8 @@ window.assert = function () {
514513
};
515514

516515
/* public */
517-
System.err = {
518-
__CLASS_NAME__ : "java.io.PrintStream"
519-
}; //new Object ();
516+
System.err = new JavaObject ();
517+
System.err.__CLASS_NAME__ = "java.io.PrintStream";
520518

521519
/* public */
522520
System.err.print = function (s) {

sources/net.sf.j2s.java.core/src/java/lang/Number.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
Clazz.load (["java.io.Serializable"], "java.lang.Number", null, function () {
22
java.lang.Number = Number;
3+
if (Clazz.supportsNativeObject) {
4+
for (var i = 0; i < Clazz.extendedObjectMethods.length; i++) {
5+
var p = Clazz.extendedObjectMethods[i];
6+
Number.prototype[p] = JavaObject.prototype[p];
7+
}
8+
}
39
//Clazz.decorateAsType (Number, "Number", null, java.io.Serializable, null, true);
410
Number.__CLASS_NAME__ = "Number";
511
Clazz.implementOf (Number, java.io.Serializable);

sources/net.sf.j2s.java.core/src/java/lang/Object.js

Lines changed: 0 additions & 42 deletions
This file was deleted.

sources/net.sf.j2s.java.core/src/java/lang/String.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
Clazz.load (["java.lang.CharSequence", "$.Comparable", "java.io.Serializable", "java.util.Comparator"], "java.lang.String", null, function () {
22
if (String.prototype.$replace == null) { // not defined yet! ClazzLoader may try to load this twice!
33
java.lang.String = String;
4+
if (Clazz.supportsNativeObject) {
5+
for (var i = 0; i < Clazz.extendedObjectMethods.length; i++) {
6+
var p = Clazz.extendedObjectMethods[i];
7+
if ("to$tring" == p || "toString" == p || "equals" == p || "hashCode" == p) {
8+
continue;
9+
}
10+
String.prototype[p] = JavaObject.prototype[p];
11+
}
12+
}
413
//Clazz.decorateAsType (String, "String", null, [java.io.Serializable, CharSequence, Comparable]);
514
Clazz.implementOf (String, [java.io.Serializable, CharSequence, Comparable]);
615
//Number.equals = Clazz.innerFunctions.equals;

0 commit comments

Comments
 (0)