You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Daniel Sun (JIRA)" <ji...@apache.org> on 2018/06/04 06:35:00 UTC

[jira] [Commented] (GROOVY-8629) Groovy STC fails on return of nested class using generics

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

Daniel Sun commented on GROOVY-8629:
------------------------------------

Here is a workaround:

{code:java}
        import groovy.transform.CompileStatic
        
        /**
         * A utility class for comparing two maps
         */
        @CompileStatic
        class MapComparison implements Iterable<IntegerPair> {
            Map<String, Integer> m1
            Map<String, Integer> m2
            Set<String> unionKeys = null
        
            MapComparison(Map<String, Integer> map1, Map<String, Integer> map2) {
                this.m1 = map1
                this.m2 = map2
            }
        
            @Override
            Iterator<IntegerPair> iterator() {
                if (unionKeys == null) {
                    unionKeys = m1.keySet() + m2.keySet()
                }
                def iterator = unionKeys.iterator()                           // workaround
                return new IntegerPairIterator(iterator)
            }
        
            class IntegerPairIterator implements Iterator<IntegerPair> {
                private Iterator<String> keyIterator
        
                IntegerPairIterator(Iterator<String> keyIterator) {
                    this.keyIterator = keyIterator
                }
        
                @Override
                boolean hasNext() {
                    return keyIterator.hasNext()
                }
        
                @Override
                IntegerPair next() {
                    String key = keyIterator.next()
                    IntegerPair comp = new IntegerPair(m1[key], m2[key])
                    return comp
                }
        
                @Override
                void remove() {
                    throw new UnsupportedOperationException()
                }
            }
        
            static class IntegerPair  {
                Integer i1;
                Integer i2;
        
                IntegerPair(Integer int1, Integer int2) {
                    i1 = int1;
                    i2 = int2;
                }
            }
        }
        
        def mc = new MapComparison([:],[:])
{code}


> Groovy STC fails on return of nested class using generics
> ---------------------------------------------------------
>
>                 Key: GROOVY-8629
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8629
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 2.5.0-rc-3, 2.5.0
>         Environment: I can reproduce from the command line with:
> groovy mapComparisonTest.groovy
> With Groovy 2.5.0 and Java 1.8.0_172 on macOS High Sierra.
>            Reporter: Sean Gilligan
>            Priority: Major
>         Attachments: mapComparisonTest.groovy
>
>
> The error returns on line 22 of the attached file:
> return new IntegerPairIterator(unionKeys.iterator())
> Assigning to a temporary variable and returning on a second line OR removing @StaticCompile will fix the problem.
> I thought this was related to GROOVY-8590 because it first occurred in Groovy 2.5.0-rc-3, but it was not fixed in Groovy 2.5.0.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)