You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2019/06/01 13:11:27 UTC

[groovy] 03/03: display annotations within methods

This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 24f94426bfdd8eccad76f8a02de5776dc633e07f
Author: Paul King <pa...@asert.com.au>
AuthorDate: Sat Jun 1 23:11:10 2019 +1000

    display annotations within methods
---
 .../console/ui/ScriptToTreeNodeAdapter.groovy      | 98 ++++++++++++----------
 1 file changed, 52 insertions(+), 46 deletions(-)

diff --git a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ScriptToTreeNodeAdapter.groovy b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ScriptToTreeNodeAdapter.groovy
index d458ead..1ea7e99 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ScriptToTreeNodeAdapter.groovy
+++ b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ScriptToTreeNodeAdapter.groovy
@@ -24,6 +24,7 @@ import groovy.transform.CompileStatic
 import groovy.transform.PackageScope
 import org.apache.groovy.io.StringBuilderWriter
 import org.codehaus.groovy.GroovyBugError
+import org.codehaus.groovy.ast.AnnotatedNode
 import org.codehaus.groovy.ast.AnnotationNode
 import org.codehaus.groovy.ast.ClassHelper
 import org.codehaus.groovy.ast.ClassNode
@@ -124,14 +125,14 @@ class ScriptToTreeNodeAdapter {
 
     static {
         try {
-            URL url =  ClassLoader.getSystemResource('groovy/inspect/swingui/AstBrowserProperties.groovy')
+            URL url =  ClassLoader.getSystemResource('groovy/console/ui/AstBrowserProperties.groovy')
             if (!url) {
-                url = ScriptToTreeNodeAdapter.class.classLoader.getResource('groovy/inspect/swingui/AstBrowserProperties.groovy')
+                url = ScriptToTreeNodeAdapter.class.classLoader.getResource('groovy/console/ui/AstBrowserProperties.groovy')
             }
-    
+
             def config = new ConfigSlurper().parse(url)
             classNameToStringForm = config.toProperties()
-    
+
             String home = System.getProperty('user.home')
             if (home) {
                 File userFile = new File(home + File.separator + '.groovy/AstBrowserProperties.groovy')
@@ -144,10 +145,10 @@ class ScriptToTreeNodeAdapter {
         } catch(ex) {
             // on restricted environments like, such calls may fail, but that should not prevent the class
             // from being loaded. Tree nodes can still get rendered with their simple names.
-            classNameToStringForm = new Properties()  
+            classNameToStringForm = new Properties()
         }
     }
-    
+
     ScriptToTreeNodeAdapter(classLoader, showScriptFreeForm, showScriptClass, showClosureClasses, nodeMaker, config = null) {
         this.classLoader = classLoader ?: new GroovyClassLoader(getClass().classLoader)
         this.showScriptFreeForm = showScriptFreeForm
@@ -158,7 +159,7 @@ class ScriptToTreeNodeAdapter {
     }
 
     /**
-    * Performs the conversion from script to TreeNode.
+     * Performs the conversion from script to TreeNode.
      *
      * @param script
      *      a Groovy script in String form
@@ -166,7 +167,7 @@ class ScriptToTreeNodeAdapter {
      *      the int based CompilePhase to compile it to.
      * @param indy
      *      if {@code true} InvokeDynamic (Indy) bytecode is generated
-    */
+     */
     def compile(String script, int compilePhase, boolean indy=false) {
         def scriptName = 'script' + System.currentTimeMillis() + '.groovy'
         GroovyCodeSource codeSource = new GroovyCodeSource(script, scriptName, '/groovy/script')
@@ -222,29 +223,29 @@ class ScriptToTreeNodeAdapter {
      */
     private List<List<String>> getPropertyTable(node) {
         node.metaClass.properties?.
-            findAll { it.getter }?.
-            collect {
-                def name = it.name.toString()
-                def value
-                try {
-                    // multiple assignment statements cannot be cast to VariableExpression so
-                    // instead reference the value through the leftExpression property, which is the same
-                    if (node instanceof DeclarationExpression &&
-                            (name == 'variableExpression' || name == 'tupleExpression')) {
-                        value = toString(node.leftExpression)
-                    } else {
-                        value = toString(it.getProperty(node))
+                findAll { it.getter }?.
+                collect {
+                    def name = it.name.toString()
+                    def value
+                    try {
+                        // multiple assignment statements cannot be cast to VariableExpression so
+                        // instead reference the value through the leftExpression property, which is the same
+                        if (node instanceof DeclarationExpression &&
+                                (name == 'variableExpression' || name == 'tupleExpression')) {
+                            value = toString(node.leftExpression)
+                        } else {
+                            value = toString(it.getProperty(node))
+                        }
+                    } catch (GroovyBugError reflectionArtefact) {
+                        // compiler throws error if it thinks a field is being accessed
+                        // before it is set under certain conditions. It wasn't designed
+                        // to be walked reflectively like this.
+                        value = null
                     }
-                } catch (GroovyBugError reflectionArtefact) {
-                    // compiler throws error if it thinks a field is being accessed
-                    // before it is set under certain conditions. It wasn't designed
-                    // to be walked reflectively like this.
-                    value = null
-                }
-                def type = it.type.simpleName.toString()
-                [name, value, type]
-            }?.
-            sort { it[0] }
+                    def type = it.type.simpleName.toString()
+                    [name, value, type]
+                }?.
+                sort { it[0] }
     }
 
     // GROOVY-8339: to avoid illegal access to a non-visible implementation class - can be removed if a more general solution is found
@@ -350,12 +351,14 @@ class TreeNodeBuildingNodeOperation extends PrimaryClassNodeOperation {
         }
     }
 
-    private void collectAnnotationData(parent, String name, ClassNode classNode) {
+    private void collectAnnotationData(parent, String name, AnnotatedNode node) {
         def allAnnotations = nodeMaker.makeNode(name)
-        if (classNode.annotations) parent.add(allAnnotations)
-        classNode.annotations?.each {AnnotationNode annotationNode ->
-            def ggrandchild = adapter.make(annotationNode)
-            allAnnotations.add(ggrandchild)
+        if (node.annotations) {
+            parent.add(allAnnotations)
+            node.annotations?.each {AnnotationNode annotationNode ->
+                def ggrandchild = adapter.make(annotationNode)
+                allAnnotations.add(ggrandchild)
+            }
         }
     }
 
@@ -401,12 +404,12 @@ class TreeNodeBuildingNodeOperation extends PrimaryClassNodeOperation {
 
         doCollectMethodData(allMethods, methods)
     }
-    
+
     private void doCollectMethodData(allMethods, List methods) {
         methods?.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)
@@ -416,14 +419,16 @@ class TreeNodeBuildingNodeOperation extends PrimaryClassNodeOperation {
                     parameter.initialExpression.visit(visitor)
                     if (visitor.currentNode) gggrandchild.add(visitor.currentNode)
                 }
+                collectAnnotationData(gggrandchild, 'Annotations', parameter)
             }
-    
+
             // print out code of method
             TreeNodeBuildingVisitor visitor = new TreeNodeBuildingVisitor(adapter)
             if (methodNode.code) {
                 methodNode.code.visit(visitor)
                 if (visitor.currentNode) ggrandchild.add(visitor.currentNode)
             }
+            collectAnnotationData(ggrandchild, 'Annotations', methodNode)
         }
     }
 
@@ -439,6 +444,7 @@ class TreeNodeBuildingNodeOperation extends PrimaryClassNodeOperation {
                 ctorNode.code.visit(visitor)
                 if (visitor.currentNode) ggrandchild.add(visitor.currentNode)
             }
+            collectAnnotationData(ggrandchild, 'Annotations', ctorNode)
         }
 
     }
@@ -462,8 +468,8 @@ class TreeNodeBuildingNodeOperation extends PrimaryClassNodeOperation {
 }
 
 /**
-* This AST visitor builds up a TreeNode.
-*/
+ * This AST visitor builds up a TreeNode.
+ */
 @PackageScope
 class TreeNodeBuildingVisitor extends CodeVisitorSupport {
 
@@ -480,12 +486,12 @@ class TreeNodeBuildingVisitor extends CodeVisitorSupport {
     }
 
     /**
-    * This method looks at the AST node and decides how to represent it in a TreeNode, then it
+     * This method looks at the AST node and decides how to represent it in a TreeNode, then it
      * continues walking the tree. If the node and the expectedSubclass are not exactly the same
      * Class object then the node is not added to the tree. This is to eliminate seeing duplicate
      * nodes, for instance seeing an ArgumentListExpression and a TupleExpression in the tree, when
      * an ArgumentList is-a Tuple.
-    */
+     */
     private void addNode(node, Class expectedSubclass, Closure superMethod) {
 
         if (expectedSubclass.getName() == node.getClass().getName()) {
@@ -640,7 +646,7 @@ class TreeNodeBuildingVisitor extends CodeVisitorSupport {
 
     @Override
     void visitClosureExpression(ClosureExpression node) {
-        addNode(node, ClosureExpression, { 
+        addNode(node, ClosureExpression, {
             it.parameters?.each { parameter -> visitParameter(parameter) }
             super.visitClosureExpression(it)
         })
@@ -786,9 +792,9 @@ class TreeNodeBuildingVisitor extends CodeVisitorSupport {
 
     @Override
     void visitCatchStatement(CatchStatement node) {
-        addNode(node, CatchStatement, { 
-            if (it.variable) visitParameter(it.variable) 
-            super.visitCatchStatement(it) 
+        addNode(node, CatchStatement, {
+            if (it.variable) visitParameter(it.variable)
+            super.visitCatchStatement(it)
         })
     }