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 2020/02/23 06:07:41 UTC

[groovy] branch GROOVY_3_0_X updated (89bd1b2 -> 04199c6)

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

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


    from 89bd1b2  minor refactor: remove small amount of duplication
     new 6361907  Tweak the logic of finding default imports and remove the redundant test code
     new 04199c6  Specify the parent classloader of `GroovyClassLoader` to find default imports

The 2 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:
 .../org/codehaus/groovy/vmplugin/v9/Java9.java     | 29 +++++++++++-----
 .../groovy/vmplugin/v9/ClassFinderTest.groovy      | 40 ----------------------
 2 files changed, 21 insertions(+), 48 deletions(-)


[groovy] 01/02: Tweak the logic of finding default imports and remove the redundant test code

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

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

commit 63619072c5284dd6b1bc72e9eab29ab52a625f42
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sun Feb 23 12:45:52 2020 +0800

    Tweak the logic of finding default imports and remove the redundant test code
    
    (cherry picked from commit 98b436016f31e9c47f3a6dc88ac834634761cffb)
---
 .../org/codehaus/groovy/vmplugin/v9/Java9.java     | 29 +++++++++++-----
 .../groovy/vmplugin/v9/ClassFinderTest.groovy      | 40 ----------------------
 2 files changed, 21 insertions(+), 48 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/vmplugin/v9/Java9.java b/src/main/java/org/codehaus/groovy/vmplugin/v9/Java9.java
index 60b354d..27c8ad6 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/v9/Java9.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/v9/Java9.java
@@ -18,8 +18,8 @@
  */
 package org.codehaus.groovy.vmplugin.v9;
 
+import groovy.lang.GroovyClassLoader;
 import groovy.lang.GroovyRuntimeException;
-import groovy.lang.GroovySystem;
 import groovy.lang.MetaClass;
 import groovy.lang.MetaMethod;
 import groovy.lang.Tuple;
@@ -54,16 +54,18 @@ import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 import java.util.stream.Collectors;
 
 /**
  * Additional Java 9 based functions will be added here as needed.
  */
 public class Java9 extends Java8 {
+    private static final Logger LOGGER = Logger.getLogger(Java9.class.getName());
+
     @Override
     public Map<String, Set<String>> getDefaultImportClasses(String[] packageNames) {
-        Map<String, Set<String>> result = new LinkedHashMap<>();
-
         List<String> javaPns = new ArrayList<>(4);
         List<String> groovyPns = new ArrayList<>(4);
         for (String prefix : packageNames) {
@@ -78,13 +80,24 @@ public class Java9 extends Java8 {
             }
         }
 
-        result.putAll(doFindClasses(URI.create("jrt:/modules/java.base/"), "java", javaPns));
-
+        Map<String, Set<String>> result = new LinkedHashMap<>(2048);
         try {
-            URI gsLocation = DefaultGroovyMethods.getLocation(GroovySystem.class).toURI();
+            result.putAll(doFindClasses(URI.create("jrt:/modules/java.base/"), "java", javaPns));
+
+            GroovyClassLoader gcl = new GroovyClassLoader();
+            URI gsLocation = DefaultGroovyMethods.getLocation(gcl.loadClass("groovy.lang.GroovySystem")).toURI();
             result.putAll(doFindClasses(gsLocation, "groovy", groovyPns));
-        } catch (Exception e) {
-            System.err.println("[WARNING] Failed to get default imported groovy classes: " + e.getMessage());
+
+            // in production environment, groovy-core classes, e.g. `GroovySystem`(java class) and `GrapeIvy`(groovy class) are all packaged in the groovy-core jar file,
+            // but in Groovy development environment, groovy-core classes are distributed in different directories
+            URI giLocation = DefaultGroovyMethods.getLocation(gcl.loadClass("groovy.grape.GrapeIvy")).toURI();
+            if (!gsLocation.equals(giLocation)) {
+                result.putAll(doFindClasses(giLocation, "groovy", groovyPns));
+            }
+        } catch (Exception ignore) {
+            if (LOGGER.isLoggable(Level.FINEST)) {
+                LOGGER.finest("Failed to find default imported classes:\n" + DefaultGroovyMethods.asString(ignore));
+            }
         }
 
         return result;
diff --git a/src/test/org/codehaus/groovy/vmplugin/v9/ClassFinderTest.groovy b/src/test/org/codehaus/groovy/vmplugin/v9/ClassFinderTest.groovy
index 37ee4b0..953bd1a 100644
--- a/src/test/org/codehaus/groovy/vmplugin/v9/ClassFinderTest.groovy
+++ b/src/test/org/codehaus/groovy/vmplugin/v9/ClassFinderTest.groovy
@@ -19,7 +19,6 @@
 package org.codehaus.groovy.vmplugin.v9
 
 import org.codehaus.groovy.control.ResolveVisitor
-import org.codehaus.groovy.runtime.DefaultGroovyMethods
 import org.codehaus.groovy.vmplugin.VMPluginFactory
 import org.junit.Test
 
@@ -102,44 +101,5 @@ class ClassFinderTest {
         Map<String, Set<String>> r1 = VMPluginFactory.getPlugin().getDefaultImportClasses(ResolveVisitor.DEFAULT_IMPORTS) as TreeMap<String, Set<String>>
 
         assert (ResolveVisitor.DEFAULT_IMPORTS as List).sort() == r1.values().stream().flatMap(e -> e.stream()).collect(Collectors.toSet()).sort()
-
-        def r2 = [:] as TreeMap
-        URI gsLocation = null;
-        try {
-            gsLocation = DefaultGroovyMethods.getLocation(GroovySystem.class).toURI();
-        } catch (URISyntaxException e) {
-            e.printStackTrace();
-        }
-
-        for (String prefix : ResolveVisitor.DEFAULT_IMPORTS) {
-            String pn = prefix.substring(0, prefix.length() - 1).replace('.', '/');
-
-            Map<String, Set<String>> map = Collections.emptyMap();
-            if (pn.startsWith("java/")) {
-                map = ClassFinder.find(URI.create("jrt:/modules/java.base/"), pn);
-            } else if (pn.startsWith("groovy/")) {
-                if (null != gsLocation) {
-                    map = ClassFinder.find(gsLocation, pn);
-                }
-            }
-
-            map = map.entrySet().stream()
-                    .collect(
-                            Collectors.toMap(
-                                    (Map.Entry entry) -> entry.getKey(),
-                                    (Map.Entry entry) -> entry.getValue().stream()
-                                            .map(e -> e.replace('/', '.') + ".")
-                                            .collect(Collectors.toSet())
-                            )
-                    );
-
-            r2.putAll(map);
-        }
-
-        assert r1.size() == r2.size()
-        assert r1.keySet() == r2.keySet()
-        r1.keySet().each { c1 ->
-            assert (r1.get(c1) as TreeSet) == (r2.get(c1) as TreeSet)
-        }
     }
 }


[groovy] 02/02: Specify the parent classloader of `GroovyClassLoader` to find default imports

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

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

commit 04199c6aeb8b0fcf1a1cbc5b9843a16082bf1f61
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sun Feb 23 12:52:09 2020 +0800

    Specify the parent classloader of `GroovyClassLoader` to find default imports
    
    (cherry picked from commit 38f119d9cdabd7555b9d4a3b4bb2ed9d48ddc785)
---
 src/main/java/org/codehaus/groovy/vmplugin/v9/Java9.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/main/java/org/codehaus/groovy/vmplugin/v9/Java9.java b/src/main/java/org/codehaus/groovy/vmplugin/v9/Java9.java
index 27c8ad6..1a87b8f 100644
--- a/src/main/java/org/codehaus/groovy/vmplugin/v9/Java9.java
+++ b/src/main/java/org/codehaus/groovy/vmplugin/v9/Java9.java
@@ -84,7 +84,7 @@ public class Java9 extends Java8 {
         try {
             result.putAll(doFindClasses(URI.create("jrt:/modules/java.base/"), "java", javaPns));
 
-            GroovyClassLoader gcl = new GroovyClassLoader();
+            GroovyClassLoader gcl = new GroovyClassLoader(this.getClass().getClassLoader());
             URI gsLocation = DefaultGroovyMethods.getLocation(gcl.loadClass("groovy.lang.GroovySystem")).toURI();
             result.putAll(doFindClasses(gsLocation, "groovy", groovyPns));