Skip to content
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

java.lang.NoSuchMethodError: java.nio.ByteBuffer.flip()Ljava/nio/ByteBuffer; #117

Open
pfirmstone opened this issue Jun 8, 2020 · 5 comments

Comments

@pfirmstone
Copy link
Owner

Covariant return breaks runtime compatibility with Java 8.

Adding casts to source code fixes this issue.

@pfirmstone
Copy link
Owner Author

java.lang.NoSuchMethodError: java.nio.ByteBuffer.position(I)Ljava/nio/ByteBuffer;

@pfirmstone
Copy link
Owner Author

According to Felix:

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.

@pfirmstone
Copy link
Owner Author

Also two abstract methods were added to Buffer in Java 9:

slice()
duplicate()

Which return Buffer, and are also implemented by ByteBuffer in previous versions of Java, but return ByteBuffer.

The solution in this case is to cast to ByteBuffer before calling these two methods.

@pfirmstone pfirmstone changed the title Module JGDMS JERI: java.lang.NoSuchMethodError: java.nio.ByteBuffer.flip()Ljava/nio/ByteBuffer; java.lang.NoSuchMethodError: java.nio.ByteBuffer.flip()Ljava/nio/ByteBuffer; Jul 7, 2020
@pfirmstone pfirmstone reopened this Nov 26, 2021
@pfirmstone
Copy link
Owner Author

Refer to Issue #141

Array types need to remain as ByteBuffer [] for compatibility reasons, as array types cannot be cast to supertypes, eg ByteBuffer [] cannot be cast to Buffer [], casts can be performed outside of arrays. #141 was created by the changes made here.

@pfirmstone pfirmstone reopened this Jan 9, 2024
@pfirmstone
Copy link
Owner Author

pfirmstone commented Jan 9, 2024

It turns out that this is a suboptimal solution to this problem, the OpenJDK team created a new complier flag for later Java platforms, to generate Java 8 compatible bytecodes. We should use the release compiler flag, instead of the source target flags.

<properties> <maven.compiler.release>8</maven.compiler.release> </properties>

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

No branches or pull requests

1 participant