javahelp.js revision 1319:bd783ddc4333
1184610Salfred/*
2184610Salfred * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3184610Salfred *
4184610Salfred * Redistribution and use in source and binary forms, with or without
5184610Salfred * modification, are permitted provided that the following conditions
6184610Salfred * are met:
7184610Salfred *
8184610Salfred *   - Redistributions of source code must retain the above copyright
9184610Salfred *     notice, this list of conditions and the following disclaimer.
10184610Salfred *
11184610Salfred *   - Redistributions in binary form must reproduce the above copyright
12184610Salfred *     notice, this list of conditions and the following disclaimer in the
13184610Salfred *     documentation and/or other materials provided with the distribution.
14184610Salfred *
15184610Salfred *   - Neither the name of Oracle nor the names of its
16184610Salfred *     contributors may be used to endorse or promote products derived
17184610Salfred *     from this software without specific prior written permission.
18184610Salfred *
19184610Salfred * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20184610Salfred * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21184610Salfred * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22184610Salfred * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23184610Salfred * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24184610Salfred * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25184610Salfred * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26184610Salfred * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27194230Sthompsa * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28194230Sthompsa * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29184610Salfred * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30184610Salfred */
31184610Salfred
32192984Sthompsa// script helpers to print meta info on Java instances and classes
33194228Sthompsa
34192984Sthompsa// print instance methods info on a Java object or static methods info of a Java class
35184610Salfredfunction methods(jobj) {
36184610Salfred    if (! Java.isJavaObject(jobj)) {
37184610Salfred        throw new TypeError("not a Java object");
38194228Sthompsa    }
39185948Sthompsa
40194228Sthompsa    var isStatic = Java.isType(jobj);
41185948Sthompsa    var obj = Object.bindProperties({}, jobj);
42194228Sthompsa    for each (var i in obj) {
43185948Sthompsa        if (Java.isJavaMethod(i)) {
44194228Sthompsa            var str = String(i);
45184610Salfred            var idx = str.indexOf(' ');
46184610Salfred            var overloaded = str.substring(0, idx).endsWith("OverloadedDynamicMethod");
47184610Salfred            var lastIdx = isStatic? str.lastIndexOf('] on') : str.lastIndexOf(']');
48194228Sthompsa            print(str.substring(idx + 1, lastIdx) + (overloaded? "*" : ""))
49194228Sthompsa        }
50194228Sthompsa    }
51194228Sthompsa}
52194228Sthompsa
53194228Sthompsa// print instance field names of a Java object or static field names of a Java class
54184610Salfredfunction fields(jobj) {
55184610Salfred    if (! Java.isJavaObject(jobj)) {
56184610Salfred        throw new TypeError("not a Java object");
57194228Sthompsa    }
58194228Sthompsa
59194228Sthompsa    var obj = Object.bindProperties({}, jobj);
60184610Salfred    for (var i in obj) {
61194230Sthompsa        if (! Java.isJavaMethod(obj[i])) {
62            print(i);
63        }
64    }
65}
66