Home » Modeling » TMF (Xtext) » Specifying Java version when running xtext.generator.GeneratorComponent from mwe2
| | | |
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 |
Larry LeBron 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 #1731123 is a reply to message #1727316] |
Tue, 03 May 2016 02:47 |
Larry LeBron 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,
|
|
| |
Goto Forum:
Current Time: Thu Sep 19 05:08:44 GMT 2024
Powered by FUDForum. Page generated in 0.04148 seconds
|