Skip to content

Commit 3ccf701

Browse files
author
zhourenjian
committed
1. Add support that loading j2slib.z.js multiple times does not break existed J2SLib core
2. Add support of Class#getClassLoader 3. Modify loading status bar's position from left-top to left-bottom
1 parent 2ee4e10 commit 3ccf701

File tree

5 files changed

+103
-21
lines changed

5 files changed

+103
-21
lines changed

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

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* @create Nov 5, 2005
1414
*******/
1515

16+
if (window["Clazz"] == null) {
1617
/*
1718
* The following *-# are used to compress the JavaScript file into small file.
1819
* For more details, please read /net.sf.j2s.lib/build/build.xml
@@ -38,12 +39,10 @@
3839
* Class Clazz. All the methods are static in this class.
3940
*/
4041
/* static */
41-
function Clazz () {
42+
Class = Clazz = function () {
4243
};
4344

44-
Class = Clazz;
45-
46-
function NullObject () {
45+
NullObject = function () {
4746
};
4847

4948
/**
@@ -1461,7 +1460,7 @@ Clazz.instantialize = function (objThis, args) {
14611460
/* protected */
14621461
/*-# innerFunctionNames -> iFN #-*/
14631462
Clazz.innerFunctionNames = [
1464-
"equals", "getName", "getResourceAsStream" /*# {$no.javascript.support} >>x #*/, "defineMethod", "defineStaticMethod",
1463+
"equals", "getName", "getClassLoader", "getResourceAsStream" /*# {$no.javascript.support} >>x #*/, "defineMethod", "defineStaticMethod",
14651464
"makeConstructor" /*# x<< #*/
14661465
];
14671466

@@ -1484,6 +1483,20 @@ Clazz.innerFunctions = {
14841483
return Clazz.getClassName (this, true);
14851484
},
14861485

1486+
getClassLoader : function () {
1487+
var clazzName = this.__CLASS_NAME__;
1488+
var baseFolder = ClazzLoader.getClasspathFor (clazzName);
1489+
var x = baseFolder.lastIndexOf (clazzName.replace (/\./g, "/"));
1490+
if (x != -1) {
1491+
baseFolder = baseFolder.substring (0, x);
1492+
} else {
1493+
baseFolder = ClazzLoader.getClasspathFor (clazzName, true);
1494+
}
1495+
var loader = ClassLoader.requireLoaderByBase (baseFolder);
1496+
loader.getResourceAsStream = Clazz.innerFunctions.getResourceAsStream;
1497+
return loader;
1498+
},
1499+
14871500
getResourceAsStream : function (name) {
14881501
var is = null;
14891502
if (name == null) {
@@ -1526,7 +1539,9 @@ Clazz.innerFunctions = {
15261539
is.url = baseFolder + name.substring (1);
15271540
}
15281541
} else {
1529-
if (window["ClazzLoader"] != null) {
1542+
if (this.base != null) {
1543+
baseFolder = this.base;
1544+
} else if (window["ClazzLoader"] != null) {
15301545
baseFolder = ClazzLoader.getClasspathFor (clazzName);
15311546
var x = baseFolder.lastIndexOf (clazzName.replace (/\./g, "/"));
15321547
if (x != -1) {
@@ -1556,12 +1571,16 @@ Clazz.innerFunctions = {
15561571
//if (baseFolder.indexOf ('/') == 0) {
15571572
// baseFolder = baseFolder.substring (1);
15581573
//}
1559-
var idx = clazzName.lastIndexOf ('.');
1560-
if (idx == -1) {
1574+
if (this.base != null) {
15611575
is.url = baseFolder + name;
15621576
} else {
1563-
is.url = baseFolder + clazzName.substring (0, idx)
1564-
.replace (/\./g, '/') + "/" + name;
1577+
var idx = clazzName.lastIndexOf ('.');
1578+
if (idx == -1 || this.base != null) {
1579+
is.url = baseFolder + name;
1580+
} else {
1581+
is.url = baseFolder + clazzName.substring (0, idx)
1582+
.replace (/\./g, '/') + "/" + name;
1583+
}
15651584
}
15661585
}
15671586
return is;
@@ -1774,4 +1793,4 @@ Clazz.declareInterface (java.util,"Comparator");
17741793
java.lang.ClassLoader = {
17751794
__CLASS_NAME__ : "ClassLoader"
17761795
};
1777-
1796+
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* @create March 10, 2006
1414
*******/
1515

16+
if (window["Clazz"] == null || window["Clazz"].unloadClass == null) {
1617
/**
1718
* Once ClassExt.js is part of Class.js.
1819
* In order to make the Class.js as small as possible, part of its content
@@ -119,6 +120,9 @@ Clazz.prepareCallback = function (objThis, args) {
119120
*/
120121
/* public */
121122
Clazz.innerTypeInstance = function (clazzInner, objThis, finalVars) {
123+
if (clazzInner == null) {
124+
clazzInner = arguments.callee.caller;
125+
}
122126
var obj = null;
123127
/*if (arguments.length == 2) {
124128
obj = new clazzInner (objThis);
@@ -638,7 +642,15 @@ Object.prototype.equals = function (obj) {
638642
};
639643

640644
Object.prototype.hashCode = function () {
641-
return this.toString ().hashCode ();
645+
try {
646+
return this.toString ().hashCode ();
647+
} catch (e) {
648+
var str = ":";
649+
for (var s in this) {
650+
str += s + ":"
651+
}
652+
return str.hashCode ();
653+
}
642654
};
643655

644656
Object.prototype.getClass = function () {
@@ -1031,4 +1043,4 @@ Clazz.unloadClass = function (qClazzName) {
10311043
}
10321044
return false;
10331045
};
1034-
1046+
}

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

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* @create July 10, 2006
1414
*******/
1515

16+
if (window["ClazzNode"] == null) {
1617
/**
1718
* TODO:
1819
* Make optimization over class dependency tree.
@@ -164,6 +165,20 @@ ClazzNode.STATUS_OPTIONALS_LOADED = 5;
164165
*/
165166
ClazzLoader = function () {};
166167

168+
ClazzLoader.loaders = [];
169+
170+
ClazzLoader.requireLoaderByBase = function (base) {
171+
for (var i = 0; i < ClazzLoader.loaders.length; i++) {
172+
if (ClazzLoader.loaders[i].base == base) {
173+
return ClazzLoader.loaders[i];
174+
}
175+
}
176+
var loader = new ClazzLoader ();
177+
loader.base = base;
178+
ClazzLoader.loaders[ClazzLoader.loaders.length] = loader;
179+
return loader;
180+
};
181+
167182
/**
168183
* Class dependency tree
169184
*/
@@ -194,6 +209,7 @@ ClazzLoader.userAgent = navigator.userAgent.toLowerCase ();
194209
ClazzLoader.isOpera = (ClazzLoader.userAgent.indexOf ("opera") != -1);
195210
ClazzLoader.isIE = (ClazzLoader.userAgent.indexOf ("msie") != -1) && !ClazzLoader.isOpera;
196211
ClazzLoader.isGecko = (ClazzLoader.userAgent.indexOf ("gecko") != -1);
212+
ClazzLoader.isChrome = (ClazzLoader.userAgent.indexOf ("chrome") != -1);
197213

198214
/*
199215
* Opera has different loading order which will result in performance degrade!
@@ -911,7 +927,11 @@ ClazzLoader.loadScript = function (file) {
911927
// Create script DOM element
912928
var script = document.createElement ("SCRIPT");
913929
script.type = "text/javascript";
914-
script.src = file;
930+
if (ClazzLoader.isChrome && ClazzLoader.reloadingClasses[file]) {
931+
script.src = file + "?" + Math.floor (100000 * Math.random ());
932+
} else {
933+
script.src = file;
934+
}
915935
var head = document.getElementsByTagName ("HEAD")[0];
916936

917937
if (ignoreOnload) {
@@ -921,13 +941,25 @@ ClazzLoader.loadScript = function (file) {
921941
}
922942
// Alert when the script is loaded
923943
if (typeof (script.onreadystatechange) == "undefined" || !ClazzLoader.isIE) { // W3C
944+
if (ClazzLoader.isGecko && (file.indexOf ("file:") == 0
945+
|| (window.location.protocol == "file:" && file.indexOf ("http") != 0))) {
946+
script.timeoutHandle = window.setTimeout ((function (scriptEl) {
947+
return function () {
948+
scriptEl.onerror ();
949+
};
950+
}) (script), 500); // 0.5s for loading a local file is considered enough long
951+
}
924952
/*
925953
* What about Safari?
926954
*/
927955
/*
928956
* Opera will trigger onload event even there are no *.js existed
929957
*/
930-
script.onload = function () {
958+
script.onload = function () {
959+
if (ClazzLoader.isGecko && this.timeoutHandle != null) {
960+
window.clearTimeout (this.timeoutHandle);
961+
this.timeoutHandle = null;
962+
}
931963
if (ClazzLoader.inLoadingThreads > 0) {
932964
ClazzLoader.inLoadingThreads--;
933965
}
@@ -967,6 +999,10 @@ ClazzLoader.loadScript = function (file) {
967999
* For Firefox/Mozilla, unexisted *.js will result in errors.
9681000
*/
9691001
script.onerror = function () { // Firefox/Mozilla
1002+
if (ClazzLoader.isGecko && this.timeoutHandle != null) {
1003+
window.clearTimeout (this.timeoutHandle);
1004+
this.timeoutHandle = null;
1005+
}
9701006
if (ClazzLoader.inLoadingThreads > 0) {
9711007
ClazzLoader.inLoadingThreads--;
9721008
}
@@ -2597,6 +2633,11 @@ ClazzLoader.lastHotspotUpdated = new Date ().getTime ();
25972633
/*-# lastHotspotSessionID -> ltSI #-*/
25982634
ClazzLoader.lastHotspotSessionID = 0;
25992635

2636+
/* Google Chrome need to load *.js using url + "?" + random number */
2637+
if (ClazzLoader.isChrome) {
2638+
ClazzLoader.reloadingClasses = new Object ();
2639+
}
2640+
26002641
/*
26012642
* This method will be called in server-return-script:
26022643
*
@@ -2644,11 +2685,17 @@ ClazzLoader.updateHotspot = function () {
26442685
for (var i = 0; i < toUpdateClasses.length; i++) {
26452686
if (needUpdateClasses[i]) {
26462687
var clzz = toUpdateClasses[i];
2688+
if (ClazzLoader.isChrome) {
2689+
ClazzLoader.reloadingClasses[ClazzLoader.getClasspathFor (clzz)] = true;
2690+
}
26472691
ClazzLoader.loadClass (clzz, (function (clazz) {
26482692
return function () {
26492693
// succeeded!
26502694
Clazz.unloadedClasses[clazz] = null;
26512695
ClazzLoader.classReloaded (clazz);
2696+
if (ClazzLoader.isChrome) {
2697+
ClazzLoader.reloadingClasses[ClazzLoader.getClasspathFor (clazz)] = false;
2698+
}
26522699
};
26532700
}) (clzz));
26542701
}
@@ -2691,7 +2738,7 @@ ClazzLoader.loadHotspotScript = function (hotspotURL, iframeID) {
26912738
var script = document.createElement ("SCRIPT");
26922739
script.type = "text/javascript";
26932740
script.src = hotspotURL;
2694-
if (typeof (script.onreadystatechange) == "undefined") { // W3C
2741+
if (typeof (script.onreadystatechange) == "undefined" || !ClazzLoader.isIE) { // W3C
26952742
script.onload = script.onerror = function () {
26962743
try {
26972744
ClazzLoader.lastHotspotScriptLoaded = true;
@@ -2773,4 +2820,4 @@ window.setTimeout (function () {
27732820
}, 324); // 0.324 seconds is considered as enough before refresh action
27742821

27752822
ClassLoader = ClazzLoader;
2776-
2823+
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
* @author zhou renjian
1313
* @create Jan 11, 2007
1414
*******/
15-
15+
16+
if (window["ClazzLoaderProgressMonitor"] == null) {
1617
/*
1718
* Update: March 5, 2007
1819
* ClassLoaderProgressMonitor were already in default j2slib.z.js.
@@ -75,7 +76,7 @@ clpm.DEFAULT_OPACITY = 55;
7576
};
7677
/* private */ clpm.createHandle = function () {
7778
var div = document.createElement ("DIV");
78-
div.style.cssText = "position:absolute;top:4px;left:4px;padding:2px 8px;"
79+
div.style.cssText = "position:absolute;bottom:4px;left:4px;padding:2px 8px;"
7980
+ "z-index:3333;background-color:#8e0000;color:yellow;"
8081
+ "font-family:Arial, sans-serif;font-size:10pt;white-space:nowrap;";
8182
div.onmouseover = function () {
@@ -140,7 +141,7 @@ clpm.showStatus = function (msg, fading) {
140141
var offTop = this.getFixedOffsetTop ();
141142
if (this.lastScrollTop != offTop) {
142143
this.lastScrollTop = offTop;
143-
this.monitorEl.style.top = (this.lastScrollTop + 4) + "px";
144+
this.monitorEl.style.bottom = (this.lastScrollTop + 4) + "px";
144145
}
145146
if (fading) {
146147
this.fadeOut();
@@ -168,3 +169,4 @@ if (window["ClazzLoader"] != null) {
168169
ClazzLoader.setLoadingMode ("script", 5);
169170
}
170171
}
172+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
* @author zhou renjian
1313
* @create Nov 5, 2005
1414
*******/
15-
15+
16+
if (window["Console"] == null) {
1617
/*-#
1718
# Console -> C_$
1819
#
@@ -619,3 +620,4 @@ window.assert = function () {
619620
return o;
620621
};
621622
//}
623+
}

0 commit comments

Comments
 (0)