author | ohair |
Wed, 30 Apr 2008 17:34:41 -0700 | |
changeset 468 | 642c8c0be52e |
parent 2 | 90ce3da70b43 |
child 715 | f16baef3a20e |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
/* |
|
27 |
* The Original Code is HAT. The Initial Developer of the |
|
28 |
* Original Code is Bill Foote, with contributions from others |
|
468
642c8c0be52e
6695553: Cleanup GPLv2+SPL legal notices in hat sources
ohair
parents:
2
diff
changeset
|
29 |
* at JavaSoft/Sun. |
2 | 30 |
*/ |
31 |
||
32 |
var hatPkg = Packages.com.sun.tools.hat.internal; |
|
33 |
||
34 |
/** |
|
35 |
* This is JavaScript interface for heap analysis using HAT |
|
36 |
* (Heap Analysis Tool). HAT classes are refered from |
|
37 |
* this file. In particular, refer to classes in hat.model |
|
38 |
* package. |
|
39 |
* |
|
40 |
* HAT model objects are wrapped as convenient script objects so that |
|
41 |
* fields may be accessed in natural syntax. For eg. Java fields can be |
|
42 |
* accessed with obj.field_name syntax and array elements can be accessed |
|
43 |
* with array[index] syntax. |
|
44 |
*/ |
|
45 |
||
46 |
// returns an enumeration that wraps elements of |
|
47 |
// the input enumeration elements. |
|
48 |
function wrapperEnumeration(e) { |
|
49 |
return new java.util.Enumeration() { |
|
50 |
hasMoreElements: function() { |
|
51 |
return e.hasMoreElements(); |
|
52 |
}, |
|
53 |
nextElement: function() { |
|
54 |
return wrapJavaValue(e.nextElement()); |
|
55 |
} |
|
56 |
}; |
|
57 |
} |
|
58 |
||
59 |
// returns an enumeration that filters out elements |
|
60 |
// of input enumeration using the filter function. |
|
61 |
function filterEnumeration(e, func, wrap) { |
|
62 |
var next = undefined; |
|
63 |
var index = 0; |
|
64 |
||
65 |
function findNext() { |
|
66 |
var tmp; |
|
67 |
while (e.hasMoreElements()) { |
|
68 |
tmp = e.nextElement(); |
|
69 |
index++; |
|
70 |
if (wrap) { |
|
71 |
tmp = wrapJavaValue(tmp); |
|
72 |
} |
|
73 |
if (func(tmp, index, e)) { |
|
74 |
next = tmp; |
|
75 |
return; |
|
76 |
} |
|
77 |
} |
|
78 |
} |
|
79 |
||
80 |
return new java.util.Enumeration() { |
|
81 |
hasMoreElements: function() { |
|
82 |
findNext(); |
|
83 |
return next != undefined; |
|
84 |
}, |
|
85 |
||
86 |
nextElement: function() { |
|
87 |
if (next == undefined) { |
|
88 |
// user may not have called hasMoreElements? |
|
89 |
findNext(); |
|
90 |
} |
|
91 |
if (next == undefined) { |
|
92 |
throw "NoSuchElementException"; |
|
93 |
} |
|
94 |
var res = next; |
|
95 |
next = undefined; |
|
96 |
return res; |
|
97 |
} |
|
98 |
}; |
|
99 |
} |
|
100 |
||
101 |
// enumeration that has no elements .. |
|
102 |
var emptyEnumeration = new java.util.Enumeration() { |
|
103 |
hasMoreElements: function() { |
|
104 |
return false; |
|
105 |
}, |
|
106 |
nextElement: function() { |
|
107 |
throw "NoSuchElementException"; |
|
108 |
} |
|
109 |
}; |
|
110 |
||
111 |
function wrapRoot(root) { |
|
112 |
if (root) { |
|
113 |
return { |
|
114 |
id: root.idString, |
|
115 |
description: root.description, |
|
116 |
referrer: wrapJavaValue(root.referer), |
|
117 |
type: root.typeName |
|
118 |
}; |
|
119 |
} else { |
|
120 |
return null; |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
function JavaClassProto() { |
|
125 |
function jclass(obj) { |
|
126 |
return obj['wrapped-object']; |
|
127 |
} |
|
128 |
||
129 |
// return whether given class is subclass of this class or not |
|
130 |
this.isSubclassOf = function(other) { |
|
131 |
var tmp = jclass(this); |
|
132 |
var otherid = objectid(other); |
|
133 |
while (tmp != null) { |
|
134 |
if (otherid.equals(tmp.idString)) { |
|
135 |
return true; |
|
136 |
} |
|
137 |
tmp = tmp.superclass; |
|
138 |
} |
|
139 |
return false; |
|
140 |
} |
|
141 |
||
142 |
// return whether given class is superclass of this class or not |
|
143 |
this.isSuperclassOf = function(other) { |
|
144 |
return other.isSubclassOf(this); |
|
145 |
} |
|
146 |
||
147 |
// includes direct and indirect superclasses |
|
148 |
this.superclasses = function() { |
|
149 |
var res = new Array(); |
|
150 |
var tmp = this.superclass; |
|
151 |
while (tmp != null) { |
|
152 |
res[res.length] = tmp; |
|
153 |
tmp = tmp.superclass; |
|
154 |
} |
|
155 |
return res; |
|
156 |
} |
|
157 |
||
158 |
/** |
|
159 |
* Returns an array containing subclasses of this class. |
|
160 |
* |
|
161 |
* @param indirect should include indirect subclasses or not. |
|
162 |
* default is true. |
|
163 |
*/ |
|
164 |
this.subclasses = function(indirect) { |
|
165 |
if (indirect == undefined) indirect = true; |
|
166 |
var classes = jclass(this).subclasses; |
|
167 |
var res = new Array(); |
|
168 |
for (var i in classes) { |
|
169 |
var subclass = wrapJavaValue(classes[i]); |
|
170 |
res[res.length] = subclass; |
|
171 |
if (indirect) { |
|
172 |
res = res.concat(subclass.subclasses()); |
|
173 |
} |
|
174 |
} |
|
175 |
return res; |
|
176 |
} |
|
177 |
this.toString = function() { return jclass(this).toString(); } |
|
178 |
} |
|
179 |
||
180 |
var theJavaClassProto = new JavaClassProto(); |
|
181 |
||
182 |
// Script wrapper for HAT model objects, values. |
|
183 |
// wraps a Java value as appropriate for script object |
|
184 |
function wrapJavaValue(thing) { |
|
185 |
if (thing == null || thing == undefined || |
|
186 |
thing instanceof hatPkg.model.HackJavaValue) { |
|
187 |
return null; |
|
188 |
} |
|
189 |
||
190 |
if (thing instanceof hatPkg.model.JavaValue) { |
|
191 |
// map primitive values to closest JavaScript primitives |
|
192 |
if (thing instanceof hatPkg.model.JavaBoolean) { |
|
193 |
return thing.toString() == "true"; |
|
194 |
} else if (thing instanceof hatPkg.model.JavaChar) { |
|
195 |
return thing.toString() + ''; |
|
196 |
} else { |
|
197 |
return java.lang.Double.parseDouble(thing.toString()); |
|
198 |
} |
|
199 |
} else { |
|
200 |
// wrap Java object as script object |
|
201 |
return wrapJavaObject(thing); |
|
202 |
} |
|
203 |
} |
|
204 |
||
205 |
// wrap Java object with appropriate script object |
|
206 |
function wrapJavaObject(thing) { |
|
207 |
||
208 |
// HAT Java model object wrapper. Handles all cases |
|
209 |
// (instance, object/primitive array and Class objects) |
|
210 |
function javaObject(jobject) { |
|
211 |
// FIXME: Do I need this? or can I assume that these would |
|
212 |
// have been resolved already? |
|
213 |
if (jobject instanceof hatPkg.model.JavaObjectRef) { |
|
214 |
jobject = jobject.dereference(); |
|
215 |
if (jobject instanceof hatPkg.model.HackJavaValue) { |
|
216 |
print(jobject); |
|
217 |
return null; |
|
218 |
} |
|
219 |
} |
|
220 |
||
221 |
if (jobject instanceof hatPkg.model.JavaObject) { |
|
222 |
return new JavaObjectWrapper(jobject); |
|
223 |
} else if (jobject instanceof hatPkg.model.JavaClass) { |
|
224 |
return new JavaClassWrapper(jobject); |
|
225 |
} else if (jobject instanceof hatPkg.model.JavaObjectArray) { |
|
226 |
return new JavaObjectArrayWrapper(jobject); |
|
227 |
} else if (jobject instanceof hatPkg.model.JavaValueArray) { |
|
228 |
return new JavaValueArrayWrapper(jobject); |
|
229 |
} else { |
|
230 |
print("unknown heap object type: " + jobject.getClass()); |
|
231 |
return jobject; |
|
232 |
} |
|
233 |
} |
|
234 |
||
235 |
// returns wrapper for Java instances |
|
236 |
function JavaObjectWrapper(instance) { |
|
237 |
var things = instance.fields; |
|
238 |
var fields = instance.clazz.fieldsForInstance; |
|
239 |
||
240 |
// instance fields can be accessed in natural syntax |
|
241 |
return new JSAdapter() { |
|
242 |
__getIds__ : function() { |
|
243 |
var res = new Array(fields.length); |
|
244 |
for (var i in fields) { |
|
245 |
res[i] = fields[i].name; |
|
246 |
} |
|
247 |
return res; |
|
248 |
}, |
|
249 |
__has__ : function(name) { |
|
250 |
for (var i in fields) { |
|
251 |
if (name == fields[i].name) return true; |
|
252 |
} |
|
253 |
return name == 'class' || name == 'toString' || |
|
254 |
name == 'wrapped-object'; |
|
255 |
}, |
|
256 |
__get__ : function(name) { |
|
257 |
||
258 |
for (var i in fields) { |
|
259 |
if(fields[i].name == name) { |
|
260 |
return wrapJavaValue(things[i]); |
|
261 |
} |
|
262 |
} |
|
263 |
||
264 |
if (name == 'class') { |
|
265 |
return wrapJavaValue(instance.clazz); |
|
266 |
} else if (name == 'toString') { |
|
267 |
return function() { |
|
268 |
return instance.toString(); |
|
269 |
} |
|
270 |
} else if (name == 'wrapped-object') { |
|
271 |
return instance; |
|
272 |
} |
|
273 |
||
274 |
return undefined; |
|
275 |
} |
|
276 |
} |
|
277 |
} |
|
278 |
||
279 |
||
280 |
// return wrapper for Java Class objects |
|
281 |
function JavaClassWrapper(jclass) { |
|
282 |
var fields = jclass.statics; |
|
283 |
||
284 |
// to access static fields of given Class cl, use |
|
285 |
// cl.statics.<static-field-name> syntax |
|
286 |
this.statics = new JSAdapter() { |
|
287 |
__getIds__ : function() { |
|
288 |
var res = new Array(fields.length); |
|
289 |
for (var i in fields) { |
|
290 |
res[i] = fields[i].field.name; |
|
291 |
} |
|
292 |
return res; |
|
293 |
}, |
|
294 |
__has__ : function(name) { |
|
295 |
for (var i in fields) { |
|
296 |
if (name == fields[i].field.name) { |
|
297 |
return true; |
|
298 |
} |
|
299 |
} |
|
300 |
return theJavaClassProto[name] != undefined; |
|
301 |
}, |
|
302 |
__get__ : function(name) { |
|
303 |
for (var i in fields) { |
|
304 |
if (name == fields[i].field.name) { |
|
305 |
return wrapJavaValue(fields[i].value); |
|
306 |
} |
|
307 |
} |
|
308 |
return theJavaClassProto[name]; |
|
309 |
} |
|
310 |
} |
|
311 |
||
312 |
if (jclass.superclass != null) { |
|
313 |
this.superclass = wrapJavaValue(jclass.superclass); |
|
314 |
} else { |
|
315 |
this.superclass = null; |
|
316 |
} |
|
317 |
||
318 |
this.loader = wrapJavaValue(jclass.getLoader()); |
|
319 |
this.signers = wrapJavaValue(jclass.getSigners()); |
|
320 |
this.protectionDomain = wrapJavaValue(jclass.getProtectionDomain()); |
|
321 |
this.instanceSize = jclass.instanceSize; |
|
322 |
this.name = jclass.name; |
|
323 |
this.fields = jclass.fields; |
|
324 |
this['wrapped-object'] = jclass; |
|
325 |
this.__proto__ = this.statics; |
|
326 |
} |
|
327 |
||
328 |
// returns wrapper for Java object arrays |
|
329 |
function JavaObjectArrayWrapper(array) { |
|
330 |
var elements = array.elements; |
|
331 |
// array elements can be accessed in natural syntax |
|
332 |
// also, 'length' property is supported. |
|
333 |
return new JSAdapter() { |
|
334 |
__getIds__ : function() { |
|
335 |
var res = new Array(elements.length); |
|
336 |
for (var i = 0; i < elements.length; i++) { |
|
337 |
res[i] = i; |
|
338 |
} |
|
339 |
return res; |
|
340 |
}, |
|
341 |
__has__: function(name) { |
|
342 |
return (typeof(name) == 'number' && |
|
343 |
name >= 0 && name < elements.length) || |
|
344 |
name == 'length' || name == 'class' || |
|
345 |
name == 'toString' || name == 'wrapped-object'; |
|
346 |
}, |
|
347 |
__get__ : function(name) { |
|
348 |
if (typeof(name) == 'number' && |
|
349 |
name >= 0 && name < elements.length) { |
|
350 |
return wrapJavaValue(elements[name]); |
|
351 |
} else if (name == 'length') { |
|
352 |
return elements.length; |
|
353 |
} else if (name == 'class') { |
|
354 |
return wrapJavaValue(array.clazz); |
|
355 |
} else if (name == 'toString') { |
|
356 |
return function() { return array.toString(); } |
|
357 |
} else if (name == 'wrapped-object') { |
|
358 |
return array; |
|
359 |
} else { |
|
360 |
return undefined; |
|
361 |
} |
|
362 |
} |
|
363 |
} |
|
364 |
} |
|
365 |
||
366 |
// returns wrapper for Java primitive arrays |
|
367 |
function JavaValueArrayWrapper(array) { |
|
368 |
var type = String(java.lang.Character.toString(array.elementType)); |
|
369 |
var elements = array.elements; |
|
370 |
// array elements can be accessed in natural syntax |
|
371 |
// also, 'length' property is supported. |
|
372 |
return new JSAdapter() { |
|
373 |
__getIds__ : function() { |
|
374 |
var r = new Array(array.length); |
|
375 |
for (var i = 0; i < array.length; i++) { |
|
376 |
r[i] = i; |
|
377 |
} |
|
378 |
return r; |
|
379 |
}, |
|
380 |
__has__: function(name) { |
|
381 |
return (typeof(name) == 'number' && |
|
382 |
name >= 0 && name < array.length) || |
|
383 |
name == 'length' || name == 'class' || |
|
384 |
name == 'toString' || name == 'wrapped-object'; |
|
385 |
}, |
|
386 |
__get__: function(name) { |
|
387 |
if (typeof(name) == 'number' && |
|
388 |
name >= 0 && name < array.length) { |
|
389 |
return elements[name]; |
|
390 |
} |
|
391 |
||
392 |
if (name == 'length') { |
|
393 |
return array.length; |
|
394 |
} else if (name == 'toString') { |
|
395 |
return function() { return array.valueString(true); } |
|
396 |
} else if (name == 'wrapped-object') { |
|
397 |
return array; |
|
398 |
} else if (name == 'class') { |
|
399 |
return wrapJavaValue(array.clazz); |
|
400 |
} else { |
|
401 |
return undefined; |
|
402 |
} |
|
403 |
} |
|
404 |
} |
|
405 |
} |
|
406 |
return javaObject(thing); |
|
407 |
} |
|
408 |
||
409 |
// unwrap a script object to corresponding HAT object |
|
410 |
function unwrapJavaObject(jobject) { |
|
411 |
if (!(jobject instanceof hatPkg.model.JavaHeapObject)) { |
|
412 |
try { |
|
413 |
jobject = jobject["wrapped-object"]; |
|
414 |
} catch (e) { |
|
415 |
print("unwrapJavaObject: " + jobject + ", " + e); |
|
416 |
jobject = undefined; |
|
417 |
} |
|
418 |
} |
|
419 |
return jobject; |
|
420 |
} |
|
421 |
||
422 |
/** |
|
423 |
* readHeapDump parses a heap dump file and returns script wrapper object. |
|
424 |
* |
|
425 |
* @param file Heap dump file name |
|
426 |
* @param stack flag to tell if allocation site traces are available |
|
427 |
* @param refs flag to tell if backward references are needed or not |
|
428 |
* @param debug debug level for HAT |
|
429 |
* @return heap as a JavaScript object |
|
430 |
*/ |
|
431 |
function readHeapDump(file, stack, refs, debug) { |
|
432 |
||
433 |
// default value of debug is 0 |
|
434 |
if (!debug) debug = 0; |
|
435 |
||
436 |
// by default, we assume no stack traces |
|
437 |
if (!stack) stack = false; |
|
438 |
||
439 |
// by default, backward references are resolved |
|
440 |
if (!refs) refs = true; |
|
441 |
||
442 |
// read the heap dump |
|
443 |
var heap = hatPkg.parser.HprofReader.readFile(file, stack, debug); |
|
444 |
||
445 |
// resolve it |
|
446 |
heap.resolve(refs); |
|
447 |
||
448 |
// wrap Snapshot as convenient script object |
|
449 |
return wrapHeapSnapshot(heap); |
|
450 |
} |
|
451 |
||
452 |
/** |
|
453 |
* The result object supports the following methods: |
|
454 |
* |
|
455 |
* forEachClass -- calls a callback for each Java Class |
|
456 |
* forEachObject -- calls a callback for each Java object |
|
457 |
* findClass -- finds Java Class of given name |
|
458 |
* findObject -- finds object from given object id |
|
459 |
* objects -- returns all objects of given class as an enumeration |
|
460 |
* classes -- returns all classes in the heap as an enumeration |
|
461 |
* reachables -- returns all objects reachable from a given object |
|
462 |
* livepaths -- returns an array of live paths because of which an |
|
463 |
* object alive. |
|
464 |
* describeRef -- returns description for a reference from a 'from' |
|
465 |
* object to a 'to' object. |
|
466 |
*/ |
|
467 |
function wrapHeapSnapshot(heap) { |
|
468 |
function getClazz(clazz) { |
|
469 |
if (clazz == undefined) clazz = "java.lang.Object"; |
|
470 |
var type = typeof(clazz); |
|
471 |
if (type == "string") { |
|
472 |
clazz = heap.findClass(clazz); |
|
473 |
} else if (type == "object") { |
|
474 |
clazz = unwrapJavaObject(clazz); |
|
475 |
} else { |
|
476 |
throw "class expected";; |
|
477 |
} |
|
478 |
return clazz; |
|
479 |
} |
|
480 |
||
481 |
// return heap as a script object with useful methods. |
|
482 |
return { |
|
483 |
snapshot: heap, |
|
484 |
||
485 |
/** |
|
486 |
* Class iteration: Calls callback function for each |
|
487 |
* Java Class in the heap. Default callback function |
|
488 |
* is 'print'. If callback returns true, the iteration |
|
489 |
* is stopped. |
|
490 |
* |
|
491 |
* @param callback function to be called. |
|
492 |
*/ |
|
493 |
forEachClass: function(callback) { |
|
494 |
if (callback == undefined) callback = print; |
|
495 |
var classes = this.snapshot.classes; |
|
496 |
while (classes.hasMoreElements()) { |
|
497 |
if (callback(wrapJavaValue(classes.nextElement()))) |
|
498 |
return; |
|
499 |
} |
|
500 |
}, |
|
501 |
||
502 |
/** |
|
503 |
* Returns an Enumeration of all roots. |
|
504 |
*/ |
|
505 |
roots: function() { |
|
506 |
var e = this.snapshot.roots; |
|
507 |
return new java.util.Enumeration() { |
|
508 |
hasMoreElements: function() { |
|
509 |
return e.hasMoreElements(); |
|
510 |
}, |
|
511 |
nextElement: function() { |
|
512 |
return wrapRoot(e.nextElement()); |
|
513 |
} |
|
514 |
}; |
|
515 |
}, |
|
516 |
||
517 |
/** |
|
518 |
* Returns an Enumeration for all Java classes. |
|
519 |
*/ |
|
520 |
classes: function() { |
|
521 |
return wrapIterator(this.snapshot.classes, true); |
|
522 |
}, |
|
523 |
||
524 |
/** |
|
525 |
* Object iteration: Calls callback function for each |
|
526 |
* Java Object in the heap. Default callback function |
|
527 |
* is 'print'.If callback returns true, the iteration |
|
528 |
* is stopped. |
|
529 |
* |
|
530 |
* @param callback function to be called. |
|
531 |
* @param clazz Class whose objects are retrieved. |
|
532 |
* Optional, default is 'java.lang.Object' |
|
533 |
* @param includeSubtypes flag to tell if objects of subtypes |
|
534 |
* are included or not. optional, default is true. |
|
535 |
*/ |
|
536 |
forEachObject: function(callback, clazz, includeSubtypes) { |
|
537 |
if (includeSubtypes == undefined) includeSubtypes = true; |
|
538 |
if (callback == undefined) callback = print; |
|
539 |
clazz = getClazz(clazz); |
|
540 |
||
541 |
if (clazz) { |
|
542 |
var instances = clazz.getInstances(includeSubtypes); |
|
543 |
while (instances.hasNextElements()) { |
|
544 |
if (callback(wrapJavaValue(instances.nextElement()))) |
|
545 |
return; |
|
546 |
} |
|
547 |
} |
|
548 |
}, |
|
549 |
||
550 |
/** |
|
551 |
* Returns an enumeration of Java objects in the heap. |
|
552 |
* |
|
553 |
* @param clazz Class whose objects are retrieved. |
|
554 |
* Optional, default is 'java.lang.Object' |
|
555 |
* @param includeSubtypes flag to tell if objects of subtypes |
|
556 |
* are included or not. optional, default is true. |
|
557 |
* @param where (optional) filter expression or function to |
|
558 |
* filter the objects. The expression has to return true |
|
559 |
* to include object passed to it in the result array. |
|
560 |
* Built-in variable 'it' refers to the current object in |
|
561 |
* filter expression. |
|
562 |
*/ |
|
563 |
objects: function(clazz, includeSubtypes, where) { |
|
564 |
if (includeSubtypes == undefined) includeSubtypes = true; |
|
565 |
if (where) { |
|
566 |
if (typeof(where) == 'string') { |
|
567 |
where = new Function("it", "return " + where); |
|
568 |
} |
|
569 |
} |
|
570 |
clazz = getClazz(clazz); |
|
571 |
if (clazz) { |
|
572 |
var instances = clazz.getInstances(includeSubtypes); |
|
573 |
if (where) { |
|
574 |
return filterEnumeration(instances, where, true); |
|
575 |
} else { |
|
576 |
return wrapperEnumeration(instances); |
|
577 |
} |
|
578 |
} else { |
|
579 |
return emptyEnumeration; |
|
580 |
} |
|
581 |
}, |
|
582 |
||
583 |
/** |
|
584 |
* Find Java Class of given name. |
|
585 |
* |
|
586 |
* @param name class name |
|
587 |
*/ |
|
588 |
findClass: function(name) { |
|
589 |
var clazz = this.snapshot.findClass(name + ''); |
|
590 |
return wrapJavaValue(clazz); |
|
591 |
}, |
|
592 |
||
593 |
/** |
|
594 |
* Find Java Object from given object id |
|
595 |
* |
|
596 |
* @param id object id as string |
|
597 |
*/ |
|
598 |
findObject: function(id) { |
|
599 |
return wrapJavaValue(this.snapshot.findThing(id)); |
|
600 |
}, |
|
601 |
||
602 |
/** |
|
603 |
* Returns an enumeration of objects in the finalizer |
|
604 |
* queue waiting to be finalized. |
|
605 |
*/ |
|
606 |
finalizables: function() { |
|
607 |
var tmp = this.snapshot.getFinalizerObjects(); |
|
608 |
return wrapperEnumeration(tmp); |
|
609 |
}, |
|
610 |
||
611 |
/** |
|
612 |
* Returns an array that contains objects referred from the |
|
613 |
* given Java object directly or indirectly (i.e., all |
|
614 |
* transitively referred objects are returned). |
|
615 |
* |
|
616 |
* @param jobject Java object whose reachables are returned. |
|
617 |
*/ |
|
618 |
reachables: function (jobject) { |
|
619 |
return reachables(jobject, this.snapshot.reachableExcludes); |
|
620 |
}, |
|
621 |
||
622 |
/** |
|
623 |
* Returns array of paths of references by which the given |
|
624 |
* Java object is live. Each path itself is an array of |
|
625 |
* objects in the chain of references. Each path supports |
|
626 |
* toHtml method that returns html description of the path. |
|
627 |
* |
|
628 |
* @param jobject Java object whose live paths are returned |
|
629 |
* @param weak flag to indicate whether to include paths with |
|
630 |
* weak references or not. default is false. |
|
631 |
*/ |
|
632 |
livepaths: function (jobject, weak) { |
|
633 |
if (weak == undefined) { |
|
634 |
weak = false; |
|
635 |
} |
|
636 |
||
637 |
function wrapRefChain(refChain) { |
|
638 |
var path = new Array(); |
|
639 |
||
640 |
// compute path array from refChain |
|
641 |
var tmp = refChain; |
|
642 |
while (tmp != null) { |
|
643 |
var obj = tmp.obj; |
|
644 |
path[path.length] = wrapJavaValue(obj); |
|
645 |
tmp = tmp.next; |
|
646 |
} |
|
647 |
||
648 |
function computeDescription(html) { |
|
649 |
var root = refChain.obj.root; |
|
650 |
var desc = root.description; |
|
651 |
if (root.referer) { |
|
652 |
var ref = root.referer; |
|
653 |
desc += " (from " + |
|
654 |
(html? toHtml(ref) : ref.toString()) + ')'; |
|
655 |
} |
|
656 |
desc += '->'; |
|
657 |
var tmp = refChain; |
|
658 |
while (tmp != null) { |
|
659 |
var next = tmp.next; |
|
660 |
var obj = tmp.obj; |
|
661 |
desc += html? toHtml(obj) : obj.toString(); |
|
662 |
if (next != null) { |
|
663 |
desc += " (" + |
|
664 |
obj.describeReferenceTo(next.obj, heap) + |
|
665 |
") ->"; |
|
666 |
} |
|
667 |
tmp = next; |
|
668 |
} |
|
669 |
return desc; |
|
670 |
} |
|
671 |
||
672 |
return new JSAdapter() { |
|
673 |
__getIds__ : function() { |
|
674 |
var res = new Array(path.length); |
|
675 |
for (var i = 0; i < path.length; i++) { |
|
676 |
res[i] = i; |
|
677 |
} |
|
678 |
return res; |
|
679 |
}, |
|
680 |
__has__ : function (name) { |
|
681 |
return (typeof(name) == 'number' && |
|
682 |
name >= 0 && name < path.length) || |
|
683 |
name == 'length' || name == 'toHtml' || |
|
684 |
name == 'toString'; |
|
685 |
}, |
|
686 |
__get__ : function(name) { |
|
687 |
if (typeof(name) == 'number' && |
|
688 |
name >= 0 && name < path.length) { |
|
689 |
return path[name]; |
|
690 |
} else if (name == 'length') { |
|
691 |
return path.length; |
|
692 |
} else if (name == 'toHtml') { |
|
693 |
return function() { |
|
694 |
return computeDescription(true); |
|
695 |
} |
|
696 |
} else if (name == 'toString') { |
|
697 |
return function() { |
|
698 |
return computeDescription(false); |
|
699 |
} |
|
700 |
} else { |
|
701 |
return undefined; |
|
702 |
} |
|
703 |
}, |
|
704 |
}; |
|
705 |
} |
|
706 |
||
707 |
jobject = unwrapJavaObject(jobject); |
|
708 |
var refChains = this.snapshot.rootsetReferencesTo(jobject, weak); |
|
709 |
var paths = new Array(refChains.length); |
|
710 |
for (var i in refChains) { |
|
711 |
paths[i] = wrapRefChain(refChains[i]); |
|
712 |
} |
|
713 |
return paths; |
|
714 |
}, |
|
715 |
||
716 |
/** |
|
717 |
* Return description string for reference from 'from' object |
|
718 |
* to 'to' Java object. |
|
719 |
* |
|
720 |
* @param from source Java object |
|
721 |
* @param to destination Java object |
|
722 |
*/ |
|
723 |
describeRef: function (from, to) { |
|
724 |
from = unwrapJavaObject(from); |
|
725 |
to = unwrapJavaObject(to); |
|
726 |
return from.describeReferenceTo(to, this.snapshot); |
|
727 |
}, |
|
728 |
}; |
|
729 |
} |
|
730 |
||
731 |
// per-object functions |
|
732 |
||
733 |
/** |
|
734 |
* Returns allocation site trace (if available) of a Java object |
|
735 |
* |
|
736 |
* @param jobject object whose allocation site trace is returned |
|
737 |
*/ |
|
738 |
function allocTrace(jobject) { |
|
739 |
try { |
|
740 |
jobject = unwrapJavaObject(jobject); |
|
741 |
var trace = jobject.allocatedFrom; |
|
742 |
return (trace != null) ? trace.frames : null; |
|
743 |
} catch (e) { |
|
744 |
print("allocTrace: " + jobject + ", " + e); |
|
745 |
return null; |
|
746 |
} |
|
747 |
} |
|
748 |
||
749 |
/** |
|
750 |
* Returns Class object for given Java object |
|
751 |
* |
|
752 |
* @param jobject object whose Class object is returned |
|
753 |
*/ |
|
754 |
function classof(jobject) { |
|
755 |
jobject = unwrapJavaObject(jobject); |
|
756 |
return wrapJavaValue(jobject.clazz); |
|
757 |
} |
|
758 |
||
759 |
/** |
|
760 |
* Find referers (a.k.a in-coming references). Calls callback |
|
761 |
* for each referrer of the given Java object. If the callback |
|
762 |
* returns true, the iteration is stopped. |
|
763 |
* |
|
764 |
* @param callback function to call for each referer |
|
765 |
* @param jobject object whose referers are retrieved |
|
766 |
*/ |
|
767 |
function forEachReferrer(callback, jobject) { |
|
768 |
jobject = unwrapJavaObject(jobject); |
|
769 |
var refs = jobject.referers; |
|
770 |
while (refs.hasMoreElements()) { |
|
771 |
if (callback(wrapJavaValue(refs.nextElement()))) { |
|
772 |
return; |
|
773 |
} |
|
774 |
} |
|
775 |
} |
|
776 |
||
777 |
/** |
|
778 |
* Compares two Java objects for object identity. |
|
779 |
* |
|
780 |
* @param o1, o2 objects to compare for identity |
|
781 |
*/ |
|
782 |
function identical(o1, o2) { |
|
783 |
return objectid(o1) == objectid(o2); |
|
784 |
} |
|
785 |
||
786 |
/** |
|
787 |
* Returns Java object id as string |
|
788 |
* |
|
789 |
* @param jobject object whose id is returned |
|
790 |
*/ |
|
791 |
function objectid(jobject) { |
|
792 |
try { |
|
793 |
jobject = unwrapJavaObject(jobject); |
|
794 |
return String(jobject.idString); |
|
795 |
} catch (e) { |
|
796 |
print("objectid: " + jobject + ", " + e); |
|
797 |
return null; |
|
798 |
} |
|
799 |
} |
|
800 |
||
801 |
/** |
|
802 |
* Prints allocation site trace of given object |
|
803 |
* |
|
804 |
* @param jobject object whose allocation site trace is returned |
|
805 |
*/ |
|
806 |
function printAllocTrace(jobject) { |
|
807 |
var frames = this.allocTrace(jobject); |
|
808 |
if (frames == null || frames.length == 0) { |
|
809 |
print("allocation site trace unavailable for " + |
|
810 |
objectid(jobject)); |
|
811 |
return; |
|
812 |
} |
|
813 |
print(objectid(jobject) + " was allocated at .."); |
|
814 |
for (var i in frames) { |
|
815 |
var frame = frames[i]; |
|
816 |
var src = frame.sourceFileName; |
|
817 |
if (src == null) src = '<unknown source>'; |
|
818 |
print('\t' + frame.className + "." + |
|
819 |
frame.methodName + '(' + frame.methodSignature + ') [' + |
|
820 |
src + ':' + frame.lineNumber + ']'); |
|
821 |
} |
|
822 |
} |
|
823 |
||
824 |
/** |
|
825 |
* Returns an enumeration of referrers of the given Java object. |
|
826 |
* |
|
827 |
* @param jobject Java object whose referrers are returned. |
|
828 |
*/ |
|
829 |
function referrers(jobject) { |
|
830 |
try { |
|
831 |
jobject = unwrapJavaObject(jobject); |
|
832 |
return wrapperEnumeration(jobject.referers); |
|
833 |
} catch (e) { |
|
834 |
print("referrers: " + jobject + ", " + e); |
|
835 |
return emptyEnumeration; |
|
836 |
} |
|
837 |
} |
|
838 |
||
839 |
/** |
|
840 |
* Returns an array that contains objects referred from the |
|
841 |
* given Java object. |
|
842 |
* |
|
843 |
* @param jobject Java object whose referees are returned. |
|
844 |
*/ |
|
845 |
function referees(jobject) { |
|
846 |
var res = new Array(); |
|
847 |
jobject = unwrapJavaObject(jobject); |
|
848 |
if (jobject != undefined) { |
|
849 |
try { |
|
850 |
jobject.visitReferencedObjects( |
|
851 |
new hatPkg.model.JavaHeapObjectVisitor() { |
|
852 |
visit: function(other) { |
|
853 |
res[res.length] = wrapJavaValue(other); |
|
854 |
}, |
|
855 |
exclude: function(clazz, field) { |
|
856 |
return false; |
|
857 |
}, |
|
858 |
mightExclude: function() { |
|
859 |
return false; |
|
860 |
} |
|
861 |
}); |
|
862 |
} catch (e) { |
|
863 |
print("referees: " + jobject + ", " + e); |
|
864 |
} |
|
865 |
} |
|
866 |
return res; |
|
867 |
} |
|
868 |
||
869 |
/** |
|
870 |
* Returns an array that contains objects referred from the |
|
871 |
* given Java object directly or indirectly (i.e., all |
|
872 |
* transitively referred objects are returned). |
|
873 |
* |
|
874 |
* @param jobject Java object whose reachables are returned. |
|
875 |
* @param excludes optional comma separated list of fields to be |
|
876 |
* removed in reachables computation. Fields are |
|
877 |
* written as class_name.field_name form. |
|
878 |
*/ |
|
879 |
function reachables(jobject, excludes) { |
|
880 |
if (excludes == undefined) { |
|
881 |
excludes = null; |
|
882 |
} else if (typeof(excludes) == 'string') { |
|
883 |
var st = new java.util.StringTokenizer(excludes, ","); |
|
884 |
var excludedFields = new Array(); |
|
885 |
while (st.hasMoreTokens()) { |
|
886 |
excludedFields[excludedFields.length] = st.nextToken().trim(); |
|
887 |
} |
|
888 |
if (excludedFields.length > 0) { |
|
889 |
excludes = new hatPkg.model.ReachableExcludes() { |
|
890 |
isExcluded: function (field) { |
|
891 |
for (var index in excludedFields) { |
|
892 |
if (field.equals(excludedFields[index])) { |
|
893 |
return true; |
|
894 |
} |
|
895 |
} |
|
896 |
return false; |
|
897 |
} |
|
898 |
}; |
|
899 |
} else { |
|
900 |
// nothing to filter... |
|
901 |
excludes = null; |
|
902 |
} |
|
903 |
} else if (! (excludes instanceof hatPkg.model.ReachableExcludes)) { |
|
904 |
excludes = null; |
|
905 |
} |
|
906 |
||
907 |
jobject = unwrapJavaObject(jobject); |
|
908 |
var ro = new hatPkg.model.ReachableObjects(jobject, excludes); |
|
909 |
var tmp = ro.reachables; |
|
910 |
var res = new Array(tmp.length); |
|
911 |
for (var i in tmp) { |
|
912 |
res[i] = wrapJavaValue(tmp[i]); |
|
913 |
} |
|
914 |
return res; |
|
915 |
} |
|
916 |
||
917 |
||
918 |
/** |
|
919 |
* Returns whether 'from' object refers to 'to' object or not. |
|
920 |
* |
|
921 |
* @param from Java object that is source of the reference. |
|
922 |
* @param to Java object that is destination of the reference. |
|
923 |
*/ |
|
924 |
function refers(from, to) { |
|
925 |
try { |
|
926 |
var tmp = unwrapJavaObject(from); |
|
927 |
if (tmp instanceof hatPkg.model.JavaClass) { |
|
928 |
from = from.statics; |
|
929 |
} else if (tmp instanceof hatPkg.model.JavaValueArray) { |
|
930 |
return false; |
|
931 |
} |
|
932 |
for (var i in from) { |
|
933 |
if (identical(from[i], to)) { |
|
934 |
return true; |
|
935 |
} |
|
936 |
} |
|
937 |
} catch (e) { |
|
938 |
print("refers: " + from + ", " + e); |
|
939 |
} |
|
940 |
return false; |
|
941 |
} |
|
942 |
||
943 |
/** |
|
944 |
* If rootset includes given jobject, return Root |
|
945 |
* object explanining the reason why it is a root. |
|
946 |
* |
|
947 |
* @param jobject object whose Root is returned |
|
948 |
*/ |
|
949 |
function root(jobject) { |
|
950 |
try { |
|
951 |
jobject = unwrapJavaObject(jobject); |
|
952 |
return wrapRoot(jobject.root); |
|
953 |
} catch (e) { |
|
954 |
return null; |
|
955 |
} |
|
956 |
} |
|
957 |
||
958 |
/** |
|
959 |
* Returns size of the given Java object |
|
960 |
* |
|
961 |
* @param jobject object whose size is returned |
|
962 |
*/ |
|
963 |
function sizeof(jobject) { |
|
964 |
try { |
|
965 |
jobject = unwrapJavaObject(jobject); |
|
966 |
return jobject.size; |
|
967 |
} catch (e) { |
|
968 |
print("sizeof: " + jobject + ", " + e); |
|
969 |
return null; |
|
970 |
} |
|
971 |
} |
|
972 |
||
973 |
/** |
|
974 |
* Returns String by replacing Unicode chars and |
|
975 |
* HTML special chars (such as '<') with entities. |
|
976 |
* |
|
977 |
* @param str string to be encoded |
|
978 |
*/ |
|
979 |
function encodeHtml(str) { |
|
980 |
return hatPkg.util.Misc.encodeHtml(str); |
|
981 |
} |
|
982 |
||
983 |
/** |
|
984 |
* Returns HTML string for the given object. |
|
985 |
* |
|
986 |
* @param obj object for which HTML string is returned. |
|
987 |
*/ |
|
988 |
function toHtml(obj) { |
|
989 |
if (obj == null) { |
|
990 |
return "null"; |
|
991 |
} |
|
992 |
||
993 |
if (obj == undefined) { |
|
994 |
return "undefined"; |
|
995 |
} |
|
996 |
||
997 |
var tmp = unwrapJavaObject(obj); |
|
998 |
if (tmp != undefined) { |
|
999 |
var id = tmp.idString; |
|
1000 |
if (tmp instanceof Packages.com.sun.tools.hat.internal.model.JavaClass) { |
|
1001 |
var name = tmp.name; |
|
1002 |
return "<a href='/class/" + id + "'>class " + name + "</a>"; |
|
1003 |
} else { |
|
1004 |
var name = tmp.clazz.name; |
|
1005 |
return "<a href='/object/" + id + "'>" + |
|
1006 |
name + "@" + id + "</a>"; |
|
1007 |
} |
|
1008 |
} else if ((typeof(obj) == 'object') || (obj instanceof JSAdapter)) { |
|
1009 |
if (obj instanceof java.lang.Object) { |
|
1010 |
// script wrapped Java object |
|
1011 |
obj = wrapIterator(obj); |
|
1012 |
// special case for enumeration |
|
1013 |
if (obj instanceof java.util.Enumeration) { |
|
1014 |
var res = "[ "; |
|
1015 |
while (obj.hasMoreElements()) { |
|
1016 |
res += toHtml(obj.nextElement()) + ", "; |
|
1017 |
} |
|
1018 |
res += "]"; |
|
1019 |
return res; |
|
1020 |
} else { |
|
1021 |
return obj; |
|
1022 |
} |
|
1023 |
} else if (obj instanceof Array) { |
|
1024 |
// script array |
|
1025 |
var res = "[ "; |
|
1026 |
for (var i in obj) { |
|
1027 |
res += toHtml(obj[i]); |
|
1028 |
if (i != obj.length - 1) { |
|
1029 |
res += ", "; |
|
1030 |
} |
|
1031 |
} |
|
1032 |
res += " ]"; |
|
1033 |
return res; |
|
1034 |
} else { |
|
1035 |
// if the object has a toHtml function property |
|
1036 |
// just use that... |
|
1037 |
if (typeof(obj.toHtml) == 'function') { |
|
1038 |
return obj.toHtml(); |
|
1039 |
} else { |
|
1040 |
// script object |
|
1041 |
var res = "{ "; |
|
1042 |
for (var i in obj) { |
|
1043 |
res += i + ":" + toHtml(obj[i]) + ", "; |
|
1044 |
} |
|
1045 |
res += "}"; |
|
1046 |
return res; |
|
1047 |
} |
|
1048 |
} |
|
1049 |
} else { |
|
1050 |
// JavaScript primitive value |
|
1051 |
return obj; |
|
1052 |
} |
|
1053 |
} |
|
1054 |
||
1055 |
/* |
|
1056 |
* Generic array/iterator/enumeration [or even object!] manipulation |
|
1057 |
* functions. These functions accept an array/iteration/enumeration |
|
1058 |
* and expression String or function. These functions iterate each |
|
1059 |
* element of array and apply the expression/function on each element. |
|
1060 |
*/ |
|
1061 |
||
1062 |
// private function to wrap an Iterator as an Enumeration |
|
1063 |
function wrapIterator(itr, wrap) { |
|
1064 |
if (itr instanceof java.util.Iterator) { |
|
1065 |
return new java.util.Enumeration() { |
|
1066 |
hasMoreElements: function() { |
|
1067 |
return itr.hasNext(); |
|
1068 |
}, |
|
1069 |
nextElement: function() { |
|
1070 |
return wrap? wrapJavaValue(itr.next()) : itr.next(); |
|
1071 |
} |
|
1072 |
}; |
|
1073 |
} else { |
|
1074 |
return itr; |
|
1075 |
} |
|
1076 |
} |
|
1077 |
||
1078 |
/** |
|
1079 |
* Converts an enumeration/iterator/object into an array |
|
1080 |
* |
|
1081 |
* @param obj enumeration/iterator/object |
|
1082 |
* @return array that contains values of enumeration/iterator/object |
|
1083 |
*/ |
|
1084 |
function toArray(obj) { |
|
1085 |
obj = wrapIterator(obj); |
|
1086 |
if (obj instanceof java.util.Enumeration) { |
|
1087 |
var res = new Array(); |
|
1088 |
while (obj.hasMoreElements()) { |
|
1089 |
res[res.length] = obj.nextElement(); |
|
1090 |
} |
|
1091 |
return res; |
|
1092 |
} else if (obj instanceof Array) { |
|
1093 |
return obj; |
|
1094 |
} else { |
|
1095 |
var res = new Array(); |
|
1096 |
for (var index in obj) { |
|
1097 |
res[res.length] = obj[index]; |
|
1098 |
} |
|
1099 |
return res; |
|
1100 |
} |
|
1101 |
} |
|
1102 |
||
1103 |
/** |
|
1104 |
* Returns whether the given array/iterator/enumeration contains |
|
1105 |
* an element that satisfies the given boolean expression specified |
|
1106 |
* in code. |
|
1107 |
* |
|
1108 |
* @param array input array/iterator/enumeration that is iterated |
|
1109 |
* @param code expression string or function |
|
1110 |
* @return boolean result |
|
1111 |
* |
|
1112 |
* The code evaluated can refer to the following built-in variables. |
|
1113 |
* |
|
1114 |
* 'it' -> currently visited element |
|
1115 |
* 'index' -> index of the current element |
|
1116 |
* 'array' -> array that is being iterated |
|
1117 |
*/ |
|
1118 |
function contains(array, code) { |
|
1119 |
array = wrapIterator(array); |
|
1120 |
var func = code; |
|
1121 |
if (typeof(func) != 'function') { |
|
1122 |
func = new Function("it", "index", "array", "return " + code); |
|
1123 |
} |
|
1124 |
||
1125 |
if (array instanceof java.util.Enumeration) { |
|
1126 |
var index = 0; |
|
1127 |
while (array.hasMoreElements()) { |
|
1128 |
var it = array.nextElement(); |
|
1129 |
if (func(it, index, array)) { |
|
1130 |
return true; |
|
1131 |
} |
|
1132 |
index++; |
|
1133 |
} |
|
1134 |
} else { |
|
1135 |
for (var index in array) { |
|
1136 |
var it = array[index]; |
|
1137 |
if (func(it, index, array)) { |
|
1138 |
return true; |
|
1139 |
} |
|
1140 |
} |
|
1141 |
} |
|
1142 |
return false; |
|
1143 |
} |
|
1144 |
||
1145 |
/** |
|
1146 |
* concatenates two arrays/iterators/enumerators. |
|
1147 |
* |
|
1148 |
* @param array1 array/iterator/enumeration |
|
1149 |
* @param array2 array/iterator/enumeration |
|
1150 |
* |
|
1151 |
* @return concatenated array or composite enumeration |
|
1152 |
*/ |
|
1153 |
function concat(array1, array2) { |
|
1154 |
array1 = wrapIterator(array1); |
|
1155 |
array2 = wrapIterator(array2); |
|
1156 |
if (array1 instanceof Array && array2 instanceof Array) { |
|
1157 |
return array1.concat(array2); |
|
1158 |
} else if (array1 instanceof java.util.Enumeration && |
|
1159 |
array2 instanceof java.util.Enumeration) { |
|
1160 |
return new Packages.com.sun.tools.hat.internal.util.CompositeEnumeration(array1, array2); |
|
1161 |
} else { |
|
1162 |
return undefined; |
|
1163 |
} |
|
1164 |
} |
|
1165 |
||
1166 |
/** |
|
1167 |
* Returns the number of array/iterator/enumeration elements |
|
1168 |
* that satisfy the given boolean expression specified in code. |
|
1169 |
* The code evaluated can refer to the following built-in variables. |
|
1170 |
* |
|
1171 |
* @param array input array/iterator/enumeration that is iterated |
|
1172 |
* @param code expression string or function |
|
1173 |
* @return number of elements |
|
1174 |
* |
|
1175 |
* 'it' -> currently visited element |
|
1176 |
* 'index' -> index of the current element |
|
1177 |
* 'array' -> array that is being iterated |
|
1178 |
*/ |
|
1179 |
function count(array, code) { |
|
1180 |
if (code == undefined) { |
|
1181 |
return length(array); |
|
1182 |
} |
|
1183 |
array = wrapIterator(array); |
|
1184 |
var func = code; |
|
1185 |
if (typeof(func) != 'function') { |
|
1186 |
func = new Function("it", "index", "array", "return " + code); |
|
1187 |
} |
|
1188 |
||
1189 |
var result = 0; |
|
1190 |
if (array instanceof java.util.Enumeration) { |
|
1191 |
var index = 0; |
|
1192 |
while (array.hasMoreElements()) { |
|
1193 |
var it = array.nextElement(); |
|
1194 |
if (func(it, index, array)) { |
|
1195 |
result++; |
|
1196 |
} |
|
1197 |
index++; |
|
1198 |
} |
|
1199 |
} else { |
|
1200 |
for (var index in array) { |
|
1201 |
var it = array[index]; |
|
1202 |
if (func(it, index, array)) { |
|
1203 |
result++; |
|
1204 |
} |
|
1205 |
} |
|
1206 |
} |
|
1207 |
return result; |
|
1208 |
} |
|
1209 |
||
1210 |
/** |
|
1211 |
* filter function returns an array/enumeration that contains |
|
1212 |
* elements of the input array/iterator/enumeration that satisfy |
|
1213 |
* the given boolean expression. The boolean expression code can |
|
1214 |
* refer to the following built-in variables. |
|
1215 |
* |
|
1216 |
* @param array input array/iterator/enumeration that is iterated |
|
1217 |
* @param code expression string or function |
|
1218 |
* @return array/enumeration that contains the filtered elements |
|
1219 |
* |
|
1220 |
* 'it' -> currently visited element |
|
1221 |
* 'index' -> index of the current element |
|
1222 |
* 'array' -> array that is being iterated |
|
1223 |
* 'result' -> result array |
|
1224 |
*/ |
|
1225 |
function filter(array, code) { |
|
1226 |
array = wrapIterator(array); |
|
1227 |
var func = code; |
|
1228 |
if (typeof(code) != 'function') { |
|
1229 |
func = new Function("it", "index", "array", "result", "return " + code); |
|
1230 |
} |
|
1231 |
if (array instanceof java.util.Enumeration) { |
|
1232 |
return filterEnumeration(array, func, false); |
|
1233 |
} else { |
|
1234 |
var result = new Array(); |
|
1235 |
for (var index in array) { |
|
1236 |
var it = array[index]; |
|
1237 |
if (func(it, index, array, result)) { |
|
1238 |
result[result.length] = it; |
|
1239 |
} |
|
1240 |
} |
|
1241 |
return result; |
|
1242 |
} |
|
1243 |
} |
|
1244 |
||
1245 |
/** |
|
1246 |
* Returns the number of elements of array/iterator/enumeration. |
|
1247 |
* |
|
1248 |
* @param array input array/iterator/enumeration that is iterated |
|
1249 |
*/ |
|
1250 |
function length(array) { |
|
1251 |
array = wrapIterator(array); |
|
1252 |
if (array instanceof Array) { |
|
1253 |
return array.length; |
|
1254 |
} else if (array instanceof java.util.Enumeration) { |
|
1255 |
var cnt = 0; |
|
1256 |
while (array.hasMoreElements()) { |
|
1257 |
array.nextElement(); |
|
1258 |
cnt++; |
|
1259 |
} |
|
1260 |
return cnt; |
|
1261 |
} else { |
|
1262 |
var cnt = 0; |
|
1263 |
for (var index in array) { |
|
1264 |
cnt++; |
|
1265 |
} |
|
1266 |
return cnt; |
|
1267 |
} |
|
1268 |
} |
|
1269 |
||
1270 |
/** |
|
1271 |
* Transforms the given object or array by evaluating given code |
|
1272 |
* on each element of the object or array. The code evaluated |
|
1273 |
* can refer to the following built-in variables. |
|
1274 |
* |
|
1275 |
* @param array input array/iterator/enumeration that is iterated |
|
1276 |
* @param code expression string or function |
|
1277 |
* @return array/enumeration that contains mapped values |
|
1278 |
* |
|
1279 |
* 'it' -> currently visited element |
|
1280 |
* 'index' -> index of the current element |
|
1281 |
* 'array' -> array that is being iterated |
|
1282 |
* 'result' -> result array |
|
1283 |
* |
|
1284 |
* map function returns an array/enumeration of values created |
|
1285 |
* by repeatedly calling code on each element of the input |
|
1286 |
* array/iterator/enumeration. |
|
1287 |
*/ |
|
1288 |
function map(array, code) { |
|
1289 |
array = wrapIterator(array); |
|
1290 |
var func = code; |
|
1291 |
if(typeof(code) != 'function') { |
|
1292 |
func = new Function("it", "index", "array", "result", "return " + code); |
|
1293 |
} |
|
1294 |
||
1295 |
if (array instanceof java.util.Enumeration) { |
|
1296 |
var index = 0; |
|
1297 |
var result = new java.util.Enumeration() { |
|
1298 |
hasMoreElements: function() { |
|
1299 |
return array.hasMoreElements(); |
|
1300 |
}, |
|
1301 |
nextElement: function() { |
|
1302 |
return func(array.nextElement(), index++, array, result); |
|
1303 |
} |
|
1304 |
}; |
|
1305 |
return result; |
|
1306 |
} else { |
|
1307 |
var result = new Array(); |
|
1308 |
for (var index in array) { |
|
1309 |
var it = array[index]; |
|
1310 |
result[result.length] = func(it, index, array, result); |
|
1311 |
} |
|
1312 |
return result; |
|
1313 |
} |
|
1314 |
} |
|
1315 |
||
1316 |
// private function used by min, max functions |
|
1317 |
function minmax(array, code) { |
|
1318 |
if (typeof(code) == 'string') { |
|
1319 |
code = new Function("lhs", "rhs", "return " + code); |
|
1320 |
} |
|
1321 |
array = wrapIterator(array); |
|
1322 |
if (array instanceof java.util.Enumeration) { |
|
1323 |
if (! array.hasMoreElements()) { |
|
1324 |
return undefined; |
|
1325 |
} |
|
1326 |
var res = array.nextElement(); |
|
1327 |
while (array.hasMoreElements()) { |
|
1328 |
var next = array.nextElement(); |
|
1329 |
if (code(next, res)) { |
|
1330 |
res = next; |
|
1331 |
} |
|
1332 |
} |
|
1333 |
return res; |
|
1334 |
} else { |
|
1335 |
if (array.length == 0) { |
|
1336 |
return undefined; |
|
1337 |
} |
|
1338 |
var res = array[0]; |
|
1339 |
for (var index = 1; index < array.length; index++) { |
|
1340 |
if (code(array[index], res)) { |
|
1341 |
res = array[index]; |
|
1342 |
} |
|
1343 |
} |
|
1344 |
return res; |
|
1345 |
} |
|
1346 |
} |
|
1347 |
||
1348 |
/** |
|
1349 |
* Returns the maximum element of the array/iterator/enumeration |
|
1350 |
* |
|
1351 |
* @param array input array/iterator/enumeration that is iterated |
|
1352 |
* @param code (optional) comparision expression or function |
|
1353 |
* by default numerical maximum is computed. |
|
1354 |
*/ |
|
1355 |
function max(array, code) { |
|
1356 |
if (code == undefined) { |
|
1357 |
code = function (lhs, rhs) { return lhs > rhs; } |
|
1358 |
} |
|
1359 |
return minmax(array, code); |
|
1360 |
} |
|
1361 |
||
1362 |
/** |
|
1363 |
* Returns the minimum element of the array/iterator/enumeration |
|
1364 |
* |
|
1365 |
* @param array input array/iterator/enumeration that is iterated |
|
1366 |
* @param code (optional) comparision expression or function |
|
1367 |
* by default numerical minimum is computed. |
|
1368 |
*/ |
|
1369 |
function min(array, code) { |
|
1370 |
if (code == undefined) { |
|
1371 |
code = function (lhs, rhs) { return lhs < rhs; } |
|
1372 |
} |
|
1373 |
return minmax(array, code); |
|
1374 |
} |
|
1375 |
||
1376 |
/** |
|
1377 |
* sort function sorts the input array. optionally accepts |
|
1378 |
* code to compare the elements. If code is not supplied, |
|
1379 |
* numerical sort is done. |
|
1380 |
* |
|
1381 |
* @param array input array/iterator/enumeration that is sorted |
|
1382 |
* @param code expression string or function |
|
1383 |
* @return sorted array |
|
1384 |
* |
|
1385 |
* The comparison expression can refer to the following |
|
1386 |
* built-in variables: |
|
1387 |
* |
|
1388 |
* 'lhs' -> 'left side' element |
|
1389 |
* 'rhs' -> 'right side' element |
|
1390 |
*/ |
|
1391 |
function sort(array, code) { |
|
1392 |
// we need an array to sort, so convert non-arrays |
|
1393 |
array = toArray(array); |
|
1394 |
||
1395 |
// by default use numerical comparison |
|
1396 |
var func = code; |
|
1397 |
if (code == undefined) { |
|
1398 |
func = function(lhs, rhs) { return lhs - rhs; }; |
|
1399 |
} else if (typeof(code) == 'string') { |
|
1400 |
func = new Function("lhs", "rhs", "return " + code); |
|
1401 |
} |
|
1402 |
return array.sort(func); |
|
1403 |
} |
|
1404 |
||
1405 |
/** |
|
1406 |
* Returns the sum of the elements of the array |
|
1407 |
* |
|
1408 |
* @param array input array that is summed. |
|
1409 |
* @param code optional expression used to map |
|
1410 |
* input elements before sum. |
|
1411 |
*/ |
|
1412 |
function sum(array, code) { |
|
1413 |
array = wrapIterator(array); |
|
1414 |
if (code != undefined) { |
|
1415 |
array = map(array, code); |
|
1416 |
} |
|
1417 |
var result = 0; |
|
1418 |
if (array instanceof java.util.Enumeration) { |
|
1419 |
while (array.hasMoreElements()) { |
|
1420 |
result += Number(array.nextElement()); |
|
1421 |
} |
|
1422 |
} else { |
|
1423 |
for (var index in array) { |
|
1424 |
result += Number(array[index]); |
|
1425 |
} |
|
1426 |
} |
|
1427 |
return result; |
|
1428 |
} |
|
1429 |
||
1430 |
/** |
|
1431 |
* Returns array of unique elements from the given input |
|
1432 |
* array/iterator/enumeration. |
|
1433 |
* |
|
1434 |
* @param array from which unique elements are returned. |
|
1435 |
* @param code optional expression (or function) giving unique |
|
1436 |
* attribute/property for each element. |
|
1437 |
* by default, objectid is used for uniqueness. |
|
1438 |
*/ |
|
1439 |
function unique(array, code) { |
|
1440 |
array = wrapIterator(array); |
|
1441 |
if (code == undefined) { |
|
1442 |
code = new Function("it", "return objectid(it);"); |
|
1443 |
} else if (typeof(code) == 'string') { |
|
1444 |
code = new Function("it", "return " + code); |
|
1445 |
} |
|
1446 |
var tmp = new Object(); |
|
1447 |
if (array instanceof java.util.Enumeration) { |
|
1448 |
while (array.hasMoreElements()) { |
|
1449 |
var it = array.nextElement(); |
|
1450 |
tmp[code(it)] = it; |
|
1451 |
} |
|
1452 |
} else { |
|
1453 |
for (var index in array) { |
|
1454 |
var it = array[index]; |
|
1455 |
tmp[code(it)] = it; |
|
1456 |
} |
|
1457 |
} |
|
1458 |
var res = new Array(); |
|
1459 |
for (var index in tmp) { |
|
1460 |
res[res.length] = tmp[index]; |
|
1461 |
} |
|
1462 |
return res; |
|
1463 |
} |