ExpectedTypeInfo: get rid of dimCount and unused insertExplicitTypeParams

This commit is contained in:
peter
2013-06-27 18:38:26 +02:00
parent edec60ddfd
commit 463014e9c6
6 changed files with 23 additions and 83 deletions
@@ -44,9 +44,5 @@ public interface ExpectedTypeInfo {
ExpectedTypeInfo[] intersect(ExpectedTypeInfo info);
boolean isArrayTypeInfo();
TailType getTailType();
boolean isInsertExplicitTypeParams();
}
@@ -28,13 +28,6 @@ public class ExpectedTypeInfoImpl implements ExpectedTypeInfo {
private final PsiType type;
private final PsiType defaultType;
private boolean myInsertExplicitTypeParams;
int getDimCount() {
return dimCount;
}
private final int dimCount;
@Override
public int getKind() {
@@ -59,12 +52,11 @@ public class ExpectedTypeInfoImpl implements ExpectedTypeInfo {
private PsiMethod myCalledMethod;
public ExpectedTypeInfoImpl(@NotNull PsiType type, int kind, int dimCount, @NotNull PsiType defaultType, @NotNull TailType myTailType) {
public ExpectedTypeInfoImpl(@NotNull PsiType type, int kind, @NotNull PsiType defaultType, @NotNull TailType myTailType) {
this.type = type;
this.kind = kind;
this.myTailType = myTailType;
this.dimCount = dimCount;
this.defaultType = defaultType;
PsiUtil.ensureValidType(type);
@@ -83,30 +75,13 @@ public class ExpectedTypeInfoImpl implements ExpectedTypeInfo {
@Override
@NotNull
public PsiType getType () {
PsiType t = type;
int dims = dimCount;
while (dims-- > 0) t = t.createArrayType();
return t;
return type;
}
@Override
@NotNull
public PsiType getDefaultType () {
PsiType t = defaultType;
int dims = dimCount;
while (dims-- > 0) t = t.createArrayType();
return t;
}
@Override
public boolean isInsertExplicitTypeParams() {
return myInsertExplicitTypeParams;
}
public void setInsertExplicitTypeParams(final boolean insertExplicitTypeParams) {
this.myInsertExplicitTypeParams = insertExplicitTypeParams;
return defaultType;
}
public boolean equals(final Object o) {
@@ -115,20 +90,18 @@ public class ExpectedTypeInfoImpl implements ExpectedTypeInfo {
final ExpectedTypeInfoImpl that = (ExpectedTypeInfoImpl)o;
if (dimCount != that.dimCount) return false;
if (kind != that.kind) return false;
if (defaultType != null ? !defaultType.equals(that.defaultType) : that.defaultType != null) return false;
if (!defaultType.equals(that.defaultType)) return false;
if (myTailType != null ? !myTailType.equals(that.myTailType) : that.myTailType != null) return false;
if (type != null ? !type.equals(that.type) : that.type != null) return false;
if (!type.equals(that.type)) return false;
return true;
}
public int hashCode() {
int result;
result = (type != null ? type.hashCode() : 0);
result = 31 * result + (defaultType != null ? defaultType.hashCode() : 0);
result = 31 * result + dimCount;
result = (type.hashCode());
result = 31 * result + (defaultType.hashCode());
result = 31 * result + kind;
result = 31 * result + (myTailType != null ? myTailType.hashCode() : 0);
return result;
@@ -141,17 +114,15 @@ public class ExpectedTypeInfoImpl implements ExpectedTypeInfo {
@SuppressWarnings({"HardCodedStringLiteral"})
public String toString() {
return "ExpectedTypeInfo[type='" + type + "' kind='" + kind + "' dims='" + dimCount+ "']";
return "ExpectedTypeInfo[type='" + type + "' kind='" + kind + "']";
}
@Override
public ExpectedTypeInfo[] intersect(ExpectedTypeInfo info) {
ExpectedTypeInfoImpl info1 = (ExpectedTypeInfoImpl)info;
LOG.assertTrue(!(type instanceof PsiArrayType) && !(info1.type instanceof PsiArrayType));
if (kind == TYPE_STRICTLY) {
if (info1.kind == TYPE_STRICTLY) {
if (dimCount != info1.dimCount) return ExpectedTypeInfo.EMPTY_ARRAY;
if (info1.type.equals(type)) return new ExpectedTypeInfoImpl[] {this};
}
else {
@@ -160,12 +131,10 @@ public class ExpectedTypeInfoImpl implements ExpectedTypeInfo {
}
else if (kind == TYPE_OR_SUBTYPE) {
if (info1.kind == TYPE_STRICTLY) {
if (dimCount != info1.dimCount) return ExpectedTypeInfo.EMPTY_ARRAY;
if (type.isAssignableFrom(info1.type)) return new ExpectedTypeInfoImpl[] {info1};
}
else if (info1.kind == TYPE_OR_SUBTYPE) {
PsiType type = dimCount == info1.dimCount ? this.type : getType();
PsiType otherType = dimCount == info1.dimCount ? info1.type : info1.getType();
PsiType otherType = info1.type;
if (type.isAssignableFrom(otherType)) return new ExpectedTypeInfoImpl[] {info1};
else if (otherType.isAssignableFrom(type)) return new ExpectedTypeInfoImpl[] {this};
}
@@ -175,17 +144,14 @@ public class ExpectedTypeInfoImpl implements ExpectedTypeInfo {
}
else if (kind == TYPE_OR_SUPERTYPE) {
if (info1.kind == TYPE_STRICTLY) {
if (dimCount != info1.dimCount) return ExpectedTypeInfo.EMPTY_ARRAY;
if (info1.type.isAssignableFrom(type)) return new ExpectedTypeInfoImpl[] {info1};
}
else if (info1.kind == TYPE_OR_SUBTYPE) {
PsiType type = dimCount == info1.dimCount ? this.type : getType();
PsiType otherType = dimCount == info1.dimCount ? info1.type : info1.getType();
PsiType otherType = info1.type;
if (otherType.isAssignableFrom(type)) return new ExpectedTypeInfoImpl[] {this};
}
else if (info1.kind == TYPE_OR_SUPERTYPE) {
PsiType type = dimCount == info1.dimCount ? this.type : getType();
PsiType otherType = dimCount == info1.dimCount ? info1.type : info1.getType();
PsiType otherType = info1.type;
if (type.isAssignableFrom(otherType)) return new ExpectedTypeInfoImpl[] {this};
else if (otherType.isAssignableFrom(type)) return new ExpectedTypeInfoImpl[] {info1};
}
@@ -199,10 +165,4 @@ public class ExpectedTypeInfoImpl implements ExpectedTypeInfo {
return ExpectedTypeInfo.EMPTY_ARRAY;
}
@Override
public boolean isArrayTypeInfo () {
return dimCount > 0;
}
}
@@ -52,7 +52,7 @@ import java.util.*;
* @author ven
*/
public class ExpectedTypesProvider {
private static final ExpectedTypeInfo VOID_EXPECTED = new ExpectedTypeInfoImpl(PsiType.VOID, ExpectedTypeInfo.TYPE_OR_SUBTYPE, 0, PsiType.VOID,
private static final ExpectedTypeInfo VOID_EXPECTED = new ExpectedTypeInfoImpl(PsiType.VOID, ExpectedTypeInfo.TYPE_OR_SUBTYPE, PsiType.VOID,
TailType.SEMICOLON);
private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.ExpectedTypesProvider");
@@ -91,13 +91,7 @@ public class ExpectedTypesProvider {
@NotNull
private static ExpectedTypeInfoImpl createInfoImpl(@NotNull PsiType type, int kind, PsiType defaultType, @NotNull TailType tailType) {
int dims = 0;
while (type instanceof PsiArrayType && defaultType instanceof PsiArrayType) {
type = ((PsiArrayType) type).getComponentType();
defaultType = ((PsiArrayType) defaultType).getComponentType();
dims++;
}
return new ExpectedTypeInfoImpl(type, kind, dims, defaultType, tailType);
return new ExpectedTypeInfoImpl(type, kind, defaultType, tailType);
}
@NotNull
@@ -139,7 +133,7 @@ public class ExpectedTypesProvider {
for (ExpectedTypeInfo info : infos) {
ExpectedTypeInfoImpl infoImpl = (ExpectedTypeInfoImpl)info;
if (infoImpl.getDefaultType() instanceof PsiClassType && infoImpl.getDimCount() == 0) {
if (infoImpl.getDefaultType() instanceof PsiClassType) {
JavaResolveResult result = ((PsiClassType)infoImpl.getDefaultType()).resolveGenerics();
PsiClass aClass = (PsiClass)result.getElement();
if (aClass instanceof PsiAnonymousClass) {
@@ -155,10 +149,10 @@ public class ExpectedTypesProvider {
}
if (infoImpl.kind == ExpectedTypeInfo.TYPE_OR_SUPERTYPE) {
processAllSuperTypes(infoImpl.getType(), infoImpl.getDimCount(), visitor, project, set);
processAllSuperTypes(infoImpl.getType(), visitor, project, set);
}
else if (infoImpl.getKind() == ExpectedTypeInfo.TYPE_OR_SUBTYPE) {
if (infoImpl.getType() instanceof PsiPrimitiveType && infoImpl.getDimCount() == 0) {
if (infoImpl.getType() instanceof PsiPrimitiveType) {
processPrimitiveTypeAndSubtypes((PsiPrimitiveType)infoImpl.getType(), visitor, set);
}
//else too expensive to search
@@ -183,7 +177,7 @@ public class ExpectedTypesProvider {
}
}
public static void processAllSuperTypes(@NotNull PsiType type, int dimCount, @NotNull PsiTypeVisitor<PsiType> visitor, @NotNull Project project, @NotNull Set<PsiType> set) {
public static void processAllSuperTypes(@NotNull PsiType type, @NotNull PsiTypeVisitor<PsiType> visitor, @NotNull Project project, @NotNull Set<PsiType> set) {
if (type instanceof PsiPrimitiveType) {
if (type.equals(PsiType.BOOLEAN) || type.equals(PsiType.VOID) || type.equals(PsiType.NULL)) return;
@@ -205,12 +199,8 @@ public class ExpectedTypesProvider {
if (type instanceof PsiClassType) {
PsiType[] superTypes = type.getSuperTypes();
for (PsiType superType : superTypes) {
PsiType wrappedType = superType;
for (int j = 0; j < dimCount; j++) {
wrappedType = wrappedType.createArrayType();
}
processType(wrappedType, visitor, set);
processAllSuperTypes(superType, dimCount, visitor, project, set);
processType(superType, visitor, set);
processAllSuperTypes(superType, visitor, project, set);
}
}
}
@@ -864,7 +854,6 @@ public class ExpectedTypesProvider {
ExpectedTypeInfo[] types = getExpectedTypes(expr, myForCompletion);
for (ExpectedTypeInfo info : types) {
ExpectedTypeInfoImpl infoImpl = (ExpectedTypeInfoImpl)info;
infoImpl.setInsertExplicitTypeParams(true);
infoImpl.myTailType = TailType.COND_EXPR_COLON;
}
myResult = types;
@@ -874,9 +863,6 @@ public class ExpectedTypesProvider {
LOG.error(Arrays.asList(expr.getChildren()) + "; " + myExpr);
}
myResult = getExpectedTypes(expr, myForCompletion);
for (ExpectedTypeInfo info : myResult) {
((ExpectedTypeInfoImpl)info).setInsertExplicitTypeParams(true);
}
}
}
@@ -1038,7 +1024,6 @@ public class ExpectedTypesProvider {
PsiType defaultType = getDefaultType(method, substitutor, parameterType, argument, args, index);
ExpectedTypeInfoImpl info = createInfoImpl(parameterType, ExpectedTypeInfo.TYPE_OR_SUBTYPE, defaultType, tailType);
info.setInsertExplicitTypeParams(true);
info.setCalledMethod(method);
NullableComputable<String> propertyName = getPropertyName(parameter);
info.expectedName = propertyName;
@@ -1048,7 +1033,6 @@ public class ExpectedTypesProvider {
//Then we may still want to call with array argument
final PsiArrayType arrayType = parameterType.createArrayType();
ExpectedTypeInfoImpl info1 = createInfoImpl(arrayType, ExpectedTypeInfo.TYPE_OR_SUBTYPE, arrayType, tailType);
info1.setInsertExplicitTypeParams(true);
info1.setCalledMethod(method);
info1.expectedName = propertyName;
array.add(info1);
@@ -269,7 +269,7 @@ public class JavaCompletionSorting {
if (type == info.getType() && defaultType == info.getDefaultType()) {
return info;
}
return new ExpectedTypeInfoImpl(type, info.getKind(), 0, defaultType, info.getTailType());
return new ExpectedTypeInfoImpl(type, info.getKind(), defaultType, info.getTailType());
}
});
myParameters = parameters;
@@ -378,11 +378,11 @@ public class JavaSmartCompletionContributor extends CompletionContributor {
final PsiClassType classType = factory
.createTypeByFQClassName(CommonClassNames.JAVA_LANG_RUNTIME_EXCEPTION, position.getResolveScope());
final List<ExpectedTypeInfo> result = new SmartList<ExpectedTypeInfo>();
result.add(new ExpectedTypeInfoImpl(classType, ExpectedTypeInfo.TYPE_OR_SUBTYPE, 0, classType, TailType.SEMICOLON));
result.add(new ExpectedTypeInfoImpl(classType, ExpectedTypeInfo.TYPE_OR_SUBTYPE, classType, TailType.SEMICOLON));
final PsiMethod method = PsiTreeUtil.getContextOfType(position, PsiMethod.class, true);
if (method != null) {
for (final PsiClassType type : method.getThrowsList().getReferencedTypes()) {
result.add(new ExpectedTypeInfoImpl(type, ExpectedTypeInfo.TYPE_OR_SUBTYPE, 0, type, TailType.SEMICOLON));
result.add(new ExpectedTypeInfoImpl(type, ExpectedTypeInfo.TYPE_OR_SUBTYPE, type, TailType.SEMICOLON));
}
}
return result.toArray(new ExpectedTypeInfo[result.size()]);
@@ -141,7 +141,7 @@ public class JavaFxUnresolvedFxIdReferenceInspection extends XmlSuppressableInsp
field = CreateFieldFromUsageHelper.insertField(targetClass, field, psiElement);
final PsiClassType fieldType = factory.createType(checkContext(reference.getXmlAttributeValue()));
final ExpectedTypeInfo[] types = {new ExpectedTypeInfoImpl(fieldType, ExpectedTypeInfo.TYPE_OR_SUBTYPE, 0, fieldType, TailType.NONE)};
final ExpectedTypeInfo[] types = {new ExpectedTypeInfoImpl(fieldType, ExpectedTypeInfo.TYPE_OR_SUBTYPE, fieldType, TailType.NONE)};
CreateFieldFromUsageFix.createFieldFromUsageTemplate(targetClass, project, types, field, false, psiElement);
}
}