From commits-return-8806-archive-asf-public=cust-asf.ponee.io@groovy.apache.org Sat Jun 1 13:11:26 2019 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id C1EB018075F for ; Sat, 1 Jun 2019 15:11:25 +0200 (CEST) Received: (qmail 52643 invoked by uid 500); 1 Jun 2019 13:11:25 -0000 Mailing-List: contact commits-help@groovy.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@groovy.apache.org Delivered-To: mailing list commits@groovy.apache.org Received: (qmail 52567 invoked by uid 99); 1 Jun 2019 13:11:25 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 01 Jun 2019 13:11:25 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id EA2C18A5A3; Sat, 1 Jun 2019 13:11:24 +0000 (UTC) Date: Sat, 01 Jun 2019 13:11:27 +0000 To: "commits@groovy.apache.org" Subject: [groovy] 03/03: display annotations within methods MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit From: paulk@apache.org In-Reply-To: <155939468471.7900.6614559155337689121@gitbox.apache.org> References: <155939468471.7900.6614559155337689121@gitbox.apache.org> X-Git-Host: gitbox.apache.org X-Git-Repo: groovy X-Git-Refname: refs/heads/master X-Git-Reftype: branch X-Git-Rev: 24f94426bfdd8eccad76f8a02de5776dc633e07f X-Git-NotificationType: diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated Message-Id: <20190601131124.EA2C18A5A3@gitbox.apache.org> 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 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> 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) }) }