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 2020/11/20 22:40:55 UTC
[groovy] 01/02: fix AbstractMap#put(K,
V) to resolve generics just like HashMap#put(K, V)
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY-6882
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 4bc126e5cbf3d11c7f17d917da4811156822f6b3
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Nov 20 15:15:08 2020 -0600
fix AbstractMap#put(K,V) to resolve generics just like HashMap#put(K,V)
- chooseBestMethod rejects HashMap#put(K,V) but not AbstractMap#put(K,V)
---
.../transform/stc/StaticTypeCheckingSupport.java | 2 +-
.../groovy/transform/stc/GenericsSTCTest.groovy | 28 +++++++++++-----------
2 files changed, 15 insertions(+), 15 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
index 488f3f6..87c0a2a 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -1038,7 +1038,7 @@ public abstract class StaticTypeCheckingSupport {
Person p = foo(b)
*/
- Map<GenericsType, GenericsType> declaringAndActualGenericsTypeMap = GenericsUtils.makeDeclaringAndActualGenericsTypeMap(declaringClassForDistance, actualReceiverForDistance);
+ Map<GenericsType, GenericsType> declaringAndActualGenericsTypeMap = GenericsUtils.makeDeclaringAndActualGenericsTypeMapOfExactType(declaringClassForDistance, actualReceiverForDistance);
Parameter[] params = makeRawTypes(safeNode.getParameters(), declaringAndActualGenericsTypeMap);
int dist = measureParametersAndArgumentsDistance(params, safeArgs);
if (dist >= 0) {
diff --git a/src/test/groovy/transform/stc/GenericsSTCTest.groovy b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
index 5db067d..469a0dc 100644
--- a/src/test/groovy/transform/stc/GenericsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
@@ -122,7 +122,7 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
shouldFailWithMessages '''
List<Integer> list = new LinkedList<>()
list.add 'Hello'
- ''', '[Static type checking] - Cannot call java.util.LinkedList <java.lang.Integer>#add(java.lang.Integer) with arguments [java.lang.String]'
+ ''', '[Static type checking] - Cannot find matching method java.util.LinkedList#add(java.lang.String). Please check if the declared type is correct and if the method exists.'
}
void testAddOnListWithDiamondAndWrongTypeUsingLeftShift() {
@@ -537,27 +537,27 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
'''
}
- void testPutMethodWithPrimitiveValue() {
+ void testPutMethodWithPrimitiveValue1() {
assertScript '''
- Map<String, Integer> map = new HashMap<String,Integer>()
+ def map = new HashMap<String, Integer>()
map.put('hello', 1)
'''
}
- void testPutMethodWithWrongValueType() {
- shouldFailWithMessages '''
- Map<String, Integer> map = new HashMap<String,Integer>()
- map.put('hello', new Object())
- ''', '[Static type checking] - Cannot call java.util.HashMap <String, Integer>#put(java.lang.String, java.lang.Integer) with arguments [java.lang.String, java.lang.Object]'
- }
-
- void testPutMethodWithPrimitiveValueAndArrayPut() {
+ void testPutMethodWithPrimitiveValue2() {
assertScript '''
- Map<String, Integer> map = new HashMap<String,Integer>()
+ def map = new HashMap<String, Integer>()
map['hello'] = 1
'''
}
+ void testPutMethodWithWrongValueType() {
+ shouldFailWithMessages '''
+ def map = new HashMap<String, Integer>()
+ map.put('hello', new Object())
+ ''', '[Static type checking] - Cannot find matching method java.util.HashMap#put(java.lang.String, java.lang.Object). Please check if the declared type is correct and if the method exists.'
+ }
+
void testShouldComplainAboutToInteger() {
String code = '''
class Test {
@@ -1020,7 +1020,7 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
})
Map<Date, Date> map = new HashMap<>()
map.put('foo', new Date())
- ''', '[Static type checking] - Cannot call java.util.HashMap <java.util.Date, java.util.Date>#put(java.util.Date, java.util.Date) with arguments [java.lang.String, java.util.Date]'
+ ''', '[Static type checking] - Cannot find matching method java.util.HashMap#put(java.lang.String, java.util.Date). Please check if the declared type is correct and if the method exists.'
}
void testInferDiamondForAssignmentWithDatesAndIllegalKeyUsingSquareBracket() {
shouldFailWithMessages '''
@@ -1058,7 +1058,7 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
})
Map<Date, Date> map = new HashMap<>()
map.put(new Date(), 'foo')
- ''', '[Static type checking] - Cannot call java.util.HashMap <java.util.Date, java.util.Date>#put(java.util.Date, java.util.Date) with arguments [java.util.Date, java.lang.String]'
+ ''', '[Static type checking] - Cannot find matching method java.util.HashMap#put(java.util.Date, java.lang.String). Please check if the declared type is correct and if the method exists.'
}
void testInferDiamondForAssignmentWithDatesAndIllegalValueUsingSquareBracket() {
shouldFailWithMessages '''