Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Setting GeneratorConfig JavaSourceVersion
Setting GeneratorConfig JavaSourceVersion [message #1766217] Sun, 18 June 2017 03:39 Go to next message
Amit Yadav is currently offline Amit YadavFriend
Messages: 28
Registered: June 2017
Junior Member
Hello,

I am using Xbase to write a small DSL. I am trying to use XBlockExpression and with the block trying to initialize a ' val Map<String, String> some = new HashMap<>()';

However, this is generating an error when trying to compile it in to java code.

"ERROR:<> cannot be resolved."

I am suspecting that the DSL is compiled with Java 5.

How can i set the version to Java 8. I am using a standalone setup to compile this DSL.

Thank You
Re: Setting GeneratorConfig JavaSourceVersion [message #1766245 is a reply to message #1766217] Mon, 19 June 2017 09:06 Go to previous messageGo to next message
Jim Wang is currently offline Jim WangFriend
Messages: 4
Registered: May 2017
Junior Member
I worked around this problem by creating a new instance of OnTheFlyJavaCompiler2 and passing in JavaVersion.JAVA8. You can then use that compiler to compile the generated code.

If others have a better way, I would be interested to find out.

@Inject extension ParseHelper<YourModel>	

OnTheFlyJavaCompiler2 compiler = new OnTheFlyJavaCompiler2(this.class.classLoader, JavaVersion.JAVA8)

def compileJava8(CharSequence model) {
    // returns Map<String,String>
    val sourceCode = generateCodeAndLoadFromFileSystem(model)
    compiler.compileToClasses(sourceCode)
}
Re: Setting GeneratorConfig JavaSourceVersion [message #1766248 is a reply to message #1766245] Mon, 19 June 2017 10:09 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1812
Registered: July 2009
Location: Firenze, Italy
Senior Member
Hi Jim

if you don't want to use the OnTheFlyJavaCompiler2 directly in your test (and simply use CompilationTestHelper) you could define a custom injector provider, (see, e.g., https://github.com/LorenzoBettini/jbase/blob/master/jbase.tests/src/jbase/tests/util/Java7JbaseTestlanguageInjectorProvider.java) and use this in your compiler test :

package jbase.tests.util;

import org.eclipse.xtext.util.JavaVersion;
import org.eclipse.xtext.xbase.testing.OnTheFlyJavaCompiler2;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import jbase.testlanguage.JbaseTestlanguageRuntimeModule;
import jbase.testlanguage.tests.JbaseTestlanguageInjectorProvider;

public class Java7JbaseTestlanguageInjectorProvider extends JbaseTestlanguageInjectorProvider {

	@Singleton
	private static class Java7OnTheFlyJavaCompiler2 extends OnTheFlyJavaCompiler2 {
		@Inject
		public Java7OnTheFlyJavaCompiler2(ClassLoader scope) {
			super(scope, JavaVersion.JAVA7);
		}
	}

	@Override
	protected JbaseTestlanguageRuntimeModule createRuntimeModule() {
		return new JbaseTestlanguageRuntimeModule() {
			@Override
			public ClassLoader bindClassLoaderToInstance() {
				// in order to access example annotations in maven build
				return JbaseTestlanguageInjectorProvider.class.getClassLoader();
			}

			@SuppressWarnings("unused")
			public Class<? extends OnTheFlyJavaCompiler2> bindOnTheFlyJavaCompiler2() {
				return Java7OnTheFlyJavaCompiler2.class;
			}
		};
	}
}


Re: Setting GeneratorConfig JavaSourceVersion [message #1766262 is a reply to message #1766248] Mon, 19 June 2017 15:21 Go to previous messageGo to next message
Amit Yadav is currently offline Amit YadavFriend
Messages: 28
Registered: June 2017
Junior Member
Since I a using JvmModelInferrer and XBase compiler, I can not use OnTheFlyCompiler2. Is there no simply way to pass this information in MWE2 file ?
Re: Setting GeneratorConfig JavaSourceVersion [message #1766367 is a reply to message #1766248] Tue, 20 June 2017 15:25 Go to previous messageGo to next message
Jim Wang is currently offline Jim WangFriend
Messages: 4
Registered: May 2017
Junior Member
@Lorenzo Very helpful indeed, thank you. I will give this a try.

[Updated on: Tue, 20 June 2017 15:27]

Report message to a moderator

Re: Setting GeneratorConfig JavaSourceVersion [message #1766526 is a reply to message #1766367] Thu, 22 June 2017 16:11 Go to previous messageGo to next message
Amit Yadav is currently offline Amit YadavFriend
Messages: 28
Registered: June 2017
Junior Member
I am still hoping that someone would now how to enable using Java8 within the DSL.
Re: Setting GeneratorConfig JavaSourceVersion [message #1766546 is a reply to message #1766526] Thu, 22 June 2017 22:05 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
what do you mean by "However, this is generating an error when trying to compile it in to java code."
can you share a few lines e.g. based on the domain model example that let me reproduce that easily


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Setting GeneratorConfig JavaSourceVersion [message #1766623 is a reply to message #1766546] Fri, 23 June 2017 14:51 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Had a look at the codebase and it seems like diamond operator
Is not supported in xbase. Leaving out the <> does the same

Can you please file a ticket on github.com/eclipse/xtext-extras


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Setting GeneratorConfig JavaSourceVersion [message #1766628 is a reply to message #1766623] Fri, 23 June 2017 15:33 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
here is a potential workaround

import "http://www.eclipse.org/xtext/xbase/Xbase" as xbase
import "http://www.eclipse.org/xtext/common/JavaVMTypes" as types

		
@Override 
XConstructorCall returns xbase::XExpression:
	{xbase::XConstructorCall}
	'new' constructor=[types::JvmConstructor|QualifiedName]
	(=>'<>'| =>'<' (typeArguments+=JvmArgumentTypeReference (',' typeArguments+=JvmArgumentTypeReference)*)? '>')?
	(=>explicitConstructorCall?='('
		(
		    arguments+=XShortClosure
		  | arguments+=XExpression (',' arguments+=XExpression)*
		)?
	')')?
	arguments+=XClosure?;
		


but it may break existing code that makes use of the "operator diamond" operator like this one:

class Dully {
	def xxx() {
		val xxxx = new Dully<>
		1
	}
	
	def operator_diamond(Dully a, int b) {
		return a
	}
}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:xtext and xcore with gradle
Next Topic:syntax highlighting for different groups of keywords
Goto Forum:
  


Current Time: Tue Apr 23 17:27:40 GMT 2024

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

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

Back to the top