diff --git a/main/coreplugins/io.sarl.lang/src/io/sarl/lang/jvmmodel/SARLJvmModelInferrer.java b/main/coreplugins/io.sarl.lang/src/io/sarl/lang/jvmmodel/SARLJvmModelInferrer.java index b82a344f5f..63c70975af 100644 --- a/main/coreplugins/io.sarl.lang/src/io/sarl/lang/jvmmodel/SARLJvmModelInferrer.java +++ b/main/coreplugins/io.sarl.lang/src/io/sarl/lang/jvmmodel/SARLJvmModelInferrer.java @@ -1601,13 +1601,18 @@ protected void transform(final XtendFunction source, final JvmGenericType contai operation.setStatic(source.isStatic()); operation.setSynchronized(source.isSynchonized()); operation.setNative(source.isNative()); - final boolean enableFunctionBody; + boolean enableFunctionBody; if (container.isInterface()) { - enableFunctionBody = context != null && context.isAtLeastJava8() - && source.getExpression() != null && !operation.isAbstract() - && !operation.isStatic() - && !Utils.toLightweightTypeReference(container, this.services).isSubtypeOf(Capacity.class); - operation.setDefault(enableFunctionBody); + enableFunctionBody = false; + if (context != null && context.isAtLeastJava8() + && !Utils.toLightweightTypeReference(container, this.services).isSubtypeOf(Capacity.class)) { + if (operation.isStatic()) { + enableFunctionBody = true; + } else if (source.getExpression() != null && !operation.isAbstract()) { + enableFunctionBody = true; + } + } + operation.setDefault(enableFunctionBody && !operation.isStatic()); operation.setAbstract(!enableFunctionBody); operation.setFinal(false); } else { diff --git a/tests/io.sarl.lang.tests/src/test/java/io/sarl/lang/tests/bugs/to00999/Bug724.java b/tests/io.sarl.lang.tests/src/test/java/io/sarl/lang/tests/bugs/to00999/Bug724.java new file mode 100644 index 0000000000..2e799e7122 --- /dev/null +++ b/tests/io.sarl.lang.tests/src/test/java/io/sarl/lang/tests/bugs/to00999/Bug724.java @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2014-2017 the original authors or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.sarl.lang.tests.bugs.to00999; + +import com.google.inject.Inject; +import org.eclipse.xtext.xbase.lib.Functions.Function1; +import org.eclipse.xtext.xbase.testing.CompilationTestHelper; +import org.junit.Test; + +import io.sarl.lang.SARLVersion; +import io.sarl.lang.sarl.SarlPackage; +import io.sarl.lang.sarl.SarlScript; +import io.sarl.tests.api.AbstractSarlTest; + +/** Testing class for issue: Invalid static function generation in interface. + * + *

https://github.com/sarl/sarl/issues/724 + * + * @author $Author: sgalland$ + * @version $Name$ $Revision$ $Date$ + * @mavengroupid $GroupId$ + * @mavenartifactid $ArtifactId$ + */ +@SuppressWarnings("all") +public class Bug724 extends AbstractSarlTest { + + private static final String SNIPSET1 = multilineString( + "package io.sarl.lang.tests.bug724", + "interface MyInterface", + "{", + " static def myfct : Object {", + " null", + " }", + " def myfct2 : Object {", + " null", + " }", + " def myfct3 : Object", + "}"); + + private final String EXPECTED1 = multilineString( + "package io.sarl.lang.tests.bug724;", + "", + "import io.sarl.lang.annotation.SarlElementType;", + "import io.sarl.lang.annotation.SarlSpecification;", + "import org.eclipse.xtext.xbase.lib.Pure;", + "", + "@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")", + "@SarlElementType(" + SarlPackage.SARL_INTERFACE + ")", + "@SuppressWarnings(\"all\")", + "public interface MyInterface {", + " @Pure", + " public static Object myfct() {", + " return null;", + " }", + " ", + " @Pure", + " public default Object myfct2() {", + " return null;", + " }", + " ", + " public abstract Object myfct3();", + "}", + ""); + + @Inject + private CompilationTestHelper compiler; + + @Test + public void parsing_01() throws Exception { + SarlScript mas = file(SNIPSET1); + final Validator validator = validate(mas); + validator.assertNoErrors(); + } + + @Test + public void compiling_01() throws Exception { + this.compiler.compile(SNIPSET1, (it) -> { + final String actual = it.getGeneratedCode("io.sarl.lang.tests.bug724.MyInterface"); + assertEquals(EXPECTED1, actual); + }); + } + +}