nashorn/samples/javaastviewer.js
author igerasim
Fri, 15 May 2015 22:09:49 +0300
changeset 30641 701f6f90dc0b
parent 26067 b32ccc3a76c9
permissions -rw-r--r--
8074657: Missing space on a boundary of concatenated strings Summary: Added missing spaces, fixed indentation, replaced StringBuffer with StringBuilder Reviewed-by: martin, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     1
#// Usage: jjs -fx javaastviewer.js -- <.java files>
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     2
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     3
/*
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     4
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 24283
diff changeset
     5
 *
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     6
 * Redistribution and use in source and binary forms, with or without
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     7
 * modification, are permitted provided that the following conditions
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
     8
 * are met:
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 24283
diff changeset
     9
 *
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    10
 *   - Redistributions of source code must retain the above copyright
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    11
 *     notice, this list of conditions and the following disclaimer.
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 24283
diff changeset
    12
 *
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    13
 *   - Redistributions in binary form must reproduce the above copyright
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    14
 *     notice, this list of conditions and the following disclaimer in the
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    15
 *     documentation and/or other materials provided with the distribution.
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 24283
diff changeset
    16
 *
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    17
 *   - Neither the name of Oracle nor the names of its
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    18
 *     contributors may be used to endorse or promote products derived
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    19
 *     from this software without specific prior written permission.
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 24283
diff changeset
    20
 *
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    21
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    22
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    23
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    24
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    25
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    26
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    27
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    28
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    29
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    30
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    31
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    32
 */
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    33
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    34
// This example demonstrates Java subclassing by Java.extend
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    35
// and javac Compiler and Tree API. This example also uses
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    36
// -fx and javafx TreeView to visualize Java AST as TreeView
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    37
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    38
if (!$OPTIONS._fx || arguments.length == 0) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    39
    print("Usage: jjs -fx javaastviewer.js -- <.java files>");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    40
    exit(1);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    41
}
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    42
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    43
// Java types used
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    44
var Enum = Java.type("java.lang.Enum");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    45
var HashSet  = Java.type("java.util.HashSet");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    46
var Name = Java.type("javax.lang.model.element.Name");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    47
var List = Java.type("java.util.List");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    48
var Set  = Java.type("java.util.Set");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    49
var SimpleTreeVisitor = Java.type("com.sun.source.util.SimpleTreeVisitor");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    50
var StringArray = Java.type("java.lang.String[]");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    51
var ToolProvider = Java.type("javax.tools.ToolProvider");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    52
var Tree = Java.type("com.sun.source.tree.Tree");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    53
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    54
function javaASTToScriptObject(args) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    55
    // properties ignored (javac implementation class properties) in AST view.
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    56
    // may not be exhaustive - any getAbc would become "abc" property or
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    57
    // public field becomes a property of same name.
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    58
    var ignoredProps = new HashSet();
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 24283
diff changeset
    59
    for each (var word in
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 24283
diff changeset
    60
        ['extending', 'implementing', 'init', 'mods', 'clazz', 'defs',
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    61
         'expr', 'tag', 'preferredPosition', 'qualid', 'recvparam',
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    62
         'restype', 'params', 'startPosition', 'thrown',
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    63
         'tree', 'typarams', 'typetag', 'vartype']) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    64
        ignoredProps.add(word);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    65
    }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    66
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    67
    // get the system compiler tool
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    68
    var compiler = ToolProvider.systemJavaCompiler;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    69
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    70
    // get standard file manager
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    71
    var fileMgr = compiler.getStandardFileManager(null, null, null);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    72
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    73
    // make a list of compilation unit from command line argument file names
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    74
    // Using Java.to convert script array (arguments) to a Java String[]
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    75
    var compUnits = fileMgr.getJavaFileObjects(Java.to(args, StringArray));
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    76
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    77
    // create a new compilation task
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    78
    var task = compiler.getTask(null, fileMgr, null, null, null, compUnits);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    79
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    80
    // subclass SimpleTreeVisitor - converts Java AST node to
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    81
    // a simple script object by walking through it
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    82
    var ConverterVisitor = Java.extend(SimpleTreeVisitor);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    83
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    84
    var visitor = new ConverterVisitor() {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    85
        // convert java AST node to a friendly script object
26067
b32ccc3a76c9 8055199: Tidy up Nashorn codebase for code standards (August 2014)
attila
parents: 24283
diff changeset
    86
        // which can be viewed. Every node ends up in defaultAction
24283
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    87
        // method of SimpleTreeVisitor method.
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    88
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    89
        defaultAction: function (node, p) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    90
            var resultObj = {};
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    91
            // Nashorn does not iterate properties and methods of Java objects
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    92
            // But, we can bind properties of any object (including java objects)
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    93
            // to a script object and iterate it!
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    94
            var obj = {};
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    95
            Object.bindProperties(obj, node);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    96
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    97
            // we don't want every property, method of java object
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    98
            for (var prop in obj) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
    99
                var val = obj[prop];
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   100
                var type = typeof val;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   101
                // ignore 'method' members
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   102
                if (type == 'function' || type == 'undefined') {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   103
                    continue;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   104
                }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   105
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   106
                // ignore properties from Javac implementation
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   107
                // classes - hack by name!!
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   108
                if (ignoredProps.contains(prop)) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   109
                    continue;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   110
                }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   111
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   112
                // subtree - recurse it
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   113
                if (val instanceof Tree) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   114
                    resultObj[prop] = visitor.visit(val, p);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   115
                } else if (val instanceof List) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   116
                    // List of trees - recurse each and make an array
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   117
                    var len = val.size();
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   118
                    if (len != 0) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   119
                        var arr = [];
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   120
                        for (var j = 0; j < len; j++) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   121
                            var e = val[j];
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   122
                            if (e instanceof Tree) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   123
                                arr.push(visitor.visit(e, p));
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   124
                            }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   125
                        }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   126
                        resultObj[prop] = arr;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   127
                    }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   128
                } else if (val instanceof Set) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   129
                    // Set - used for modifier flags
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   130
                    // make array
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   131
                    var len = val.size();
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   132
                    if (len != 0) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   133
                        var arr = [];
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   134
                        for each (var e in val) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   135
                            if (e instanceof Enum || typeof e == 'string') {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   136
                                arr.push(e.toString());
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   137
                            }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   138
                        }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   139
                        resultObj[prop] = arr;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   140
                    }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   141
                } else if (val instanceof Enum || val instanceof Name) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   142
                    // make string for any Enum or Name
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   143
                    resultObj[prop] = val.toString();
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   144
                } else if (type != 'object') {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   145
                    // primitives 'as is'
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   146
                    resultObj[prop] = val;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   147
                }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   148
            }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   149
            return resultObj;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   150
        }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   151
    }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   152
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   153
    // top level object with one property for each compilation unit
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   154
    var scriptObj = {};
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   155
    for each (var cu in task.parse()) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   156
        scriptObj[cu.sourceFile.name] = cu.accept(visitor, null);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   157
    }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   158
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   159
    return scriptObj;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   160
}
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   161
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   162
// JavaFX classes used
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   163
var StackPane = Java.type("javafx.scene.layout.StackPane");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   164
var Scene     = Java.type("javafx.scene.Scene");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   165
var TreeItem  = Java.type("javafx.scene.control.TreeItem");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   166
var TreeView  = Java.type("javafx.scene.control.TreeView");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   167
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   168
// Create a javafx TreeItem to view a script object
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   169
function treeItemForObject(obj, name) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   170
    var item = new TreeItem(name);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   171
    for (var prop in obj) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   172
       var node = obj[prop];
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   173
       if (typeof node == 'object') {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   174
           if (node == null) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   175
               // skip nulls
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   176
               continue;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   177
           }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   178
           var subitem = treeItemForObject(node, prop);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   179
       } else {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   180
           var subitem = new TreeItem(prop + ": " + node);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   181
       }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   182
       item.children.add(subitem);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   183
    }
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   184
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   185
    item.expanded = true;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   186
    return item;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   187
}
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   188
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   189
var commandArgs = arguments;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   190
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   191
// JavaFX start method
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   192
function start(stage) {
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   193
    var obj = javaASTToScriptObject(commandArgs);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   194
    stage.title = "Java AST Viewer"
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   195
    var rootItem = treeItemForObject(obj, "AST");
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   196
    rootItem.expanded = true;
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   197
    var tree = new TreeView(rootItem);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   198
    var root = new StackPane();
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   199
    root.children.add(tree);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   200
    stage.scene = new Scene(root, 300, 450);
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   201
    stage.show();
bda887c0088a 8042600: Add more samples in nashorn/samples directory
sundar
parents:
diff changeset
   202
}