Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » InMemoryJavaCompiler: test for Java 8 compatibility
InMemoryJavaCompiler: test for Java 8 compatibility [message #1857533] Tue, 14 February 2023 10:10 Go to next message
Simon Cockx is currently offline Simon CockxFriend
Messages: 69
Registered: October 2021
Member
We are generating Java code that should be Java 8 compatible. To test this, we use the `org.eclipse.xtext.xbase.testing.InMemoryJavaCompiler` class with `JavaVersion.JAVA8`.

However, I've just ran into an issue where I've accidentally used a Java 9 feature, but our unit tests didn't fail, causing me to believe it was Java 8 compatible and releasing, only having to find out later that projects depending on our DSL where failing to build.

The feature that I used is the static method `List::of`, which doesn't exist until Java 9.

In short, what I've found out by digging into the source code of `InMemoryJavaCompiler` and related classes:
- Passing `JavaVersion.JAVA8` results in the `-source` and `-target` flags being set to Java 8. What I actually need instead is to set the `-release` flag to Java 8, which would also override the `-bootclasspath` to be Java 8 compatible.
- The class used to represent compiler options, i.e., `org.eclipse.jdt.internal.compiler.impl.CompilerOptions`, doesn't seem to support the `-release` flag. A snippet from `CompilerOptions::getMap`:

optionsMap.put(OPTION_Compliance, versionFromJdkLevel(this.complianceLevel));
optionsMap.put(OPTION_Release, DISABLED); // ----------------------------------------> see this line
optionsMap.put(OPTION_Source, versionFromJdkLevel(this.sourceLevel));
optionsMap.put(OPTION_TargetPlatform, versionFromJdkLevel(this.targetJDK));


In essence, this means I'm only able to unit test that we're not using Java 9+ language features, but I'm not able to test that we're not using Java 9+ standard library features.

Is there a way to fix this? Or does the internal Eclipse compiler being used just not have support for the `-release` flag?

[Updated on: Tue, 14 February 2023 10:12]

Report message to a moderator

Re: InMemoryJavaCompiler: test for Java 8 compatibility [message #1857534 is a reply to message #1857533] Tue, 14 February 2023 11:11 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
my proposal: subclass and customize

beside that i guess this is a question to ask jdt team


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Tue, 14 February 2023 12:37]

Report message to a moderator

Re: InMemoryJavaCompiler: test for Java 8 compatibility [message #1857537 is a reply to message #1857534] Tue, 14 February 2023 14:12 Go to previous messageGo to next message
Simon Cockx is currently offline Simon CockxFriend
Messages: 69
Registered: October 2021
Member
Yeah, I've briefly tried the subclassing route, but it seems the compiler wasn't using my release option.

I'll ask on the JDT forum as well, thanks for the suggestion. Cross-posted here: https://www.eclipse.org/forums/index.php/t/1112498/

[Updated on: Tue, 14 February 2023 14:13]

Report message to a moderator

Re: InMemoryJavaCompiler: test for Java 8 compatibility [message #1857580 is a reply to message #1857537] Wed, 15 February 2023 19:07 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Checking against a specific Java version only works if that version is actually available. Have you registered a java 8 installation in your Eclipse?
Re: InMemoryJavaCompiler: test for Java 8 compatibility [message #1857594 is a reply to message #1857580] Thu, 16 February 2023 11:37 Go to previous message
Simon Cockx is currently offline Simon CockxFriend
Messages: 69
Registered: October 2021
Member
@Ed Willink That's not a problem. Compilation with Java 8 works perfectly, and the tests will fail whenever I use a language feature from a newer Java version.

However, the tests don't fail when I use a class or method of the standard library from a newer Java version, e.g., `List.of()` which was added to the standard library since Java 9. And this is actually the intended and documented behaviour. From https://openjdk.org/jeps/247: (see emphasized)

Quote:
javac provides two command line options, -source and -target, which can be used to select the version of the Java language accepted by the compiler and the version of the class files it produces, respectively. By default, however, javac compiles against the most-recent version of the platform APIs. The compiled program can therefore accidentally use APIs only available in the current version of the platform. Such programs cannot run on older versions of the platform, regardless of the values passed to the -source and -target options. This is a long-term usability pain point, since users expect that by using these options they'll get class files that can run on the the platform version specified by -target.


That's the motivation for the `-release` flag, which was introduced in Java 9 to compile for older platforms. The problem is that the API of the Eclipse compiler doesn't seem to support the `-release` flag.

[Updated on: Thu, 16 February 2023 11:39]

Report message to a moderator

Previous Topic:Data Type Rule
Next Topic:DSL Type Parameter is not used in extends
Goto Forum:
  


Current Time: Thu Mar 28 20:17:42 GMT 2024

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

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

Back to the top