IDEA-91483 infer type of closure parameter if closure is casted to interface

This commit is contained in:
Max Medvedev
2012-09-14 15:08:31 +04:00
parent def999a4e2
commit ac72d80207
5 changed files with 97 additions and 2 deletions
+1
View File
@@ -127,6 +127,7 @@
<psiEnhancerCategory implementation="org.jetbrains.plugins.groovy.dsl.psi.PsiExpressionCategory"/>
<variableEnhancer implementation="org.jetbrains.plugins.groovy.lang.psi.typeEnhancers.ClosureParameterEnhancer"/>
<variableEnhancer implementation="org.jetbrains.plugins.groovy.lang.psi.typeEnhancers.ClosureAsAnonymousParameterEnhancer"/>
<membersContributor implementation="org.jetbrains.plugins.groovy.gant.GantMemberContributor"/>
@@ -114,7 +114,7 @@ public class GppClosureParameterTypeProvider extends AbstractClosureParameterEnh
}
@Nullable
private static PsiType getSingleMethodParameterType(@Nullable PsiType type, int index, GrClosableBlock closure) {
public static PsiType getSingleMethodParameterType(@Nullable PsiType type, int index, GrClosableBlock closure) {
final PsiType[] signature = findSingleAbstractMethodSignature(type);
if (signature != null && GrClosureSignatureUtil.isSignatureApplicable(GrClosureSignatureUtil.createSignature(closure), signature, closure)) {
return signature.length > index ? signature[index] : PsiType.NULL;
@@ -76,7 +76,7 @@ public class GppTypeConverter extends GrTypeConverter {
return true;
}
}
else if (rType instanceof GrClosureType) {
else if (rType instanceof GrClosureType && hasTypedContext(context)) {
final PsiType[] methodParameters = GppClosureParameterTypeProvider.findSingleAbstractMethodSignature(lType);
if (isClosureOverride(methodParameters, (GrClosureType)rType, context)) return true;
}
@@ -0,0 +1,75 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.plugins.groovy.lang.psi.typeEnhancers;
import com.intellij.psi.PsiManager;
import com.intellij.psi.PsiType;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.gpp.GppClosureParameterTypeProvider;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression;
import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement;
import org.jetbrains.plugins.groovy.lang.psi.expectedTypes.GroovyExpectedTypesProvider;
import org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.TypesUtil;
import java.util.HashSet;
import java.util.Set;
/**
* @author Max Medvedev
*/
public class ClosureAsAnonymousParameterEnhancer extends AbstractClosureParameterEnhancer {
@Nullable
@Override
protected PsiType getClosureParameterType(GrClosableBlock closure, int index) {
Set<PsiType> expectedTypes;
if (closure.getParent() instanceof GrSafeCastExpression) {
GrSafeCastExpression safeCastExpression = (GrSafeCastExpression)closure.getParent();
GrTypeElement typeElement = safeCastExpression.getCastTypeElement();
if (typeElement != null) {
PsiType castType = typeElement.getType();
expectedTypes = new HashSet<PsiType>(GroovyExpectedTypesProvider.getDefaultExpectedTypes(safeCastExpression));
PsiManager manager = closure.getManager();
GlobalSearchScope scope = closure.getResolveScope();
for (PsiType expected : expectedTypes) {
if (!TypesUtil.isAssignable(expected, castType, manager, scope)) {
expectedTypes.remove(expected);
}
}
if (expectedTypes.isEmpty()) expectedTypes.add(castType);
}
else {
expectedTypes = GroovyExpectedTypesProvider.getDefaultExpectedTypes(closure);
}
}
else {
expectedTypes = GroovyExpectedTypesProvider.getDefaultExpectedTypes(closure);
}
for (PsiType constraint : expectedTypes) {
final PsiType suggestion = GppClosureParameterTypeProvider.getSingleMethodParameterType(constraint, index, closure);
if (suggestion != null) {
return suggestion;
}
}
return null;
}
}
@@ -1452,6 +1452,25 @@ List<String> foo() {[]}
def (int <warning descr="Cannot assign 'String' to 'int'">x</warning>, String y) = foo()
List<String> foo() {[]}
''', GroovyAssignabilityCheckInspection)
}
void testCastClosureToInterface() {
testHighlighting('''\
interface Function<D, F> {
F fun(D d)
}
def foo(Function<String, String> function) {
// print function.fun('abc')
}
foo<warning descr="'foo' in '_' cannot be applied to '(Function<java.lang.Double,java.lang.Double>)'">({println it.byteValue()} as Function<Double, Double>)</warning>
foo({println it.substring(1)} as Function)
foo({println it.substring(1)} as Function<String, String>)
foo<warning descr="'foo' in '_' cannot be applied to '(groovy.lang.Closure<java.lang.Void>)'">({println it})</warning>
''', GroovyAssignabilityCheckInspection)
}