diamond: replace wildcards with corresponding bound when inference (IDEA-66124)

This commit is contained in:
anna
2011-03-08 17:45:01 +01:00
parent 675a660b14
commit f0db1be49c
2 changed files with 24 additions and 13 deletions
@@ -27,6 +27,8 @@ import com.intellij.psi.util.TypeConversionUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import java.util.List;
/**
* User: anna
* Date: 1/28/11
@@ -77,13 +79,13 @@ public class ExplicitTypeCanBeDiamondInspection extends BaseJavaLocalInspectionT
if (typeElements.length == 1 && typeElements[0].getType() instanceof PsiDiamondType) return;
final PsiDiamondType.DiamondInferenceResult inferenceResult = PsiDiamondType.resolveInferredTypes(expression);
if (inferenceResult.getErrorMessage() == null) {
final PsiType[] types = inferenceResult.getTypes();
final List<PsiType> types = inferenceResult.getInferredTypes();
final PsiType[] typeArguments = parameterList.getTypeArguments();
if (types.length == typeArguments.length) {
if (types.size() == typeArguments.length) {
for (int i = 0, typeArgumentsLength = typeArguments.length; i < typeArgumentsLength; i++) {
PsiType typeArgument = typeArguments[i];
if (types[i] instanceof PsiWildcardType) {
final PsiWildcardType wildcardType = (PsiWildcardType)types[i];
if (types.get(i) instanceof PsiWildcardType) {
final PsiWildcardType wildcardType = (PsiWildcardType)types.get(i);
final PsiType bound = wildcardType.getBound();
if (bound != null) {
if (wildcardType.isExtends()) {
@@ -93,7 +95,7 @@ public class ExplicitTypeCanBeDiamondInspection extends BaseJavaLocalInspectionT
}
}
}
if (!typeArgument.equals(types[i])) {
if (!typeArgument.equals(types.get(i))) {
return;
}
}
@@ -259,7 +259,22 @@ public class PsiDiamondType extends PsiType {
if (myErrorMessage != null) {
return NULL_TYPES;
}
return myInferredTypes.toArray(new PsiType[myInferredTypes.size()]);
final PsiType[] result = new PsiType[myInferredTypes.size()];
for (int i = 0, myInferredTypesSize = myInferredTypes.size(); i < myInferredTypesSize; i++) {
PsiType inferredType = myInferredTypes.get(i);
if (inferredType instanceof PsiWildcardType) {
final PsiType bound = ((PsiWildcardType)inferredType).getBound();
result[i] = bound != null ? bound : PsiType.getJavaLangObject(PsiManager.getInstance(myProject), GlobalSearchScope.allScope(myProject));
}
else {
result[i] = inferredType;
}
}
return result;
}
public List<PsiType> getInferredTypes() {
return myInferredTypes;
}
public String getErrorMessage() {
@@ -274,13 +289,7 @@ public class PsiDiamondType extends PsiType {
myErrorMessage = "Cannot infer type arguments for " +
myNewExpressionPresentableText + " because type " + psiType.getPresentableText() + " inferred is not allowed in current context";
} else {
if (psiType instanceof PsiWildcardType) {
final PsiType bound = ((PsiWildcardType)psiType).getBound();
myInferredTypes.add(bound != null ? bound : PsiType.getJavaLangObject(PsiManager.getInstance(myProject), GlobalSearchScope.allScope(myProject)));
}
else {
myInferredTypes.add(psiType);
}
myInferredTypes.add(psiType);
}
}