I’ve got a bug in my code, I’m trying to modify a List that is really a Guava ImmutableList passed in from a client program/programmer.
What is the best way to test for this, rather than waiting for the exception when the .add() fails?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you don’t know if the returned list is mutable, you should copy it to a mutable collection anyway. If the returned list is another
ArrayList, the cost of the copy is minimal — it reduces to a blazing fastSystem.arraycopy.(That said, for the client library to sometimes return modifiable lists and sometimes unmodifiable lists is itself an act of evil.)
If you must check at runtime, use
instanceof ImmutableMaporImmutableCollectionor whatever’s appropriate.