Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_6_X 00cc9e5e5 -> a7e5722be


GROOVY-8545: DGM#intersect(Set, Iterable) throws ClassCastException if Iterable 
is larger than Set (closes #645)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/01449f0e
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/01449f0e
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/01449f0e

Branch: refs/heads/GROOVY_2_6_X
Commit: 01449f0e6b2ac72f1bab14ff296c7f38b886a3f0
Parents: 00cc9e5
Author: Hugues Lerebours <h.lerebo...@criteo.com>
Authored: Tue Dec 19 18:37:56 2017 +0100
Committer: Paul King <pa...@asert.com.au>
Committed: Mon Apr 16 15:35:46 2018 +1000

----------------------------------------------------------------------
 .../groovy/runtime/DefaultGroovyMethods.java         | 10 ++--------
 src/test/groovy/GroovyMethodsTest.groovy             | 15 ++++++++++++---
 .../shell/completion/ImportsSyntaxCompletor.groovy   |  6 +++---
 3 files changed, 17 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/01449f0e/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java 
b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 07b7c25..a52abf9 100644
--- a/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -12224,18 +12224,12 @@ public class DefaultGroovyMethods extends 
DefaultGroovyMethodsSupport {
         if (left.isEmpty() || right.isEmpty())
             return createSimilarCollection(left, 0);
 
-        if (left.size() < right.size()) {
-            Collection<T> swaptemp = left;
-            left = right;
-            right = swaptemp;
-        }
-
         // TODO optimise if same type?
         // boolean nlgnSort = sameType(new Collection[]{left, right});
 
-        Collection<T> result = createSimilarCollection(left, left.size());
+        Collection<T> result = createSimilarCollection(left, 
Math.min(left.size(), right.size()));
         //creates the collection to look for values.
-        Collection<T> pickFrom = new TreeSet<T>(new 
NumberAwareComparator<T>());
+        Collection<T> pickFrom = new TreeSet<>(new NumberAwareComparator<>());
         pickFrom.addAll(left);
 
         for (final T t : right) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/01449f0e/src/test/groovy/GroovyMethodsTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/GroovyMethodsTest.groovy 
b/src/test/groovy/GroovyMethodsTest.groovy
index 7f550af..903ea02 100644
--- a/src/test/groovy/GroovyMethodsTest.groovy
+++ b/src/test/groovy/GroovyMethodsTest.groovy
@@ -809,10 +809,10 @@ class GroovyMethodsTest extends GroovyTestCase {
     }
 
     void doIt(col) {
-        col.clear();
-        col.addAll(leftCol);
+        col.clear()
+        col.addAll(leftCol)
         // not really concerned about  correctness, rather that the method can 
be called, however..
-        assert col.intersect(rightCol) == ["2"]
+        assert col.intersect(rightCol) as List == ["2"]
     }
 
     void testFileWithReader() {
@@ -1565,6 +1565,15 @@ class GroovyMethodsTest extends GroovyTestCase {
         assert [4, 5] == iterableA.intersect(iterableB)
     }
 
+    void testIntersectForSets() {
+        assert [].toSet() == [].toSet().intersect([] as Iterable)
+        assert [].toSet() == [].toSet().intersect([1, 2, 3] as Iterable)
+        assert [].toSet() == [1, 2, 3].toSet().intersect([] as Iterable)
+        assert [2, 3].toSet() == [2, 3, 4].toSet().intersect([1, 2, 3] as 
Iterable)
+        assert [2, 3, 4].toSet() == [2, 3, 4].toSet().intersect([1, 2, 3, 4] 
as Iterable)
+        assert [2, 3].toSet() == [2, 3, 4, 5].toSet().intersect([1, 2, 3] as 
Iterable)
+    }
+
     void testIntersectForMaps() {
         // GROOVY-7602
         def list1 = [[language: 'Java'], [language: 'Groovy'], [language: 
'Scala']]

http://git-wip-us.apache.org/repos/asf/groovy/blob/01449f0e/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/completion/ImportsSyntaxCompletor.groovy
----------------------------------------------------------------------
diff --git 
a/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/completion/ImportsSyntaxCompletor.groovy
 
b/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/completion/ImportsSyntaxCompletor.groovy
index cb446a6..67c9b22 100644
--- 
a/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/completion/ImportsSyntaxCompletor.groovy
+++ 
b/subprojects/groovy-groovysh/src/main/groovy/org/codehaus/groovy/tools/shell/completion/ImportsSyntaxCompletor.groovy
@@ -105,12 +105,12 @@ class ImportsSyntaxCompletor implements 
IdentifierCompletor {
                 Class clazz = shell.interp.evaluate([className]) as Class
                 if (clazz != null) {
                     List<String> clazzSymbols = 
ReflectionCompletor.getPublicFieldsAndMethods(clazz, '')*.value
-                    List<String> importedSymbols;
+                    Collection<String> importedSymbols
                     if (symbolName == '*') {
-                        importedSymbols = clazzSymbols;
+                        importedSymbols = clazzSymbols
                     } else {
                         Set<String> acceptableMatches = [symbolName, 
symbolName + '(', symbolName + '()']
-                        importedSymbols = 
acceptableMatches.intersect(clazzSymbols)
+                        importedSymbols = (acceptableMatches as 
Collection).intersect(clazzSymbols)
                     }
                     matches.addAll(importedSymbols)
                 }

Reply via email to