Index: src/test/groovy/inspect/swingui/ScriptToTreeNodeAdapterTest.groovy =================================================================== --- src/test/groovy/inspect/swingui/ScriptToTreeNodeAdapterTest.groovy (revision 16476) +++ src/test/groovy/inspect/swingui/ScriptToTreeNodeAdapterTest.groovy (working copy) @@ -37,7 +37,6 @@ ScriptToTreeNodeAdapter adapter = new ScriptToTreeNodeAdapter() TreeNode root = adapter.compile(script, Phases.SEMANTIC_ANALYSIS) - printnode(root) def result = root.children()?.find { it.toString() == 'BlockStatement' }?.children()?.find { @@ -55,7 +54,6 @@ ScriptToTreeNodeAdapter adapter = new ScriptToTreeNodeAdapter() TreeNode root = adapter.compile(script, Phases.SEMANTIC_ANALYSIS) - printnode(root) def result = root.children()?.find { it.toString() == 'ClassNode - Foo' }?.children()?.find { @@ -70,6 +68,87 @@ assertNotNull('Could not locate ClassExpression in AST', result) } + + public void testCompile_MethodWithParameter() { + def script = " def foo(String bar) { println bar } " + ScriptToTreeNodeAdapter adapter = new ScriptToTreeNodeAdapter() + TreeNode root = adapter.compile(script, Phases.SEMANTIC_ANALYSIS) + + def result = root.children()?.find { + it.toString().startsWith('ClassNode - script') + }?.children()?.find { + it.toString() == 'Methods' + }?.children()?.find { + it.toString() == 'MethodNode - foo' + }?.children()?.find { + it.toString() == 'Parameter - bar' + } + assertNotNull('Could not locate ClassExpression in AST', result) + } + + public void testCompile_MethodWithParameterAndInitialValue() { + def script = """ def foo(String bar = "some_value") { println bar } """ + ScriptToTreeNodeAdapter adapter = new ScriptToTreeNodeAdapter() + TreeNode root = adapter.compile(script, Phases.SEMANTIC_ANALYSIS) + + def result = root.children()?.find { + it.toString().startsWith('ClassNode - script') + }?.children()?.find { + it.toString() == 'Methods' + }?.children()?.find { + it.toString() == 'MethodNode - foo' + }?.children()?.find { + it.toString() == 'Parameter - bar' + }?.children()?.find { + it.toString() == 'Constant - some_value : java.lang.String' + } + assertNotNull('Could not locate ClassExpression in AST', result) + } + + public void testCompile_ClosureParameters() { + + def script = " def x = { parm1 -> println parm1 } " + ScriptToTreeNodeAdapter adapter = new ScriptToTreeNodeAdapter() + TreeNode root = adapter.compile(script, Phases.SEMANTIC_ANALYSIS) + def result = root.children()?.find { + it.toString() == 'BlockStatement' + }?.children()?.find { + it.toString() == 'BlockStatement' + }?.children()?.find { + it.toString() == 'ExpressionStatement' + }?.children()?.find { + it.toString().startsWith('Declaration - (x =') + }?.children()?.find { + it.toString() == 'ClosureExpression' + }?.children()?.find { + it.toString() == 'Parameter - parm1' + } + assertNotNull('Could not locate ClassExpression in AST', result) + } + + public void testCompile_ClosureParametersWithInitialValue() { + + def script = """ def x = { parm1 = "some_value" -> println parm1 } """ + ScriptToTreeNodeAdapter adapter = new ScriptToTreeNodeAdapter() + TreeNode root = adapter.compile(script, Phases.SEMANTIC_ANALYSIS) + def result = root.children()?.find { + it.toString() == 'BlockStatement' + }?.children()?.find { + it.toString() == 'BlockStatement' + }?.children()?.find { + it.toString() == 'ExpressionStatement' + }?.children()?.find { + it.toString().startsWith('Declaration - (x =') + }?.children()?.find { + it.toString() == 'ClosureExpression' + }?.children()?.find { + it.toString() == 'Parameter - parm1' + }?.children()?.find { + it.toString() == 'Constant - some_value : java.lang.String' + } + assertNotNull('Could not locate ClassExpression in AST', result) + } + /** * Helper method to print out the TreeNode to a test form in system out. * Warning, this uses recursion. Index: src/main/groovy/inspect/swingui/ScriptToTreeNodeAdapter.groovy =================================================================== --- src/main/groovy/inspect/swingui/ScriptToTreeNodeAdapter.groovy (revision 16476) +++ src/main/groovy/inspect/swingui/ScriptToTreeNodeAdapter.groovy (working copy) @@ -184,6 +184,19 @@ classNode.methodsList?.each { MethodNode methodNode -> def ggrandchild = adapter.make(methodNode) allMethods.add(ggrandchild) + + // print out parameters of method + methodNode.parameters?.each { Parameter parameter -> + def gggrandchild = adapter.make(parameter) + ggrandchild.add(gggrandchild) + if (parameter.initialExpression) { + TreeNodeBuildingVisitor visitor = new TreeNodeBuildingVisitor(adapter) + parameter.initialExpression.visit(visitor) + if (visitor.currentNode) gggrandchild.add(visitor.currentNode) + } + } + + // print out code of method TreeNodeBuildingVisitor visitor = new TreeNodeBuildingVisitor(adapter) if (methodNode.code) { methodNode.code.visit(visitor) @@ -375,7 +388,16 @@ } public void visitClosureExpression(ClosureExpression node) { - addNode(node, ClosureExpression, { super.visitClosureExpression(it) }); + addNode(node, ClosureExpression, { + it.parameters?.each { parameter -> + addNode(parameter, Parameter, { + if (parameter.initialExpression) { + parameter.initialExpression.visit(this) + } + }); + } + super.visitClosureExpression(it) + }); } public void visitTupleExpression(TupleExpression node) { Index: src/main/groovy/inspect/swingui/AstBrowserProperties.groovy =================================================================== --- src/main/groovy/inspect/swingui/AstBrowserProperties.groovy (revision 16476) +++ src/main/groovy/inspect/swingui/AstBrowserProperties.groovy (working copy) @@ -27,6 +27,7 @@ FieldNode = "FieldNode - \$expression.name : \$expression.type" PropertyNode = "PropertyNode - \${expression.field?.name} : \${expression.field?.type}" AnnotationNode = "AnnotationNode - \${expression.classNode?.name}" + Parameter = "Parameter - \$expression.name" stmt { BlockStatement = "BlockStatement"