Skip to content

Commit

Permalink
Merge pull request #3031 from LorenzoBettini/lb_2349
Browse files Browse the repository at this point in the history
Provide a JvmGenericTypeValidator
  • Loading branch information
LorenzoBettini committed May 16, 2024
2 parents aa5fb22 + 4f5b36a commit f212edb
Show file tree
Hide file tree
Showing 126 changed files with 146,133 additions and 1,013 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ 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.eclipse.xtext.xbase.validation.IssueCodes
import org.junit.Test

class AccessorsCompilerTest extends AbstractXtendCompilerTest {
Expand Down Expand Up @@ -89,7 +89,7 @@ class AccessorsCompilerTest extends AbstractXtendCompilerTest {
@Accessors int foo
}
'''
file(source).assertError(XtendPackage.Literals.XTEND_FIELD, org.eclipse.xtext.xbase.validation.IssueCodes.INCOMPATIBLE_RETURN_TYPE,
file(source).assertError(XtendPackage.Literals.XTEND_FIELD, IssueCodes.INCOMPATIBLE_RETURN_TYPE,
source.indexOf("int"), "int".length,
"incompatible", "getFoo")
}
Expand Down Expand Up @@ -181,7 +181,7 @@ class AccessorsCompilerTest extends AbstractXtendCompilerTest {

@Test
def void testCannotOverrideSetterWithIncompatibleReturnType() {
file('''
val source = '''
import org.eclipse.xtend.lib.annotations.Accessors
class Foo {
def Object setFoo(String foo) {null}
Expand All @@ -190,7 +190,10 @@ 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")
'''
file(source).assertError(XtendPackage.Literals.XTEND_FIELD, IssueCodes.INCOMPATIBLE_RETURN_TYPE,
source.lastIndexOf("String"), "String".length,
"setFoo(String)", "incompatible")
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import org.eclipse.xtext.testing.InjectWith
import org.eclipse.xtext.testing.validation.ValidationTestHelper
import org.junit.Test

import static org.eclipse.xtend.core.validation.IssueCodes.*
import static org.eclipse.xtend.core.xtend.XtendPackage.Literals.*
import static org.eclipse.xtext.xbase.XbasePackage.Literals.*
import static org.eclipse.xtext.xbase.validation.IssueCodes.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import static org.eclipse.xtext.xbase.validation.IssueCodes.*;

import org.eclipse.xtend.core.tests.AbstractXtendTestCase;
import org.eclipse.xtend.core.validation.IssueCodes;
import org.eclipse.xtend.core.xtend.XtendFile;
import org.eclipse.xtend.core.xtend.XtendPackage;
import org.eclipse.xtext.common.types.TypesPackage;
Expand Down Expand Up @@ -100,14 +99,14 @@ public void clearPreferences() {
@Test public void testFeatureCall_03() throws Exception {
XtendFile file = file("class C { def void m() throws String {} def void n() { m() } }");
helper.assertNoError(file, UNHANDLED_EXCEPTION);
helper.assertError(file, XtendPackage.Literals.XTEND_FUNCTION, IssueCodes.EXCEPTION_NOT_THROWABLE,
helper.assertError(file, XtendPackage.Literals.XTEND_FUNCTION, EXCEPTION_NOT_THROWABLE,
"No exception of type String can be thrown; an exception type must be a subclass of Throwable");
}

@Test public void testFeatureCall_04() throws Exception {
XtendFile file = file("class C { def void m() throws DoesNotExist {} def void n() { m() } }");
helper.assertNoError(file, UNHANDLED_EXCEPTION);
helper.assertNoError(file, IssueCodes.EXCEPTION_NOT_THROWABLE);
helper.assertNoError(file, EXCEPTION_NOT_THROWABLE);
}

@Test public void testFeatureCall_05() throws Exception {
Expand Down Expand Up @@ -148,14 +147,14 @@ public void clearPreferences() {
@Test public void testGenericFeatureCall_02() throws Exception {
XtendFile file = file("class C { def <E extends String> void m() throws E {} def void n() { m() } }");
helper.assertNoError(file, UNHANDLED_EXCEPTION);
helper.assertError(file, XtendPackage.Literals.XTEND_FUNCTION, IssueCodes.EXCEPTION_NOT_THROWABLE,
helper.assertError(file, XtendPackage.Literals.XTEND_FUNCTION, EXCEPTION_NOT_THROWABLE,
"No exception of type E can be thrown; an exception type must be a subclass of Throwable");
}

@Test public void testGenericFeatureCall_03() throws Exception {
XtendFile file = file("class C { def <E extends DoesNotExist> void m() throws E {} def void n() { m() } }");
helper.assertNoError(file, UNHANDLED_EXCEPTION);
helper.assertError(file, XtendPackage.Literals.XTEND_FUNCTION, IssueCodes.EXCEPTION_NOT_THROWABLE,
helper.assertError(file, XtendPackage.Literals.XTEND_FUNCTION, EXCEPTION_NOT_THROWABLE,
"No exception of type E can be thrown; an exception type must be a subclass of Throwable");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ package org.eclipse.xtend.core.tests.validation

import com.google.inject.Inject
import org.eclipse.xtend.core.tests.AbstractXtendTestCase
import org.eclipse.xtend.core.validation.IssueCodes
import org.eclipse.xtext.xbase.validation.IssueCodes
import org.eclipse.xtend.core.xtend.XtendFile
import org.eclipse.xtend.core.xtend.XtendPackage
import org.eclipse.xtext.testing.util.ParseHelper
Expand Down Expand Up @@ -53,7 +53,7 @@ class ValidationBug435020Test extends AbstractXtendTestCase {
'''
val c = parser.parse(source)
c.assertError(XtendPackage.Literals.XTEND_FIELD, IssueCodes.ANONYMOUS_CLASS_STATIC_FIELD,
source.indexOf("static"), "static".length,
source.indexOf("x"), 1,
"A static field of an anonymous class must be final"
)
}
Expand Down Expand Up @@ -88,7 +88,7 @@ class ValidationBug435020Test extends AbstractXtendTestCase {
'''
val c = parser.parse(source)
c.assertError(XtendPackage.Literals.XTEND_FUNCTION, IssueCodes.ANONYMOUS_CLASS_STATIC_METHOD,
source.indexOf("static"), "static".length,
source.indexOf("bar"), "bar".length,
"A method of an anonymous class cannot be static"
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static org.eclipse.xtend.core.xtend.XtendPackage.Literals.*;
import static org.eclipse.xtext.xbase.XbasePackage.Literals.*;
import static org.eclipse.xtext.xbase.validation.IssueCodes.*;
import static org.eclipse.xtext.xbase.validation.IssueCodes.CYCLIC_INHERITANCE;
import static org.eclipse.xtext.xtype.XtypePackage.Literals.*;

import java.util.Iterator;
Expand Down Expand Up @@ -952,10 +953,10 @@ public void clearPreferences() {
@Test public void testClassUniqueNames() throws Exception {
var source = "class Foo {} class Foo {}";
XtendClass clazz = clazz(source);
helper.assertError(clazz, XTEND_CLASS, DUPLICATE_TYPE_NAME,
helper.assertError(clazz, XTEND_CLASS, DUPLICATE_TYPE,
source.indexOf("Foo"), "Foo".length(),
"type", "already defined");
helper.assertError(clazz, XTEND_CLASS, DUPLICATE_TYPE_NAME,
helper.assertError(clazz, XTEND_CLASS, DUPLICATE_TYPE,
source.lastIndexOf("Foo"), "Foo".length(),
"type", "already defined");
}
Expand Down Expand Up @@ -1006,14 +1007,14 @@ public void clearPreferences() {

@Test public void testTypesUniqueNames() throws Exception {
XtendFile file = file("class Foo {} interface Foo {} annotation Foo {}");
helper.assertError(file, XTEND_INTERFACE, DUPLICATE_TYPE_NAME, "type", "already defined");
helper.assertError(file, XTEND_ANNOTATION_TYPE, DUPLICATE_TYPE_NAME, "type", "already defined");
helper.assertError(file, XTEND_INTERFACE, DUPLICATE_TYPE, "type", "already defined");
helper.assertError(file, XTEND_ANNOTATION_TYPE, DUPLICATE_TYPE, "type", "already defined");
}

@Test public void testNestedTypesUniqueNames() throws Exception {
XtendFile file = file("class C { static class Foo {} interface Foo {} annotation Foo {} }");
helper.assertError(file, XTEND_INTERFACE, DUPLICATE_TYPE_NAME, "Duplicate nested type Foo");
helper.assertError(file, XTEND_ANNOTATION_TYPE, DUPLICATE_TYPE_NAME, "Duplicate nested type Foo");
helper.assertError(file, XTEND_INTERFACE, DUPLICATE_TYPE, "Duplicate nested type Foo");
helper.assertError(file, XTEND_ANNOTATION_TYPE, DUPLICATE_TYPE, "Duplicate nested type Foo");
}

@Test public void testNestedTypesOuterNameShadowing() throws Exception {
Expand Down Expand Up @@ -2452,8 +2453,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 @@ -13,7 +13,6 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Objects;
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 @@ -171,7 +171,7 @@ public void testCannotOverrideWithConflictingReturnType() {
_builder.append("}");
_builder.newLine();
final String source = _builder.toString();
this._validationTestHelper.assertError(this.file(source), XtendPackage.Literals.XTEND_FIELD, org.eclipse.xtext.xbase.validation.IssueCodes.INCOMPATIBLE_RETURN_TYPE,
this._validationTestHelper.assertError(this.file(source), XtendPackage.Literals.XTEND_FIELD, IssueCodes.INCOMPATIBLE_RETURN_TYPE,
source.indexOf("int"), "int".length(),
"incompatible", "getFoo");
} catch (Throwable _e) {
Expand Down Expand Up @@ -359,7 +359,10 @@ 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");
final String source = _builder.toString();
this._validationTestHelper.assertError(this.file(source), XtendPackage.Literals.XTEND_FIELD, IssueCodes.INCOMPATIBLE_RETURN_TYPE,
source.lastIndexOf("String"), "String".length(),
"setFoo(String)", "incompatible");
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.google.inject.Inject;
import org.eclipse.xtend.core.tests.AbstractXtendTestCase;
import org.eclipse.xtend.core.tests.java8.Java8RuntimeInjectorProvider;
import org.eclipse.xtend.core.validation.IssueCodes;
import org.eclipse.xtend.core.xtend.XtendFile;
import org.eclipse.xtend.core.xtend.XtendPackage;
import org.eclipse.xtend2.lib.StringConcatenation;
Expand All @@ -21,6 +20,7 @@
import org.eclipse.xtext.xbase.XbasePackage;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.validation.IssueCodes;
import org.junit.Test;

/**
Expand Down Expand Up @@ -975,7 +975,7 @@ public void testAbstractMethodCall() {
_builder.newLine();
_builder.append("}");
_builder.newLine();
this._validationTestHelper.assertError(this.file(_builder.toString()), XbasePackage.Literals.XMEMBER_FEATURE_CALL, org.eclipse.xtext.xbase.validation.IssueCodes.ABSTRACT_METHOD_INVOCATION,
this._validationTestHelper.assertError(this.file(_builder.toString()), XbasePackage.Literals.XMEMBER_FEATURE_CALL, IssueCodes.ABSTRACT_METHOD_INVOCATION,
"Cannot directly invoke the abstract method foo() of the type A");
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
Expand All @@ -999,7 +999,7 @@ public void testInterfaceSuperCall() {
_builder.newLine();
_builder.append("}");
_builder.newLine();
this._validationTestHelper.assertError(this.file(_builder.toString()), XbasePackage.Literals.XMEMBER_FEATURE_CALL, org.eclipse.xtext.xbase.validation.IssueCodes.NO_ENCLOSING_INSTANCE_AVAILABLE,
this._validationTestHelper.assertError(this.file(_builder.toString()), XbasePackage.Literals.XMEMBER_FEATURE_CALL, IssueCodes.NO_ENCLOSING_INSTANCE_AVAILABLE,
"The enclosing type does not extend or implement the interface List");
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

import com.google.inject.Inject;
import org.eclipse.xtend.core.tests.AbstractXtendTestCase;
import org.eclipse.xtend.core.validation.IssueCodes;
import org.eclipse.xtend.core.xtend.XtendFile;
import org.eclipse.xtend.core.xtend.XtendPackage;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.testing.util.ParseHelper;
import org.eclipse.xtext.testing.validation.ValidationTestHelper;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.validation.IssueCodes;
import org.junit.Test;

/**
Expand Down Expand Up @@ -97,7 +97,7 @@ public void test_02() {
final String source = _builder.toString();
final XtendFile c = this.parser.parse(source);
this.helper.assertError(c, XtendPackage.Literals.XTEND_FIELD, IssueCodes.ANONYMOUS_CLASS_STATIC_FIELD,
source.indexOf("static"), "static".length(),
source.indexOf("x"), 1,
"A static field of an anonymous class must be final");
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
Expand Down Expand Up @@ -166,7 +166,7 @@ public void test_04() {
final String source = _builder.toString();
final XtendFile c = this.parser.parse(source);
this.helper.assertError(c, XtendPackage.Literals.XTEND_FUNCTION, IssueCodes.ANONYMOUS_CLASS_STATIC_METHOD,
source.indexOf("static"), "static".length(),
source.indexOf("bar"), "bar".length(),
"A method of an anonymous class cannot be static");
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
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 @@ -38,12 +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 INTERFACE_EXPECTED = ISSUE_CODE_PREFIX + "interface_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;
public static final String CONFLICTING_DEFAULT_METHODS = ISSUE_CODE_PREFIX + "conflicting_default_methods";
public static final String MISSING_ABSTRACT = ISSUE_CODE_PREFIX + "missing_abstract";
public static final String MISSING_ABSTRACT_IN_ANONYMOUS = ISSUE_CODE_PREFIX + "missing_abstract_in_anonymous";
public static final String MISSING_OVERRIDE = ISSUE_CODE_PREFIX + "missing_override";
Expand All @@ -54,25 +48,9 @@ private IssueCodes() {
public static final String XBASE_LIB_NOT_ON_CLASSPATH = ISSUE_CODE_PREFIX + "xbase_lib_not_on_classpath";
public static final String JDK_NOT_ON_CLASSPATH = ISSUE_CODE_PREFIX + "jdk_not_on_classpath";

public static final String CLASS_MUST_BE_ABSTRACT = ISSUE_CODE_PREFIX + "class_must_be_defined_abstract";
public static final String ANONYMOUS_CLASS_MISSING_MEMBERS = ISSUE_CODE_PREFIX + "anonymous_class_missing_members";
public static final String ANONYMOUS_CLASS_STATIC_METHOD = ISSUE_CODE_PREFIX + "anonymous_class_static_method";
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";
public static final String CYCLIC_INHERITANCE = ISSUE_CODE_PREFIX + "cyclic_inheritance";

public static final String UNUSED_PRIVATE_MEMBER = ISSUE_CODE_PREFIX + "unused_private_member";
public static final String FIELD_NOT_INITIALIZED = ISSUE_CODE_PREFIX + "field_not_initialized";
public static final String EXCEPTION_NOT_THROWABLE = ISSUE_CODE_PREFIX + "exception_not_throwable";
public static final String EXCEPTION_DECLARED_TWICE = ISSUE_CODE_PREFIX + "exception_declared_twice";

public static final String MISSING_CONSTRUCTOR = ISSUE_CODE_PREFIX + "missing_constructor";
public static final String MUST_INVOKE_SUPER_CONSTRUCTOR = ISSUE_CODE_PREFIX + "must_invoke_super_constructor";
public static final String CONSTRUCTOR_TYPE_PARAMS_NOT_SUPPORTED = ISSUE_CODE_PREFIX
+ "constructor_type_params_not_supported";
public static final String CONSTRUCTOR_NOT_PERMITTED = ISSUE_CODE_PREFIX + "constructor_not_permitted";
Expand All @@ -91,8 +69,6 @@ private IssueCodes() {
public static final String MISSING_STATIC_MODIFIER = ISSUE_CODE_PREFIX + "missing_static_modifier";
public static final String MODIFIER_DOES_NOT_MATCH_TYPENAME = ISSUE_CODE_PREFIX + "missing_abstract_modifier";

public static final String WILDCARD_IN_SUPERTYPE = ISSUE_CODE_PREFIX + "wildcard_in_supertype";

public static final String INVALID_EXTENSION_TYPE = ISSUE_CODE_PREFIX + "invalid_extension_type";

public static final String INVALID_OPERATOR_SIGNATURE = ISSUE_CODE_PREFIX + "invalid_operator_signature";
Expand Down Expand Up @@ -121,9 +97,5 @@ private IssueCodes() {
* @since 2.18
*/
public static final String TERNARY_EXPRESSION_NOT_ALLOWED = ISSUE_CODE_PREFIX + "ternary_if_operator_is_not_allowed";
/**
* @since 2.34
*/
public static final String DUPLICATE_INTERFACE = ISSUE_CODE_PREFIX + "duplicate_interface";

}
Loading

0 comments on commit f212edb

Please sign in to comment.