Skip to content

Commit

Permalink
some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoBettini committed Feb 5, 2024
1 parent 8d76826 commit 12c925d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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.JvmIdentifiableElement;
import org.eclipse.xtext.common.types.JvmOperation;
import org.eclipse.xtext.common.types.JvmType;
import org.eclipse.xtext.common.types.JvmTypeReference;
Expand Down Expand Up @@ -149,12 +150,12 @@ protected EStructuralFeature returnTypeFeature(EObject member, IResolvedOperatio
* Dispatcher {@link JvmOperation}s must not be checked by the {@link JvmGenericTypeValidator}.
*/
@Override
protected boolean isAssociatedToSource(EObject element) {
protected boolean shouldBeValidated(JvmIdentifiableElement element) {
if (element instanceof JvmOperation) {
JvmOperation op = (JvmOperation) element;
if (dispatchHelper.isDispatcherFunction(op))
return false;
}
return super.isAssociatedToSource(element);
return super.shouldBeValidated(element);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import com.google.inject.Inject;

/**
Expand Down Expand Up @@ -121,22 +119,29 @@ public void interceptRoot(EObject o) {

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

protected boolean isAssociatedToSource(EObject e) {
return !associations.getSourceElements(e).isEmpty();
/**
* Whether the element is to be considered for validation from this
* validator. The default implementation checks whether there is a
* source element associated to the the passed {@link JvmIdentifiableElement} element, using {@link IJvmModelAssociations#getSourceElements(EObject)}.
*
* @param element
*/
protected boolean shouldBeValidated(JvmIdentifiableElement element) {
return !associations.getSourceElements(element).isEmpty();
}

protected void checkJvmGenericType(JvmGenericType type) {
var sourceType = getPrimarySourceElement(type);
handleExceptionDuringValidation(() -> checkDefaultSuperConstructor(sourceType, type));
handleExceptionDuringValidation(() -> checkSuperTypes(sourceType, type));
type.getDeclaredFields().forEach(field -> {
if (isAssociatedToSource(field))
if (shouldBeValidated(field))
handleExceptionDuringValidation(() -> checkField(field));
});
handleExceptionDuringValidation(() -> checkMemberNamesAreUnique(type));
Expand All @@ -158,7 +163,7 @@ protected EObject getPrimarySourceElement(JvmGenericType type) {

protected void checkJvmExecutables(List<? extends JvmMember> contents) {
contents.stream()
.filter(this::isAssociatedToSource)
.filter(this::shouldBeValidated)
.filter(JvmExecutable.class::isInstance)
.map(JvmExecutable.class::cast)
.forEach(this::checkJvmExecutable);
Expand Down Expand Up @@ -261,7 +266,7 @@ protected EObject getSuperTypeSourceElement(JvmTypeReference extendedType) {
}

protected void checkAnonymousClassStaticMembers(JvmGenericType type) {
type.getMembers().stream().filter(this::isAssociatedToSource).forEach(member -> {
type.getMembers().stream().filter(this::shouldBeValidated).forEach(member -> {
var source = associations.getPrimarySourceElement(member);
if (member instanceof JvmOperation) {
JvmOperation operation = (JvmOperation) member;
Expand Down Expand Up @@ -386,7 +391,7 @@ protected void checkDuplicateAndOverriddenFunctions(EObject sourceType, JvmGener
JavaVersion targetVersion = getGeneratorConfig(sourceType).getJavaSourceVersion();
ResolvedFeatures resolvedFeatures = overrideHelper.getResolvedFeatures(type, targetVersion);

Set<EObject> flaggedOperations = Sets.newHashSet();
Set<EObject> flaggedOperations = new HashSet<>();
doCheckDuplicateExecutables(type, resolvedFeatures, flaggedOperations);
doCheckOverriddenMethods(sourceType, type, resolvedFeatures, flaggedOperations);
doCheckFunctionOverrides(resolvedFeatures, flaggedOperations);
Expand All @@ -396,7 +401,7 @@ private <T1 extends JvmMember, T2 extends T1> Map<String, List<T2>> groupMembers
List<T1> members,
Class<T2> memberType) {
return members.stream()
.filter(this::isAssociatedToSource)
.filter(this::shouldBeValidated)
.filter(memberType::isInstance)
.map(memberType::cast)
.filter(e -> Objects.nonNull(e.getSimpleName()))
Expand All @@ -413,8 +418,13 @@ protected void checkField(JvmField field) {
}

protected void checkJvmExecutable(JvmExecutable executable) {
var parameters = executable.getParameters();
var sourceExecutable = associations.getPrimarySourceElement(executable);
checkParameters(sourceExecutable, executable);
checkExceptions(sourceExecutable, executable);
}

protected void checkParameters(EObject sourceExecutable, JvmExecutable executable) {
var parameters = executable.getParameters();
for (int i = 0; i < parameters.size(); ++i) {
var parameter = parameters.get(i);
var sourceParameter = associations.getPrimarySourceElement(parameter);
Expand All @@ -439,14 +449,13 @@ protected void checkJvmExecutable(JvmExecutable executable) {
+ executable.getSimpleName(), sourceParameterType, null, INVALID_USE_OF_TYPE);
}
}
checkExceptions(sourceExecutable, executable);
}

protected void checkExceptions(EObject sourceExecutable, JvmExecutable executable) {
List<JvmTypeReference> exceptions = executable.getExceptions();
if (exceptions.isEmpty())
return;
Set<String> declaredExceptionNames = Sets.newHashSet();
Set<String> declaredExceptionNames = new HashSet<>();
JvmTypeReference throwableType = getServices().getTypeReferences().getTypeForName(Throwable.class, executable);
if (throwableType == null) {
return;
Expand Down Expand Up @@ -559,7 +568,7 @@ protected void doCheckDuplicateExecutables(JvmGenericType inferredType, final Re
}
return Collections.emptyList();
}
List<IResolvedConstructor> result = Lists.newArrayListWithCapacity(declaredConstructors.size());
List<IResolvedConstructor> result = new ArrayList<>(declaredConstructors.size());
for(IResolvedConstructor constructor: declaredConstructors) {
if (erasedSignature.equals(constructor.getResolvedErasureSignature())) {
result.add(constructor);
Expand All @@ -571,7 +580,7 @@ protected void doCheckDuplicateExecutables(JvmGenericType inferredType, final Re

protected <Executable extends IResolvedExecutable> void doCheckDuplicateExecutables(JvmGenericType inferredType,
List<Executable> declaredOperations, Function<String, List<Executable>> bySignature, Set<EObject> flaggedOperations) {
Set<Executable> processed = Sets.newHashSet();
Set<Executable> processed = new HashSet<>();
for(Executable declaredExecutable: declaredOperations) {
if (!processed.contains(declaredExecutable)) {
List<Executable> sameErasure = bySignature.apply(declaredExecutable.getResolvedErasureSignature());
Expand Down Expand Up @@ -615,7 +624,7 @@ protected void doCheckOverriddenMethods(EObject sourceType, JvmGenericType infer
List<IResolvedOperation> operationsMissingImplementation = null;
boolean doCheckAbstract = !inferredType.isAbstract();
if (doCheckAbstract) {
operationsMissingImplementation = Lists.newArrayList();
operationsMissingImplementation = new ArrayList<>();
}
IVisibilityHelper visibilityHelper = new ContextualVisibilityHelper(this.visibilityHelper, resolvedFeatures.getType());
boolean flaggedType = false;
Expand Down Expand Up @@ -716,7 +725,7 @@ protected EObject findPrimarySourceElement(IResolvedOperation operation) {
* Determine whether the given type contributes to the conflict caused by the given default interface implementation.
*/
private boolean contributesToConflict(JvmGenericType rootType, ConflictingDefaultOperation conflictingDefaultOperation) {
Set<JvmDeclaredType> involvedInterfaces = Sets.newHashSet();
Set<JvmDeclaredType> involvedInterfaces = new HashSet<>();
involvedInterfaces.add(conflictingDefaultOperation.getDeclaration().getDeclaringType());
for (IResolvedOperation conflictingOperation : conflictingDefaultOperation.getConflictingOperations()) {
involvedInterfaces.add(conflictingOperation.getDeclaration().getDeclaringType());
Expand Down Expand Up @@ -825,7 +834,7 @@ protected void doCheckFunctionOverrides(EObject sourceElement, IResolvedOperatio
sourceElement, getFeatureForIssue(sourceElement), OVERRIDE_REDUCES_VISIBILITY);
} else if (details.contains(OverrideCheckDetails.EXCEPTION_MISMATCH)) {
if (exceptionMismatch == null)
exceptionMismatch = Lists.newArrayListWithCapacity(allInherited.size());
exceptionMismatch = new ArrayList<>(allInherited.size());
exceptionMismatch.add(inherited);
} else if (details.contains(OverrideCheckDetails.RETURN_MISMATCH)) {
error("The return type is incompatible with " + inherited.getSimpleSignature(), sourceElement,
Expand Down

0 comments on commit 12c925d

Please sign in to comment.