groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [07/22] groovy git commit: GROOVY-4636: AST Browser does not show object initializer statements (closes #473)
Date Fri, 13 Jan 2017 15:58:20 GMT
GROOVY-4636: AST Browser does not show object initializer statements (closes #473)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/9ae86075
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/9ae86075
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/9ae86075

Branch: refs/heads/parrot
Commit: 9ae860758ff19f01dbc0cc24c05d07907cc86607
Parents: 3e2d5fd
Author: John Wagenleitner <jwagenleitner@apache.org>
Authored: Sat Dec 31 10:47:32 2016 -0800
Committer: John Wagenleitner <jwagenleitner@apache.org>
Committed: Thu Jan 5 20:49:40 2017 -0800

----------------------------------------------------------------------
 .../swingui/AstNodeToScriptAdapter.groovy       | 15 +++++++++++
 .../swingui/ScriptToTreeNodeAdapter.groovy      | 22 +++++++++++++++++
 .../swingui/AstNodeToScriptAdapterTest.groovy   | 18 ++++++++++++++
 .../swingui/ScriptToTreeNodeAdapterTest.groovy  | 26 ++++++++++++++++++++
 4 files changed, 81 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/9ae86075/subprojects/groovy-console/src/main/groovy/groovy/inspect/swingui/AstNodeToScriptAdapter.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-console/src/main/groovy/groovy/inspect/swingui/AstNodeToScriptAdapter.groovy
b/subprojects/groovy-console/src/main/groovy/groovy/inspect/swingui/AstNodeToScriptAdapter.groovy
index 083adcf..554b953 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/inspect/swingui/AstNodeToScriptAdapter.groovy
+++ b/subprojects/groovy-console/src/main/groovy/groovy/inspect/swingui/AstNodeToScriptAdapter.groovy
@@ -288,6 +288,8 @@ class AstNodeToScriptVisitor extends PrimaryClassNodeOperation implements
Groovy
             printDoubleBreak()
             node?.declaredConstructors?.each { visitConstructor(it) }
             printLineBreak()
+            visitObjectInitializerBlocks(node)
+            printLineBreak()
             node?.methods?.each { visitMethod(it) }
         }
         print '}'
@@ -295,6 +297,19 @@ class AstNodeToScriptVisitor extends PrimaryClassNodeOperation implements
Groovy
         classNameStack.pop()
     }
 
+    private void visitObjectInitializerBlocks(ClassNode node) {
+        for (Statement stmt : node.getObjectInitializerStatements()) {
+            print '{'
+            printLineBreak()
+            indented {
+                stmt.visit(this)
+            }
+            printLineBreak()
+            print '}'
+            printDoubleBreak()
+        }
+    }
+
     private void visitGenerics(GenericsType[] generics) {
 
         if (generics) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/9ae86075/subprojects/groovy-console/src/main/groovy/groovy/inspect/swingui/ScriptToTreeNodeAdapter.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-console/src/main/groovy/groovy/inspect/swingui/ScriptToTreeNodeAdapter.groovy
b/subprojects/groovy-console/src/main/groovy/groovy/inspect/swingui/ScriptToTreeNodeAdapter.groovy
index bafab26..be80a59 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/inspect/swingui/ScriptToTreeNodeAdapter.groovy
+++ b/subprojects/groovy-console/src/main/groovy/groovy/inspect/swingui/ScriptToTreeNodeAdapter.groovy
@@ -238,6 +238,7 @@ class TreeNodeBuildingNodeOperation extends PrimaryClassNodeOperation
{
         root.add(child)
 
         collectConstructorData(child, 'Constructors', classNode)
+        collectObjectInitializers(child, 'Object Initializers', classNode)
         collectMethodData(child, 'Methods', classNode)
         collectFieldData(child, 'Fields', classNode)
         collectPropertyData(child, 'Properties', classNode)
@@ -261,6 +262,7 @@ class TreeNodeBuildingNodeOperation extends PrimaryClassNodeOperation
{
             root.add(child)
 
             collectConstructorData(child, 'Constructors', innerClassNode)
+            collectObjectInitializers(child, 'Object Initializers', innerClassNode)
             collectMethodData(child, 'Methods', innerClassNode)
             collectFieldData(child, 'Fields', innerClassNode)
             collectPropertyData(child, 'Properties', innerClassNode)
@@ -360,6 +362,26 @@ class TreeNodeBuildingNodeOperation extends PrimaryClassNodeOperation
{
         }
 
     }
+
+    private void collectObjectInitializers(parent, String name, ClassNode node) {
+        List<Statement> initStatements = node.getObjectInitializerStatements()
+        if (!initStatements) {
+            return
+        }
+        def allInitializers = nodeMaker.makeNode(name)
+        parent.add(allInitializers)
+        for (Statement stmt : initStatements) {
+            Statement initBlock = ((BlockStatement)stmt).statements.first()
+            def ggrandchild = adapter.make(initBlock)
+            allInitializers.add(ggrandchild)
+            TreeNodeBuildingVisitor visitor = new TreeNodeBuildingVisitor(adapter)
+            initBlock.visit(visitor)
+            if (visitor.currentNode) {
+                ggrandchild.add(visitor.currentNode)
+            }
+        }
+    }
+
 }
 
 /**

http://git-wip-us.apache.org/repos/asf/groovy/blob/9ae86075/subprojects/groovy-console/src/test/groovy/groovy/inspect/swingui/AstNodeToScriptAdapterTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-console/src/test/groovy/groovy/inspect/swingui/AstNodeToScriptAdapterTest.groovy
b/subprojects/groovy-console/src/test/groovy/groovy/inspect/swingui/AstNodeToScriptAdapterTest.groovy
index 48c0640..c526f17 100644
--- a/subprojects/groovy-console/src/test/groovy/groovy/inspect/swingui/AstNodeToScriptAdapterTest.groovy
+++ b/subprojects/groovy-console/src/test/groovy/groovy/inspect/swingui/AstNodeToScriptAdapterTest.groovy
@@ -869,4 +869,22 @@ class AstNodeToScriptAdapterTest extends GroovyTestCase {
         assert result.contains("assert this.f([*: ['a': 10, 'b': 20, 'c': 30], 'e': 50])
== 30 : null")
         assert result.contains("assert this.f(['e': 100, *: ['a': 10, 'b': 20, 'c': 30]],
*[4, 5], 6) == [['e': 100, 'b': 20, 'c': 30, 'a': 10], 4, 5, 6] : null")
     }
+
+    // GROOVY-4636
+    void testObjectInitializers() {
+        String script = '''
+            class A {
+                def v
+                A() { v = 1 }
+                { v = 2 }
+            }
+        '''
+
+        String result = compileToScript(script)
+        assert result =~ /\{\s*v = 2\s*\}/
+
+        // During class gen the initializers should have been merged in with the ctors
+        result = compileToScript(script, CompilePhase.CLASS_GENERATION)
+        assert result =~ /(?s)public A\(\) \{.*?v = 2\s*v = 1\s*\}/
+    }
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/9ae86075/subprojects/groovy-console/src/test/groovy/groovy/inspect/swingui/ScriptToTreeNodeAdapterTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-console/src/test/groovy/groovy/inspect/swingui/ScriptToTreeNodeAdapterTest.groovy
b/subprojects/groovy-console/src/test/groovy/groovy/inspect/swingui/ScriptToTreeNodeAdapterTest.groovy
index e796ec5..b3196cb 100644
--- a/subprojects/groovy-console/src/test/groovy/groovy/inspect/swingui/ScriptToTreeNodeAdapterTest.groovy
+++ b/subprojects/groovy-console/src/test/groovy/groovy/inspect/swingui/ScriptToTreeNodeAdapterTest.groovy
@@ -558,4 +558,30 @@ class ScriptToTreeNodeAdapterTest extends GroovyTestCase {
                 ],
                 adapter)
     }
+
+    // GROOVY-4636
+    void testScriptWithObjectInitializers() {
+        ScriptToTreeNodeAdapter adapter = createAdapter(false, true, true)
+
+        def source = '''
+            class A {
+                int i = 0
+                A() {
+                    i = 5
+                }
+                {
+                    i *= 2
+                }
+            }
+            '''
+
+        assertTreeStructure(source, CompilePhase.CONVERSION,
+                [
+                        startsWith('ClassNode - A'),
+                        eq('Object Initializers')
+                ],
+                adapter)
+
+    }
+
 }


Mime
View raw message