You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2019/09/11 05:00:59 UTC

[groovy] branch master updated: GROOVY-9236: Avoid unnecessary resolving

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

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


The following commit(s) were added to refs/heads/master by this push:
     new fbc442d  GROOVY-9236: Avoid unnecessary resolving
fbc442d is described below

commit fbc442da25335ebcb8f61623cd4a89f551c86331
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Aug 31 04:19:53 2019 +0800

    GROOVY-9236: Avoid unnecessary resolving
---
 .../codehaus/groovy/control/ResolveVisitor.java    |   6 +-
 .../groovy/bugs/groovy9236/Groovy9236.groovy       |  23 ++++
 .../bugs/groovy9236/Groovy9236SamePkg.groovy       |  23 ++++
 .../groovy/bugs/groovy9236/Main.groovy             |  31 ++++++
 .../groovy/bugs/groovy9236/Groovy9236Bug.groovy    | 124 +++++++++++++++++++++
 5 files changed, 204 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
index 958a233..9d96690 100644
--- a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
+++ b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
@@ -496,12 +496,12 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
             return true;
         }
 
-        return resolveNestedClass(type) ||
+        return  resolveNestedClass(type) ||
                 resolveFromModule(type, testModuleImports) ||
                 resolveFromCompileUnit(type) ||
                 resolveFromDefaultImports(type, testDefaultImports) ||
-                resolveFromStaticInnerClasses(type, testStaticInnerClasses) ||
-                resolveToOuter(type);
+                resolveToOuter(type) ||
+                resolveFromStaticInnerClasses(type, testStaticInnerClasses);
     }
 
     private boolean resolveNestedClass(ClassNode type) {
diff --git a/src/test-resources/groovy/bugs/groovy9236/Groovy9236.groovy b/src/test-resources/groovy/bugs/groovy9236/Groovy9236.groovy
new file mode 100644
index 0000000..ef1a1ac
--- /dev/null
+++ b/src/test-resources/groovy/bugs/groovy9236/Groovy9236.groovy
@@ -0,0 +1,23 @@
+/*
+ *  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.groovy9236
+
+interface Groovy9236 {
+
+}
\ No newline at end of file
diff --git a/src/test-resources/groovy/bugs/groovy9236/Groovy9236SamePkg.groovy b/src/test-resources/groovy/bugs/groovy9236/Groovy9236SamePkg.groovy
new file mode 100644
index 0000000..69a5f56
--- /dev/null
+++ b/src/test-resources/groovy/bugs/groovy9236/Groovy9236SamePkg.groovy
@@ -0,0 +1,23 @@
+/*
+ *  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.groovy9236
+
+class Groovy9236SamePkg {
+
+}
\ No newline at end of file
diff --git a/src/test-resources/groovy/bugs/groovy9236/Main.groovy b/src/test-resources/groovy/bugs/groovy9236/Main.groovy
new file mode 100644
index 0000000..e1767b4
--- /dev/null
+++ b/src/test-resources/groovy/bugs/groovy9236/Main.groovy
@@ -0,0 +1,31 @@
+/*
+ *  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.groovy9236
+
+class Outer {
+    static class Groovy9236 {}
+
+    def test() {
+        assert new Groovy9236()
+        assert new Outer.Groovy9236()
+        assert new Groovy9236SamePkg()
+    }
+}
+
+new Outer().test()
diff --git a/src/test/groovy/bugs/groovy9236/Groovy9236Bug.groovy b/src/test/groovy/bugs/groovy9236/Groovy9236Bug.groovy
new file mode 100644
index 0000000..0ad30d0
--- /dev/null
+++ b/src/test/groovy/bugs/groovy9236/Groovy9236Bug.groovy
@@ -0,0 +1,124 @@
+/*
+ *  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.groovy9236
+
+import org.codehaus.groovy.tools.GroovyStarter
+
+class Groovy9236Bug extends GroovyTestCase {
+    /* groovy-3.0.0-beta-3 will try to guess and load the following classes:
+Script1$_p_lambda1BeanInfo
+Script1$_p_lambda1Customizer
+Script1BeanInfo
+Script1Customizer
+groovy$transform$CompileStatic
+groovy.lang.GroovyObject$Collectors
+groovy.lang.GroovyObject$CompileStatic
+groovy.lang.GroovyObject$groovy$transform$CompileStatic
+groovy.lang.GroovyObject$java$util$stream$Collectors
+groovy.lang.GroovyObject$java$util$stream$Stream
+groovy.lang.groovy$transform$CompileStatic
+groovy.lang.java$util$stream$Collectors
+groovy.lang.java$util$stream$Stream
+groovy.transform$CompileStatic
+groovy.util.groovy$transform$CompileStatic
+groovy.util.java$util$stream$Collectors
+groovy.util.java$util$stream$Stream
+java$util$stream$Collectors
+java$util$stream$Stream
+java.io.groovy$transform$CompileStatic
+java.io.java$util$stream$Collectors
+java.io.java$util$stream$Stream
+java.lang.groovy$transform$CompileStatic
+java.lang.java$util$stream$Collectors
+java.lang.java$util$stream$Stream
+java.net.groovy$transform$CompileStatic
+java.net.java$util$stream$Collectors
+java.net.java$util$stream$Stream
+java.util$stream$Collectors
+java.util$stream$Stream
+java.util.groovy$transform$CompileStatic
+java.util.java$util$stream$Collectors
+java.util.java$util$stream$Stream
+java.util.stream$Collectors
+java.util.stream$Stream
+     */
+    void testAvoidUnnecessaryResolving() {
+        def cl = new Groovy9236ClassLoader()
+        def gs = new GroovyShell(cl)
+        gs.evaluate('''
+                import groovy.transform.CompileStatic
+                import java.util.stream.Collectors
+                import java.util.stream.Stream
+                
+                @CompileStatic
+                void p() {
+                    assert [2, 3, 4] == [1, 2, 3].stream().map(e -> e.plus 1).collect(Collectors.toList())
+                }
+                
+                p()
+        ''')
+
+        final classNamesShouldAvoidToGuess = ['java.lang.java$util$stream$Collectors',
+                                              'java.util.java$util$stream$Collectors',
+                                              'java.io.java$util$stream$Collectors',
+                                              'java.net.java$util$stream$Collectors',
+                                              'groovy.lang.java$util$stream$Collectors',
+                                              'groovy.util.java$util$stream$Collectors',
+                                              'java$util$stream$Collectors',
+                                              'java.util$stream$Collectors',
+                                              'java.util.stream$Collectors',
+                                              'java.lang.groovy$transform$CompileStatic',
+                                              'java.util.groovy$transform$CompileStatic',
+                                              'java.io.groovy$transform$CompileStatic',
+                                              'java.net.groovy$transform$CompileStatic',
+                                              'groovy.lang.groovy$transform$CompileStatic',
+                                              'groovy.util.groovy$transform$CompileStatic',
+                                              'groovy$transform$CompileStatic',
+                                              'groovy.transform$CompileStatic',
+                                              'java.lang.java$util$stream$Stream',
+                                              'java.util.java$util$stream$Stream',
+                                              'java.io.java$util$stream$Stream',
+                                              'java.net.java$util$stream$Stream',
+                                              'groovy.lang.java$util$stream$Stream',
+                                              'groovy.util.java$util$stream$Stream',
+                                              'java$util$stream$Stream',
+                                              'java.util$stream$Stream',
+                                              'java.util.stream$Stream']
+
+        assert cl.guessedClassNameList.every(n -> !classNamesShouldAvoidToGuess.contains(n))
+    }
+
+    void testResolvingPrecedence() {
+        def mainScriptPath = new File(this.getClass().getResource('/groovy/bugs/groovy9236/Main.groovy').toURI()).absolutePath
+        runScript(mainScriptPath)
+    }
+
+    static void runScript(String path) {
+        GroovyStarter.main(new String[] { "--main", "groovy.ui.GroovyMain", path })
+    }
+
+    private static class Groovy9236ClassLoader extends GroovyClassLoader {
+        def guessedClassNameList = []
+
+        protected Class<?> findClass(final String name) throws ClassNotFoundException {
+            guessedClassNameList << name
+            super.findClass(name)
+        }
+    }
+}