forked from BobHanson/java2script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClass.js0
More file actions
2218 lines (2125 loc) · 64.3 KB
/
Copy pathClass.js0
File metadata and controls
2218 lines (2125 loc) · 64.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// DEPRECATED -- SEE J2SSwingJS.js
/******************************************************************************
* Copyright (c) 2007 java2script.org and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Zhou Renjian - initial API and implementation
*****************************************************************************/
/*******
* @author zhou renjian
* @create Nov 5, 2005
*******/
if (window["Clazz"] == null) {
/*
* The following *-# are used to compress the JavaScript file into small file.
* For more details, please read /net.sf.j2s.lib/build/build.xml
*/
/*-#
# _x_CLASS_NAME__ -> C$N
# _x_PKG_NAME__ -> P$N
#
# clazzThis -> Tz
# objThis -> To
# clazzHost -> Hz
# hostThis -> Th
# hostSuper -> Sh
# clazzFun -> Fc
# clazzName -> Nc
# funName -> Nf
# funBody -> Bf
# objType -> oT
#
# qClazzName ->Nq
#-*/
/**
* Class Clazz. All the methods are static in this class.
*/
/* static */
Class = Clazz = function () {
};
NullObject = function () {
};
JavaObject = Object;
/* protected */
Clazz.supportsNativeObject = window["j2s.object.native"];
Clazz.supportsNativeArray = window["j2s.array.native"];
if (Clazz.supportsNativeObject) {
JavaObject = function () {};
}
JavaObject.prototype.equals = function (obj) {
return this == obj;
};
Clazz.internalHashCode = 1;
JavaObject.prototype.hashCode = function () {
if (!this.internalHashCode) {
this.internalHashCode = Clazz.internalHashCode++;
}
return this.internalHashCode;
};
JavaObject.prototype.getClass = function () {
return Clazz.getClass (this);
};
JavaObject.prototype.clone = function () {
var o = new this.constructor ();
for (var i in this) {
o[i] = this[i];
}
return o;
};
/*
* Methods for thread in Object
*/
JavaObject.prototype.finalize = function () {};
JavaObject.prototype.notify = function () {};
JavaObject.prototype.notifyAll = function () {};
JavaObject.prototype.wait = function () {};
JavaObject.prototype.to$tring = Object.prototype.toString;
JavaObject.prototype.toString = function () {
if (this.__CLASS_NAME__ != null) {
return "[" + this.__CLASS_NAME__ + " object]";
} else {
return JavaObject.prototype.to$tring.apply (this, arguments);
}
};
(function() {
if (Clazz.supportsNativeObject) {
/* protected */
Clazz.extendedObjectMethods = [
"equals", "hashCode", "getClass", "clone", "finalize", "notify", "notifyAll", "wait", "to$tring", "toString"
];
if (!Clazz.supportsNativeArray) {
Array.prototype.to$tring = Array.prototype.toString;
for (var i = 0; i < Clazz.extendedObjectMethods.length; i++) {
var p = Clazz.extendedObjectMethods[i];
if (p == "toString" || p == "to$tring") continue;
Array.prototype[p] = JavaObject.prototype[p];
}
}
JavaObject.__CLASS_NAME__ = "Object";
JavaObject["getClass"] = function () { return JavaObject; };
}
}) ();
/**
* Try to fix bug on Safari
*/
InternalFunction = Object;
/**
* Return the class name of the given class or object.
*
* @param clazzHost given class or object
* @return class name
*/
/* public */
Clazz.getClassName = function (clazzHost) {
if (clazzHost == null) {
/*
* null is always treated as Object.
* But what about "undefined"?
*/
return "NullObject";
}
if (typeof clazzHost == "function") {
var clazz = clazzHost;
if (clazz.__CLASS_NAME__ != null) {
if (arguments[1] != true) {
return "Class";
}
/* user defined class name */
return clazz.__CLASS_NAME__;
}
/*-# clazzStr -> Sc #-*/
if (clazz.toString === Clazz.innerFunctions.toString) {
// Avoid running into recursion of #toString calling #getClassName
return "Object";
}
var clazzStr = clazz.toString ();
var idx0 = clazzStr.indexOf ("function");
if (idx0 == -1) {
// For Firefox 1.5.0.1+, those HTML element are no longer
// bound with function () { [native] } constructors
if (clazzStr.charAt (0) == '[') {
var clazzName = clazzStr.substring (1, clazzStr.length - 1);
if (clazzName.indexOf ("object ") != -1) { // IE
return clazzName.substring (7);
}
return clazzName;
} else {
return clazzStr.replace(/[^a-zA-Z0-9]/g, '');
}
}
var idx1 = idx0 + 8;
var idx2 = clazzStr.indexOf ("(", idx1);
if (idx2 == -1) {
return "Object";
}
var clazzName = clazzStr.substring (idx1, idx2)
.replace (/^\s+/, "").replace (/\s+$/, ""); // .trim ()
if (clazzName == "anonymous") {
return "Function";
} else {
return clazzName;
}
} else {
var obj = clazzHost;
if (obj instanceof Clazz.CastedNull || obj instanceof Clazz.CastedObject) {
return obj.clazzName;
} else {
var objType = typeof obj;
if (objType == "string") {
/*
* Always treat the constant string as String object.
* This will be compatiable with Java String instance.
*/
return "String";
} else if (objType == "object") {
if (obj.__CLASS_NAME__ != null) {
/* user defined class name */
return obj.__CLASS_NAME__;
} else if (obj.constructor == null) {
return "Object"; // For HTML Element in IE
} else {
if (obj.constructor.__CLASS_NAME__ == null) {
if (obj instanceof Number) {
return "Number";
} else if (obj instanceof Boolean) {
return "Boolean";
} else if (obj instanceof Array) {
return "Array";
}
}
var depth = arguments[2];
if (depth == null) {
depth = 1;
} else if (depth >= 4) {
return "Object";
}
return Clazz.getClassName (obj.constructor, true, depth++);
}
} else if (objType == "number") {
return "Number";
} else if (objType == "boolean") {
return "Boolean";
}
var depth = arguments[2];
if (depth == null) {
depth = 1;
} else if (depth >= 4) {
return "Object";
}
return Clazz.getClassName (obj.constructor, true, depth++);
}
}
};
/**
* Return the class of the given class or object.
*
* @param clazzHost given class or object
* @return class name
*/
/* public */
Clazz.getClass = function (clazzHost) {
if (clazzHost == null) {
/*
* null is always treated as Object.
* But what about "undefined"?
*/
return JavaObject;
}
if (typeof clazzHost == "function") {
return clazzHost;
} else {
var clazzName = null;
var obj = clazzHost;
if (obj instanceof Clazz.CastedNull || obj instanceof Clazz.CastedObject) {
clazzName = obj.clazzName;
} else {
var objType = typeof obj;
if (objType == "string") {
return String;
} else if (typeof obj == "object") {
/* user defined class name */
if (obj.__CLASS_NAME__ != null) {
clazzName = obj.__CLASS_NAME__;
} else if (obj.constructor == null) {
return JavaObject; // Is it safe?
} else {
return obj.constructor;
}
}
}
if (clazzName != null) {
//var hostedClazz = null;
//eval ("hostedClazz = " + clazzName + ";");
//return hostedClazz;
return Clazz.evalType (clazzName, true);
} else {
return obj.constructor;
}
}
};
/*
* Be used to copy members of class
*/
/* protected */
/*-# extendsProperties -> eP #-*/
Clazz.extendsProperties = function (hostThis, hostSuper) {
for (var o in hostSuper) {
if (o != "b$" && o != "prototype" && o != "superClazz"
&& o != "__CLASS_NAME__" && o != "implementz"
&& !Clazz.checkInnerFunction (hostSuper, o)) {
hostThis[o] = hostSuper[o];
}
}
};
/* private */
/*-# checkInnerFunction -> cIF #-*/
Clazz.checkInnerFunction = function (hostSuper, funName) {
for (var k = 0; k < Clazz.innerFunctionNames.length; k++) {
if (funName == Clazz.innerFunctionNames[k] &&
Clazz.innerFunctions[funName] === hostSuper[funName]) {
return true;
}
}
return false;
};
/*
* Be used to copy members of interface
*/
/* protected */
/*-# implementsProperties -> ip #-*/
Clazz.implementsProperties = function (hostThis, hostSuper) {
for (var o in hostSuper) {
if (o != "b$" && o != "prototype" && o != "superClazz"
&& o != "__CLASS_NAME__" && o != "implementz") {
if (typeof hostSuper[o] == "function") {
/*
* static final member of interface may be a class, which may
* be function.
*/
if (Clazz.checkInnerFunction (hostSuper, o)) {
continue;
}
}
hostThis[o] = hostSuper[o];
hostThis.prototype[o] = hostSuper[o];
}
}
/*
* There is no concrete fields or methods in interfaces!
* Folllowing lines see non-sense!
* March 10, 2006
*/
/*
for (var o in hostSuper.prototype) {
if (o != "__CLASS_NAME__") {
hostThis.prototype[o] = hostSuper.prototype[o];
}
}
*/
};
/*-# args4InheritClass -> aIC #-*/
Clazz.args4InheritClass = function () {
};
Clazz.inheritArgs = new Clazz.args4InheritClass ();
/**
* Inherit class with "extends" keyword and also copy those static members.
* Example, as in Java, if NAME is a static member of ClassA, and ClassB
* extends ClassA then ClassB.NAME can be accessed in some ways.
*
* @param clazzThis child class to be extended
* @param clazzSuper super class which is inherited from
* @param objSuper super class instance
*/
/* protected */
/*-#
# inheritClass -> xic
#
# objSuper -> oSp
#-*/
Clazz.inheritClass = function (clazzThis, clazzSuper, objSuper) {
//var thisClassName = Clazz.getClassName (clazzThis);
Clazz.extendsProperties (clazzThis, clazzSuper);
if (Clazz.isClassUnloaded (clazzThis)) {
// Don't change clazzThis.protoype! Keep it!
} else if (objSuper != null) {
// ! Unsafe of reference prototype to an instance!
// Feb 19, 2006 --josson
// OK for this reference to an instance, as this is anonymous instance,
// which is not referenced elsewhere.
// March 13, 2006
clazzThis.prototype = objSuper;
} else if (clazzSuper !== Number) {
clazzThis.prototype = new clazzSuper (Clazz.inheritArgs);
} else { // Number
clazzThis.prototype = new Number ();
}
clazzThis.superClazz = clazzSuper;
/*
* Is it necessary to reassign the class name?
* Mar 10, 2006 --josson
*/
//clazzThis.__CLASS_NAME__ = thisClassName;
clazzThis.prototype.__CLASS_NAME__ = clazzThis.__CLASS_NAME__;
};
/**
* Implementation of Java's keyword "implements".
* As in JavaScript there are on "implements" keyword implemented, a property
* of "implementz" is added to the class to record the interfaces the class
* is implemented.
*
* @param clazzThis the class to implement
* @param interfacez Array of interfaces
*/
/* public */
Clazz.implementOf = function (clazzThis, interfacez) {
if (arguments.length >= 2) {
if (clazzThis.implementz == null) {
clazzThis.implementz = new Array ();
}
var impls = clazzThis.implementz;
if (arguments.length == 2) {
if (typeof interfacez == "function") {
impls[impls.length] = interfacez;
Clazz.implementsProperties (clazzThis, interfacez);
} else if (interfacez instanceof Array) {
for (var i = 0; i < interfacez.length; i++) {
impls[impls.length] = interfacez[i];
Clazz.implementsProperties (clazzThis, interfacez[i]);
}
}
} else {
for (var i = 1; i < arguments.length; i++) {
impls[impls.length] = arguments[i];
Clazz.implementsProperties (clazzThis, arguments[i]);
}
}
}
};
/**
* TODO: More should be done for interface's inheritance
*/
/* public */
Clazz.extendInterface = Clazz.implementOf;
/* protected */
/*-#
# equalsOrExtendsLevel -> eOE
#
# clazzAncestor -> anc
#-*/
Clazz.equalsOrExtendsLevel = function (clazzThis, clazzAncestor) {
if (clazzThis === clazzAncestor) {
return 0;
}
if (clazzThis.implementz != null) {
var impls = clazzThis.implementz;
for (var i = 0; i < impls.length; i++) {
var level = Clazz.equalsOrExtendsLevel (impls[i], clazzAncestor);
if (level >= 0) {
return level + 1;
}
}
}
return -1;
};
/* protected */
/*-#
# getInheritedLevel -> gIL
#
# clazzBase -> bs
# clazzTarget -> tg
#-*/
Clazz.getInheritedLevel = function (clazzTarget, clazzBase) {
if (clazzTarget === clazzBase) {
return 0;
}
var isTgtStr = (typeof clazzTarget == "string");
var isBaseStr = (typeof clazzBase == "string");
if ((isTgtStr && ("void" == clazzTarget || "unknown" == clazzTarget))
|| (isBaseStr && ("void" == clazzBase
|| "unknown" == clazzBase))) {
return -1;
}
/*
* ? The following lines are confusing
* March 10, 2006
*/
if ((isTgtStr && "NullObject" == clazzTarget)
|| NullObject === clazzTarget) {
if (clazzBase !== Number && clazzBase !== Boolean
&& clazzBase !== NullObject) {
return 0;
}
}
if (isTgtStr) {
clazzTarget = Clazz.evalType (clazzTarget);
}
if (isBaseStr) {
clazzBase = Clazz.evalType (clazzBase);
}
if (clazzBase == null || clazzTarget == null) {
return -1;
}
var level = 0;
var zzalc = clazzTarget; // zzalc <--> clazz
while (zzalc !== clazzBase && level < 10) {
/* maybe clazzBase is interface */
if (zzalc.implementz != null) {
var impls = zzalc.implementz;
for (var i = 0; i < impls.length; i++) {
var implsLevel = Clazz.equalsOrExtendsLevel (impls[i],
clazzBase);
if (implsLevel >= 0) {
return level + implsLevel + 1;
}
}
}
zzalc = zzalc.superClazz;
if (zzalc == null) {
if (clazzBase === Object || clazzBase === JavaObject) {
/*
* getInheritedLevel(String, CharSequence) == 1
* getInheritedLevel(String, Object) == 1.5
* So if both #test(CharSequence) and #test(Object) existed,
* #test("hello") will correctly call #test(CharSequence)
* insted of #test(Object).
*/
return level + 1.5; // 1.5! Special!
} else {
return -1;
}
}
level++;
}
return level;
};
/**
* Implements Java's keyword "instanceof" in JavaScript's way.
* As in JavaScript part of the object inheritance is implemented in only-
* JavaScript way.
*
* @param obj the object to be tested
* @param clazz the class to be checked
* @return whether the object is an instance of the class
*/
/* public */
Clazz.instanceOf = function (obj, clazz) {
if (obj == null) {
return false; // return clazz == undefined; // should usually false
}
if (clazz == null) {
return false;
}
if (obj instanceof clazz) {
return true;
} else {
/*
* To check all the inherited interfaces.
*/
var clazzName = Clazz.getClassName (obj);
return Clazz.getInheritedLevel (clazzName, clazz) >= 0;
}
};
/**
* Call super method of the class.
* The same effect as Java's expression:
* <code> super.*() </code>
*
* @param objThis host object
* @param clazzThis class of declaring method scope. It's hard to determine
* which super class is right class for "super.*()" call when it's in runtime
* environment. For example,
* 1. ClasssA has method #run()
* 2. ClassB extends ClassA overriding method #run() with "super.run()" call
* 3. ClassC extends ClassB
* 4. objC is an instance of ClassC
* Now we have to decide which super #run() method is to be invoked. Without
* explicit clazzThis parameter, we only know that objC.getClass() is ClassC
* and current method scope is #run(). We do not known we are in scope
* ClassA#run() or scope of ClassB#run(). if ClassB is given, Clazz can search
* all super methods that are before ClassB and get the correct super method.
* This is the reason why there must be an extra clazzThis parameter.
* @param funName method name to be called
* @param funParams Array of method parameters
*/
/* public */
Clazz.superCall = function (objThis, clazzThis, funName, funParams) {
var fx = null;
var clazzFun = objThis[funName];
if (clazzFun != null) {
if (clazzFun.claxxOwner != null) {
// claxxOwner is a mark for methods that is single.
if (clazzFun.claxxOwner !== clazzThis) {
// This is a single method, call directly!
fx = clazzFun;
}
} else if (clazzFun.stacks == null && !(clazzFun.lastClaxxRef != null
&& clazzFun.lastClaxxRef.prototype[funName] != null
&& clazzFun.lastClaxxRef.prototype[funName].stacks != null)) { // super.toString
fx = clazzFun;
} else { // normal wrapped method
var stacks = clazzFun.stacks;
if (stacks == null) {
stacks = clazzFun.lastClaxxRef.prototype[funName].stacks;
}
var length = stacks.length;
for (var i = length - 1; i >= 0; i--) {
// Once super call is computed precisely, there are no need
// to calculate the inherited level but just an equals comparison
// var level = Clazz.getInheritedLevel (clazzThis, stacks[i]);
if (clazzThis === stacks[i]) { // level == 0
if (i > 0) {
fx = stacks[--i].prototype[funName];
} else {
/* Will this case be reachable? - March 4, 2006
Should never reach here if all are compiled by Java2Script */
fx = stacks[0].prototype[funName]["\\unknown"];
}
break;
} else if (Clazz.getInheritedLevel (clazzThis, stacks[i]) > 0) {
fx = stacks[i].prototype[funName];
break;
}
} // end of for loop
} // end of normal wrapped method
} // end of clazzFun != null
if (fx != null) {
/*# {$no.debug.support} >>x #*/
if (Clazz.tracingCalling) {
var caller = arguments.callee.caller;
if (caller === Clazz.superConstructor) {
caller = caller.arguments.callee.caller;
}
Clazz.pu$hCalling (new Clazz.callingStack (caller, clazzThis));
var ret = fx.apply (objThis, (funParams == null) ? [] : funParams);
Clazz.p0pCalling ();
return ret;
}
/*# x<< #*/
return fx.apply (objThis, (funParams == null) ? [] : funParams);
}
throw new Clazz.MethodNotFoundException (objThis, clazzThis, funName,
Clazz.getParamsType (funParams).typeString);
};
/**
* Call super constructor of the class.
* The same effect as Java's expression:
* <code> super() </code>
*/
/* public */
Clazz.superConstructor = function (objThis, clazzThis, funParams) {
var funName = "construct";
var fx = null;
var clazzSuper = clazzThis.superClazz;
var superTopBased = clazzSuper == null || clazzSuper.superClazz == null;
var clazzFun = objThis[funName];
if (clazzFun != null) {
if (clazzFun.claxxOwner != null) {
// claxxOwner is a mark for methods that is single.
if (clazzFun.claxxOwner !== clazzThis) {
// This is a single method, call directly!
fx = clazzFun;
clazzSuper = clazzFun.claxxOwner;
superTopBased = clazzSuper == null || clazzSuper.superClazz == null;
}
/*
// As suer constructor needs to check preparing fields, do NOT use cached
// methods by previous invocation.
} else if (clazzFun.stacks == null && !(clazzFun.lastClaxxRef != null
&& clazzFun.lastClaxxRef.prototype[funName] != null
&& clazzFun.lastClaxxRef.prototype[funName].stacks != null)) { // super.toString
fx = clazzFun;
clazzSuper = clazzFun.lastClaxxRef;
// */
} else { // normal wrapped method
var stacks = clazzFun.stacks;
/*
// As suer constructor needs to check preparing fields, do NOT use cached
// methods by previous invocation.
if (stacks == null) {
stacks = clazzFun.lastClaxxRef.prototype[funName].stacks;
}
// */
var length = stacks.length;
for (var i = length - 1; i >= 0; i--) {
// Once super call is computed precisely, there are no need
// to calculate the inherited level but just an equals comparison
// var level = Clazz.getInheritedLevel (clazzThis, stacks[i]);
if (clazzThis === stacks[i]) { // level == 0
if (i > 0) {
fx = (clazzSuper = stacks[--i]).prototype[funName];
superTopBased = i == 0;
} else {
/* Will this case be reachable? - March 4, 2006
Should never reach here if all are compiled by Java2Script */
fx = (clazzSuper = stacks[0]).prototype[funName]["\\unknown"];
superTopBased = true;
}
break;
} else if (Clazz.getInheritedLevel (clazzThis, stacks[i]) > 0) {
fx = (clazzSuper = stacks[i]).prototype[funName];
superTopBased = i == 0;
break;
}
} // end of for loop
} // end of normal wrapped method
} // end of clazzFun != null
if (fx != null) {
if (superTopBased && clazzSuper != null && clazzSuper.con$truct != null) {
clazzSuper.con$truct.apply (objThis, [0, clazzSuper.con$truct.index]);
}
/*# {$no.debug.support} >>x #*/
if (Clazz.tracingCalling) {
var caller = arguments.callee.caller;
if (caller === Clazz.superConstructor) {
caller = caller.arguments.callee.caller;
}
Clazz.pu$hCalling (new Clazz.callingStack (caller, clazzThis));
fx.apply (objThis, (funParams == null) ? [] : funParams);
Clazz.p0pCalling ();
} else {
/*# x<< #*/
fx.apply (objThis, (funParams == null) ? [] : funParams);
/*# {$no.debug.support} >>x #*/
}
/*# x<< #*/
}
/* If there are members which are initialized out of the constructor */
if (clazzThis.con$truct != null) {
// try to call back all field preparing function starting from
// clazzThis' super class which has constructor
// con$truct.index records the level of last class which has constructor
if (clazzSuper != null && clazzSuper.con$truct != null) {
clazzThis.con$truct.apply (objThis, [clazzSuper.con$truct.index, clazzThis.con$truct.index]);
} else {
clazzThis.con$truct.apply (objThis, [0, clazzThis.con$truct.index]);
}
}
};
/**
* Call this constructor of the class.
* The same effect as Java's expression:
* <code> this(*) </code>
*
* @param objThis host object
* @param clazzThis class of declaring method scope.
* @param funParams Array of method parameters
*/
/* public */
Clazz.thisConstructor = function (objThis, clazzThis, funParams) {
var funName = "construct";
var fx = null;
var clazzFun = objThis[funName];
if (clazzFun != null) {
if (clazzFun.claxxOwner != null) {
// claxxOwner is a mark for methods that is single.
if (clazzFun.claxxOwner !== clazzThis) {
// This is a single method, call directly!
fx = clazzFun;
}
} else if (clazzFun.stacks == null && !(clazzFun.lastClaxxRef != null
&& clazzFun.lastClaxxRef.prototype[funName] != null
&& clazzFun.lastClaxxRef.prototype[funName].stacks != null)) { // super.toString
fx = clazzFun;
} else { // normal wrapped method
var stacks = clazzFun.stacks;
if (stacks == null) {
stacks = clazzFun.lastClaxxRef.prototype[funName].stacks;
}
var length = stacks.length;
for (var i = length - 1; i >= 0; i--) {
// Once super call is computed precisely, there are no need
// to calculate the inherited level but just an equals comparison
// var level = Clazz.getInheritedLevel (clazzThis, stacks[i]);
if (clazzThis === stacks[i]) { // level == 0
fx = stacks[i].prototype[funName];
break;
}
} // end of for loop
} // end of normal wrapped method
} // end of clazzFun != null
if (fx != null) {
/*# {$no.debug.support} >>x #*/
if (Clazz.tracingCalling) {
var caller = arguments.callee.caller;
if (caller === Clazz.thisConstructor) {
caller = caller.arguments.callee.caller;
}
Clazz.pu$hCalling (new Clazz.callingStack (caller, clazzThis));
fx.apply (objThis, (funParams == null) ? [] : funParams);
Clazz.p0pCalling ();
} else {
/*# x<< #*/
fx.apply (objThis, (funParams == null) ? [] : funParams);
/*# {$no.debug.support} >>x #*/
}
/*# x<< #*/
}
};
/**
* Class for null with a given class as to be casted.
* This class will be used as an implementation of Java's casting way.
* For example,
* <code> this.call ((String) null); </code>
*/
/* protcted */
Clazz.CastedNull = function (asClazz) {
if (asClazz != null) {
if (asClazz instanceof String) {
this.clazzName = asClazz;
} else if (asClazz instanceof Function) {
this.clazzName = Clazz.getClassName (asClazz, true);
} else {
this.clazzName = "" + asClazz;
}
} else {
this.clazzName = "Object";
}
this.toString = function () {
return null;
};
this.valueOf = function () {
return null;
};
};
/**
* API for Java's casting null.
* @see Clazz.CastedNull
*
* @param asClazz given class
* @return an instance of class Clazz.CastedNull
*/
/* public */
Clazz.castNullAs = function (asClazz) {
return new Clazz.CastedNull (asClazz);
};
/* protcted */
Clazz.CastedObject = function (obj, asClazz) {
if (asClazz != null) {
if (asClazz instanceof String || typeof asClazz == "string") {
if (asClazz.indexOf ("$wt.") == 0) {
this.clazzName = "org.eclipse.swt." + asClazz.substring(4);
} else {
this.clazzName = asClazz;
}
} else if (asClazz instanceof Function) {
this.clazzName = Clazz.getClassName (asClazz, true);
} else {
this.clazzName = "" + asClazz;
}
} else {
this.clazzName = "Object";
}
this.target = obj;
this.toString = function () {
return this.target;
};
this.getObject = this.valueOf = function () {
return this.target;
};
};
/* public */
Clazz.castObjectAs = function (obj, asClazz) {
return new Clazz.CastedObject (obj, asClazz);
};
/**
* MethodException will be used as a signal to notify that the method is
* not found in the current clazz hierarchy.
*/
/* private */
Clazz.MethodException = function () {
/*
this.message = "The static Clazz instance can not found the method!";
this.toString = function () {
return this.message;
};
*/
};
/* protected */
Clazz.MethodNotFoundException = function () {
this.toString = function () {
return "MethodNotFoundException";
};
};
/* private */
/*x-# getParamsType -> gPT #-x*/
Clazz.getParamsType = function (funParams) {
var params = new Array ();
params.hasCastedVariable = false;
if (funParams != null) {
for (var i = 0; i < funParams.length; i++) {
params[i] = Clazz.getClassName (funParams[i]);
if (funParams[i] instanceof Clazz.CastedNull || funParams[i] instanceof Clazz.CastedObject) {
params.hasCastedVariable = true;
}
}
}
if (params.length == 0) {
params[0] = "void";
}
params.typeString = "\\" + params.join ('\\');
//params.hasCastedVariable = hasCastedVariable;
return params;
};
/**
* Search the given class prototype, find the method with the same
* method name and the same parameter signatures by the given
* parameters, and then run the method with the given parameters.
*
* @param objThis the current host object
* @param claxxRef the current host object's class
* @param fxName the method name
* @param funParams the given arguments
* @return the result of the specified method of the host object,
* the return maybe void.
* @throws Clazz.MethodNotFoundException if no matched method is found
*/
/* protected */
/*-# searchAndExecuteMethod -> saem #-*/
Clazz.searchAndExecuteMethod = function (objThis, claxxRef, fxName, funParams) {
var params = Clazz.getParamsType (funParams);
var fx = objThis[fxName];
/*
* Cache last or previous matched method
*/
var cached = 0; // 0: no cache; 1: last matched; 2: previous matched
if (fx.lastParams == params.typeString && fx.lastClaxxRef === claxxRef) {
cached = 1;
} else if (fx.prevParams == params.typeString && fx.prevClaxxRef === claxxRef) {
cached = 2;
}
if (cached != 0) {
var methodParams = null;
if (params.hasCastedVariable) {
methodParams = new Array ();
for (var k = 0; k < funParams.length; k++) {
if (funParams[k] instanceof Clazz.CastedNull) {
/*
* For Clazz.CastedNull instances, the type name is
* already used to indentified the method in Clazz#
* searchMethod.
*/
methodParams[k] = null;
} else if (funParams[k] instanceof Clazz.CastedObject) {
/*
* For Clazz.CastedObject instances, the type name is
* already used to indentified the method in Clazz#
* searchMethod.
*/
methodParams[k] = funParams[k].getObject();
} else {
methodParams[k] = funParams[k];
}
}
} else {
methodParams = funParams;
}
if (cached == 1 && fx.lastMethod != null) {
return fx.lastMethod.apply (objThis, methodParams);
} else if (cached == 2 && fx.prevMethod != null) {
var f = fx.prevMethod;
fx.prevParams = fx.lastParams;
fx.prevClaxxRef = fx.lastClaxxRef;
fx.prevMethod = fx.lastMethod;
fx.lastParams = params.typeString;
fx.lastClaxxRef = claxxRef;
fx.lastMethod = f;
return f.apply (objThis, methodParams);
} else { // missed default constructor ?
return ;
}
}
fx.prevParams = fx.lastParams;
fx.prevClaxxRef = fx.lastClaxxRef;
fx.lastParams = params.typeString;
fx.lastClaxxRef = claxxRef;
var stacks = fx.stacks;
if (stacks == null) {
stacks = claxxRef.prototype[fxName].stacks;
}
var length = stacks.length;
/*
* Search the inheritance stacks to get the given class' function
*/
var began = false; // began to search its super classes
for (var i = length - 1; i > -1; i--) {
//if (Clazz.getInheritedLevel (claxxRef, stacks[i]) >= 0) {
/*
* No need to calculate the inherited level as there always exist a
* right claxxRef in the stacks, and the inherited level of stacks
* are in order.
*/
if (began || stacks[i] === claxxRef) {
/*
* First try to search method within the same class scope
* with stacks[i] === claxxRef
*/
var clazzFun = stacks[i].prototype[fxName];
var ret = Clazz.tryToSearchAndExecute (objThis, clazzFun, params,
funParams/*, isSuper, clazzThis*/, fx);
if (!(ret instanceof Clazz.MethodException)) {