Skip to content

Commit

Permalink
checkMemberNamesAreUnique in XtendJvmGenericTypeValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoBettini committed Feb 5, 2024
1 parent a094669 commit f8e65b3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,28 @@

import static org.eclipse.xtend.core.validation.IssueCodes.*;
import static org.eclipse.xtend.core.xtend.XtendPackage.Literals.*;
import static org.eclipse.xtext.util.Strings.*;
import static org.eclipse.xtext.xbase.validation.IssueCodes.*;

import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtend.core.xtend.XtendField;
import org.eclipse.xtend.core.xtend.XtendFunction;
import org.eclipse.xtend.core.xtend.XtendMember;
import org.eclipse.xtend.core.xtend.XtendTypeDeclaration;
import org.eclipse.xtext.common.types.JvmGenericType;
import org.eclipse.xtext.common.types.JvmType;
import org.eclipse.xtext.common.types.JvmTypeReference;
import org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation;
import org.eclipse.xtext.xbase.typesystem.override.IResolvedOperation;
import org.eclipse.xtext.xbase.validation.JvmGenericTypeValidator;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;

/**
* A specialization of {@link JvmGenericTypeValidator} to deal with specific features of Xtend.
*
Expand All @@ -28,6 +40,38 @@
*/
public class XtendJvmGenericTypeValidator extends JvmGenericTypeValidator {

@Override
protected void checkMemberNamesAreUnique(JvmGenericType genericType) {
super.checkMemberNamesAreUnique(genericType);
var primarySourceElement = getPrimarySourceElement(genericType);
if (primarySourceElement instanceof XtendTypeDeclaration) {
XtendTypeDeclaration xtendType = (XtendTypeDeclaration) primarySourceElement;
final Multimap<JvmType, XtendField> type2extension = HashMultimap.create();
for (XtendMember member : xtendType.getMembers()) {
if (member instanceof XtendField) {
XtendField field = (XtendField) member;
if (isEmpty(field.getName())) {
if (field.isExtension()) {
JvmTypeReference typeReference = field.getType();
if (typeReference != null) {
JvmType type = typeReference.getType();
if (type != null)
type2extension.put(type, field);
}
}
}
}
}
for(JvmType type: type2extension.keySet()) {
Collection<XtendField> fields = type2extension.get(type);
if(fields.size() >1) {
for(XtendField field: fields)
error("Duplicate extension with same type", field, XTEND_FIELD__TYPE, DUPLICATE_FIELD);
}
}
}
}

@Override
protected void doCheckFunctionOverrides(IResolvedOperation operation, Set<EObject> flaggedOperations) {
super.doCheckFunctionOverrides(operation, flaggedOperations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import static org.eclipse.xtend.core.xtend.XtendPackage.Literals.*;
import static org.eclipse.xtext.util.JavaVersion.*;
import static org.eclipse.xtext.util.Strings.*;
import static org.eclipse.xtext.util.Strings.isEmpty;
import static org.eclipse.xtext.xbase.validation.IssueCodes.*;

import java.lang.annotation.ElementType;
Expand Down Expand Up @@ -486,34 +485,6 @@ public void checkNoTypeNameShadowing(XtendTypeDeclaration type) {
}
}
}

@Check
public void checkMemberNamesAreUnique(XtendTypeDeclaration xtendType) {
// duplicated standard fields and duplicated nested types are checked by JvmGenericTypeValidator
final Multimap<JvmType, XtendField> type2extension = HashMultimap.create();
for (XtendMember member : xtendType.getMembers()) {
if (member instanceof XtendField) {
XtendField field = (XtendField) member;
if (isEmpty(field.getName())) {
if (field.isExtension()) {
JvmTypeReference typeReference = field.getType();
if (typeReference != null) {
JvmType type = typeReference.getType();
if (type != null)
type2extension.put(type, field);
}
}
}
}
}
for(JvmType type: type2extension.keySet()) {
Collection<XtendField> fields = type2extension.get(type);
if(fields.size() >1) {
for(XtendField field: fields)
error("Duplicate extension with same type", field, XTEND_FIELD__TYPE, DUPLICATE_FIELD);
}
}
}

@Check
public void checkVarArgIsNotExtension(XtendParameter param) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private boolean isAssociatedToSource(EObject e) {
}

protected void checkJvmGenericType(JvmGenericType type) {
var sourceType = associations.getPrimarySourceElement(type);
var sourceType = getPrimarySourceElement(type);
handleExceptionDuringValidation(() -> checkDefaultSuperConstructor(sourceType, type));
handleExceptionDuringValidation(() -> checkSuperTypes(sourceType, type));
type.getDeclaredFields().forEach(field -> {
Expand All @@ -146,6 +146,10 @@ protected void checkJvmGenericType(JvmGenericType type) {
});
}

protected EObject getPrimarySourceElement(JvmGenericType type) {
return associations.getPrimarySourceElement(type);
}

protected void checkJvmExecutables(List<? extends JvmMember> contents) {
contents.stream()
.filter(this::isAssociatedToSource)
Expand Down

0 comments on commit f8e65b3

Please sign in to comment.