Skip to content

Commit

Permalink
check super type of anonymous class in JvmGenericTypeValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoBettini committed Feb 5, 2024
1 parent d069831 commit 3b42279
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2382,8 +2382,8 @@ public void testAnonymousClassExtendsFinal() throws Exception {
+ "}";
XtendFile file = file(source);
helper.assertError(file.getXtendTypes().get(0),
XCONSTRUCTOR_CALL, OVERRIDDEN_FINAL,
source.indexOf("Bar"), "Bar".length(),
ANONYMOUS_CLASS, OVERRIDDEN_FINAL,
source.indexOf("new Bar()"), "new Bar()".length(),
"Attempt to override final class");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,8 @@ public void accept(JvmTypeReference capturingTypeReference) {

protected JvmParameterizedTypeReference createSuperTypeReference(JvmType superType, XConstructorCall constructorCall) {
JvmParameterizedTypeReference result = TypesFactory.eINSTANCE.createJvmParameterizedTypeReference();
// we need the association later when checking anonymous class in JvmGenericTypeValidator
typesBuilder.associate(constructorCall, result);
result.setType(superType);
for(JvmTypeReference typeReference: constructorCall.getTypeArguments()) {
result.getArguments().add(typesBuilder.cloneWithProxies(typeReference));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
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.XbasePackage.Literals.*;
import static org.eclipse.xtext.xbase.validation.IssueCodes.*;

import java.lang.annotation.ElementType;
Expand Down Expand Up @@ -602,17 +601,6 @@ protected void doCheckWhitespaceIn(RichString richString) {
richStringProcessor.process(richString, helper, helper);
}

@Check
public void checkSuperTypes(AnonymousClass anonymousClass) {
JvmGenericType inferredType = associations.getInferredType(anonymousClass);
if (inferredType != null) {
JvmTypeReference superTypeRef = Iterables.getLast(inferredType.getSuperTypes());
JvmType superType = superTypeRef.getType();
if(superType instanceof JvmGenericType && ((JvmGenericType) superType).isFinal())
error("Attempt to override final class", anonymousClass.getConstructorCall(), XCONSTRUCTOR_CALL__CONSTRUCTOR, INSIGNIFICANT_INDEX, OVERRIDDEN_FINAL);
}
}

@Check
public void checkStaticMembers(AnonymousClass anonymousClass) {
for (XtendMember member : anonymousClass.getMembers()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@
import org.eclipse.xtext.xbase.typesystem.override.OverrideHelper;
import org.eclipse.xtext.xbase.typesystem.override.ResolvedFeatures;
import org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference;
import org.eclipse.xtext.xtype.XComputedTypeReference;

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;
Expand Down Expand Up @@ -131,8 +133,8 @@ protected void checkSuperTypes(JvmGenericType type) {
primarySourceElement,
getFeatureForIssue(primarySourceElement), CYCLIC_INHERITANCE);
}
var superTypes = type.getSuperTypes();
if (type.isInterface()) {
var superTypes = type.getSuperTypes();
for (int i = 0; i < superTypes.size(); ++i) {
JvmTypeReference extendedType = superTypes.get(i);
var associated = associations.getPrimarySourceElement(extendedType);
Expand All @@ -146,12 +148,23 @@ protected void checkSuperTypes(JvmGenericType type) {
extendedType,
eContainingFeature, i);
}
} else if (isAnonymous(type)) {
JvmTypeReference extendedType = Iterables.getLast(superTypes);
var associated = getSuperTypeSourceElement(extendedType);
var eContainingFeature = associated.eContainingFeature();
if (((JvmGenericType) extendedType.getType()).isFinal()) {
error("Attempt to override final class",
primarySourceElement,
eContainingFeature, OVERRIDDEN_FINAL);
}
checkWildcardSupertype(primarySourceElement, notNull(type.getSimpleName()),
extendedType,
eContainingFeature, INSIGNIFICANT_INDEX);
} else {
var superTypes = type.getSuperTypes();
var expectedInterfaceIndex = -1;
for (int i = 0; i < superTypes.size(); ++i) {
JvmTypeReference extendedType = superTypes.get(i);
var associated = associations.getPrimarySourceElement(extendedType);
var associated = getSuperTypeSourceElement(extendedType);
if (associated == null)
continue; // synthetic superclass (e.g., Object)
var eContainingFeature = associated.eContainingFeature();
Expand Down Expand Up @@ -191,6 +204,23 @@ protected void checkSuperTypes(JvmGenericType type) {
}
}

/**
* Workaround for https://github.com/eclipse/xtext/issues/2886
*/
protected boolean isAnonymous(JvmGenericType type) {
return type.isAnonymous() ||
Iterables.getLast(type.getSuperTypes()) instanceof XComputedTypeReference;
}

protected EObject getSuperTypeSourceElement(JvmTypeReference extendedType) {
var associated = associations.getPrimarySourceElement(extendedType);
while (associated == null && extendedType instanceof XComputedTypeReference) {
extendedType = ((XComputedTypeReference) extendedType).getEquivalent();
associated = associations.getPrimarySourceElement(extendedType);
}
return associated;
}

protected void checkMemberNamesAreUnique(JvmGenericType type) {
Map<String, List<JvmField>> fieldsByName = groupMembersByName(type.getMembers(),
JvmField.class);
Expand Down

0 comments on commit 3b42279

Please sign in to comment.