-
-
Notifications
You must be signed in to change notification settings - Fork 438
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
Conversation
@@ -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); |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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. 😞
There was a problem hiding this comment.
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]>
4eee88b
to
ea045b6
Compare
There was a problem hiding this 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!
…on" (openhab#686) * Revert "Replace all ".md" with ".html" inside zwave files on generation (openhab#678)" This reverts commit a69a154.
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
Split off from #685