Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtend generated code uses Java8 API's even though the project is still JavaSE-1.7
Xtend generated code uses Java8 API's even though the project is still JavaSE-1.7 [message #1461307] Tue, 04 November 2014 12:26 Go to next message
Markus Duft is currently offline Markus DuftFriend
Messages: 148
Registered: February 2013
Senior Member
Hey,

I switched to a Java 8 JDK recently, and noticed that the generated Xtend code changes, even though the project compatibility is still Java 7. That would break at runtime if using a Java 7 VM, right? Is there any hints/rationale/... on that?

Thanks, Cheers,
Markus
Re: Xtend generated code uses Java8 API's even though the project is still JavaSE-1.7 [message #1461383 is a reply to message #1461307] Tue, 04 November 2014 13:56 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3098
Registered: July 2009
Senior Member
It will generate Java7 code if you have a Java7 JRE on the project's
classpath.

Best,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: Xtend generated code uses Java8 API's even though the project is still JavaSE-1.7 [message #1461402 is a reply to message #1461383] Tue, 04 November 2014 14:21 Go to previous messageGo to next message
Markus Duft is currently offline Markus DuftFriend
Messages: 148
Registered: February 2013
Senior Member
aehm. the code itself may be java7 compliant, yes. but it uses java8 only interfaces (Consumer, ...) that are not available in a Java7 JDK installation. isn't that nearly as bad?
Re: Xtend generated code uses Java8 API's even though the project is still JavaSE-1.7 [message #1461479 is a reply to message #1461402] Tue, 04 November 2014 15:56 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3098
Registered: July 2009
Senior Member
Am 04.11.14 15:21, schrieb Markus Duft:
> aehm. the code itself may be java7 compliant, yes. but it uses java8
> only interfaces (Consumer, ...) that are not available in a Java7 JDK
> installation. isn't that nearly as bad?

That's what I meant if you put JDK 1.8 on the CP, Xtend will link
against its methods. As Java would also do. Put JDK 1.7 on the CP, and
everything will be fine.

Best,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: Xtend generated code uses Java8 API's even though the project is still JavaSE-1.7 [message #1461483 is a reply to message #1461479] Tue, 04 November 2014 16:03 Go to previous messageGo to next message
Markus Duft is currently offline Markus DuftFriend
Messages: 148
Registered: February 2013
Senior Member
It's a little bit of a different situation than with plain java i think. We're using JDK8 to develop as we have things that requires this. OTOH we have plugins that MUST work with Java7 at runtime. Now how could I possibly avoid changing the generated code without the need to install a second JDK (we're bundling THE (single) JDK with the IDE)? Is there a way to force it?

Thanks!
Re: Xtend generated code uses Java8 API's even though the project is still JavaSE-1.7 [message #1461496 is a reply to message #1461483] Tue, 04 November 2014 16:18 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3098
Registered: July 2009
Senior Member
Am 04.11.14 17:03, schrieb Markus Duft:
> It's a little bit of a different situation than with plain java i think.
> We're using JDK8 to develop as we have things that requires this. OTOH
> we have plugins that MUST work with Java7 at runtime. Now how could I
> possibly avoid changing the generated code without the need to install a
> second JDK (we're bundling THE (single) JDK with the IDE)? Is there a
> way to force it?
>
> Thanks!

You could use JDTs execution environment descriptions. That should work.
Meanwhile you may want to try list.forEach(null) in a Java class in your
Java7 project. I suspect it will compile it.

Sorry for any inconvenience,
Sebastian
--
Looking for professional support for Xtext, Xtend or Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: Xtend generated code uses Java8 API's even though the project is still JavaSE-1.7 [message #1461501 is a reply to message #1461496] Tue, 04 November 2014 16:24 Go to previous messageGo to next message
Markus Duft is currently offline Markus DuftFriend
Messages: 148
Registered: February 2013
Senior Member
Yep, I'm not only sure, I can guarantee that it will compile. But that is exactly the point. It will compile, but when executed with a Java7 VM then later on, when rolled out to the customer, the code will throw exceptions because the expected runtime classes/methods are not there.

And there is no inconvenience to excuse Wink

Cheers,
Markus
Re: Xtend generated code uses Java8 API's even though the project is still JavaSE-1.7 [message #1463082 is a reply to message #1461501] Thu, 06 November 2014 07:33 Go to previous messageGo to next message
Markus Duft is currently offline Markus DuftFriend
Messages: 148
Registered: February 2013
Senior Member
No more ideas on how to prevent compile-time vs. runtime issues?

Thanks in advance
Re: Xtend generated code uses Java8 API's even though the project is still JavaSE-1.7 [message #1463470 is a reply to message #1463082] Thu, 06 November 2014 15:46 Go to previous messageGo to next message
Stefan Oehme is currently offline Stefan OehmeFriend
Messages: 159
Registered: April 2010
Location: Kiel
Senior Member

If your plugins must work with Java 7, then compiling them against JDK 8 is a bad idea. This doesn't just apply to the Xtend compiler. It's easy for a developer to accidentally use some method or class that was introduced in Java 8 this way. The only way to ensure compatibility is to compile against your target JDK.

Edit:

I guess the deeper problem is that historically, most Java devs (me included) didn't care much about correct cross compilation. IDEs/build systems usually only gave a mild warning. But Java 8 really introduced a lot of APIs and even broke some things. So it's the first time that the JDK you compile against really matters. I'm afraid you'll have to get used to the idea of installing/providing the JDK that you want to run against.

[Updated on: Thu, 06 November 2014 15:50]

Report message to a moderator

Re: Xtend generated code uses Java8 API's even though the project is still JavaSE-1.7 [message #1464421 is a reply to message #1463470] Fri, 07 November 2014 14:36 Go to previous messageGo to next message
Markus Duft is currently offline Markus DuftFriend
Messages: 148
Registered: February 2013
Senior Member
OK, thanks for the reply. We'll try to switch everything to Java8 ASAP... we'll see how that goes Smile
Re: Xtend generated code uses Java8 API's even though the project is still JavaSE-1.7 [message #1777958 is a reply to message #1461307] Wed, 06 December 2017 19:06 Go to previous message
Markus Hoffrogge is currently offline Markus HoffroggeFriend
Messages: 1
Registered: December 2017
Junior Member
Hi,

I could solve the issue for my Maven based project builds:
If the project is compiled with Maven, then this issue can be solved by configuring the Maven toolchain plugin in the according projects or parent projects pom.
If you e.g. want to generate Java 7 compliant XText class code but your Maven build runs with a Java 8 or 9 JVM, then you have to configure your toolchain plugin with a Java 7 JRE/JDK reference within the appropriate Maven pom.
The classes generated by XText will then not contain any Java 8 "Consumer" or other non Java 7 available imports.

Note: The toolchain configuration does not replace the java compile version configured for the maven compile plugin - this is must be configured as usual for the maven compile plugin in your pom.

Regarding Maven toolchain config have a look at maven.apache.org/guides/mini/guide-using-toolchains.html

Regards
Markus
Previous Topic:Access restriction: The type Logger is not accessible due to restriction...
Next Topic:Generating piece of code after selecting proposal provider in DSL editor.
Goto Forum:
  


Current Time: Wed Jan 17 07:42:41 GMT 2018

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

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