Skip to content

Commit

Permalink
Fix for #1527: use first param type as object type for Type::nonStatic
Browse files Browse the repository at this point in the history
GROOVY-10734, GROOVY-11259
  • Loading branch information
eric-milles committed Jan 1, 2024
1 parent 742a3cc commit dca052d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2023 the original author or authors.
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -1102,6 +1102,27 @@ public void testClosure24() {
}
}

@Test // https://github.com/groovy/groovy-eclipse/issues/1527
public void testClosure25() {
String contents =
"void test(Map<String, String> map) {\n" +
" def eSet = map.entrySet()\n" +
" def eStr = eSet.stream()\n" +
" def kStr = eStr.map(Map.Entry.&getKey)\n" +
" def kSet = kStr.toSet()\n" +
"}\n";
assertType(contents, "eSet", "java.util.Set<java.util.Map$Entry<java.lang.String,java.lang.String>>");
assertType(contents, "eStr", "java.util.stream.Stream<java.util.Map$Entry<java.lang.String,java.lang.String>>");
assertType(contents, "kStr", "java.util.stream.Stream<java.lang.String>"); // GROOVY-11259: not java.lang.Object
assertType(contents, "kSet", "java.util.Set<java.lang.String>");

contents = contents.replace("Map.Entry.&getKey", isParrotParser() ? "e -> e.key" : "{e -> e.key}");
assertType(contents, "eSet", "java.util.Set<java.util.Map$Entry<java.lang.String,java.lang.String>>");
assertType(contents, "eStr", "java.util.stream.Stream<java.util.Map$Entry<java.lang.String,java.lang.String>>");
assertType(contents, "kStr", "java.util.stream.Stream<java.lang.String>");
assertType(contents, "kSet", "java.util.Set<java.lang.String>");
}

@Test
public void testArrayDGM() {
String contents =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1937,7 +1937,7 @@ static ClassNode applyGenericsContext(final Map<GenericsTypeName, GenericsType>
return applyGenericsContext(spec, type.getComponentType()).makeArray();
}

GenericsType[] gt = type.getGenericsTypes();
GenericsType[] gt = type.getGenericsTypes(); if (gt == null) return type;
if (asBoolean(spec)) {
gt = applyGenericsContext(spec, gt);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2023 the original author or authors.
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
import static java.util.stream.IntStream.range;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -198,9 +199,11 @@ public TypeLookupResult resolveTypeParameterization(final ClassNode objExprType,
GenericsMapper mapper;

if (scope.getEnclosingNode() instanceof MethodPointerExpression) {
// apply type arguments from the object expression to the referenced method
mapper = GenericsMapper.gatherGenerics(argumentTypes, objectType, method);
method = VariableScope.resolveTypeParameterization(mapper, method);
if (!isStatic && !method.isStatic()) {
// apply type arguments from the object expression to the referenced method
mapper = GenericsMapper.gatherGenerics(argumentTypes, objectType, method);
method = VariableScope.resolveTypeParameterization(mapper, method);
}

// check for Closure or SAM-type coercion of the method pointer/reference
VariableScope.CallAndType cat = scope.getEnclosingMethodCallExpression();
Expand Down Expand Up @@ -236,10 +239,13 @@ public TypeLookupResult resolveTypeParameterization(final ClassNode objExprType,
mapper = GenericsMapper.gatherGenerics(singletonList(returnType), declaringType, returnTypeStub(method));
method = VariableScope.resolveTypeParameterization(mapper, method);
} else {
MethodNode sam = ClassHelper.findSAM(targetType);
if (sam != null) {
if (ClassHelper.isSAMType(targetType)) {
ClassNode[] pt = GenericsUtils.parameterizeSAM(targetType).getV1();
if (isStatic && !method.isStatic()) { // GROOVY-10734, GROOVY-11259
objectType = pt[0]; pt = Arrays.copyOfRange(pt, 1, pt.length);
}
// use parameter types of SAM as "argument types" for referenced method to help resolve type parameters
mapper = GenericsMapper.gatherGenerics(GroovyUtils.getParameterTypes(sam.getParameters()), declaringType, method);
mapper = GenericsMapper.gatherGenerics(Arrays.asList(pt), objectType, method);
method = VariableScope.resolveTypeParameterization(mapper, method);

mapper = GenericsMapper.gatherGenerics(targetType);
Expand Down

0 comments on commit dca052d

Please sign in to comment.