Skip to content

Commit

Permalink
further simplified checkSuperTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoBettini committed Feb 5, 2024
1 parent 0bd0219 commit fa654d4
Showing 1 changed file with 49 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,72 +185,68 @@ protected void checkSuperTypes(EObject sourceType, JvmGenericType type) {
getFeatureForIssue(sourceType), CYCLIC_INHERITANCE);
}
var superTypes = type.getSuperTypes();
if (type.isInterface()) {
for (int i = 0; i < superTypes.size(); ++i) {
JvmTypeReference extendedType = superTypes.get(i);
var associated = associations.getPrimarySourceElement(extendedType);
var eContainingFeature = associated.eContainingFeature();
var mismatchedSuperInterfaceErrorPrerix = type.isInterface() ? "Extended" : "Implemented";
var expectedInterfaceIndex = -1;
for (int i = 0; i < superTypes.size(); ++i) {
JvmTypeReference extendedType = superTypes.get(i);
var associated = getSuperTypeSourceElement(extendedType);
if (associated == null)
continue; // synthetic superclass (e.g., Object)
var eContainingFeature = associated.eContainingFeature();
int featureIndex = INSIGNIFICANT_INDEX;
if (eContainingFeature.isMany()) {
featureIndex = ++expectedInterfaceIndex;
}
if (JvmTypeReferenceUtil.isExpectedAsInterface(extendedType)) {
expectedInterfaceIndex++;
if (!isInterface(extendedType) && !isAnnotation(extendedType)) {
error("Extended interface must be an interface",
error(mismatchedSuperInterfaceErrorPrerix + " interface must be an interface",
sourceType,
eContainingFeature, i, INTERFACE_EXPECTED);
eContainingFeature, featureIndex, INTERFACE_EXPECTED);
}
} else if (JvmTypeReferenceUtil.isExpectedAsClass(extendedType)) {
if (!isClass(extendedType)) {
error("Superclass must be a class",
sourceType,
eContainingFeature, CLASS_EXPECTED);
}
checkWildcardSupertype(sourceType, notNull(type.getSimpleName()),
extendedType,
eContainingFeature, i);
}
} else if (isAnonymous(type)) {
JvmTypeReference extendedType = Iterables.getLast(superTypes);
var associated = getSuperTypeSourceElement(extendedType);
var eContainingFeature = associated.eContainingFeature();
if (((JvmDeclaredType) extendedType.getType()).isFinal()) {
if (isFinal(extendedType)) {
error("Attempt to override final class",
sourceType,
eContainingFeature, OVERRIDDEN_FINAL);
}
checkWildcardSupertype(sourceType, notNull(type.getSimpleName()),
extendedType,
eContainingFeature, INSIGNIFICANT_INDEX);
} else {
var expectedInterfaceIndex = -1;
for (int i = 0; i < superTypes.size(); ++i) {
JvmTypeReference extendedType = superTypes.get(i);
var associated = getSuperTypeSourceElement(extendedType);
if (associated == null)
continue; // synthetic superclass (e.g., Object)
var eContainingFeature = associated.eContainingFeature();
int featureIndex = INSIGNIFICANT_INDEX;
if (eContainingFeature.isMany()) {
featureIndex = ++expectedInterfaceIndex;
}
if (JvmTypeReferenceUtil.isExpectedAsInterface(extendedType)) {
expectedInterfaceIndex++;
if (!isInterface(extendedType.getType()) && !isAnnotation(extendedType.getType())) {
error("Implemented interface must be an interface",
sourceType,
eContainingFeature, featureIndex, INTERFACE_EXPECTED);
}
} else if (JvmTypeReferenceUtil.isExpectedAsClass(extendedType)) {
if (!(extendedType.getType() instanceof JvmGenericType)
|| ((JvmGenericType) extendedType.getType()).isInterface()) {
error("Superclass must be a class",
sourceType,
eContainingFeature, CLASS_EXPECTED);
} else {
if (((JvmGenericType) extendedType.getType()).isFinal()) {
error("Attempt to override final class",
sourceType,
eContainingFeature, OVERRIDDEN_FINAL);
}
}
}
checkWildcardSupertype(sourceType, notNull(type.getSimpleName()),
extendedType,
eContainingFeature, featureIndex);
}
eContainingFeature, featureIndex);
}
}

protected boolean isInterface(JvmTypeReference typeRef) {
return isInterface(typeRef.getType());
}

protected boolean isInterface(JvmType type) {
return type instanceof JvmGenericType && ((JvmGenericType) type).isInterface();
}

protected boolean isAnnotation(JvmTypeReference typeRef) {
return isAnnotation(typeRef.getType());
}

protected boolean isAnnotation(JvmType type) {
return type instanceof JvmAnnotationType;
}

protected boolean isFinal(JvmTypeReference typeRef) {
return typeRef.getType() instanceof JvmGenericType &&((JvmGenericType) typeRef.getType()).isFinal();
}

protected boolean isClass(JvmTypeReference typeRef) {
return typeRef.getType() instanceof JvmGenericType
&& !((JvmGenericType) typeRef.getType()).isInterface();
}

/**
* Workaround for https://github.com/eclipse/xtext/issues/2886
*/
Expand Down Expand Up @@ -513,22 +509,6 @@ private boolean hasCycleInHierarchy(JvmGenericType type, Set<JvmGenericType> pro
return false;
}

protected boolean isInterface(JvmTypeReference typeRef) {
return isInterface(typeRef.getType());
}

protected boolean isInterface(JvmType type) {
return type instanceof JvmGenericType && ((JvmGenericType) type).isInterface();
}

protected boolean isAnnotation(JvmTypeReference typeRef) {
return isAnnotation(typeRef.getType());
}

protected boolean isAnnotation(JvmType type) {
return type instanceof JvmAnnotationType;
}

protected void checkWildcardSupertype(EObject sourceElement,
String name,
JvmTypeReference superTypeReference,
Expand Down

0 comments on commit fa654d4

Please sign in to comment.