今天在使用Java的retainAll方法的时候,遇到了匪夷所思的问题,报了一个什么Js Bridge的问题,但是可以肯定的是JS桥的框架肯定没有问题,跑了这么久了。那么问题就在最终debug的retainAll方法的使用上。
由于retainAll那句之后虽然页面直接闪退了,但是retainAll本身并没有抛错,于是尝试在那句代码上强行try catch看下抛了什么错,debug下来发现是抛的UnsupportedOperationException的错。意思是不支持的操作,也就是没有这个方法?于是我右键点击了retainAll的方法,发现了retainAll方法的注释是这样的,如下:
/**
* Retains only the elements in this list that are contained in the
* specified collection (optional operation). In other words, removes
* from this list all of its elements that are not contained in the
* specified collection.
*
* @param c collection containing elements to be retained in this list
* @return <tt>true</tt> if this list changed as a result of the call
* @throws UnsupportedOperationException if the <tt>retainAll</tt> operation
* is not supported by this list
* @throws ClassCastException if the class of an element of this list
* is incompatible with the specified collection
* (<a href="Collection.html#optional-restrictions">optional</a>)
* @throws NullPointerException if this list contains a null element and the
* specified collection does not permit null elements
* (<a href="Collection.html#optional-restrictions">optional</a>),
* or if the specified collection is null
* @see #remove(Object)
* @see #contains(Object)
*/
然后我看了下我调retainAll的那个数组的对象,是通过Arrays.asList生成的,关键的来了,这是asList的方法,如下:
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
/**
* @serial include
*/
private static class ArrayList<E> extends AbstractList<E>
implements RandomAccess, java.io.Serializable
{
private static final long serialVersionUID = -2764017481108945198L;
private final E[] a;
ArrayList(E[] array) {
a = Objects.requireNonNull(array);
}
@Override
public int size() {
return a.length;
}
@Override
public Object[] toArray() {
return a.clone();
}
@Override
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
int size = size();
if (a.length < size)
return Arrays.copyOf(this.a, size,
(Class<? extends T[]>) a.getClass());
System.arraycopy(this.a, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
@Override
public E get(int index) {
return a[index];
}
@Override
public E set(int index, E element) {
E oldValue = a[index];
a[index] = element;
return oldValue;
}
@Override
public int indexOf(Object o) {
E[] a = this.a;
if (o == null) {
for (int i = 0; i < a.length; i++)
if (a[i] == null)
return i;
} else {
for (int i = 0; i < a.length; i++)
if (o.equals(a[i]))
return i;
}
return -1;
}
@Override
public boolean contains(Object o) {
return indexOf(o) != -1;
}
@Override
public Spliterator<E> spliterator() {
return Spliterators.spliterator(a, Spliterator.ORDERED);
}
@Override
public void forEach(Consumer<? super E> action) {
Objects.requireNonNull(action);
for (E e : a) {
action.accept(e);
}
}
@Override
public void replaceAll(UnaryOperator<E> operator) {
Objects.requireNonNull(operator);
E[] a = this.a;
for (int i = 0; i < a.length; i++) {
a[i] = operator.apply(a[i]);
}
}
@Override
public void sort(Comparator<? super E> c) {
Arrays.sort(a, c);
}
}
也就是说asList生成的数组对象类型是这个内部类arrayList,然而这个类继承的是AbstractList,并没有retainAll方法,retainAll方法是java.util.List这个类里面的方法,阿西吧。。破案。
那么到这里,我想到我好像还有其他地方用了asList的方法,看来需要排查下了,毕竟add和remove方法它也没有。
mark!
总结:这是一个好问题,触及到了知识的盲区,也让我更加谨慎起来,以前真的是什么方法能用就用,不真正的关注方法的实现,等到报错了才后知后觉。对于程序员来说其实有点致命,因为问题已经产生了。因此还是要在平时多多阅读源码,进行学习充电。