JDK-8040819 : (bf) Generify java.nio.Buffer
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 8
  • Priority: P4
  • Status: Resolved
  • Resolution: Won't Fix
  • OS: windows_7
  • CPU: x86
  • Submitted: 2014-04-16
  • Updated: 2018-08-07
  • Resolved: 2018-08-07
Related Reports
Relates :  
Relates :  
Relates :  
Description
A DESCRIPTION OF THE REQUEST :
The Buffer class has been designed with method chaining in mind. E.g. method.flip() simply returns the Buffer it is part of. However when calling method that have been defined (and not overwritten) in e.g. ByteBuffer, the return type is a Buffer instead of the type of the underlying class, ByteBuffer. To retrieve the ByteBuffer again it is required to perform a cast - but that makes the method chaining almost unusable.

As the methods to get values from the Buffer are actually defined in the subclasses (mistake in the class design?) the cast is required to do anything useful after calling e.g. flip() almost all of the time.

PS willing to write the source code to retrofit Buffer with the type argument.

JUSTIFICATION :
Fulfil the promise inherent in the design of Buffer to be able to perform method chaining.

Making clear in the API that e.g. flip() does not return just any Buffer but the instance that the flip() is called on.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
int i = intBuffer.flip().getInt()
ACTUAL -
int i = ((ByteBuffer) (intBuffer.flip())).getInt()

---------- BEGIN SOURCE ----------
int i = intBuffer.flip().getInt()

Fails to compile.
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
int i = ((ByteBuffer) (intBuffer.flip())).getInt();

or

intBuffer.flip();
int i = intBuffer.getInt();


Comments
Problems reported in this issue have been fixed in the linked issues.
07-08-2018

Can we resolve this given that as mentioned above JDK-4774077 was resolved?
07-08-2018

See JDK-4774077 and also the discussion on core-libs-dev/nio-dev about this topic.
24-04-2014