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 f13b9a36446..13b2ef33f4f 100644 --- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java +++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java @@ -2517,6 +2517,10 @@ && getType(nameExpr).equals(STRING_TYPE)) { if (isClassClassNodeWrappingConcreteType(type)){ type = type.getGenericsTypes()[0].getType(); storeType(expression,wrapClosureType(type)); + // GROOVY-11385: check if create possible + if (type.isAbstract() && !type.isArray()) + addStaticTypeError("Cannot instantiate the type " + + prettyPrintTypeName(type), expression); } return; } diff --git a/src/test/groovy/transform/stc/MethodReferenceTest.groovy b/src/test/groovy/transform/stc/MethodReferenceTest.groovy index 501aea0ff86..f9c8735187a 100644 --- a/src/test/groovy/transform/stc/MethodReferenceTest.groovy +++ b/src/test/groovy/transform/stc/MethodReferenceTest.groovy @@ -1034,6 +1034,35 @@ final class MethodReferenceTest { ''' } + @Test // class::new -- GROOVY-11385 + void testFunctionCN8() { + def err = shouldFail imports + ''' + abstract class A { + A(String s) {} + } + @CompileStatic + void test() { + Function f = A::new + f.apply("") // InstantiationException + } + + test() + ''' + assert err =~ /Cannot instantiate the type A/ + + for (op in ['::','.&']) { + err = shouldFail imports + """ + @CompileStatic + void test() { + Supplier s = Number${op}new + } + + test() + """ + assert err =~ /Cannot instantiate the type java.lang.Number/ + } + } + @Test // arrayClass::new void testIntFunctionCN() { assertScript imports + '''