From a59ca2ea9f986a397612b7aab778c0a15c6a3244 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Fri, 23 Oct 2015 15:19:00 +0300 Subject: [PATCH] Simplify obsolete code in OverridingUtil --- .../kotlin/types/JetOverridingTest.java | 6 +- .../kotlin/resolve/OverridingUtil.java | 96 +++++-------------- 2 files changed, 31 insertions(+), 71 deletions(-) diff --git a/compiler/tests/org/jetbrains/kotlin/types/JetOverridingTest.java b/compiler/tests/org/jetbrains/kotlin/types/JetOverridingTest.java index 6e64ce717ea..2e5d9511af8 100644 --- a/compiler/tests/org/jetbrains/kotlin/types/JetOverridingTest.java +++ b/compiler/tests/org/jetbrains/kotlin/types/JetOverridingTest.java @@ -157,7 +157,11 @@ public class JetOverridingTest extends JetLiteFixture { FunctionDescriptor a = makeFunction(superFun); FunctionDescriptor b = makeFunction(subFun); OverridingUtil.OverrideCompatibilityInfo overridableWith = OverridingUtil.DEFAULT.isOverridableBy(a, b); - assertEquals(overridableWith.getMessage(), expectedIsError, overridableWith.getResult() != OverridingUtil.OverrideCompatibilityInfo.Result.OVERRIDABLE); + assertEquals( + overridableWith.getDebugMessage(), + expectedIsError, + overridableWith.getResult() != OverridingUtil.OverrideCompatibilityInfo.Result.OVERRIDABLE + ); } private FunctionDescriptor makeFunction(String funDecl) { diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/OverridingUtil.java b/core/descriptors/src/org/jetbrains/kotlin/resolve/OverridingUtil.java index c366cc99ddc..fd916b2abe2 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/OverridingUtil.java +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/OverridingUtil.java @@ -78,19 +78,18 @@ public class OverridingUtil { @NotNull CallableDescriptor subDescriptor, boolean checkReturnType ) { - if (superDescriptor instanceof FunctionDescriptor) { - if (!(subDescriptor instanceof FunctionDescriptor)) return OverrideCompatibilityInfo.memberKindMismatch(); + if (superDescriptor instanceof FunctionDescriptor && !(subDescriptor instanceof FunctionDescriptor) || + superDescriptor instanceof PropertyDescriptor && !(subDescriptor instanceof PropertyDescriptor)) { + return OverrideCompatibilityInfo.incompatible("Member kind mismatch"); } - else if (superDescriptor instanceof PropertyDescriptor) { - if (!(subDescriptor instanceof PropertyDescriptor)) return OverrideCompatibilityInfo.memberKindMismatch(); - } - else { + + if (!(superDescriptor instanceof FunctionDescriptor) && !(superDescriptor instanceof PropertyDescriptor)) { throw new IllegalArgumentException("This type of CallableDescriptor cannot be checked for overridability: " + superDescriptor); } // TODO: check outside of this method if (!superDescriptor.getName().equals(subDescriptor.getName())) { - return OverrideCompatibilityInfo.nameMismatch(); + return OverrideCompatibilityInfo.incompatible("Name mismatch"); } OverrideCompatibilityInfo receiverAndParameterResult = checkReceiverAndParameterCount(superDescriptor, subDescriptor); @@ -110,10 +109,10 @@ public class OverridingUtil { KotlinType subValueParameterType = getUpperBound(subValueParameters.get(i)); // TODO: compare erasure if (!KotlinTypeChecker.DEFAULT.equalTypes(superValueParameterType, subValueParameterType)) { - return OverrideCompatibilityInfo.typeParameterNumberMismatch(); + return OverrideCompatibilityInfo.incompatible("Type parameter number mismatch"); } } - return OverrideCompatibilityInfo.valueParameterTypeMismatch(null, null, OverrideCompatibilityInfo.Result.CONFLICT); + return OverrideCompatibilityInfo.conflict("Type parameter number mismatch"); } final Map matchingTypeConstructors = new HashMap(); @@ -142,7 +141,7 @@ public class OverridingUtil { TypeParameterDescriptor subTypeParameter = subTypeParameters.get(i); if (!areTypesEquivalent(superTypeParameter.getUpperBoundsAsType(), subTypeParameter.getUpperBoundsAsType(), localEqualityAxioms)) { - return OverrideCompatibilityInfo.boundsMismatch(superTypeParameter, subTypeParameter); + return OverrideCompatibilityInfo.incompatible("Type parameter bounds mismatch"); } } @@ -151,7 +150,7 @@ public class OverridingUtil { KotlinType subValueParameter = subValueParameters.get(i); if (!areTypesEquivalent(superValueParameter, subValueParameter, localEqualityAxioms)) { - return OverrideCompatibilityInfo.valueParameterTypeMismatch(superValueParameter, subValueParameter, INCOMPATIBLE); + return OverrideCompatibilityInfo.incompatible("Value parameter type mismatch"); } } @@ -162,7 +161,7 @@ public class OverridingUtil { if (superReturnType != null && subReturnType != null) { boolean bothErrors = subReturnType.isError() && superReturnType.isError(); if (!bothErrors && !KotlinTypeChecker.withAxioms(localEqualityAxioms).isSubtypeOf(subReturnType, superReturnType)) { - return OverrideCompatibilityInfo.returnTypeMismatch(superReturnType, subReturnType); + return OverrideCompatibilityInfo.conflict("Return type mismatch"); } } } @@ -170,7 +169,7 @@ public class OverridingUtil { for (ExternalOverridabilityCondition externalCondition : EXTERNAL_CONDITIONS) { if (!externalCondition.isOverridable(superDescriptor, subDescriptor)) { - return OverrideCompatibilityInfo.externalConditionFailed(externalCondition.getClass()); + return OverrideCompatibilityInfo.incompatible("External condition failed"); } } @@ -183,11 +182,11 @@ public class OverridingUtil { CallableDescriptor subDescriptor ) { if ((superDescriptor.getExtensionReceiverParameter() == null) != (subDescriptor.getExtensionReceiverParameter() == null)) { - return OverrideCompatibilityInfo.receiverPresenceMismatch(); + return OverrideCompatibilityInfo.incompatible("Receiver presence mismatch"); } if (superDescriptor.getValueParameters().size() != subDescriptor.getValueParameters().size()) { - return OverrideCompatibilityInfo.valueParameterNumberMismatch(); + return OverrideCompatibilityInfo.incompatible("Value parameter number mismatch"); } return null; @@ -207,7 +206,7 @@ public class OverridingUtil { static List compiledValueParameters(CallableDescriptor callableDescriptor) { ReceiverParameterDescriptor receiverParameter = callableDescriptor.getExtensionReceiverParameter(); - ArrayList parameters = new ArrayList(); + List parameters = new ArrayList(); if (receiverParameter != null) { parameters.add(receiverParameter.getType()); } @@ -484,8 +483,6 @@ public class OverridingUtil { maxVisibility = visibility; } } - // TODO: IDEA seems to issue an incorrect warning here - //noinspection ConstantConditions if (maxVisibility == null) { return null; } @@ -529,7 +526,6 @@ public class OverridingUtil { } public static class OverrideCompatibilityInfo { - public enum Result { OVERRIDABLE, INCOMPATIBLE, @@ -544,71 +540,31 @@ public class OverridingUtil { } @NotNull - public static OverrideCompatibilityInfo nameMismatch() { - return new OverrideCompatibilityInfo(INCOMPATIBLE, "nameMismatch"); // TODO + public static OverrideCompatibilityInfo incompatible(@NotNull String debugMessage) { + return new OverrideCompatibilityInfo(INCOMPATIBLE, debugMessage); } @NotNull - public static OverrideCompatibilityInfo typeParameterNumberMismatch() { - return new OverrideCompatibilityInfo(INCOMPATIBLE, "typeParameterNumberMismatch"); // TODO + public static OverrideCompatibilityInfo conflict(@NotNull String debugMessage) { + return new OverrideCompatibilityInfo(CONFLICT, debugMessage); } - @NotNull - public static OverrideCompatibilityInfo receiverPresenceMismatch() { - return new OverrideCompatibilityInfo(INCOMPATIBLE, "receiverPresenceMismatch"); // TODO - } - - @NotNull - public static OverrideCompatibilityInfo valueParameterNumberMismatch() { - return new OverrideCompatibilityInfo(INCOMPATIBLE, "valueParameterNumberMismatch"); // TODO - } - - @NotNull - public static OverrideCompatibilityInfo boundsMismatch(TypeParameterDescriptor superTypeParameter, TypeParameterDescriptor subTypeParameter) { - return new OverrideCompatibilityInfo(INCOMPATIBLE, "boundsMismatch"); // TODO - } - - @NotNull - public static OverrideCompatibilityInfo valueParameterTypeMismatch(KotlinType superValueParameter, KotlinType subValueParameter, Result result) { - return new OverrideCompatibilityInfo(result, "valueParameterTypeMismatch"); // TODO - } - - @NotNull - public static OverrideCompatibilityInfo memberKindMismatch() { - return new OverrideCompatibilityInfo(INCOMPATIBLE, "memberKindMismatch"); // TODO - } - - @NotNull - public static OverrideCompatibilityInfo returnTypeMismatch(KotlinType substitutedSuperReturnType, KotlinType unsubstitutedSubReturnType) { - return new OverrideCompatibilityInfo(Result.CONFLICT, "returnTypeMismatch: " + unsubstitutedSubReturnType + " >< " + substitutedSuperReturnType); // TODO - } - - @NotNull - public static OverrideCompatibilityInfo varOverriddenByVal() { - return new OverrideCompatibilityInfo(INCOMPATIBLE, "varOverriddenByVal"); // TODO - } - - @NotNull - public static OverrideCompatibilityInfo externalConditionFailed(Class conditionClass) { - return new OverrideCompatibilityInfo(INCOMPATIBLE, "externalConditionFailed: " + conditionClass.getName()); // TODO - } - - //////////////////////////////////////////////////////////////////////////////////////////////////////////////// - private final Result overridable; - private final String message; + private final String debugMessage; - public OverrideCompatibilityInfo(Result success, String message) { + public OverrideCompatibilityInfo(@NotNull Result success, @NotNull String debugMessage) { this.overridable = success; - this.message = message; + this.debugMessage = debugMessage; } + @NotNull public Result getResult() { return overridable; } - public String getMessage() { - return message; + @NotNull + public String getDebugMessage() { + return debugMessage; } } }