Skip to content

Commit

Permalink
checkWildcardSupertype in JvmeGenericTypeValidator only
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoBettini committed Feb 5, 2024
1 parent 7f75245 commit 98f882a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,10 @@
import org.eclipse.xtext.common.types.JvmOperation;
import org.eclipse.xtext.common.types.JvmParameterizedTypeReference;
import org.eclipse.xtext.common.types.JvmPrimitiveType;
import org.eclipse.xtext.common.types.JvmSpecializedTypeReference;
import org.eclipse.xtext.common.types.JvmType;
import org.eclipse.xtext.common.types.JvmTypeParameter;
import org.eclipse.xtext.common.types.JvmTypeReference;
import org.eclipse.xtext.common.types.JvmVisibility;
import org.eclipse.xtext.common.types.JvmWildcardTypeReference;
import org.eclipse.xtext.common.types.TypesPackage;
import org.eclipse.xtext.common.types.util.AnnotationLookup;
import org.eclipse.xtext.common.types.util.DeprecationUtil;
Expand Down Expand Up @@ -659,15 +657,13 @@ public void checkSuperTypes(XtendClass xtendClass) {
if (((JvmGenericType) superClass.getType()).isFinal()) {
error("Attempt to override final class", XTEND_CLASS__EXTENDS, OVERRIDDEN_FINAL);
}
checkWildcardSupertype(xtendClass, superClass, XTEND_CLASS__EXTENDS, INSIGNIFICANT_INDEX);
}
}
for (int i = 0; i < xtendClass.getImplements().size(); ++i) {
JvmTypeReference implementedType = xtendClass.getImplements().get(i);
if (!isInterface(implementedType.getType()) && !isAnnotation(implementedType.getType())) {
error("Implemented interface must be an interface", XTEND_CLASS__IMPLEMENTS, i, INTERFACE_EXPECTED);
}
checkWildcardSupertype(xtendClass, implementedType, XTEND_CLASS__IMPLEMENTS, i);
}
}

Expand Down Expand Up @@ -706,30 +702,6 @@ public void checkStaticMembers(AnonymousClass anonymousClass) {
}
}
}

protected void checkWildcardSupertype(XtendTypeDeclaration xtendType, JvmTypeReference superTypeReference,
EStructuralFeature feature, int index) {
if(isInvalidWildcard(superTypeReference))
error("The type "
+ notNull(xtendType.getName())
+ " cannot extend or implement "
+ superTypeReference.getIdentifier()
+ ". A supertype may not specify any wildcard", feature, index, WILDCARD_IN_SUPERTYPE);
}

protected boolean isInvalidWildcard(JvmTypeReference typeRef) {
if (typeRef instanceof JvmWildcardTypeReference)
return true;
else if (typeRef instanceof JvmParameterizedTypeReference) {
for(JvmTypeReference typeArgument: ((JvmParameterizedTypeReference) typeRef).getArguments()) {
if(typeArgument instanceof JvmWildcardTypeReference)
return true;
}
} else if (typeRef instanceof JvmSpecializedTypeReference) {
return isInvalidWildcard(((JvmSpecializedTypeReference) typeRef).getEquivalent());
}
return false;
}

@Check
public void checkDuplicateAndOverriddenFunctions(XtendTypeDeclaration xtendType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ protected void checkSuperTypes(JvmGenericType type) {
extendedType,
eContainingFeature, i);
}
} else {
var superTypes = type.getSuperTypes();
for (int i = 0; i < superTypes.size(); ++i) {
JvmTypeReference extendedType = superTypes.get(i);
var associated = associations.getPrimarySourceElement(extendedType);
if (associated == null)
continue; // synthetic superclass (e.g., Object)
var eContainingFeature = associated.eContainingFeature();
checkWildcardSupertype(primarySourceElement, notNull(type.getSimpleName()),
extendedType,
eContainingFeature, i);
}
}
}

Expand Down

0 comments on commit 98f882a

Please sign in to comment.