Skip to content

Commit

Permalink
GROOVY-11385: STC: Type::new for abstract class is impossible
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jun 20, 2024
1 parent 2e969a1 commit 7d01a83
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
29 changes: 29 additions & 0 deletions src/test/groovy/transform/stc/MethodReferenceTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, A> f = A::new
f.apply("") // InstantiationException
}
test()
'''
assert err =~ /Cannot instantiate the type A/

for (op in ['::','.&']) {
err = shouldFail imports + """
@CompileStatic
void test() {
Supplier<Number> s = Number${op}new
}
test()
"""
assert err =~ /Cannot instantiate the type java.lang.Number/
}
}

@Test // arrayClass::new
void testIntFunctionCN() {
assertScript imports + '''
Expand Down

0 comments on commit 7d01a83

Please sign in to comment.