Skip to content

Commit

Permalink
duplicate fields and types in JvmGenericTypeValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoBettini committed Feb 5, 2024
1 parent 42c3838 commit 666d19e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,6 @@ public void checkNoTypeNameShadowing(XtendTypeDeclaration type) {

@Check
public void checkMemberNamesAreUnique(XtendTypeDeclaration xtendType) {
final Multimap<String, XtendField> name2field = HashMultimap.create();
final Multimap<String, XtendTypeDeclaration> name2type = HashMultimap.create();
final Multimap<JvmType, XtendField> type2extension = HashMultimap.create();
for (XtendMember member : xtendType.getMembers()) {
if (member instanceof XtendField) {
Expand All @@ -531,30 +529,9 @@ public void checkMemberNamesAreUnique(XtendTypeDeclaration xtendType) {
type2extension.put(type, field);
}
}
} else {
name2field.put(field.getName(), field);
}
} else if (member instanceof XtendTypeDeclaration) {
String name = ((XtendTypeDeclaration) member).getName();
if (name != null && name.length() > 0) {
name2type.put(name, (XtendTypeDeclaration) member);
}
}
}
for(String name: name2field.keySet()) {
Collection<XtendField> fields = name2field.get(name);
if(fields.size() >1) {
for(XtendField field: fields)
error("Duplicate field " + name, field, XtendPackage.Literals.XTEND_FIELD__NAME, DUPLICATE_FIELD);
}
}
for(String name: name2type.keySet()) {
Collection<XtendTypeDeclaration> types = name2type.get(name);
if(types.size() >1) {
for(XtendTypeDeclaration type: types)
error("Duplicate nested type " + name, type, XtendPackage.Literals.XTEND_TYPE_DECLARATION__NAME, DUPLICATE_TYPE);
}
}
for(JvmType type: type2extension.keySet()) {
Collection<XtendField> fields = type2extension.get(type);
if(fields.size() >1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
Expand Down Expand Up @@ -75,6 +78,7 @@ protected void checkJvmGenericType(JvmGenericType type) {
if (isAssociatedToSource(field))
handleExceptionDuringValidation(() -> checkField(field));
});
handleExceptionDuringValidation(() -> checkMemberNamesAreUnique(type));
var members = type.getMembers();
handleExceptionDuringValidation(() -> checkJvmExecutables(members));
handleExceptionDuringValidation(() -> checkJvmGenericTypes(members));
Expand Down Expand Up @@ -157,6 +161,44 @@ protected void checkSuperTypes(JvmGenericType type) {
}
}

protected void checkMemberNamesAreUnique(JvmGenericType type) {
Map<String, List<JvmField>> fieldsByName = groupMembersByName(type.getMembers(),
JvmField.class);
Map<String, List<JvmDeclaredType>> typesByName = groupMembersByName(type.getMembers(),
JvmDeclaredType.class);
for (String name : fieldsByName.keySet()) {
var withSameName = fieldsByName.get(name);
if (withSameName.size() > 1) {
for (var duplicate : withSameName) {
var originalSource = associations.getPrimarySourceElement(duplicate);
error("Duplicate field " + name,
originalSource, getFeatureForIssue(originalSource), DUPLICATE_FIELD);
}
}
}
for (String name : typesByName.keySet()) {
var withSameName = typesByName.get(name);
if (withSameName.size() > 1) {
for (var duplicate : withSameName) {
var originalSource = associations.getPrimarySourceElement(duplicate);
error("Duplicate nested type " + name,
originalSource, getFeatureForIssue(originalSource), DUPLICATE_TYPE);
}
}
}
}

private <T1 extends JvmMember, T2 extends T1> Map<String, List<T2>> groupMembersByName(
List<T1> members,
Class<T2> memberType) {
return members.stream()
.filter(this::isAssociatedToSource)
.filter(memberType::isInstance)
.map(memberType::cast)
.filter(e -> Objects.nonNull(e.getSimpleName()))
.collect(Collectors.groupingBy(JvmMember::getSimpleName));
}

protected void checkField(JvmField field) {
var declaredFieldType = field.getType();
if (isPrimitiveVoid(declaredFieldType)) {
Expand Down

0 comments on commit 666d19e

Please sign in to comment.