Skip to content

Commit 15b2e67

Browse files
author
zhourenjian@gmail.com
committed
ClassLoader now cache two methods so it will run much faster in those cases making super calls.
Fixed bug of ClassLoader running into endless loop or missing class loading Add number related operations and overriding constructor methods to Clazz
1 parent ead1904 commit 15b2e67

File tree

5 files changed

+333
-120
lines changed

5 files changed

+333
-120
lines changed

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

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,9 @@ Clazz.inheritClass = function (clazzThis, clazzSuper, objSuper) {
346346
if (Clazz.isClassUnloaded (clazzThis)) {
347347
// Don't change clazzThis.protoype! Keep it!
348348
} else if (objSuper != null) {
349-
// ! Unsafe of refrence prototype to an instance!
349+
// ! Unsafe of reference prototype to an instance!
350350
// Feb 19, 2006 --josson
351-
// OK for this refrence to an instance, as this is anonymous instance,
351+
// OK for this reference to an instance, as this is anonymous instance,
352352
// which is not referenced elsewhere.
353353
// March 13, 2006
354354
clazzThis.prototype = objSuper;
@@ -765,9 +765,15 @@ Clazz.searchAndExecuteMethod = function (objThis, claxxRef, fxName, funParams) {
765765
var params = Clazz.getParamsType (funParams);
766766
var fx = objThis[fxName];
767767
/*
768-
* Cache last matched method
768+
* Cache last or previous matched method
769769
*/
770+
var cached = 0; // 0: no cache; 1: last matched; 2: previous matched
770771
if (fx.lastParams == params.typeString && fx.lastClaxxRef === claxxRef) {
772+
cached = 1;
773+
} else if (fx.prevParams == params.typeString && fx.prevClaxxRef === claxxRef) {
774+
cached = 2;
775+
}
776+
if (cached != 0) {
771777
var methodParams = null;
772778
if (params.hasCastedNull) {
773779
methodParams = new Array ();
@@ -786,12 +792,23 @@ Clazz.searchAndExecuteMethod = function (objThis, claxxRef, fxName, funParams) {
786792
} else {
787793
methodParams = funParams;
788794
}
789-
if (fx.lastMethod != null) {
795+
if (cached == 1 && fx.lastMethod != null) {
790796
return fx.lastMethod.apply (objThis, methodParams);
797+
} else if (cached == 2 && fx.prevMethod != null) {
798+
var f = fx.prevMethod;
799+
fx.prevParams = fx.lastParams;
800+
fx.prevClaxxRef = fx.lastClaxxRef;
801+
fx.prevMethod = fx.lastMethod;
802+
fx.lastParams = params.typeString;
803+
fx.lastClaxxRef = claxxRef;
804+
fx.lastMethod = f;
805+
return f.apply (objThis, methodParams);
791806
} else { // missed default constructor ?
792807
return ;
793808
}
794809
}
810+
fx.prevParams = fx.lastParams;
811+
fx.prevClaxxRef = fx.lastClaxxRef;
795812
fx.lastParams = params.typeString;
796813
fx.lastClaxxRef = claxxRef;
797814

@@ -982,6 +999,7 @@ Clazz.tryToSearchAndExecute = function (objThis, clazzFun, params, funParams/*,
982999
}
9831000
Clazz.pu$hCalling (new Clazz.callingStack (caller, owner));
9841001
}
1002+
fx.prevMethod = fx.lastMethod;
9851003
fx.lastMethod = f;
9861004
var ret = f.apply (objThis, methodParams);
9871005
if (noInnerWrapper) {
@@ -993,6 +1011,7 @@ Clazz.tryToSearchAndExecute = function (objThis, clazzFun, params, funParams/*,
9931011
return ret;
9941012
}
9951013
/*# x<< #*/
1014+
fx.prevMethod = fx.lastMethod;
9961015
fx.lastMethod = f;
9971016
return f.apply (objThis, methodParams);
9981017
//}
@@ -1323,8 +1342,24 @@ Clazz.defineMethod = function (clazzThis, funName, funBody, funParams) {
13231342
*/
13241343
/* public */
13251344
Clazz.makeConstructor = function (clazzThis, funBody, funParams) {
1326-
var funName = "construct";
1327-
Clazz.defineMethod (clazzThis, funName, funBody, funParams);
1345+
Clazz.defineMethod (clazzThis, "construct", funBody, funParams);
1346+
if (clazzThis.con$truct != null) {
1347+
clazzThis.con$truct.index = clazzThis.con$truct.stacks.length;
1348+
}
1349+
//clazzThis.con$truct = clazzThis.prototype.con$truct = null;
1350+
};
1351+
1352+
/**
1353+
* Override constructor for the class with the given function body and
1354+
* parameters signature.
1355+
*
1356+
* @param clazzThis host class
1357+
* @param funBody constructor body
1358+
* @param funParams constructor parameters signature
1359+
*/
1360+
/* public */
1361+
Clazz.overrideConstructor = function (clazzThis, funBody, funParams) {
1362+
Clazz.overrideMethod (clazzThis, "construct", funBody, funParams);
13281363
if (clazzThis.con$truct != null) {
13291364
clazzThis.con$truct.index = clazzThis.con$truct.stacks.length;
13301365
}
@@ -1957,9 +1992,6 @@ Clazz.exceptionOf=function(e, clazz) {
19571992
else
19581993
return false;
19591994
};
1960-
1961-
/* sgurin: preserve Number.prototype.toString */
1962-
Number.prototype._numberToString=Number.prototype.toString;
19631995

19641996
Clazz.declarePackage ("java.io");
19651997
//Clazz.declarePackage ("java.lang");

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

Lines changed: 107 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Clazz.prepareCallback = function (objThis, args) {
8181
*/
8282
obs[className.replace (/org\.eclipse\.swt\./, "$wt.")] = classThisObj;
8383
var clazz = Clazz.getClass (classThisObj);
84-
while (clazz.superClazz != null) {
84+
while (clazz != null && clazz.superClazz != null) {
8585
clazz = clazz.superClazz;
8686
//obs[Clazz.getClassName (clazz)] = classThisObj;
8787
/*
@@ -124,65 +124,77 @@ Clazz.innerTypeInstance = function (clazzInner, objThis, finalVars) {
124124
clazzInner = arguments.callee.caller;
125125
}
126126
var obj = null;
127-
/*if (arguments.length == 2) {
128-
obj = new clazzInner (objThis);
129-
} else */if (arguments.length == 3) {
130-
obj = new clazzInner (objThis);
131-
} else if (arguments.length == 4) {
132-
if (objThis.__CLASS_NAME__ == clazzInner.__CLASS_NAME__
133-
&& arguments[3] === Clazz.inheritArgs) {
134-
obj = objThis;
127+
if (finalVars == null && objThis.$finals == null) {
128+
/*if (arguments.length == 2) {
129+
obj = new clazzInner (objThis);
130+
} else */if (arguments.length == 3) {
131+
obj = new clazzInner (objThis);
132+
} else if (arguments.length == 4) {
133+
if (objThis.__CLASS_NAME__ == clazzInner.__CLASS_NAME__
134+
&& arguments[3] === Clazz.inheritArgs) {
135+
obj = objThis;
136+
} else {
137+
obj = new clazzInner (objThis, arguments[3]);
138+
}
139+
} else if (arguments.length == 5) {
140+
obj = new clazzInner (objThis, arguments[3], arguments[4]);
141+
} else if (arguments.length == 6) {
142+
obj = new clazzInner (objThis, arguments[3], arguments[4],
143+
arguments[5]);
144+
} else if (arguments.length == 7) {
145+
obj = new clazzInner (objThis, arguments[3], arguments[4],
146+
arguments[5], arguments[6]);
147+
} else if (arguments.length == 8) {
148+
obj = new clazzInner (objThis, arguments[3], arguments[4],
149+
arguments[5], arguments[6], arguments[7]);
150+
} else if (arguments.length == 9) {
151+
obj = new clazzInner (objThis, arguments[3], arguments[4],
152+
arguments[5], arguments[6], arguments[7], arguments[8]);
153+
} else if (arguments.length == 10) {
154+
obj = new clazzInner (objThis, arguments[3], arguments[4],
155+
arguments[5], arguments[6], arguments[7], arguments[8],
156+
arguments[9]);
135157
} else {
136-
obj = new clazzInner (objThis, arguments[3]);
158+
/*
159+
* Should construct instance manually.
160+
*/
161+
obj = new clazzInner (objThis, Clazz.inheritArgs);
162+
//if (obj.construct == null) {
163+
// throw new String ("No support anonymous class constructor with "
164+
// + "more than 7 parameters.");
165+
//}
166+
var args = new Array ();
167+
for (var i = 3; i < arguments.length; i++) {
168+
args[i - 3] = arguments[i];
169+
}
170+
//obj.construct.apply (obj, args);
171+
Clazz.instantialize (obj, args);
137172
}
138-
} else if (arguments.length == 5) {
139-
obj = new clazzInner (objThis, arguments[3], arguments[4]);
140-
} else if (arguments.length == 6) {
141-
obj = new clazzInner (objThis, arguments[3], arguments[4],
142-
arguments[5]);
143-
} else if (arguments.length == 7) {
144-
obj = new clazzInner (objThis, arguments[3], arguments[4],
145-
arguments[5], arguments[6]);
146-
} else if (arguments.length == 8) {
147-
obj = new clazzInner (objThis, arguments[3], arguments[4],
148-
arguments[5], arguments[6], arguments[7]);
149-
} else if (arguments.length == 9) {
150-
obj = new clazzInner (objThis, arguments[3], arguments[4],
151-
arguments[5], arguments[6], arguments[7], arguments[8]);
152-
} else if (arguments.length == 10) {
153-
obj = new clazzInner (objThis, arguments[3], arguments[4],
154-
arguments[5], arguments[6], arguments[7], arguments[8],
155-
arguments[9]);
156173
} else {
157-
/*
158-
* Should construct instance manually.
159-
*/
160-
obj = new clazzInner ();
161-
if (obj.construct == null) {
162-
throw new String ("No support anonymous class constructor with "
163-
+ "more than 7 parameters.");
174+
obj = new clazzInner (objThis, Clazz.inheritArgs);
175+
// f$ is short for the once choosen "$finals"
176+
if (finalVars != null && objThis.f$ == null) {
177+
obj.f$ = finalVars;
178+
} else if (finalVars == null && objThis.f$ != null) {
179+
obj.f$ = objThis.f$;
180+
} else if (finalVars != null && objThis.f$ != null) {
181+
var o = new Object ();
182+
for (var attr in objThis.f$) {
183+
o[attr] = objThis.f$[attr];
184+
}
185+
for (var attr in finalVars) {
186+
o[attr] = finalVars[attr];
187+
}
188+
obj.f$ = o;
164189
}
190+
165191
var args = new Array ();
166192
for (var i = 3; i < arguments.length; i++) {
167193
args[i - 3] = arguments[i];
168194
}
169-
obj.construct.apply (obj, args);
170-
}
171-
// f$ is short for the once choosen "$finals"
172-
if (finalVars != null && objThis.f$ == null) {
173-
obj.f$ = finalVars;
174-
} else if (finalVars == null && objThis.f$ != null) {
175-
obj.f$ = objThis.f$;
176-
} else if (finalVars != null && objThis.f$ != null) {
177-
var o = new Object ();
178-
for (var attr in objThis.f$) {
179-
o[attr] = objThis.f$[attr];
180-
}
181-
for (var attr in finalVars) {
182-
o[attr] = finalVars[attr];
183-
}
184-
obj.f$ = o;
195+
Clazz.instantialize (obj, args);
185196
}
197+
186198
/*
187199
if (finalVars != null && objThis.$finals == null) {
188200
obj.$finals = finalVars;
@@ -685,6 +697,20 @@ System = {
685697
dest[destPos + i] = swap[i];
686698
}
687699
}
700+
},
701+
identityHashCode: function (obj) {
702+
if (obj == null) {
703+
return 0;
704+
}
705+
try {
706+
return obj.toString ().hashCode ();
707+
} catch (e) {
708+
var str = ":";
709+
for (var s in obj) {
710+
str += s + ":"
711+
}
712+
return str.hashCode ();
713+
}
688714
}
689715
};
690716
System.out = new JavaObject ();
@@ -828,6 +854,19 @@ Clazz.int0RightShift = function (n, o) { // 64bit
828854
return n >>> o; // no needs for this shifting wrapper
829855
};
830856

857+
Clazz.floatToInt = function (x) {
858+
return x < 0 ? Math.ceil(x) : Math.floor(x);
859+
};
860+
861+
Clazz.floatToByte = Clazz.floatToShort = Clazz.floatToLong = Clazz.floatToInt;
862+
Clazz.doubleToByte = Clazz.doubleToShort = Clazz.doubleToLong = Clazz.doubleToInt = Clazz.floatToInt;
863+
864+
Clazz.floatToChar = function (x) {
865+
return String.fromCharCode (x < 0 ? Math.ceil(x) : Math.floor(x));
866+
};
867+
868+
Clazz.doubleToChar = Clazz.floatToChar;
869+
831870
// Compress the common public API method in shorter name
832871
$_L=Clazz.load;
833872
$_W=Clazz.declareAnonymous;
@@ -842,6 +881,7 @@ $_P=Clazz.p0p;
842881
$_B=Clazz.prepareCallback;
843882
$_N=Clazz.innerTypeInstance;
844883
$_K=Clazz.makeConstructor;
884+
$_k=Clazz.overrideConstructor;
845885
$_U=Clazz.superCall;
846886
$_R=Clazz.superConstructor;
847887
$_M=Clazz.defineMethod;
@@ -860,6 +900,16 @@ $_AB=Clazz.newByteArray;
860900
$_AC=Clazz.newCharArray;
861901
$_Ab=Clazz.newBooleanArray;
862902
//$_AX=Clazz.newStringArray;
903+
$_fI=Clazz.floatToInt;
904+
$_fS=Clazz.floatToShort;
905+
$_fB=Clazz.floatToByte;
906+
$_fL=Clazz.floatToLong;
907+
$_fC=Clazz.floatToChar;
908+
$_dI=Clazz.doubleToInt;
909+
$_dS=Clazz.doubleToShort;
910+
$_dB=Clazz.doubleToByte;
911+
$_dL=Clazz.doubleToLong;
912+
$_dC=Clazz.doubleToChar;
863913
$_O=Clazz.instanceOf;
864914
$_G=Clazz.inheritArgs;
865915
$_X=Clazz.checkPrivateMethod;
@@ -976,6 +1026,12 @@ Clazz.cleanDelegateMethod = function (m) {
9761026
m.lastParams = null;
9771027
m.lastClaxxRef = null;
9781028
}
1029+
if (typeof m == "function" && m.prevMethod != null
1030+
&& m.prevParams != null && m.prevClaxxRef != null) {
1031+
m.prevMethod = null;
1032+
m.prevParams = null;
1033+
m.prevClaxxRef = null;
1034+
}
9791035
};
9801036

9811037
/* public */

0 commit comments

Comments
 (0)