You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (JIRA)" <ji...@apache.org> on 2019/08/12 18:12:00 UTC

[jira] [Commented] (GROOVY-9031) Traits using generics generate incorrect stub

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

Eric Milles commented on GROOVY-9031:
-------------------------------------

{{JavaStubGenerator}} adds trait property methods at the top of this method:
{code:java}
    private void printClassContents(PrintWriter out, ClassNode classNode) {
        ...
        try {
            Verifier verifier = new Verifier() {
                @Override
                public void visitClass(final ClassNode node) {
                    List<Statement> savedStatements = new ArrayList<Statement>(node.getObjectInitializerStatements());
                    super.visitClass(node);
                    node.getObjectInitializerStatements().addAll(savedStatements);
                    for (ClassNode trait : Traits.findTraits(node)) {
                        for (PropertyNode traitProperty : trait.getProperties()) {
                            super.visitProperty(traitProperty); // <-- "V value" is processed here and the methods are added to the stub
                        }
                    }
                }
{code}

First problem is that {{Traits.findTraits}} drops the type argument {{String}} in favor of {{V}} because {{GenericsUtils.parameterizeType}} is used to remove the type argument in favor of the type parameter.

Secondly, {{traitProperty}} should probably have generics corrected  before passing to {{visitProperty}} so the property methods have resolved types.

> Traits using generics generate incorrect stub
> ---------------------------------------------
>
>                 Key: GROOVY-9031
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9031
>             Project: Groovy
>          Issue Type: Bug
>          Components: Stub generator / Joint compiler
>    Affects Versions: 2.5.6
>            Reporter: Keegan Witt
>            Priority: Major
>
> {code:groovy}
> trait Test<V> {
>     V value
> }
> class TestImpl implements Test<String> { }
> {code}
> Generates a stub like this
> {code:groovy}
> import java.lang.*;
> import java.io.*;
> import java.net.*;
> import java.util.*;
> import groovy.lang.*;
> import groovy.util.*;
> public class TestImpl
>   extends java.lang.Object  implements
>     Test<java.lang.String>,    groovy.lang.GroovyObject {
> ;
> @groovy.transform.Generated() @groovy.transform.Internal() public  groovy.lang.MetaClass getMetaClass() { return (groovy.lang.MetaClass)null;}
> @groovy.transform.Generated() @groovy.transform.Internal() public  void setMetaClass(groovy.lang.MetaClass mc) { }
> @groovy.transform.Generated() @groovy.transform.Internal() public  java.lang.Object invokeMethod(java.lang.String method, java.lang.Object arguments) { return null;}
> @groovy.transform.Generated() @groovy.transform.Internal() public  java.lang.Object getProperty(java.lang.String property) { return null;}
> @groovy.transform.Generated() @groovy.transform.Internal() public  void setProperty(java.lang.String property, java.lang.Object value) { }
> public  V getValue() { return null;}
> public  void setValue(V value) { }
> }
> {code}
> Note that {{value}} has type {{V}} instead of type {{String}} and the generic reference isn't declared in the class either.



--
This message was sent by Atlassian JIRA
(v7.6.14#76016)