[ 
https://issues.apache.org/jira/browse/COLLECTIONS-842?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17762070#comment-17762070
 ] 

Alex Herbert commented on COLLECTIONS-842:
------------------------------------------

Some additional info
{code:java}
// AbstractLinkedList methods incompatible with return void in JDK 21 
java.util.List<E>
public boolean addFirst(E o)
public boolean addLast(E o)
{code}
This affects AbstractLinkedList, CursorableLinkedList or NodeCachingLinkedList. 
There is no occurrence where the return value of the method used.

It does seem strange that these methods in collections return a boolean. They 
are undocumented so the intention of the return type is not known. The primary 
reason that an add operation for a list fails is a capacity error. This should 
be handled with exceptions. I would guess that the boolean return type was 
simply copied from the return type of 'boolean add(E o)'. However that method 
is inherited from a collection where it may have a meaning other than failure, 
for example adding to a Set. The List specific 'add(int index, E o)' method 
returns void.

In principle changing to a void return type should not be an issue. It does 
however break binary compatibility, and two methods with the same name and 
parameters that return either void or boolean cannot coexist.

Note that collections4 is targeting Java 8. The current code compiles under JDK 
21 (tested using the current release candidate) as the maven.compiler.release 
property is set to 8. This however does not work:
{noformat}
mvn clean compile -Dmaven.compiler.release=21
{noformat}
In this case the incompatible API produces a compile error.

I have verified that a simple class that *uses* NodeCachingLinkedList can be 
compiled and run on JDK 21. Since the class is importing a pre-compiled class, 
the incompatibility is not identified:
{code:java}
class LLTest {
  public static void main(String[] args) {
    NodeCachingLinkedList<String> ll = new NodeCachingLinkedList<>();
    ll.add("one");
    ll.addFirst("two");
    ll.addLast("three");
    System.out.println(ll.size());
  }
} 
{code}
{noformat}
> javac -cp commons-collections4-4.4.jar LLTest.java
> java -cp commons-collections4-4.4.jar:. LLTest
3
{noformat}
If the AbstractLinkedList is used then JDK 21 must compile the concrete class 
and the incompatibility is identified:
{code:java}
class LLTest {
  public static void main(String[] args) {
    AbstractLinkedList<String> ll = new AbstractLinkedList<>() {};
    ll.add("one");
    ll.addFirst("two");
    ll.addLast("three");
    System.out.println(ll.size());
  }
}
{code}
{noformat}
> javac -cp commons-collections4-4.4.jar LLTest.java
LLTest.java:5: error: addLast(E#1) in AbstractLinkedList cannot implement 
addLast(E#2) in List
        AbstractLinkedList<String> ll = new AbstractLinkedList<>() {};
                                                                   ^
  return type boolean is not compatible with void
  where E#1,E#2 are type-variables:
    E#1 extends Object declared in class AbstractLinkedList
    E#2 extends Object declared in interface List
1 error
{noformat}
So the issue is that the AbstractLinkedList is not compile-time compatible with 
a Java source level of 21.

This issue will affect any user who is extending either AbstractLinkedList, 
CursorableLinkedList or NodeCachingLinkedList, and compiling with a source 
value of 21.

A user who is using the AbstractLinkedList, CursorableLinkedList or 
NodeCachingLinkedList, even with a Java source level of 21, should not have an 
issue. However note that my simple test may not be representative of all use 
cases of this type.
h2. Possible fix

Mark the methods as deprecated, document that they will be replaced with a void 
return type in collections5, and note that extending the AbstractLinkedList 
class is not source compatible with java 21.

> AbstractLinkedList apparently incompatible with JDK 21's java.util.List
> -----------------------------------------------------------------------
>
>                 Key: COLLECTIONS-842
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-842
>             Project: Commons Collections
>          Issue Type: Bug
>          Components: List
>            Reporter: Julian Reschke
>            Priority: Major
>
> ...it returns "boolean" in commons-collections4, but is void in JDK 21 (see 
> https://download.java.net/java/early_access/jdk21/docs/api/java.base/java/util/List.html#addLast(E))



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to