Skip to content

Commit

Permalink
checkSuperTypes for class in JvmGenericTypeValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoBettini committed Dec 18, 2023
1 parent e053310 commit 61771e2
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ package org.eclipse.xtend.core.tests.compiler

import com.google.inject.Inject
import java.lang.reflect.Modifier
import org.eclipse.xtend.core.validation.IssueCodes
import org.eclipse.xtend.core.xtend.XtendPackage
import org.eclipse.xtext.testing.validation.ValidationTestHelper
import org.junit.Test

import static org.eclipse.xtext.xbase.validation.IssueCodes.*

class AccessorsCompilerTest extends AbstractXtendCompilerTest {
@Inject
extension ValidationTestHelper
Expand Down Expand Up @@ -71,7 +72,7 @@ class AccessorsCompilerTest extends AbstractXtendCompilerTest {
class Buzz extends Fizz {
@Accessors String foo
}
''').assertError(XtendPackage.Literals.XTEND_FIELD, IssueCodes.OVERRIDDEN_FINAL, "final", "getFoo")
''').assertError(XtendPackage.Literals.XTEND_FIELD, OVERRIDDEN_FINAL, "final", "getFoo")
}

@Test
Expand All @@ -85,7 +86,7 @@ class AccessorsCompilerTest extends AbstractXtendCompilerTest {
class Bar extends Foo {
@Accessors int foo
}
''').assertError(XtendPackage.Literals.XTEND_FIELD, org.eclipse.xtext.xbase.validation.IssueCodes.INCOMPATIBLE_RETURN_TYPE, "incompatible", "getFoo")
''').assertError(XtendPackage.Literals.XTEND_FIELD, INCOMPATIBLE_RETURN_TYPE, "incompatible", "getFoo")
}

@Test
Expand Down Expand Up @@ -167,7 +168,7 @@ class AccessorsCompilerTest extends AbstractXtendCompilerTest {
class Bar extends Foo {
@Accessors String foo
}
''').assertError(XtendPackage.Literals.XTEND_FIELD, IssueCodes.OVERRIDDEN_FINAL, "setFoo(String)", "final")
''').assertError(XtendPackage.Literals.XTEND_FIELD, OVERRIDDEN_FINAL, "setFoo(String)", "final")
}

@Test
Expand All @@ -181,7 +182,7 @@ class AccessorsCompilerTest extends AbstractXtendCompilerTest {
class Bar extends Foo {
@Accessors String foo
}
''').assertError(XtendPackage.Literals.XTEND_FIELD, org.eclipse.xtext.xbase.validation.IssueCodes.INCOMPATIBLE_RETURN_TYPE, "setFoo(String)", "incompatible")
''').assertError(XtendPackage.Literals.XTEND_FIELD, INCOMPATIBLE_RETURN_TYPE, "setFoo(String)", "incompatible")
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import org.eclipse.xtend.core.validation.IssueCodes;
import org.eclipse.xtend.core.xtend.XtendPackage;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.testing.validation.ValidationTestHelper;
Expand All @@ -26,6 +25,7 @@
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.eclipse.xtext.xbase.testing.CompilationTestHelper;
import org.eclipse.xtext.xbase.validation.IssueCodes;
import org.junit.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -167,7 +167,7 @@ public void testCannotOverrideWithConflictingReturnType() {
_builder.newLine();
_builder.append("}");
_builder.newLine();
this._validationTestHelper.assertError(this.file(_builder.toString()), XtendPackage.Literals.XTEND_FIELD, org.eclipse.xtext.xbase.validation.IssueCodes.INCOMPATIBLE_RETURN_TYPE, "incompatible", "getFoo");
this._validationTestHelper.assertError(this.file(_builder.toString()), XtendPackage.Literals.XTEND_FIELD, IssueCodes.INCOMPATIBLE_RETURN_TYPE, "incompatible", "getFoo");
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
Expand Down Expand Up @@ -350,7 +350,7 @@ public void testCannotOverrideSetterWithIncompatibleReturnType() {
_builder.newLine();
_builder.append("}");
_builder.newLine();
this._validationTestHelper.assertError(this.file(_builder.toString()), XtendPackage.Literals.XTEND_FIELD, org.eclipse.xtext.xbase.validation.IssueCodes.INCOMPATIBLE_RETURN_TYPE, "setFoo(String)", "incompatible");
this._validationTestHelper.assertError(this.file(_builder.toString()), XtendPackage.Literals.XTEND_FIELD, IssueCodes.INCOMPATIBLE_RETURN_TYPE, "setFoo(String)", "incompatible");
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ private IssueCodes() {
public static final String CREATE_FUNCTIONS_MUST_NOT_BE_ABSTRACT = "create_functions_must_not_be_abstract";
public static final String WRONG_PACKAGE = ISSUE_CODE_PREFIX + "wrong_package";
public static final String WRONG_FILE = ISSUE_CODE_PREFIX + "wrong_file";
public static final String CLASS_EXPECTED = ISSUE_CODE_PREFIX + "class_expected";
public static final String DUPLICATE_METHOD = ISSUE_CODE_PREFIX + "duplicate_method";
public static final String DUPLICATE_FIELD = ISSUE_CODE_PREFIX + "duplicate_field";
public static final String DUPLICATE_TYPE_NAME = org.eclipse.xtext.xbase.validation.IssueCodes.DUPLICATE_TYPE;
Expand All @@ -59,7 +58,6 @@ private IssueCodes() {
public static final String ANONYMOUS_CLASS_STATIC_FIELD = ISSUE_CODE_PREFIX + "anonymous_class_static_field";
public static final String DUPLICATE_PARAMETER_NAME = ISSUE_CODE_PREFIX + "duplicate_parameter_name";

public static final String OVERRIDDEN_FINAL = ISSUE_CODE_PREFIX + "overridden_final";
public static final String OVERRIDE_REDUCES_VISIBILITY = ISSUE_CODE_PREFIX + "override_reduces_visibility";
public static final String MISSING_SYNCHRONIZED = ISSUE_CODE_PREFIX + "missing_synchronized";
public static final String INCOMPATIBLE_THROWS_CLAUSE = ISSUE_CODE_PREFIX + "incompatible_throws_clause";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -645,31 +645,6 @@ protected void doCheckWhitespaceIn(RichString richString) {
richStringProcessor.process(richString, helper, helper);
}

@Check
public void checkSuperTypes(XtendClass xtendClass) {
JvmTypeReference superClass = xtendClass.getExtends();
if (superClass != null && superClass.getType() != null) {
if (!(superClass.getType() instanceof JvmGenericType)
|| ((JvmGenericType) superClass.getType()).isInterface()) {
error("Superclass must be a class", XTEND_CLASS__EXTENDS, CLASS_EXPECTED);
} else {
if (((JvmGenericType) superClass.getType()).isFinal()) {
error("Attempt to override final class", XTEND_CLASS__EXTENDS, OVERRIDDEN_FINAL);
}
}
}
for (int i = 0; i < xtendClass.getImplements().size(); ++i) {
JvmTypeReference implementedType = xtendClass.getImplements().get(i);
if (!isInterface(implementedType.getType()) && !isAnnotation(implementedType.getType())) {
error("Implemented interface must be an interface", XTEND_CLASS__IMPLEMENTS, i, INTERFACE_EXPECTED);
}
}
}

protected boolean isAnnotation(JvmType jvmType) {
return jvmType instanceof JvmAnnotationType;
}

@Check
public void checkSuperTypes(AnonymousClass anonymousClass) {
JvmGenericType inferredType = associations.getInferredType(anonymousClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5559,7 +5559,7 @@ public void missingSuperClass() {
_builder.newLine();
_builder.append("}");
_builder.newLine();
this.builder.create("Foo.xtend", _builder.toString()).assertIssueCodes(Diagnostic.LINKING_DIAGNOSTIC, org.eclipse.xtend.core.validation.IssueCodes.CLASS_EXPECTED).assertResolutionLabelsSubset("Create Xtend class \'Bar\'", "Create Java class \'Bar\'", "Create local Xtend class \'Bar\'").assertNoResolutionLabels("Create Java interface \'Bar\'", "Create Xtend interface \'Bar\'", "Create local Xtend interface \'Bar\'");
this.builder.create("Foo.xtend", _builder.toString()).assertIssueCodes(Diagnostic.LINKING_DIAGNOSTIC, IssueCodes.CLASS_EXPECTED).assertResolutionLabelsSubset("Create Xtend class \'Bar\'", "Create Java class \'Bar\'", "Create local Xtend class \'Bar\'").assertNoResolutionLabels("Create Java interface \'Bar\'", "Create Xtend interface \'Bar\'", "Create local Xtend interface \'Bar\'");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,18 @@ public class IssueCodes extends org.eclipse.xtext.validation.IssueCodes {
* @since 2.34
*/
public static final String INTERFACE_EXPECTED = ISSUE_CODE_PREFIX + "interface_expected";
/**
* @since 2.34
*/
public static final String CLASS_EXPECTED = ISSUE_CODE_PREFIX + "class_expected";
/**
* @since 2.34
*/
public static final String WILDCARD_IN_SUPERTYPE = ISSUE_CODE_PREFIX + "wildcard_in_supertype";
/**
* @since 2.34
*/
public static final String OVERRIDDEN_FINAL = ISSUE_CODE_PREFIX + "overridden_final";

private IssueCodes() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,33 @@ protected void checkSuperTypes(JvmGenericType type) {
if (associated == null)
continue; // synthetic superclass (e.g., Object)
var eContainingFeature = associated.eContainingFeature();
// there's no direct way to tell whether the supertype is meant
// to be an extended class or an implemented interface, so we
// check whether the original source element's containing feature
// is single or multiple, assuming that the source element allows
// for a single extended class
if (eContainingFeature.isMany()) {
// assume to be expected as an interface
if (!isInterface(extendedType.getType()) && !isAnnotation(extendedType.getType())) {
error("Implemented interface must be an interface",
primarySourceElement,
eContainingFeature, i, INTERFACE_EXPECTED);
}
} else {
// assume to be expected as a class
if (!(extendedType.getType() instanceof JvmGenericType)
|| ((JvmGenericType) extendedType.getType()).isInterface()) {
error("Superclass must be a class",
primarySourceElement,
eContainingFeature, CLASS_EXPECTED);
} else {
if (((JvmGenericType) extendedType.getType()).isFinal()) {
error("Attempt to override final class",
primarySourceElement,
eContainingFeature, OVERRIDDEN_FINAL);
}
}
}
checkWildcardSupertype(primarySourceElement, notNull(type.getSimpleName()),
extendedType,
eContainingFeature, i);
Expand Down Expand Up @@ -136,7 +163,7 @@ protected void checkWildcardSupertype(EObject sourceElement,
JvmTypeReference superTypeReference,
EStructuralFeature feature, int index) {
if(isInvalidWildcard(superTypeReference))
error("The type "
error("The type "
+ name
+ " cannot extend or implement "
+ superTypeReference.getIdentifier()
Expand Down

0 comments on commit 61771e2

Please sign in to comment.