You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2022/02/18 22:11:12 UTC

[groovy] branch GROOVY_2_5_X updated (4e3c21b -> 02a5642)

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

emilles pushed a change to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git.


    from 4e3c21b  GROOVY-9183: `@NamedVariant`: no `Map#getOrDefault(Object,V)` on JDK7
     new c05b3ea  GROOVY-9376: add grab/grape resolver to start of chain
     new fb0b892  GROOVY-10379: interface default methods in `ClassNode#hasPossibleMethod`
     new df31b32  GROOVY-9678: check for override of super class property
     new c44841b  GROOVY-5364: find static members from static script methods
     new 02a5642  fix test

The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/main/groovy/groovy/grape/GrapeIvy.groovy       |  24 +++--
 .../java/org/codehaus/groovy/ast/ClassNode.java    |   7 ++
 .../groovy/classgen/VariableScopeVisitor.java      |  18 ++--
 src/test/groovy/bugs/Groovy5364.groovy             | 104 +++++++++++++++++++++
 src/test/groovy/bugs/Groovy8964Bug.groovy          |  53 +++++++++++
 .../bugs/{Groovy9031.groovy => Groovy9678.groovy}  |  39 ++++----
 src/test/groovy/lang/MetaClassPropertyTest.groovy  |  16 ++--
 7 files changed, 218 insertions(+), 43 deletions(-)
 create mode 100644 src/test/groovy/bugs/Groovy5364.groovy
 copy src/test/groovy/bugs/{Groovy9031.groovy => Groovy9678.groovy} (70%)

[groovy] 04/05: GROOVY-5364: find static members from static script methods

Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit c44841b4255274d3574906e40b1ef3782002517d
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Feb 18 15:56:54 2022 -0600

    GROOVY-5364: find static members from static script methods
    
    Conflicts:
    	src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
---
 .../groovy/classgen/VariableScopeVisitor.java      |   4 -
 src/test/groovy/bugs/Groovy5364.groovy             | 104 +++++++++++++++++++++
 2 files changed, 104 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java b/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
index 6774500..5c8a0b7 100644
--- a/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
+++ b/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
@@ -168,10 +168,6 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
         final boolean abstractType = node.isAbstract();
 
         for (ClassNode cn = node; cn != null && !cn.equals(ClassHelper.OBJECT_TYPE); cn = cn.getSuperClass()) {
-            if (cn.isScript()) {
-                return new DynamicVariable(name, false);
-            }
-
             for (FieldNode fn : cn.getFields()) {
                 if (name.equals(fn.getName())) return fn;
             }
diff --git a/src/test/groovy/bugs/Groovy5364.groovy b/src/test/groovy/bugs/Groovy5364.groovy
new file mode 100644
index 0000000..5ab849e
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy5364.groovy
@@ -0,0 +1,104 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package groovy.bugs
+
+import org.junit.Test
+
+import static groovy.test.GroovyAssert.assertScript
+import static groovy.test.GroovyAssert.shouldFail
+
+final class Groovy5364 {
+
+    @Test
+    void testStaticScriptMethodAsProperty1() {
+        assertScript '''
+            static getStaticProperty() {
+                'x'
+            }
+
+            assert 'x' == getStaticProperty()
+            assert 'x' == staticProperty
+        '''
+    }
+
+    @Test
+    void testStaticScriptMethodAsProperty2() {
+        assertScript '''
+            static getSomething() { 'x' }
+            something = 'y' // script var
+
+            assert 'x' == getSomething()
+            assert 'y' == something
+        '''
+    }
+
+    @Test
+    void testStaticScriptMethodAsProperty3() {
+        new GroovyShell(new Binding(something: 'y')).evaluate '''
+            static getSomething() { 'x' }
+
+            assert 'x' == getSomething()
+            assert 'y' == something
+        '''
+    }
+
+    @Test
+    void testStaticScriptMethodAsProperty4() {
+        assertScript '''
+            static getStaticProperty() {
+                'x'
+            }
+
+            void test() {
+                assert 'x' == getStaticProperty()
+                assert 'x' == staticProperty
+            }
+            test()
+        '''
+    }
+
+    @Test
+    void testStaticScriptMethodAsProperty5() {
+        assertScript '''
+            static getStaticProperty() {
+                'x'
+            }
+
+            static void test() {
+                assert 'x' == getStaticProperty()
+                assert 'x' == staticProperty // Apparent variable 'staticProperty' was found in a static scope but doesn't refer to a local variable, static field or class
+            }
+            test()
+        '''
+    }
+
+    @Test
+    void testStaticScriptMethodAsProperty6() {
+        def err = shouldFail '''
+            def getNonStaticProperty() {
+                'x'
+            }
+
+            static void test() {
+                nonStaticProperty
+            }
+        '''
+        assert err =~ /Apparent variable 'nonStaticProperty' was found in a static scope but doesn't refer to a local variable, static field or class/
+    }
+}

[groovy] 03/05: GROOVY-9678: check for override of super class property

Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit df31b3239aa70da2f234e1cd9eee6d63a2e0fe44
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Feb 18 15:52:27 2022 -0600

    GROOVY-9678: check for override of super class property
---
 .../groovy/classgen/VariableScopeVisitor.java      | 14 ++++-
 src/test/groovy/bugs/Groovy9678.groovy             | 73 ++++++++++++++++++++++
 2 files changed, 84 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java b/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
index 0dea9d0..6774500 100644
--- a/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
+++ b/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java
@@ -57,6 +57,7 @@ import java.util.LinkedList;
 import static java.lang.reflect.Modifier.isFinal;
 import static java.lang.reflect.Modifier.isStatic;
 import static org.apache.groovy.ast.tools.MethodNodeUtils.getPropertyName;
+import static org.codehaus.groovy.ast.tools.GeneralUtils.getAllProperties;
 
 /**
  * Initializes the variable scopes for an AST.
@@ -181,7 +182,12 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
 
             for (MethodNode mn : cn.getMethods()) {
                 if ((abstractType || !mn.isAbstract()) && name.equals(getPropertyName(mn))) {
-                    FieldNode fn = new FieldNode(name, mn.getModifiers() & 0xF, ClassHelper.OBJECT_TYPE, cn, null);
+                    // check for override of super class property
+                    for (PropertyNode pn : getAllProperties(cn.getSuperClass())) {
+                        if (name.equals(pn.getName())) return pn;
+                    }
+
+                    FieldNode fn = new FieldNode(name, mn.getModifiers() & 0xF, ClassHelper.DYNAMIC_TYPE, cn, null);
                     fn.setHasNoRealSourcePosition(true);
                     fn.setDeclaringClass(cn);
                     fn.setSynthetic(true);
@@ -192,9 +198,11 @@ public class VariableScopeVisitor extends ClassCodeVisitorSupport {
                 }
             }
 
-            for (ClassNode face : cn.getAllInterfaces()) {
-                FieldNode fn = face.getDeclaredField(name);
+            for (ClassNode in : cn.getAllInterfaces()) {
+                FieldNode fn = in.getDeclaredField(name);
                 if (fn != null) return fn;
+                PropertyNode pn = in.getProperty(name);
+                if (pn != null) return pn;
             }
         }
 
diff --git a/src/test/groovy/bugs/Groovy9678.groovy b/src/test/groovy/bugs/Groovy9678.groovy
new file mode 100644
index 0000000..b2e9073
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy9678.groovy
@@ -0,0 +1,73 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package groovy.bugs
+
+import org.codehaus.groovy.control.CompilerConfiguration
+import org.codehaus.groovy.tools.javac.JavaAwareCompilationUnit
+import org.junit.Test
+
+final class Groovy9678 {
+
+    @Test
+    void testTraitStaticProperty() {
+        def config = new CompilerConfiguration(
+            targetDirectory: File.createTempDir(),
+            jointCompilationOptions: [stubDir: File.createTempDir()]
+        )
+
+        def parentDir = File.createTempDir()
+        try {
+            def a = new File(parentDir, 'T.groovy')
+            a.write '''
+                trait T {
+                    static p = 1
+                }
+            '''
+            def b = new File(parentDir, 'C.groovy')
+            b.write '''
+                class C implements T {
+                    static m() {
+                        p = 2
+                        p += 1
+                        return p
+                    }
+                }
+            '''
+            def c = new File(parentDir, 'Main.java')
+            c.write '''
+                public class Main {
+                    public static void main(String[] args) {
+                        if (!C.m().equals(3)) throw new AssertionError();
+                    }
+                }
+            '''
+
+            def loader = new GroovyClassLoader(this.class.classLoader)
+            def cu = new JavaAwareCompilationUnit(config, loader)
+            cu.addSources(a, b, c)
+            cu.compile()
+
+            loader.loadClass('Main').main()
+        } finally {
+            config.jointCompilationOptions.stubDir.deleteDir()
+            config.targetDirectory.deleteDir()
+            parentDir.deleteDir()
+        }
+    }
+}

[groovy] 02/05: GROOVY-10379: interface default methods in `ClassNode#hasPossibleMethod`

Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit fb0b892158ac8b84fbd3b3a74a6f2f1f07b2aeac
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Nov 23 12:13:14 2021 -0600

    GROOVY-10379: interface default methods in `ClassNode#hasPossibleMethod`
    
    Conflicts:
    	src/test/groovy/bugs/Groovy8964.groovy
---
 .../java/org/codehaus/groovy/ast/ClassNode.java    |  7 +++
 src/test/groovy/bugs/Groovy8964Bug.groovy          | 53 ++++++++++++++++++++++
 2 files changed, 60 insertions(+)

diff --git a/src/main/java/org/codehaus/groovy/ast/ClassNode.java b/src/main/java/org/codehaus/groovy/ast/ClassNode.java
index f324fdf..48bd454 100644
--- a/src/main/java/org/codehaus/groovy/ast/ClassNode.java
+++ b/src/main/java/org/codehaus/groovy/ast/ClassNode.java
@@ -1244,6 +1244,13 @@ public class ClassNode extends AnnotatedNode implements Opcodes {
                     return true;
                 }
             }
+            for (ClassNode in : cn.getAllInterfaces()) {
+                for (MethodNode mn : in.getDeclaredMethods(name)) {
+                    if (mn.isDefault() && hasCompatibleNumberOfArgs(mn, count)) {
+                        return true;
+                    }
+                }
+            }
         }
 
         return false;
diff --git a/src/test/groovy/bugs/Groovy8964Bug.groovy b/src/test/groovy/bugs/Groovy8964Bug.groovy
index 8222ccb..a5285bb 100644
--- a/src/test/groovy/bugs/Groovy8964Bug.groovy
+++ b/src/test/groovy/bugs/Groovy8964Bug.groovy
@@ -18,6 +18,9 @@
  */
 package groovy.bugs
 
+import org.codehaus.groovy.control.CompilerConfiguration
+import org.codehaus.groovy.tools.javac.JavaAwareCompilationUnit
+
 class Groovy8964Bug extends GroovyTestCase {
 
     void testInstanceVarargMethodNotMaskedByStaticMethodWithSameNumberOfArgs() {
@@ -72,6 +75,56 @@ class Groovy8964Bug extends GroovyTestCase {
         '''
     }
 
+    // GROOVY-10379
+    void testInstanceMethodNotMaskedByStaticMethodWithSameNumberOfArgs3() {
+        def config = new CompilerConfiguration(
+            targetDirectory: File.createTempDir(),
+            jointCompilationOptions: [stubDir: File.createTempDir()]
+        )
+
+        def parentDir = File.createTempDir()
+        try {
+            new File(parentDir, 'p').mkdir()
+
+            def a = new File(parentDir, 'p/A.java')
+            a.write '''
+                package p;
+                public abstract class A implements I {
+                    public static String m(Number n) { return "number"; }
+                }
+            '''
+            def b = new File(parentDir, 'p/I.java')
+            b.write '''
+                package p;
+                public interface I {
+                    default String m(String s) { return "string"; }
+                }
+            '''
+            def c = new File(parentDir, 'Main.groovy')
+            c.write '''
+                @groovy.transform.CompileStatic
+                class C extends p.A {
+                    void test() {
+                        String result = m('') // GroovyCastException: Cannot cast object 'class C' with class 'java.lang.Class' to class 'p.I'
+                        assert result == 'string'
+                    }
+                }
+                new C().test()
+            '''
+
+            def loader = new GroovyClassLoader(this.class.classLoader)
+            def cu = new JavaAwareCompilationUnit(config, loader)
+            cu.addSources(a, b, c)
+            cu.compile()
+
+            loader.loadClass('Main').main()
+        } finally {
+            config.jointCompilationOptions.stubDir.deleteDir()
+            config.targetDirectory.deleteDir()
+            parentDir.deleteDir()
+        }
+    }
+
     static abstract class A {
         static void m(Integer i) {}
         protected void m(String s) {}

[groovy] 05/05: fix test

Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 02a56426b3655655e229aa65f550e26aa6d3f385
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Feb 18 16:11:03 2022 -0600

    fix test
---
 src/test/groovy/lang/MetaClassPropertyTest.groovy | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/src/test/groovy/lang/MetaClassPropertyTest.groovy b/src/test/groovy/lang/MetaClassPropertyTest.groovy
index e20d22c..b53105e 100644
--- a/src/test/groovy/lang/MetaClassPropertyTest.groovy
+++ b/src/test/groovy/lang/MetaClassPropertyTest.groovy
@@ -21,7 +21,7 @@ package groovy.lang
 class MetaClassPropertyTest extends GroovyTestCase {
 
     void testForJavaClass() {
-        checkMetaClassBehavior("hello world")
+        checkMetaClassBehavior(Short.valueOf('1'))
     }
 
     void testForGroovyClass() {
@@ -30,29 +30,29 @@ class MetaClassPropertyTest extends GroovyTestCase {
 
     private checkMetaClassBehavior(Object foo) {
         // check metaclass points to correct class
-        assertEquals foo.class, foo.metaClass.theClass
+        assert foo.metaClass.theClass.equals(foo.getClass())
 
         // check defaults
         assert foo.metaClass.adaptee instanceof MetaClassImpl
-        assert foo.class.metaClass.adaptee instanceof MetaClassImpl
+        assert foo.getClass().metaClass.adaptee instanceof MetaClassImpl
 
         // use metaclass builder on instance
         foo.metaClass.dummy = {}
         assert foo.metaClass.adaptee instanceof ExpandoMetaClass
-        assert foo.class.metaClass.adaptee instanceof MetaClassImpl
+        assert foo.getClass().metaClass.adaptee instanceof MetaClassImpl
 
         // use metaclass builder on class
         foo.class.metaClass.dummy = {}
         assert foo.metaClass.adaptee instanceof ExpandoMetaClass
         // a little fragile but ExpandoMetaProperty is not public
-        assert foo.class.metaClass.adaptee.getClass().name.contains('ExpandoMetaClass$ExpandoMetaProperty')
+        assert foo.getClass().metaClass.adaptee.getClass().name.contains('ExpandoMetaClass$ExpandoMetaProperty')
 
         // remove class-based metaclass
-        GroovySystem.metaClassRegistry.removeMetaClass(foo.class)
+        GroovySystem.metaClassRegistry.removeMetaClass(foo.getClass())
         assert foo.metaClass.adaptee instanceof ExpandoMetaClass
-        assert foo.class.metaClass.adaptee instanceof MetaClassImpl
+        assert foo.getClass().metaClass.adaptee instanceof MetaClassImpl
     }
 }
 
 class MCPTest1 {
-}
\ No newline at end of file
+}

[groovy] 01/05: GROOVY-9376: add grab/grape resolver to start of chain

Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit c05b3ea587680db2799a6d3db606208d19e35d1d
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Mon Feb 3 11:07:39 2020 -0600

    GROOVY-9376: add grab/grape resolver to start of chain
---
 src/main/groovy/groovy/grape/GrapeIvy.groovy | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/src/main/groovy/groovy/grape/GrapeIvy.groovy b/src/main/groovy/groovy/grape/GrapeIvy.groovy
index c70ab67..9939bbf 100644
--- a/src/main/groovy/groovy/grape/GrapeIvy.groovy
+++ b/src/main/groovy/groovy/grape/GrapeIvy.groovy
@@ -19,6 +19,8 @@
 package groovy.grape
 
 import groovy.transform.CompileStatic
+import groovy.transform.NamedParam
+import groovy.transform.NamedParams
 import org.apache.groovy.plugin.GroovyRunner
 import org.apache.groovy.plugin.GroovyRunnerRegistry
 import org.apache.ivy.Ivy
@@ -753,16 +755,20 @@ class GrapeIvy implements GrapeEngine {
 
     @Override
     @CompileStatic
-    void addResolver(Map<String, Object> args) {
-        ChainResolver chainResolver = (ChainResolver) settings.getResolver('downloadGrapes')
-
-        IBiblioResolver resolver = new IBiblioResolver(
-                name: (String) args.name,
-                root: (String) args.root,
-                m2compatible: (boolean) (args.m2Compatible ?: true),
-                settings: (ResolverSettings) settings)
+    void addResolver(@NamedParams([
+        @NamedParam(value='name', type=String, required=true),
+        @NamedParam(value='root', type=String, required=true),
+        @NamedParam(value='m2Compatible', type=Boolean, required=false)
+    ]) Map<String, Object> args) {
+        def resolver = new IBiblioResolver(
+            name: (String) args.name,
+            root: (String) args.root,
+            m2compatible: (boolean) args.getOrDefault('m2Compatible', Boolean.TRUE),
+            settings: (ResolverSettings) settings
+        )
 
-        chainResolver.add(resolver)
+        def chainResolver = (ChainResolver) settings.getResolver('downloadGrapes')
+        chainResolver.resolvers.add(0, resolver)
 
         ivyInstance = Ivy.newInstance(settings)
         resolvedDependencies = []