Skip to content

Commit

Permalink
[all] Add support for try-with-resource statements.
Browse files Browse the repository at this point in the history
close #714

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Jun 27, 2019
1 parent 0b52147 commit 3fda23a
Show file tree
Hide file tree
Showing 2 changed files with 245 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ non-final variables:

[:Success:]
package io.sarl.docs.reference.gsr
import java.io.IOException
import java.io.IOException
agent A {
[:On]
def readFromFile : String { }
Expand All @@ -75,6 +75,46 @@ non-final variables:
[:End:]


## The try-with-resources Statement

The try-with-resources statement is a try statement that declares one or more resources. A resource
is an object that must be closed after the program is finished with it. The try-with-resources
statement ensures that each resource is closed at the end of the statement. Any object that
implements `java.lang.AutoCloseable`, which includes all objects which implement `java.io.Closeable`,
can be used as a resource.

The following example reads the first line from a file. It uses an instance of [:bufferedreadertype:]
to read data from the file. [:bufferedreadertype:] is a resource that must be closed after the
program is finished with it:

[:Success:]
package io.sarl.docs.reference.gsr
import java.io.BufferedReader
import java.io.FileReader
class A {
[:On]
static def readFirstLineFromFile(path : String) : String {
[:trykw](try) (var br = new [:bufferedreadertype](BufferedReader)(new FileReader(path))) {
return br.readLine
}
}
[:Off]
}
[:End:]



In this example, the resource declared in the try-with-resources statement is a [:bufferedreadertype:].
The declaration statement appears within parentheses immediately after the [:trykw:] keyword.
The class [:bufferedreadertype:] implements the interface `java.lang.AutoCloseable`.
Because the [:bufferedreadertype:] instance is declared in a try-with-resource statement, it will be
closed regardless of whether the try statement completes normally or abruptly (as a result of the method
`[:bufferedreadertype!].readLine` throwing an `IOException`).






[:Include:](../generalsyntaxref.inc)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2019 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: Generates try-with-resources.
*
* <p>https://github.com/sarl/sarl/issues/714
*
* @author $Author: sgalland$
* @version $Name$ $Revision$ $Date$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
@SuppressWarnings("all")
public class Bug714 extends AbstractSarlTest {

private static final String SARL_CODE_01 = multilineString(
"package io.sarl.lang.tests.bug714",
"import java.io.FileReader",
"import java.nio.CharBuffer",
"class X {",
" def test {",
" try (val text = new FileReader('file.txt')) {",
" val buf = CharBuffer::allocate(1024)",
" text.read(buf)",
" buf.rewind",
" buf.toString",
" }",
" }",
"}");

private final String JAVA_CODE_01 = multilineString(
"package io.sarl.lang.tests.bug714;",
"",
"import io.sarl.lang.annotation.SarlElementType;",
"import io.sarl.lang.annotation.SarlSpecification;",
"import io.sarl.lang.annotation.SyntheticMember;",
"import java.io.FileReader;",
"import java.nio.CharBuffer;",
"import org.eclipse.xtext.xbase.lib.Exceptions;",
"import org.eclipse.xtext.xbase.lib.Functions.Function0;",
"",
"@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")",
"@SarlElementType(" + SarlPackage.SARL_CLASS + ")",
"@SuppressWarnings(\"all\")",
"public class X {",
" public String test() {",
" try {",
" String _xtrycatchfinallyexpression = null;",
" try (final FileReader text = new Function0<FileReader>() {",
" public FileReader apply() {",
" try {",
" return new FileReader(\"file.txt\");",
" } catch (Throwable _e) {",
" throw Exceptions.sneakyThrow(_e);",
" }",
" }",
" }.apply()) {",
" String _xblockexpression = null;",
" {",
" final CharBuffer buf = CharBuffer.allocate(1024);",
" text.read(buf);",
" buf.rewind();",
" _xblockexpression = buf.toString();",
" }",
" _xtrycatchfinallyexpression = _xblockexpression;",
" }",
" return _xtrycatchfinallyexpression;",
" } catch (Throwable _e) {",
" throw Exceptions.sneakyThrow(_e);",
" }",
" }",
" ",
" @SyntheticMember",
" public X() {",
" super();",
" }",
"}",
"");

@Test
public void parsing_01() throws Exception {
SarlScript mas = file(SARL_CODE_01);
final Validator validator = validate(mas);
validator.assertNoErrors();
}

@Test
public void compiling_01() throws Exception {
getCompileHelper().compile(SARL_CODE_01, (it) -> {
final String actual = it.getGeneratedCode("io.sarl.lang.tests.bug714.X");
assertEquals(JAVA_CODE_01, actual);
});
}

private static final String SARL_CODE_02 = multilineString(
"package io.sarl.lang.tests.bug714",
"import java.io.FileReader",
"import java.nio.CharBuffer",
"class X {",
" def test {",
" try (var text = new FileReader('file.txt')) {",
" val buf = CharBuffer::allocate(1024)",
" text.read(buf)",
" buf.rewind",
" buf.toString",
" }",
" }",
"}");

private final String JAVA_CODE_02 = multilineString(
"package io.sarl.lang.tests.bug714;",
"",
"import io.sarl.lang.annotation.SarlElementType;",
"import io.sarl.lang.annotation.SarlSpecification;",
"import io.sarl.lang.annotation.SyntheticMember;",
"import java.io.FileReader;",
"import java.nio.CharBuffer;",
"import org.eclipse.xtext.xbase.lib.Exceptions;",
"import org.eclipse.xtext.xbase.lib.Functions.Function0;",
"",
"@SarlSpecification(\"" + SARLVersion.SPECIFICATION_RELEASE_VERSION_STRING + "\")",
"@SarlElementType(" + SarlPackage.SARL_CLASS + ")",
"@SuppressWarnings(\"all\")",
"public class X {",
" public String test() {",
" try {",
" String _xtrycatchfinallyexpression = null;",
" try (FileReader text = new Function0<FileReader>() {",
" public FileReader apply() {",
" try {",
" return new FileReader(\"file.txt\");",
" } catch (Throwable _e) {",
" throw Exceptions.sneakyThrow(_e);",
" }",
" }",
" }.apply()) {",
" String _xblockexpression = null;",
" {",
" final CharBuffer buf = CharBuffer.allocate(1024);",
" text.read(buf);",
" buf.rewind();",
" _xblockexpression = buf.toString();",
" }",
" _xtrycatchfinallyexpression = _xblockexpression;",
" }",
" return _xtrycatchfinallyexpression;",
" } catch (Throwable _e) {",
" throw Exceptions.sneakyThrow(_e);",
" }",
" }",
" ",
" @SyntheticMember",
" public X() {",
" super();",
" }",
"}",
"");

@Test
public void parsing_02() throws Exception {
SarlScript mas = file(SARL_CODE_02);
final Validator validator = validate(mas);
validator.assertNoErrors();
}

@Test
public void compiling_02() throws Exception {
getCompileHelper().compile(SARL_CODE_02, (it) -> {
final String actual = it.getGeneratedCode("io.sarl.lang.tests.bug714.X");
assertEquals(JAVA_CODE_02, actual);
});
}

}

0 comments on commit 3fda23a

Please sign in to comment.