Skip to content

Avoid ByteBuffer incompatibility when compiling with JDK9+ #686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2019

Conversation

wborn
Copy link
Member

@wborn wborn commented Mar 31, 2019

Split off from #685

@@ -96,7 +97,7 @@ public void testPostWithContent() throws Exception {
private String getContentFromProvider(ContentProvider value) {
ByteBuffer element = value.iterator().next();
byte[] data = new byte[element.limit()];
((ByteBuffer) element.duplicate().clear()).get(data);
((ByteBuffer) ((Buffer) element.duplicate()).clear()).get(data);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fully correct, but I am not sure if this is really readable.

Why not using e.g.

final ByteBuffer duplicate = element.duplicate(); // work on a copy to not modify the source
duplicate.clear(); // Prepare buffer for reading
duplicate.get(data); // read buffer into data

So, we do not need any casting at all.

(I do not test the code just write it here on Github)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The solution in this PR is based on apache/felix#114. Are you sure your approach will still work after reading the explanation for why it occurs?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why it shouldn't.

The methods (e.g. clear) return this and it differs between the version by using the parent class Buffer or the specific class ByteBuffer.
If you are using the return value by writing all in one line it does matter.

If we do not use the return value (it is this, so no need for), why should we (developer, compiler, runtime, ...) care about at all?

But my understanding could be wrong.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, okay, while reading it again, it could matter...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a comment like the linked one (// Explicit cast for compatibility with covariant return type on JDK 9's ByteBuffer) in the code and copy the explanation in the first linked comment to the commit message?
Then a git blame will identify why this casts are necessary, without requiring to open Github). IMO the commit message should contain the information why the change is necessary.

WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's tricky stuff and I'm happy to get it working. If I remember correctly it occurs if you compile this code with Java 8 and then use it with Java 11 or vice versa. 😞

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you're right about adding some comments for this sorcery! 😉

Incompatible code can be used/generated when using JDK8 on the command line and JDK11 in Eclipse (or vice versa).

The explanation for this is given in apache/felix#114 :

Java 9 introduces overridden methods with covariant return types for the following methods in java.nio.ByteBuffer:

* position​(int newPosition)
* limit​(int newLimit)
* flip​()
* clear​()
* mark​()
* reset​()
* rewind​()

In Java 9 they all now return ByteBuffer, whereas the methods they override return Buffer,
resulting in exceptions like this when executing on Java 8 and lower:
java.lang.NoSuchMethodError: java.nio.ByteBuffer.limit(I)Ljava/nio/ByteBuffer
This is because the generated byte code includes the static return type of the method, which is not found on Java 8 and lower because the overloaded methods with covariant return types don't exist (the issue appears even with source and target 8 or lower in compilation parameters).
The solution is to cast ByteBuffer instances to Buffer before calling the method.

Signed-off-by: Wouter Born <[email protected]>
@wborn wborn force-pushed the avoid-bb-incompat branch from 4eee88b to ea045b6 Compare March 31, 2019 12:31
@wborn wborn requested a review from maggu2810 March 31, 2019 12:32
Copy link

@maggu2810 maggu2810 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for moving the Java 11 compatibility forward!

@maggu2810 maggu2810 merged commit 648cfac into openhab:master Mar 31, 2019
@wborn wborn deleted the avoid-bb-incompat branch March 31, 2019 12:53
@wborn wborn added this to the 2.5 milestone Jul 30, 2019
Rosi2143 pushed a commit to Rosi2143/openhab-core that referenced this pull request Dec 26, 2020
…on" (openhab#686)

* Revert "Replace all ".md" with ".html" inside zwave files on generation (openhab#678)"

This reverts commit a69a154.
splatch pushed a commit to ConnectorIO/copybara-hab-core that referenced this pull request Jul 11, 2023
Incompatible code can be used/generated when using JDK8 on the command line and JDK11 in Eclipse (or vice versa).

The explanation for this is given in apache/felix#114 :

Java 9 introduces overridden methods with covariant return types for the following methods in java.nio.ByteBuffer:

* position​(int newPosition)
* limit​(int newLimit)
* flip​()
* clear​()
* mark​()
* reset​()
* rewind​()

In Java 9 they all now return ByteBuffer, whereas the methods they override return Buffer,
resulting in exceptions like this when executing on Java 8 and lower:
java.lang.NoSuchMethodError: java.nio.ByteBuffer.limit(I)Ljava/nio/ByteBuffer
This is because the generated byte code includes the static return type of the method, which is not found on Java 8 and lower because the overloaded methods with covariant return types don't exist (the issue appears even with source and target 8 or lower in compilation parameters).
The solution is to cast ByteBuffer instances to Buffer before calling the method.

Signed-off-by: Wouter Born <[email protected]>
GitOrigin-RevId: 648cfac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants