[JAVA-2559] Handle Java 9 additions of covariant return types to java.nio.ByteBuffer methods Created: 17/Jul/17  Updated: 29/Oct/23  Resolved: 22/Aug/17

Status: Closed
Project: Java Driver
Component/s: Internal
Affects Version/s: None
Fix Version/s: 3.6.0

Type: Improvement Priority: Major - P3
Reporter: Jeffrey Yemin Assignee: Jeffrey Yemin
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Epic Link: Java9

 Description   

Java 9 introduces overridden methods with covariant return types for the following methods in java.nio.ByteBuffer that are used by the driver:

  • position
  • limit
  • flip
  • clear

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 solution is to cast ByteBuffer instances to Buffer before calling the method.



 Comments   
Comment by Githook User [ 22/Aug/17 ]

Author:

{'username': 'jyemin', 'email': 'jeff.yemin@10gen.com', 'name': 'Jeff Yemin'}

Message: JAVA-2559: Cast java.nio.ByteBuffer instances where necessary to java.nio.Buffer to avoid NoSuchMethodError when running on Java 6 to Java 8

The Java 9 ByteBuffer classes introduces overloaded methods with covariant return types for the following methods used by the driver:

  • position
  • limit
  • flip
  • clear

Without casting, exceptions like this are thrown 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.
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/21c91bd364d38489e0bbe2e390efdb3746ee3fff

Generated at Thu Feb 08 08:57:32 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.