Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Specifying Java version when running xtext.generator.GeneratorComponent from mwe2
Specifying Java version when running xtext.generator.GeneratorComponent from mwe2 [message #1711218] Wed, 14 October 2015 07:42 Go to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1751
Registered: July 2009
Location: Firenze, Italy
Senior Member
Hi

I have a mwe2 in one of my test projects to run the compiler of my Xbase
DSL in that project from Eclipse (to avoid running another Eclipse
instance just to compile my input files). Something like this:

component = org.eclipse.xtext.generator.GeneratorComponent {
register = MyDslStandaloneSetup {}
slot = 'model'
outlet = {
path = targetDir
}
}

Everything works, but Xbase generates everything according to Java 5,
although the launch configuration for mwe2 uses the same JRE of the
workspace, and the project has Java 7 requirement...

is there a way to pass the Java version to the GeneratorComponent so
that the Xbase compiler uses the right Java version?

thanks in advance
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it
Xtext Book:
http://www.packtpub.com/implementing-domain-specific-languages-with-xtext-and-xtend/book
Re: Specifying Java version when running xtext.generator.GeneratorComponent from mwe2 [message #1711764 is a reply to message #1711218] Mon, 19 October 2015 14:17 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3099
Registered: July 2009
Senior Member
Hi Lorenzo,

I'm afraid you'll need a custom runtime binding for the
org.eclipse.xtext.xbase.compiler.GeneratorConfigProvider to adjust the
Java version if you use the GeneratorComponent.
Maybe someone else has a better idea.

Best,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Find help at http://xtext.itemis.com or xtext(@)itemis.com
Blog: zarnekow.blogspot.com
Twitter: @szarnekow
Google+: https://www.google.com/+SebastianZarnekow

On 14.10.15 09:42, Lorenzo Bettini wrote:
> Hi
>
> I have a mwe2 in one of my test projects to run the compiler of my Xbase
> DSL in that project from Eclipse (to avoid running another Eclipse
> instance just to compile my input files). Something like this:
>
> component = org.eclipse.xtext.generator.GeneratorComponent {
> register = MyDslStandaloneSetup {}
> slot = 'model'
> outlet = {
> path = targetDir
> }
> }
>
> Everything works, but Xbase generates everything according to Java 5,
> although the launch configuration for mwe2 uses the same JRE of the
> workspace, and the project has Java 7 requirement...
>
> is there a way to pass the Java version to the GeneratorComponent so
> that the Xbase compiler uses the right Java version?
>
> thanks in advance
> Lorenzo
>
Re: Specifying Java version when running xtext.generator.GeneratorComponent from mwe2 [message #1711865 is a reply to message #1711764] Tue, 20 October 2015 08:07 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1751
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 19/10/2015 16:17, Sebastian Zarnekow wrote:
> Hi Lorenzo,
>
> I'm afraid you'll need a custom runtime binding for the
> org.eclipse.xtext.xbase.compiler.GeneratorConfigProvider to adjust the
> Java version if you use the GeneratorComponent.
> Maybe someone else has a better idea.
>
> Best,
> Sebastian

Hi Sebastian

I suspected that ;)

do you think it's worthwhile to file a feature enhancement?

cheers
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
HOME: http://www.lorenzobettini.it
Xtext Book:
http://www.packtpub.com/implementing-domain-specific-languages-with-xtext-and-xtend/book
Re: Specifying Java version when running xtext.generator.GeneratorComponent from mwe2 [message #1712115 is a reply to message #1711865] Wed, 21 October 2015 11:38 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3099
Registered: July 2009
Senior Member
Hi Lorenzo,

We do accept patches :p
For the time being a feature request would be fine, too.

Best,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Find help at http://xtext.itemis.com or xtext(@)itemis.com
Blog: zarnekow.blogspot.com
Twitter: @szarnekow
Google+: https://www.google.com/+SebastianZarnekow
Re: Specifying Java version when running xtext.generator.GeneratorComponent from mwe2 [message #1727307 is a reply to message #1712115] Tue, 22 March 2016 00:12 Go to previous messageGo to next message
Larry LeBron is currently offline Larry LeBronFriend
Messages: 124
Registered: October 2015
Senior Member
I've been running into what may be a related issue with trying to test my DSL using CompilationTestHelper.

My language uses features that are new to Java 1.8 (e.g. lambda expressions). They work fine when using runtime eclipse, but fail with these kinds of errors when I attempt to run my tests:

java.lang.AssertionError: java.lang.IllegalArgumentException: Java code compiled with errors:
Pb(645) Lambda expressions are allowed only at source level 1.8 or above
Pb(891) Additional bounds are not allowed in cast operator at source levels below 1.8


I did a bit of digging, and it looks like CompilationTestHelper has an injected OnTheFlyJavaCompiler2 which is initialized during setJavaCompilerClassPath(). During this method it initializes the OnTheFlyJavaCompiler2's java version via:
generatorConfigProvider.get(null).getJavaSourceVersion()


This calls the injected IGeneratorConfigProvider which is presumably an instance of GeneratorConfigProvider, which, in its get() method returns a new GeneratorConfig. GeneratorConfig seems to be hard-coded to use JavaVersion.JAVA5.

So, would binding a custom org.eclipse.xtext.xbase.compiler.GeneratorConfigProvider be my best bet, or is there a way to more easily update the CompilationTestHelper's version to JavaVersion.8?

I just stumbled across the JavaVersionModule class which sounds like it might also do what I'm looking for, but it's unclear how precisely to use it.

Thanks!

[Updated on: Tue, 22 March 2016 00:22]

Report message to a moderator

Re: Specifying Java version when running xtext.generator.GeneratorComponent from mwe2 [message #1727316 is a reply to message #1727307] Tue, 22 March 2016 07:03 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 12388
Registered: July 2009
Senior Member
have a look at https://github.com/eclipse/xtext/blob/04fd352e4ba151e7a267a58c14ba8707f3433c66/tests/org.eclipse.xtext.xbase.tests/src/org/eclipse/xtext/xbase/tests/compiler/AbstractOutputComparingCompilerTests.java



Need professional support for Xtext, Xpand, EMF?
Go to: https://xtext.itemis.com
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Specifying Java version when running xtext.generator.GeneratorComponent from mwe2 [message #1731123 is a reply to message #1727316] Tue, 03 May 2016 02:47 Go to previous messageGo to next message
Larry LeBron is currently offline Larry LeBronFriend
Messages: 124
Registered: October 2015
Senior Member
Thanks for the pointer.

I attempted to solve this by making a custom injector for my tests project, with custom bindings for GeneratorConfigProvider and GeneratorConfig to supply JavaVersion.JAVA8 as the Java version.

Unfortunately, for my tests, the Java compiler being injected for CompilationTestHelper wasn't being initialized with this value. It looks like, in this case, CompilationTestHelper was using the @Inject version of the OnTheFlyJavaCompiler2 constructor, which is hard-coded to initialize the Java Compiler to JavaVersion.JAVA6.

So, for now, my solution has been to just copy the entire logic of CompilationTestHelper into my own version, where I've customized the compile method like this. Maybe not the most elegant solution, but it's working.

	public void compile(final ResourceSet resourceSet, IAcceptor<Result> acceptor) {
		try {
			List<Resource> resourcesToCheck = newArrayList(resourceSet.getResources());
			Result result = resultProvider.get();
                       
                        // create a new java compiler, using Java 1.8
			this.javaCompiler = new OnTheFlyJavaCompiler2(new ClassLoader() {}, JavaVersion.JAVA8);
			result.setJavaCompiler(javaCompiler);
			result.setCheckMode(getCheckMode());
			result.setResources(resourcesToCheck);
			result.setResourceSet(resourceSet);
			result.setOutputConfigurations(getOutputConfigurations());
			result.doGenerate();
			acceptor.accept(result);
		} catch (Exception e) {
			Exceptions.throwUncheckedException(e);
		}
	}


So,
Re: Specifying Java version when running xtext.generator.GeneratorComponent from mwe2 [message #1783859 is a reply to message #1731123] Mon, 19 March 2018 16:17 Go to previous message
Martin Trummer is currently offline Martin TrummerFriend
Messages: 17
Registered: December 2017
Junior Member
since xtext 2.11 we can use: CompilationTestHelper.setJava()

e.g. in my CompilerTestBase class:
import javax.inject.Inject
import org.eclipse.xtext.xbase.testing.CompilationTestHelper
import org.junit.Before
import org.eclipse.xtext.util.JavaVersion

class CompileTestBase extends TestBase {
	
	@Inject 
	protected extension CompilationTestHelper

	@Before
	public def setJavaVersion() {
		javaVersion = JavaVersion.JAVA8
	}	
}
Previous Topic:Installing xtext
Next Topic:instantiate object with type parameter
Goto Forum:
  


Current Time: Fri Nov 16 11:40:37 GMT 2018

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

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

Back to the top