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 50103d5348f..d46827ebadf 100644 --- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java +++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java @@ -1401,7 +1401,7 @@ protected static boolean typeCheckMethodsWithGenerics(final ClassNode receiver, return typeCheckMethodsWithGenerics(receiver.getGenericsTypes()[0].getType(), argumentTypes, candidateMethod); } - return typeCheckMethodsWithGenerics(receiver, argumentTypes, candidateMethod, false); + return typeCheckMethodsWithGenerics(StaticTypeCheckingVisitor.wrapTypeIfNecessary(receiver), argumentTypes, candidateMethod, false); } private static boolean typeCheckMethodsWithGenerics(final ClassNode receiver, final ClassNode[] argumentTypes, final MethodNode candidateMethod, final boolean isExtensionMethod) { diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java index 988cd5d6e8c..f13b9a36446 100644 --- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java +++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java @@ -5845,7 +5845,7 @@ protected boolean typeCheckMethodsWithGenericsOrFail(final ClassNode receiver, f } } - addStaticTypeError("Cannot call " + (mgt == null ? "" : GenericsUtils.toGenericTypesString(mgt)) + receiver.toString(false) + "#" + + addStaticTypeError("Cannot call " + (mgt == null ? "" : GenericsUtils.toGenericTypesString(mgt)) + prettyPrintTypeName(wrapTypeIfNecessary(receiver)) + "#" + toMethodParametersString(candidateMethod.getName(), paramTypes) + " with arguments " + formatArgumentList(arguments), location); return false; } @@ -5899,6 +5899,8 @@ protected void addStaticTypeError(final String msg, final ASTNode expr) { protected void addNoMatchingMethodError(ClassNode receiver, final String name, final ClassNode[] args, final Expression call) { if (isClassClassNodeWrappingConcreteType(receiver)) { receiver = receiver.getGenericsTypes()[0].getType(); + } else { + receiver = wrapTypeIfNecessary(receiver); } addStaticTypeError("Cannot find matching method " + prettyPrintTypeName(receiver) + "#" + toMethodParametersString(name, args) + ". Please check if the declared type is correct and if the method exists.", call); } diff --git a/src/test/groovy/bugs/Groovy8609Bug.groovy b/src/test/groovy/bugs/Groovy8609Bug.groovy index f30f9580853..d0900fa5bab 100644 --- a/src/test/groovy/bugs/Groovy8609Bug.groovy +++ b/src/test/groovy/bugs/Groovy8609Bug.groovy @@ -98,7 +98,7 @@ final class Groovy8609Bug extends GroovyTestCase { } } ''' - assert err.contains('Cannot call A #getFirstRecord(java.util.ArrayList ) with arguments [java.util.ArrayList ]') + assert err.contains('Cannot call A#getFirstRecord(java.util.ArrayList ) with arguments [java.util.ArrayList ]') } void testUpperBoundWithGenericsThroughWrongType2() { @@ -118,7 +118,7 @@ final class Groovy8609Bug extends GroovyTestCase { } } ''' - assert err.contains('Cannot call A #getFirstRecord(java.util.ArrayList ) with arguments [java.util.ArrayList ]') + assert err.contains('Cannot call A#getFirstRecord(java.util.ArrayList ) with arguments [java.util.ArrayList ]') } void testUpperBoundWithGenericsThroughWrongType3() { @@ -138,6 +138,6 @@ final class Groovy8609Bug extends GroovyTestCase { } } ''' - assert err.contains('Cannot call A #getFirstRecord(java.util.ArrayList ) with arguments [java.util.ArrayList ]') + assert err.contains('Cannot call A#getFirstRecord(java.util.ArrayList ) with arguments [java.util.ArrayList ]') } } diff --git a/src/test/groovy/transform/stc/BugsSTCTest.groovy b/src/test/groovy/transform/stc/BugsSTCTest.groovy index 2b5c4b44669..1c2260a74da 100644 --- a/src/test/groovy/transform/stc/BugsSTCTest.groovy +++ b/src/test/groovy/transform/stc/BugsSTCTest.groovy @@ -30,61 +30,71 @@ class BugsSTCTest extends StaticTypeCheckingTestCase { shouldFailWithMessages ''' def foo(Closure cls) {} def bar() { foo { it / 2 } } - ''', 'Cannot find matching method java.lang.Object#div(int)' + ''', + 'Cannot find matching method java.lang.Object#div(int)' } void testShouldNotAllowDivBynUntypedVariable() { shouldFailWithMessages ''' def foo(Closure cls) {} def bar() { foo { 2 / it } } - ''', 'Cannot find matching method int#div(java.lang.Object)' + ''', + 'Cannot find matching method java.lang.Integer#div(java.lang.Object)' } void testShouldNotAllowModOnUntypedVariable() { shouldFailWithMessages ''' def foo(Closure cls) {} def bar() { foo { it % 2 } } - ''', 'Cannot find matching method java.lang.Object#mod(int)' + ''', + 'Cannot find matching method java.lang.Object#mod(int)' } void testShouldNotAllowModBynUntypedVariable() { shouldFailWithMessages ''' def foo(Closure cls) {} def bar() { foo { 2 % it } } - ''', 'Cannot find matching method int#mod(java.lang.Object)' + ''', + 'Cannot find matching method java.lang.Integer#mod(java.lang.Object)' } void testShouldNotAllowMulOnUntypedVariable() { shouldFailWithMessages ''' def foo(Closure cls) {} def bar() { foo { it * 2 } } - ''', 'Cannot find matching method java.lang.Object#multiply(int)' + ''', + 'Cannot find matching method java.lang.Object#multiply(int)' } void testShouldNotAllowMulBynUntypedVariable() { shouldFailWithMessages ''' def foo(Closure cls) {} def bar() { foo { 2 * it } } - ''', 'Cannot find matching method int#multiply(java.lang.Object)' + ''', + 'Cannot find matching method java.lang.Integer#multiply(java.lang.Object)' } void testShouldNotAllowPlusOnUntypedVariable() { shouldFailWithMessages ''' def foo(Closure cls) {} def bar() { foo { it + 2 } } - ''', 'Cannot find matching method java.lang.Object#plus(int)' + ''', + 'Cannot find matching method java.lang.Object#plus(int)' } void testShouldNotAllowPlusWithUntypedVariable() { shouldFailWithMessages ''' def foo(Closure cls) {} def bar() { foo { 2 + it } } - ''', 'Cannot find matching method int#plus(java.lang.Object)' + ''', + 'Cannot find matching method java.lang.Integer#plus(java.lang.Object)' } void testShouldNotAllowMinusOnUntypedVariable() { shouldFailWithMessages ''' def foo(Closure cls) {} def bar() { foo { it - 2 } } - ''', 'Cannot find matching method java.lang.Object#minus(int)' + ''', + 'Cannot find matching method java.lang.Object#minus(int)' } void testShouldNotAllowMinusByUntypedVariable() { shouldFailWithMessages ''' def foo(Closure cls) {} def bar() { foo { 2 - it } } - ''', 'Cannot find matching method int#minus(java.lang.Object)' + ''', + 'Cannot find matching method java.lang.Integer#minus(java.lang.Object)' } // GROOVY-7929 @@ -100,7 +110,8 @@ class BugsSTCTest extends StaticTypeCheckingTestCase { x() } } - ''', 'Cannot find matching method #x' + ''', + 'Cannot find matching method #x' } // GROOVY-10102 @@ -204,7 +215,8 @@ class BugsSTCTest extends StaticTypeCheckingTestCase { L items = ['foo', 'bar'] as L items.removeIf({a, b -> 1} as Comparator) assert items - ''', 'Cannot call L #removeIf(java.util.Comparator ) with arguments [java.util.Comparator ]' + ''', + 'Cannot call L#removeIf(java.util.Comparator ) with arguments [java.util.Comparator ]' } void testGroovy5482ListsAndFlowTyping() { diff --git a/src/test/groovy/transform/stc/DefaultGroovyMethodsSTCTest.groovy b/src/test/groovy/transform/stc/DefaultGroovyMethodsSTCTest.groovy index 9385e957288..f351707f942 100644 --- a/src/test/groovy/transform/stc/DefaultGroovyMethodsSTCTest.groovy +++ b/src/test/groovy/transform/stc/DefaultGroovyMethodsSTCTest.groovy @@ -114,8 +114,8 @@ class DefaultGroovyMethodsSTCTest extends StaticTypeCheckingTestCase { numbers.sequence numbers.string ''', - 'Cannot call java.util.ArrayList #getSequence() with arguments []', - 'Cannot call java.util.ArrayList #getString() with arguments []', + 'Cannot call java.util.ArrayList#getSequence() with arguments []', + 'Cannot call java.util.ArrayList#getString() with arguments []', 'No such property: sequence for class: java.util.ArrayList', 'No such property: string for class: java.util.ArrayList' } diff --git a/src/test/groovy/transform/stc/GenericsSTCTest.groovy b/src/test/groovy/transform/stc/GenericsSTCTest.groovy index 874760f3cbd..68e6f23d7bb 100644 --- a/src/test/groovy/transform/stc/GenericsSTCTest.groovy +++ b/src/test/groovy/transform/stc/GenericsSTCTest.groovy @@ -86,7 +86,7 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase { List list = [] list << 1 ''', - 'Cannot call java.util.ArrayList #leftShift(T) with arguments [int]' + 'Cannot call java.util.ArrayList#leftShift(T) with arguments [int]' } void testAddOnList2UsingLeftShift() { @@ -143,7 +143,7 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase { List list = new LinkedList<>() list << 'Hello' ''', - 'Cannot call java.util.LinkedList #leftShift(T) with arguments [java.lang.String]' + 'Cannot call java.util.LinkedList#leftShift(T) with arguments [java.lang.String]' } void testAddOnListWithDiamondAndNullUsingLeftShift() { @@ -153,6 +153,21 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase { ''' } + void testCompareToOnPrimitive() { + assertScript ''' + int a = 1 + assert (a < 2) + assert !(a < 1) + assert (a <= 1) + ''' + // GROOVY-11383 + shouldFailWithMessages ''' + int a = 42 + def b = (a < new Object()) + ''', + 'Cannot find matching method java.lang.Integer#compareTo(java.lang.Object)' + } + void testReturnTypeInference1() { assertScript ''' class Foo { @@ -1941,7 +1956,7 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase { shouldFailWithMessages ''' List list = new LinkedList([1,2,3]) ''', - 'Cannot call java.util.LinkedList #(java.util.Collection ) with arguments [java.util.ArrayList ]' + 'Cannot call java.util.LinkedList#(java.util.Collection ) with arguments [java.util.ArrayList ]' } void testCompatibleGenericAssignmentWithInference() { @@ -2336,7 +2351,7 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase { A a = new B() a.test() ''', - 'Cannot call A #(java.lang.Class , java.lang.Class ) with arguments [java.lang.Class , java.lang.Class ]' + 'Cannot call A#(java.lang.Class , java.lang.Class ) with arguments [java.lang.Class , java.lang.Class ]' } void testConstructorCallWithClassParameterUsingClassLiteralArg() { @@ -2392,13 +2407,13 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase { def map = new HashMap() map[123] = new Object() ''', - 'Cannot call java.util.HashMap #putAt(java.lang.Integer, java.lang.Integer) with arguments [int, java.lang.Object]' + 'Cannot call java.util.HashMap#putAt(java.lang.Integer, java.lang.Integer) with arguments [int, java.lang.Object]' shouldFailWithMessages ''' def map = new HashMap() map['x'] = new Object() ''', - 'Cannot call java.util.HashMap #putAt(java.lang.String, java.lang.Integer) with arguments [java.lang.String, java.lang.Object]' + 'Cannot call java.util.HashMap#putAt(java.lang.String, java.lang.Integer) with arguments [java.lang.String, java.lang.Object]' } // GROOVY-9069 @@ -2414,8 +2429,8 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase { } } ''', - 'Cannot call java.util.Map #put(java.lang.String, java.util.Map ) with arguments [java.lang.String, java.util.LinkedHashMap ]', - 'Cannot call java.util.Map #putAt(java.lang.String, java.util.Map ) with arguments [java.lang.String, java.util.LinkedHashMap ]' + 'Cannot call java.util.Map#put(java.lang.String, java.util.Map ) with arguments [java.lang.String, java.util.LinkedHashMap ]', + 'Cannot call java.util.Map#putAt(java.lang.String, java.util.Map ) with arguments [java.lang.String, java.util.LinkedHashMap ]' assertScript ''' void test(Map>> maps) { @@ -2559,7 +2574,7 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase { } } ''', - 'Cannot call groovy.transform.stc.GenericsSTCTest$ClassA #bar(java.lang.Class ) with arguments [java.lang.Class ]' + 'Cannot call groovy.transform.stc.GenericsSTCTest$ClassA#bar(java.lang.Class ) with arguments [java.lang.Class ]' } // GROOVY-8961, GROOVY-9915 @@ -2929,7 +2944,7 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase { Collection numbers = (Collection) [1,2,3] boolean r = list.addAll(numbers) ''', - 'Cannot call java.util.ArrayList #addAll(java.util.Collection ) with arguments [java.util.Collection ]' + 'Cannot call java.util.ArrayList#addAll(java.util.Collection ) with arguments [java.util.Collection ]' } // GROOVY-5528 @@ -3241,7 +3256,7 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase { Map map = new HashMap<>() map['foo'] = new Date() ''', - 'Cannot call java.util.HashMap #putAt(java.util.Date, java.util.Date) with arguments [java.lang.String, java.util.Date]' + 'Cannot call java.util.HashMap#putAt(java.util.Date, java.util.Date) with arguments [java.lang.String, java.util.Date]' } void testInferDiamondForAssignmentWithDatesAndIllegalValueUsingPut() { shouldFailWithMessages ''' @@ -3281,7 +3296,7 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase { Map map = new HashMap<>() map[new Date()] = 'foo' ''', - 'Cannot call java.util.HashMap #putAt(java.util.Date, java.util.Date) with arguments [java.util.Date, java.lang.String]' + 'Cannot call java.util.HashMap#putAt(java.util.Date, java.util.Date) with arguments [java.util.Date, java.lang.String]' } void testCallMethodWithParameterizedArrayList() { diff --git a/src/test/groovy/transform/stc/STCAssignmentTest.groovy b/src/test/groovy/transform/stc/STCAssignmentTest.groovy index 64539ed353d..257927f18a1 100644 --- a/src/test/groovy/transform/stc/STCAssignmentTest.groovy +++ b/src/test/groovy/transform/stc/STCAssignmentTest.groovy @@ -146,7 +146,7 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase { int i = 0 i += new Object() ''', - 'Cannot find matching method int#plus(java.lang.Object)' + 'Cannot find matching method java.lang.Integer#plus(java.lang.Object)' } void testIntMinusEqualsObject() { @@ -154,7 +154,7 @@ class STCAssignmentTest extends StaticTypeCheckingTestCase { int i = 0 i -= new Object() ''', - 'Cannot find matching method int#minus(java.lang.Object)' + 'Cannot find matching method java.lang.Integer#minus(java.lang.Object)' } void testStringPlusEqualsString() { diff --git a/src/test/groovy/transform/stc/STCnAryExpressionTest.groovy b/src/test/groovy/transform/stc/STCnAryExpressionTest.groovy index e3d85f85f35..bff8c0a82ad 100644 --- a/src/test/groovy/transform/stc/STCnAryExpressionTest.groovy +++ b/src/test/groovy/transform/stc/STCnAryExpressionTest.groovy @@ -54,7 +54,7 @@ class STCnAryExpressionTest extends StaticTypeCheckingTestCase { int num = 2 num+obj ''', - 'Cannot find matching method int#plus(java.lang.Object)' + 'Cannot find matching method java.lang.Integer#plus(java.lang.Object)' } void testPrimitiveComparison() { diff --git a/src/test/groovy/transform/stc/TypeCheckingExtensionsTest.groovy b/src/test/groovy/transform/stc/TypeCheckingExtensionsTest.groovy index b82274b65c1..f9515f068eb 100644 --- a/src/test/groovy/transform/stc/TypeCheckingExtensionsTest.groovy +++ b/src/test/groovy/transform/stc/TypeCheckingExtensionsTest.groovy @@ -345,7 +345,7 @@ class TypeCheckingExtensionsTest extends StaticTypeCheckingTestCase { Date y = new Date() x+y ''', - 'Cannot find matching method int#plus(java.util.Date)' + 'Cannot find matching method java.lang.Integer#plus(java.util.Date)' extension = 'groovy/transform/stc/BinaryOperatorTestExtension.groovy' assertScript ''' @@ -364,7 +364,7 @@ class TypeCheckingExtensionsTest extends StaticTypeCheckingTestCase { Date y = new Date() x << y ''', - 'Cannot find matching method int#leftShift(java.util.Date)' + 'Cannot find matching method java.lang.Integer#leftShift(java.util.Date)' extension = 'groovy/transform/stc/BinaryOperatorTestExtension.groovy' assertScript ''' diff --git a/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy b/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy index 786b230199a..4530535d1ca 100644 --- a/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy +++ b/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy @@ -893,7 +893,7 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase { method().toUpperCase() } ''', - 'Cannot find matching method int#toUpperCase()' + 'Cannot find matching method java.lang.Integer#toUpperCase()' } void testDeclarationTypeInference() { diff --git a/src/test/org/codehaus/groovy/transform/InheritConstructorsTransformTest.groovy b/src/test/org/codehaus/groovy/transform/InheritConstructorsTransformTest.groovy index 91f0caa5474..7e48e9721c2 100644 --- a/src/test/org/codehaus/groovy/transform/InheritConstructorsTransformTest.groovy +++ b/src/test/org/codehaus/groovy/transform/InheritConstructorsTransformTest.groovy @@ -224,7 +224,7 @@ class InheritConstructorsTransformTest extends GroovyShellTestCase { assert op.toString() == '3|DOWN' ''' assert message.contains('Cannot find matching method OrderPublisher#(java.util.Date)') - assert message.contains('Cannot call OrderPublisher #(java.util.Set ) with arguments [java.util.HashSet ]') + assert message.contains('Cannot call OrderPublisher#(java.util.Set ) with arguments [java.util.HashSet ]') } // GROOVY-9323