eliminateWildcards -> GenericsUtil; lambda: eliminate wildcards before formal types declaration (IDEA-90578)

This commit is contained in:
Anna Kozlova
2012-08-24 21:24:08 +04:00
parent 7b7399e020
commit cf4b4e6831
16 changed files with 144 additions and 48 deletions
@@ -129,42 +129,6 @@ public class JavaCompletionUtil {
.completeVariableNameForRefactoring(codeStyleManager, new CamelHumpMatcher(""), varType, varKind, suggestedNameInfo, true, false);
}
public static PsiType eliminateWildcards(PsiType type) {
return eliminateWildcardsInner(type, true);
}
static PsiType eliminateWildcardsInner(PsiType type, final boolean eliminateInTypeArguments) {
if (eliminateInTypeArguments && type instanceof PsiClassType) {
PsiClassType classType = ((PsiClassType)type);
JavaResolveResult resolveResult = classType.resolveGenerics();
PsiClass aClass = (PsiClass)resolveResult.getElement();
if (aClass != null) {
PsiManager manager = aClass.getManager();
PsiTypeParameter[] typeParams = aClass.getTypeParameters();
Map<PsiTypeParameter, PsiType> map = new HashMap<PsiTypeParameter, PsiType>();
for (PsiTypeParameter typeParam : typeParams) {
PsiType substituted = resolveResult.getSubstitutor().substitute(typeParam);
if (substituted instanceof PsiWildcardType) {
substituted = ((PsiWildcardType)substituted).getBound();
if (substituted == null) substituted = PsiType.getJavaLangObject(manager, aClass.getResolveScope());
}
map.put(typeParam, substituted);
}
PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
PsiSubstitutor substitutor = factory.createSubstitutor(map);
type = factory.createType(aClass, substitutor);
}
}
else if (type instanceof PsiArrayType) {
return eliminateWildcardsInner(((PsiArrayType)type).getComponentType(), false).createArrayType();
}
else if (type instanceof PsiWildcardType) {
return ((PsiWildcardType)type).getExtendsBound();
}
return type;
}
public static boolean isInExcludedPackage(@NotNull final PsiMember member, boolean allowInstanceInnerClasses) {
final String name = StaticImportMethodFix.getMemberQualifiedName(member);
if (name == null) return false;
@@ -124,7 +124,7 @@ public class JavaInheritorsGetter extends CompletionProvider<CompletionParameter
return null;
}
PsiType psiType = JavaCompletionUtil.eliminateWildcards(type);
PsiType psiType = GenericsUtil.eliminateWildcards(type);
if (JavaSmartCompletionContributor.AFTER_NEW.accepts(parameters.getOriginalPosition()) &&
PsiUtil.getLanguageLevel(parameters.getOriginalFile()).isAtLeast(LanguageLevel.JDK_1_7)) {
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(psiClass.getProject());
@@ -21,7 +21,8 @@ import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.InheritanceUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.*;
import com.intellij.util.containers.HashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -337,4 +338,42 @@ public class GenericsUtil {
}
return PsiSubstitutor.EMPTY.putAll(psiClass, result.toArray(new PsiType[result.size()]));
}
public static PsiType eliminateWildcards(PsiType type) {
return eliminateWildcardsInner(type, true);
}
static PsiType eliminateWildcardsInner(PsiType type, final boolean eliminateInTypeArguments) {
if (eliminateInTypeArguments && type instanceof PsiClassType) {
PsiClassType classType = ((PsiClassType)type);
JavaResolveResult resolveResult = classType.resolveGenerics();
PsiClass aClass = (PsiClass)resolveResult.getElement();
if (aClass != null) {
PsiManager manager = aClass.getManager();
PsiTypeParameter[] typeParams = aClass.getTypeParameters();
Map<PsiTypeParameter, PsiType> map = new HashMap<PsiTypeParameter, PsiType>();
for (PsiTypeParameter typeParam : typeParams) {
PsiType substituted = resolveResult.getSubstitutor().substitute(typeParam);
if (substituted instanceof PsiWildcardType) {
substituted = ((PsiWildcardType)substituted).getBound();
if (substituted == null) substituted = PsiType.getJavaLangObject(manager, aClass.getResolveScope());
}
map.put(typeParam, substituted);
}
PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
PsiSubstitutor substitutor = factory.createSubstitutor(map);
type = factory.createType(aClass, substitutor);
}
}
else if (type instanceof PsiArrayType) {
return eliminateWildcardsInner(((PsiArrayType)type).getComponentType(), false).createArrayType();
}
else if (type instanceof PsiWildcardType) {
final PsiType bound = ((PsiWildcardType)type).getBound();
return bound != null ? bound
: ((PsiWildcardType)type).getExtendsBound();//object
}
return type;
}
}
@@ -143,7 +143,7 @@ public class LambdaUtil {
}
if (!lambdaFormalType
.equals(resolveResult.getSubstitutor().substitute(methodSignature.getSubstitutor().substitute(methodParameterType)))) {
.isAssignableFrom(GenericsUtil.eliminateWildcards(resolveResult.getSubstitutor().substitute(methodSignature.getSubstitutor().substitute(methodParameterType))))) {
return false;
}
}
@@ -7,7 +7,7 @@ class NoInferenceResult {
<T> void m1(T t) { }
void test() {
m(<error descr="Cyclic inference">(String s1) -> (String s2) -> s1 + s2</error>);
m(<error descr="Incompatible return type <lambda expression> in lambda expression">(String s1) -> (String s2) -> s1 + s2</error>);
m((String s1) -> s1.length());
m((String s1) -> s1);
@@ -0,0 +1,48 @@
/*
* 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.
*/
class Test1 {
{
Comparable<? extends Integer> c = (Integer o)->{
return 0;
};
Comparable<? super Integer> c1 = (Integer o)->{
return 0;
};
}
}
class Test2<U> {
interface I<T> {
void m(T t);
}
private void foo(I<? super U> i1) {}
private void bar() {
foo((U u) -> {});
foo(u -> {});
}
interface I1<T> {}
private <A extends I1<? super U>> A bar(A a) {
foo((U u)->{});
foo(u->{});
return a;
}
}
@@ -49,6 +49,10 @@ public class LambdaHighlightingTest extends LightDaemonAnalyzerTestCase {
doTest();
}
public void testTypeArgsConsistencyMisc2() throws Exception {
doTest();
}
public void testTypeArgsConsistencyWithoutParams() throws Exception {
doTest();
}
@@ -15,13 +15,10 @@
*/
package com.siyeh.ipp.types;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.Function;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
@@ -66,7 +63,7 @@ public class InferLambdaParameterTypeIntention extends Intention {
if (parameters.length != lambdaParameters.length) return null;
for (int i = 0; i < parameters.length; i++) {
PsiParameter parameter = parameters[i];
final PsiType psiType = resolveResult.getSubstitutor().substitute(parameter.getType());
final PsiType psiType = GenericsUtil.eliminateWildcards(resolveResult.getSubstitutor().substitute(parameter.getType()));
if (psiType != null) {
buf.append(psiType.getPresentableText()).append(" ").append(lambdaParameters[i].getName());
}
@@ -49,8 +49,9 @@ public class ReplaceLambdaWithAnonymousIntention extends Intention {
protected void processIntention(Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
final PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(element, PsiLambdaExpression.class);
LOG.assertTrue(lambdaExpression != null);
final PsiType functionalInterfaceType = lambdaExpression.getFunctionalInterfaceType();
PsiType functionalInterfaceType = lambdaExpression.getFunctionalInterfaceType();
LOG.assertTrue(functionalInterfaceType != null);
functionalInterfaceType = GenericsUtil.eliminateWildcards(functionalInterfaceType);
final PsiMethod method = LambdaUtil.getFunctionalInterfaceMethod(functionalInterfaceType);
LOG.assertTrue(method != null);
@@ -0,0 +1,8 @@
class Test1<U> {
public static void main(String[] args) {
Comparable<? extends Integer> c = <caret>o->{
return 0;
};
}
}
@@ -0,0 +1,8 @@
class Test1<U> {
public static void main(String[] args) {
Comparable<? extends Integer> c = (Integer o)->{
return 0;
};
}
}
@@ -0,0 +1,8 @@
class Test1<U> {
public static void main(String[] args) {
Comparable<? extends Integer> c = <caret>o->{
return 0;
};
}
}
@@ -0,0 +1,11 @@
class Test1<U> {
public static void main(String[] args) {
Comparable<? extends Integer> c = new Comparable<Integer>() {
@Override
public int compareTo(Integer o) {
<selection>return 0;</selection>
}
};
}
}
@@ -27,6 +27,10 @@ public class InferLambdaParameterTypeIntentionTest extends IPPTestCase {
doTest("Expand lambda to (String o1, String o2) -> {...}");
}
public void testSimpleWildcard() {
doTest("Expand lambda to (Integer o) -> {...}");
}
public void testAlreadyExist() throws Exception {
assertIntentionNotAvailable("Expand lambda to (String o) -> {...}");
}
@@ -26,6 +26,10 @@ public class ReplaceLambdaWithAnonymousIntentionTest extends IPPTestCase {
public void testWithSubstitution() {
doTest();
}
public void testSimpleWildcard() {
doTest();
}
public void testInsertFinal() {
doTest();
@@ -223,7 +223,7 @@ public class GroovySmartCompletionContributor extends CompletionContributor {
final Set<PsiType> types = GroovyExpectedTypesProvider.getDefaultExpectedTypes(expression);
for (PsiType type : types) {
if (type instanceof PsiArrayType) {
final LookupItem item = PsiTypeLookupItem.createLookupItem(JavaCompletionUtil.eliminateWildcards(type), place);
final LookupItem item = PsiTypeLookupItem.createLookupItem(GenericsUtil.eliminateWildcards(type), place);
if (item.getObject() instanceof PsiClass) {
JavaCompletionUtil.setShowFQN(item);
item.setInsertHandler(new InsertHandler<LookupItem>() {
@@ -241,7 +241,7 @@ public class GroovySmartCompletionContributor extends CompletionContributor {
for (PsiType psiType : types) {
if (psiType instanceof PsiClassType) {
PsiType type = JavaCompletionUtil.eliminateWildcards(JavaCompletionUtil.originalize(psiType));
PsiType type = GenericsUtil.eliminateWildcards(JavaCompletionUtil.originalize(psiType));
final PsiClassType classType = (PsiClassType)type;
if (classType.resolve() != null) {
expectedClassTypes.add(classType);
@@ -315,7 +315,7 @@ public class GroovySmartCompletionContributor extends CompletionContributor {
}
}
final LookupItem item = PsiTypeLookupItem.createLookupItem(JavaCompletionUtil.eliminateWildcards(type), place, isDiamond);
final LookupItem item = PsiTypeLookupItem.createLookupItem(GenericsUtil.eliminateWildcards(type), place, isDiamond);
JavaCompletionUtil.setShowFQN(item);
item.setInsertHandler(new AfterNewClassInsertHandler((PsiClassType)type, true));
return item;