Skip to content

Commit

Permalink
[#2259] allow extension fields in java 8 interfaces
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Dietrich <[email protected]>
  • Loading branch information
cdietrich committed Apr 18, 2023
1 parent 7adea6f commit 1a2549c
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
* Copyright (c) 2015, 2023 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
Expand Down Expand Up @@ -36,6 +36,30 @@ class Java8Compiler2Test extends XtendCompilerTest {
''')
}

@Test def void testStaticMethodInInterface2() {
'''
class B implements A {
def void m2(int i) {
i.substring();
}
}
interface A {
@Extension
public static String s;
def void m1(int i) {
i.substring();
}
}
'''.assertCompilesTo('''
@SuppressWarnings("all")
public class B implements A {
public void m2(final int i) {
A.s.substring(i);
}
}
''')
}

@Test
override testMultiCatch_02() {
assertCompilesTo('''
Expand Down Expand Up @@ -1576,4 +1600,35 @@ class Java8Compiler2Test extends XtendCompilerTest {
''')
}

}
@Test def void testExtensionFieldInInterface() {
'''
package foo;
interface Foo {
extension Bar bar = new Bar
def void someMethod(String assert) {
"".doit
}
}
class Bar {
def void doit(String a) {}
}
'''.assertCompilesTo('''
package foo;
import org.eclipse.xtext.xbase.lib.Extension;
@SuppressWarnings("all")
public interface Foo {
@Extension
static final Bar bar = new Bar();
default void someMethod(final String assert_) {
Foo.bar.doit("");
}
}
''')
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2015, 2016 itemis AG (http://www.itemis.eu) and others.
* Copyright (c) 2015, 2023 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
Expand Down Expand Up @@ -519,4 +519,12 @@ class Java8ValidationTest extends AbstractXtendTestCase {
"The enclosing type does not extend or implement the interface List")
}

@Test
def void testInterfaceExtensionField() {
file('''
interface MyIf {
extension Integer foo
}
''').assertNoErrors
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
* Copyright (c) 2015, 2023 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
Expand Down Expand Up @@ -55,6 +55,60 @@ public void testStaticMethodInInterface() {
this.assertCompilesTo(_builder, _builder_1);
}

@Test
public void testStaticMethodInInterface2() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("class B implements A {");
_builder.newLine();
_builder.append("\t");
_builder.append("def void m2(int i) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("i.substring();");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.append("interface A {");
_builder.newLine();
_builder.append("\t");
_builder.append("@Extension");
_builder.newLine();
_builder.append("\t");
_builder.append("public static String s;");
_builder.newLine();
_builder.append("\t");
_builder.append("def void m1(int i) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("i.substring();");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("@SuppressWarnings(\"all\")");
_builder_1.newLine();
_builder_1.append("public class B implements A {");
_builder_1.newLine();
_builder_1.append(" ");
_builder_1.append("public void m2(final int i) {");
_builder_1.newLine();
_builder_1.append(" ");
_builder_1.append("A.s.substring(i);");
_builder_1.newLine();
_builder_1.append(" ");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
this.assertCompilesTo(_builder, _builder_1);
}

@Test
@Override
public void testMultiCatch_02() {
Expand Down Expand Up @@ -3582,4 +3636,66 @@ public void testJavaKeywordsUsed() {
_builder_1.newLine();
this.assertCompilesTo(_builder, _builder_1);
}

@Test
public void testExtensionFieldInInterface() {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package foo;");
_builder.newLine();
_builder.newLine();
_builder.append("interface Foo {");
_builder.newLine();
_builder.append("\t");
_builder.append("extension Bar bar = new Bar");
_builder.newLine();
_builder.append("\t");
_builder.append("def void someMethod(String assert) {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("\"\".doit");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("class Bar {");
_builder.newLine();
_builder.append("\t");
_builder.append("def void doit(String a) {}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package foo;");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("import org.eclipse.xtext.xbase.lib.Extension;");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append("@SuppressWarnings(\"all\")");
_builder_1.newLine();
_builder_1.append("public interface Foo {");
_builder_1.newLine();
_builder_1.append(" ");
_builder_1.append("@Extension");
_builder_1.newLine();
_builder_1.append(" ");
_builder_1.append("static final Bar bar = new Bar();");
_builder_1.newLine();
_builder_1.newLine();
_builder_1.append(" ");
_builder_1.append("default void someMethod(final String assert_) {");
_builder_1.newLine();
_builder_1.append(" ");
_builder_1.append("Foo.bar.doit(\"\");");
_builder_1.newLine();
_builder_1.append(" ");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
this.assertCompilesTo(_builder, _builder_1);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2015, 2016 itemis AG (http://www.itemis.eu) and others.
* Copyright (c) 2015, 2023 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
Expand Down Expand Up @@ -997,4 +997,21 @@ public void testInterfaceSuperCall() {
throw Exceptions.sneakyThrow(_e);
}
}

@Test
public void testInterfaceExtensionField() {
try {
StringConcatenation _builder = new StringConcatenation();
_builder.append("interface MyIf {");
_builder.newLine();
_builder.append("\t");
_builder.append("extension Integer foo");
_builder.newLine();
_builder.append("}");
_builder.newLine();
this._validationTestHelper.assertNoErrors(this.file(_builder.toString()));
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2011, 2018 itemis AG (http://www.itemis.eu) and others.
* Copyright (c) 2011, 2023 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
Expand Down Expand Up @@ -282,9 +282,13 @@ protected GeneratorConfig getGeneratorConfig(EObject element) {
if (result.getJavaSourceVersion().isAtLeast(JAVA8)) {
methodInInterfaceModifierValidator = new ModifierValidator(
newArrayList("public", "abstract", "static", "def", "override"), this);
fieldInInterfaceModifierValidator = new ModifierValidator(
newArrayList("public", "static", "final", "val", "extension"), this);
} else {
methodInInterfaceModifierValidator = new ModifierValidator(
newArrayList("public", "abstract", "def", "override"), this);
fieldInInterfaceModifierValidator = new ModifierValidator(
newArrayList("public", "static", "final", "val"), this);
}
}
return result;
Expand Down Expand Up @@ -2014,16 +2018,15 @@ protected boolean hasAnnotation(Iterable<? extends XAnnotation> annotations, Cla
private final ModifierValidator fieldModifierValidator = new ModifierValidator(
newArrayList("public", "protected", "package", "private", "static", "final", "val", "var", "extension", "volatile", "transient"), this);

private final ModifierValidator fieldInInterfaceModifierValidator = new ModifierValidator(
newArrayList("public", "static", "final", "val"), this);

private final ModifierValidator constructorModifierValidator = new ModifierValidator(
newArrayList(visibilityModifers), this);

private final ModifierValidator methodModifierValidator = new ModifierValidator(
newArrayList("public", "protected", "package", "private", "static", "abstract", "dispatch", "final", "def", "override", "strictfp", "native", "synchronized"), this);

private ModifierValidator methodInInterfaceModifierValidator;

private ModifierValidator fieldInInterfaceModifierValidator;

@Check
protected void checkModifiers(XtendClass xtendClass) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2012 itemis AG (http://www.itemis.eu) and others.
* Copyright (c) 2012, 2023 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
Expand Down Expand Up @@ -1011,10 +1011,12 @@ protected IFeatureScopeSession addExtensionFieldsToMemberSession(
}
}
// traverse the type hierarchy to create the feature scope sessions
JvmTypeReference superType = getExtendedClass(type);
IFeatureScopeSession result = featureScopeSession;
if (superType != null) {
result = addExtensionFieldsToMemberSession(resolvedTypes, featureScopeSession, (JvmDeclaredType) superType.getType(), thisFeature, seenNames, seenTypes);
for (JvmTypeReference st : type.getSuperTypes()) {
JvmType candidateType = st.getType();
if (candidateType instanceof JvmGenericType) {
result = addExtensionFieldsToMemberSession(resolvedTypes, featureScopeSession, (JvmDeclaredType) candidateType, thisFeature, seenNames, seenTypes);
}
}
if (extensionProviders != null) {
result = result.addToExtensionScope(extensionProviders);
Expand Down

0 comments on commit 1a2549c

Please sign in to comment.