Skip to content

ObjectArrays.concat(T[], T) and (T, T[]) can throw ArrayStoreException #3768

@cpovirk

Description

@cpovirk

I doubt that anyone has ever hit this in practice. (I didn't see any bugs filed internally about it, anyway, nor any mentions of it on the web.) But I thought it was neat :)

$ cat ConcatTs.java 
import static com.google.common.collect.ObjectArrays.concat;

public class ConcatTs {
  public static void main(String[] args) {
    String[] array = new String[0];
    Object[] concatenated = concat(array, 0);
  }
}
 $ javac -cp $HOME/.m2/repository/com/google/guava/guava/28.2-jre/guava-28.2-jre.jar ConcatTs.java && java -cp .:$HOME/.m2/repository/com/google/guava/guava/28.2-jre/guava-28.2-jre.jar ConcatTs
Exception in thread "main" java.lang.ArrayStoreException: java.lang.Integer
  at com.google.common.collect.ObjectArrays.concat(ObjectArrays.java:102)
  at ConcatTs.main(ConcatTs.java:6)

Interestingly, we got this right in concat(T[], T[], Class<T>) -- which requires a Class to use to create the array -- back in CL 3734425. But we got it wrong in the new methods in CL 7931853.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions