You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "John Wagenleitner (JIRA)" <ji...@apache.org> on 2017/08/14 00:01:35 UTC

[jira] [Commented] (GROOVY-8250) VariableScopeVisitor does not set declaring class on property nodes

    [ https://issues.apache.org/jira/browse/GROOVY-8250?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16125093#comment-16125093 ] 

John Wagenleitner commented on GROOVY-8250:
-------------------------------------------

Setting the type on the property based on the first match will cause a problem if there are multiple setters for the given property.  For reference, [commit 4f28eab3cc|https://github.com/apache/groovy/commit/4f28eab3cca03a15ca146c7c8301f5ecddf6fa89] contains tests that will fail if the first resolved setter parameter type is chosen.

One alternative may be to check all methods and if a single type if found use it else fall back to Object.

> VariableScopeVisitor does not set declaring class on property nodes
> -------------------------------------------------------------------
>
>                 Key: GROOVY-8250
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8250
>             Project: Groovy
>          Issue Type: Bug
>            Reporter: Eric Milles
>
> VariableScopeVisitor.findClassMember creates PropertyNode instances for methods (ex: foo.bar will have a property node created if getBar() is implemented).  This is done inside the MethodNode for loop circa line 165.
> The nodes created by this visitor lack a declaring class setting as well as having an improper type.  Without these values, type inferencing is more difficult for anyone that processes the AST.  These are reasonable simple to remedy:
> {code}
>     private Variable findClassMember(ClassNode cn, String name) {
>         if (cn == null) return null;
>         if (cn.isScript()) {
>             return new DynamicVariable(name, false);
>         }
>         for (FieldNode fn : cn.getFields()) {
>             if (fn.getName().equals(name)) return fn;
>         }
>         for (MethodNode mn : cn.getMethods()) {
>             String pName = getPropertyName(mn);
>             // GRECLIPSE edit
>             //if (pName != null && pName.equals(name))
>             //    return new PropertyNode(pName, mn.getModifiers(), ClassHelper.OBJECT_TYPE, cn, null, null, null);
>             if (pName != null && pName.equals(name)) {
>                 PropertyNode property = new PropertyNode(pName, mn.getModifiers(), getPropertyType(mn), cn, null, null, null);
>                 property.getField().setDeclaringClass(cn);
>                 property.setDeclaringClass(cn);
>                 return property;
>             }
>             // GRECLIPSE end
>         }
>         ...
>     // GRECLIPSE add
>     private ClassNode getPropertyType(MethodNode m) {
>         if (m.getReturnType() != ClassHelper.VOID_TYPE) {
>             return m.getReturnType();
>         }
>         return m.getParameters()[0].getType();
>     }
>     // GRECLIPSE end
> {code}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)