Conversation
…at(T[], element). Both single-element concat overloads built the result array with the same runtime component type as the input array (via newArray/Arrays.copyOf), then stored the new element into it directly. When the element's runtime type wasn't assignable to that component type (e.g. concatenating a String[] with an Integer, both erased to Object at the call site), storing the element threw ArrayStoreException instead of succeeding, unlike concat(T[], T[], Class<T>), which never has this problem since it builds its result array from an explicit Class. Now each method checks whether the element is storable in an array of the same type as the input before allocating; if not, it falls back to a plain Object[] (unchecked-cast back to T[], mirroring the existing unsoundlyCovariantArray pattern used elsewhere in this file), so no exception is thrown. Fixes google#3768
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
|
Hey @Junnie18 ... go to https://cla.developers.google.com/ and sign in , or the googleBot will block it, individual CLA is a few minutes. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ObjectArrays.concat(T element, T[] array)andObjectArrays.concat(T[] array, T element)build their result array with the same runtime component type as the input array (vianewArray/Arrays.copyOf), then storeelementdirectly into it. Ifelement's runtime type isn't assignable to that component type — which erasure allows the caller to do, since both methods are generic — storing it throwsArrayStoreExceptioninstead of succeeding:This is inconsistent with
ObjectArrays.concat(T[], T[], Class<T>), which never has this problem because it always builds its result array from the explicitly suppliedClass<T>rather than inferring a component type from one of its array arguments.Fix
Each of the two single-element overloads now checks whether
elementis storable in an array with the same component type as the input array before allocating the result that way. If it isn't, it falls back to a plainObject[](unchecked-cast back toT[]), which mirrors the existingunsoundlyCovariantArraypattern already used elsewhere in this file for the same class of unsoundness. In the overwhelmingly common case (element and array are actually type-compatible), behavior and the returned array's runtime type are unchanged.No public API was changed.
Testing
Added 4 regression tests to
ObjectArraysTest(prepend/append, with both an empty and a non-empty backing array) that concatenate aString[]with anIntegervia an explicit<Object>type witness, reproducing the exact shape of bug report's repro. Verified:ArrayStoreException, confirming they exercise the reported bug.mvn test -pl guava,guava-tests -am -Dtest=ObjectArraysTest→ 30/30 pass.com.google.common.collect.*Testsuite (207 test classes) afterward to check for regressions: all pass.google-java-format.Fixes #3768.