Skip to content

core, alts, cronet: fix ByteBuffer covariant method usages - #7349

Merged
voidzcy merged 4 commits into
grpc:masterfrom
voidzcy:bugfix/fix_bytebuffer_covariant_method_usages
Aug 26, 2020
Merged

core, alts, cronet: fix ByteBuffer covariant method usages#7349
voidzcy merged 4 commits into
grpc:masterfrom
voidzcy:bugfix/fix_bytebuffer_covariant_method_usages

Conversation

@voidzcy

@voidzcy voidzcy commented Aug 22, 2020

Copy link
Copy Markdown
Contributor

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.


See details in #7348.

Fixed with find-replace, I can't guarantee not missing one or two. Tweaked with animal sniffer, but seems it doesn't catch it.

Comment thread core/src/main/java/io/grpc/internal/CompositeReadableBuffer.java

@ejona86 ejona86 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you make sure to add more information to the commit description? The issue is useful for all the nitty-gritty, but a short summary or pointer would be helpful directly in the commit.

@voidzcy
voidzcy merged commit c30505d into grpc:master Aug 26, 2020
dfawley pushed a commit to dfawley/grpc-java that referenced this pull request Jan 15, 2021
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.
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jun 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants