Skip to content

Commit

Permalink
checkFieldType in JvmGenericTypeValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoBettini committed Feb 5, 2024
1 parent 4be3461 commit 6e88110
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -498,15 +498,6 @@ protected EObject getContainingAnnotationTarget(XAnnotation annotation) {
return eContainer;
}

@Check
public void checkXtendFieldNotPrimitiveVoid(XtendField field) {
JvmTypeReference declaredFieldType = field.getType();
if (isPrimitiveVoid(declaredFieldType)) {
error("void is an invalid type for the field " + field.getName(),
field.getType(), null, INVALID_USE_OF_TYPE);
}
}

@Check
public void checkNoTypeNameShadowing(XtendTypeDeclaration type) {
String name = type.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.xtext.common.types.JvmAnnotationType;
import org.eclipse.xtext.common.types.JvmDeclaredType;
import org.eclipse.xtext.common.types.JvmField;
import org.eclipse.xtext.common.types.JvmGenericType;
import org.eclipse.xtext.common.types.JvmParameterizedTypeReference;
import org.eclipse.xtext.common.types.JvmSpecializedTypeReference;
import org.eclipse.xtext.common.types.JvmType;
import org.eclipse.xtext.common.types.JvmTypeReference;
import org.eclipse.xtext.common.types.JvmVoid;
import org.eclipse.xtext.common.types.JvmWildcardTypeReference;
import org.eclipse.xtext.validation.AbstractDeclarativeValidator;
import org.eclipse.xtext.validation.Check;
Expand Down Expand Up @@ -55,14 +57,22 @@ public void interceptRoot(EObject o) {

protected void checkJvmGenericTypes(List<? extends EObject> contents) {
contents.stream()
.filter(e -> !associations.getSourceElements(e).isEmpty())
.filter(this::isAssociatedToSource)
.filter(JvmGenericType.class::isInstance)
.map(JvmGenericType.class::cast)
.forEach(this::checkJvmGenericType);
}

private boolean isAssociatedToSource(EObject e) {
return !associations.getSourceElements(e).isEmpty();
}

protected void checkJvmGenericType(JvmGenericType type) {
checkSuperTypes(type);
type.getDeclaredFields().forEach(field -> {
if (isAssociatedToSource(field))
checkField(field);
});
checkJvmGenericTypes(type.getMembers());
}

Expand Down Expand Up @@ -135,6 +145,22 @@ protected void checkSuperTypes(JvmGenericType type) {
}
}

protected void checkField(JvmField field) {
JvmTypeReference declaredFieldType = field.getType();
var associated = associations.getPrimarySourceElement(declaredFieldType);
if (isPrimitiveVoid(declaredFieldType)) {
error("Primitive void cannot be a dependency.",
associated, null, INVALID_USE_OF_TYPE);
}
}

/**
* Copied from {@link XbaseValidator#isPrimitiveVoid(JvmTypeReference)}
*/
protected boolean isPrimitiveVoid(JvmTypeReference typeReference) {
return typeReference != null && typeReference.getType() != null && !typeReference.getType().eIsProxy() && typeReference.getType() instanceof JvmVoid;
}

private boolean hasCycleInHierarchy(JvmGenericType type, Set<JvmGenericType> processedSuperTypes) {
JvmDeclaredType container = type;
do {
Expand Down

0 comments on commit 6e88110

Please sign in to comment.