Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtend with Maven does not preserved semantic
Xtend with Maven does not preserved semantic [message #1837458] Sat, 30 January 2021 14:34
Théo Giraudet is currently offline Théo GiraudetFriend
Messages: 9
Registered: January 2021
Junior Member
Hey!
I try to work a bit with Xtend and Xcore for a student project. For that, I configured a Maven project with dependencies to generate xtend/xcore sources by the Maven "generate-sources" phase.
Xtend classes and Xcore models are well compiled to Java, but it seems that a Xtend constant (val) in a try-with-resource is not compile to a final variable in Java when I execute the "mvn clean generate-sources" command.
However, when Eclipse compile the xtend/xcore classes, this problem does not appear.
I reproducted the problem in a minimal project:

Bar Xtend class:

class Bar {
	
	def static void main(String... args) {
		try(val foo = new BufferedInputStream(System.in)) {
			val bar = [| bar(foo)]
			bar.apply
		}
	}
	
	def static void bar(BufferedInputStream bis) {
		System.out.println(new BufferedReader((new InputStreamReader(bis))).lines().collect(Collectors.joining("\n")))
	}
	
}


Product code by Maven:

public class Bar {
  public static void main(final String... args) {
    try {
      List<Throwable> _ts = new ArrayList<Throwable>();
      BufferedInputStream foo = null;
      try {
        foo = new BufferedInputStream(System.in);
        final Procedure0 _function = new Procedure0() {
          public void apply() {
            Bar.bar(foo);
          }
        };
        final Procedure0 bar = _function;
        bar.apply();
      } finally {
        if (foo != null) {
          try {
            foo.close();
          } catch (Throwable _t) {
            _ts.add(_t);
          }
        }
        if(!_ts.isEmpty()) throw Exceptions.sneakyThrow(_ts.get(0));
      }
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  public static void bar(final BufferedInputStream bis) {
    InputStreamReader _inputStreamReader = new InputStreamReader(bis);
    System.out.println(new BufferedReader(_inputStreamReader).lines().collect(Collectors.joining("\n")));
  }
}


The product code has a compilation error. Indeed, the line 10 (Bar.bar(foo);), throw "Local variable foo defined in an enclosing scope must be final or effectively final".

To compare, product code by Eclipse:

@SuppressWarnings("all")
public class Bar {
  public static void main(final String... args) {
    try {
      try (final BufferedInputStream foo = new BufferedInputStream(System.in)) {
        final Procedure0 _function = () -> {
          Bar.bar(foo);
        };
        final Procedure0 bar = _function;
        bar.apply();
      }
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  public static void bar(final BufferedInputStream bis) {
    InputStreamReader _inputStreamReader = new InputStreamReader(bis);
    System.out.println(new BufferedReader(_inputStreamReader).lines().collect(Collectors.joining("\n")));
  }
}


I have the impression that Maven does not compile to Java 8 code but I think I set up the version in the pom.xml.

My pom.xml for the minimal project:
	<modelVersion>4.0.0</modelVersion>
	<groupId>fr.theogiraudet</groupId>
	<version>1.0.0-SNAPSHOT</version>
	<artifactId>fr.theogiraudet.foo</artifactId>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>

		<core-resources-version>3.7.100</core-resources-version>
		<emf-version>2.23.0</emf-version>
		<emf-common-version>2.21.0</emf-common-version>
		<xtext-version>2.24.0</xtext-version>
		<ecore-xtext-version>1.5.0</ecore-xtext-version>
		<ecore-xcore-version>1.16.0</ecore-xcore-version>
		<ecore-xcore-lib-version>1.5.0</ecore-xcore-lib-version>
		
		<xtend-path>${basedir}/src/main/xtend</xtend-path>
		<xcore-path>${basedir}/model</xcore-path>
		<xtend-gen-path>${basedir}/src/main/javagen</xtend-gen-path>
		<xcore-gen-path>${basedir}/src/main/javagen</xcore-gen-path>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.eclipse.emf</groupId>
			<artifactId>org.eclipse.emf.common</artifactId>
			<version>${emf-common-version}</version>
		</dependency>
		<dependency>
			<groupId>org.eclipse.emf</groupId>
			<artifactId>org.eclipse.emf.ecore</artifactId>
			<version>${emf-version}</version>
		</dependency>
		<dependency>
			<groupId>org.eclipse.emf</groupId>
			<artifactId>org.eclipse.emf.ecore.xcore.lib</artifactId>
			<version>${ecore-xcore-lib-version}</version>
		</dependency>
		<dependency>
			<groupId>org.eclipse.xtext</groupId>
			<artifactId>org.eclipse.xtext.xbase.lib</artifactId>
			<version>${xtext-version}</version>
		</dependency>
		<dependency>
			<groupId>org.eclipse.xtend</groupId>
			<artifactId>org.eclipse.xtend.lib</artifactId>
			<version>${xtext-version}</version>
		</dependency>
		<dependency>
			<groupId>org.eclipse.emf</groupId>
			<artifactId>org.eclipse.emf.ecore.xmi</artifactId>
			<version>2.16.0</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.3</version>
			</plugin>

			<plugin>
				<artifactId>maven-clean-plugin</artifactId>
				<version>2.6.1</version>
				<configuration>
					<filesets>
						<fileset>
							<excludes>
								<exclude>.dummy.txt</exclude>
							</excludes>
							<directory>${xtend-gen-path}</directory>
						</fileset>
						<fileset>
							<excludes>
								<exclude>.dummy.txt</exclude>
							</excludes>
							<directory>${xtend-gen-path}</directory>
						</fileset>
					</filesets>
				</configuration>
			</plugin>

			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>build-helper-maven-plugin</artifactId>
				<version>1.9.1</version>
				<executions>
					<execution>
						<id>add-source</id>
						<phase>generate-sources</phase>
						<goals>
							<goal>add-source</goal>
						</goals>
						<configuration>
							<sources>
								<source>${xtend-gen-path}</source>
							</sources>
						</configuration>
					</execution>
				</executions>
			</plugin>

			<plugin>
				<groupId>org.eclipse.xtext</groupId>
				<artifactId>xtext-maven-plugin</artifactId>
				<version>${xtext-version}</version>
				<executions>
					<execution>
						<phase>generate-sources</phase>
						<goals>
							<goal>generate</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<languages>
						<language>
							<setup>org.eclipse.emf.codegen.ecore.xtext.GenModelSupport</setup>
						</language>
						<language>
							<setup>org.eclipse.xtend.core.XtendStandaloneSetup</setup>
							<outputConfigurations>
								<outputConfiguration>
									<outputDirectory>${xtend-gen-path}</outputDirectory>
								</outputConfiguration>
							</outputConfigurations>
						</language>
						<language>
							<setup>org.eclipse.emf.ecore.xcore.XcoreStandaloneSetup</setup>
							<outputConfigurations>
								<outputConfiguration>
									<outputDirectory>${xcore-gen-path}</outputDirectory>
								</outputConfiguration>
							</outputConfigurations>

						</language>
					</languages>
					<sourceRoots>
						<root>${xtend-path}</root>
						<root>${xcore-path}</root>
					</sourceRoots>
					<javaSourceVersion>1.8</javaSourceVersion>
				</configuration>
				<dependencies>
					<dependency>
						<groupId>org.eclipse.core</groupId>
						<artifactId>org.eclipse.core.resources</artifactId>
						<version>${core-resources-version}</version>
					</dependency>
					<dependency>
						<groupId>org.eclipse.xtend</groupId>
						<artifactId>org.eclipse.xtend.core</artifactId>
						<version>${xtext-version}</version>
					</dependency>
					<dependency>
						<groupId>org.eclipse.emf</groupId>
						<artifactId>org.eclipse.emf.codegen.ecore.xtext</artifactId>
						<version>${ecore-xtext-version}</version>
					</dependency>
					<dependency>
						<groupId>org.eclipse.emf</groupId>
						<artifactId>org.eclipse.emf.ecore.xcore</artifactId>
						<version>${ecore-xcore-version}</version>
					</dependency>
					<dependency>
						<groupId>org.eclipse.emf</groupId>
						<artifactId>org.eclipse.emf.ecore.xcore.lib</artifactId>
						<version>${ecore-xcore-lib-version}</version>
					</dependency>
				</dependencies>
			</plugin>
		</plugins>
	</build>


For Eclipse, I use:
- Xtend IDE plugin (v.2.24.0)
- Xtext Complete SDK (v.2.24.0)
- EMF - Eclipse Modeling Framework Xcore SDK (v.1.16.0)

You can clone the minimal project here:
theogiraudet/XtendMinimalProject.git

Thank you in advance for your help!

[Updated on: Sat, 30 January 2021 16:41]

Report message to a moderator

Previous Topic:Defining variable inside your language document?
Next Topic:Use XText in batch mode
Goto Forum:
  


Current Time: Thu Apr 25 08:52:59 GMT 2024

Powered by FUDForum. Page generated in 0.02746 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top