昨晚测试环境报了一个错误,
exceptionHandler, error:java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:148)
at java.util.AbstractList.add(AbstractList.java:108)
at java.util.AbstractCollection.addAll(AbstractCollection.java:344)
at com.xxxxxx.services.facade.impl.HomXXXXFacadeImpl.projecDistributionState(HomeXXXXFacadeImpl.java:190)
at com.xxxxx.api.controller.HomePageController.projecXXXXXXXXState(HomeXXXController.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
看到这个错误第一时间就想到了Arrays.asList
生成的list执行添加删除操作导致的报错,但转念一想,这块代码运行几个月了都没报错,不会啊。点进方法一看,好家伙,还真改了。
我们进源码看看,可以看到此处的ArrayList可不是我们理解的Arraylist,这个ArrayList是一个静态内部类,和java.util.ArrayList不同
这个是一个低配版的,只有初始化和一些获取属性方法的ArrayList,也是基于数组实现,但是不能添加和删除。
package java.util;
/**
* @author Josh Bloch
* @author Neal Gafter
* @author John Rose
* @since 1.2
*/
public class Arrays {
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) {
// xxxx
}
@Override
public E get(int index) {
// xxxx
}
@Override
public E set(int index, E element) {
// xxxx
}
@Override
public int indexOf(Object o) {
E[] a = this.a;
// xxxx
}
@Override
public boolean contains(Object o) {
// xxxx
}
@Override
public Spliterator<E> spliterator() {
// xxxx
}
@Override
public void forEach(Consumer<? super E> action) {
// xxxx action.accept(e);
}
@Override
public void replaceAll(UnaryOperator<E> operator) {
// xxxx
}
@Override
public void sort(Comparator<? super E> c) {
// xxxx
}
}
与Arrays.asList穿一条裤子的还有Collections.singletonList(arr)
,这个也是final修饰,也不支持添加删除操作。
package java.util;
public class Collections {
// Suppresses default constructor, ensuring non-instantiability.
private Collections() {
}
public static <T> List<T> singletonList(T o) {
return new SingletonList<>(o);
}
/**
* @serial include
*/
private static class SingletonList<E>
extends AbstractList<E>
implements RandomAccess, Serializable {
private static final long serialVersionUID = 3093736618740652951L;
private final E element;
SingletonList(E obj) {element = obj;}
public Iterator<E> iterator() {
return singletonIterator(element);
}
public int size() {return 1;}
public boolean contains(Object obj) {return eq(obj, element);}
public E get(int index) {
if (index != 0)
throw new IndexOutOfBoundsException("Index: "+index+", Size: 1");
return element;
}
// Override default methods for Collection
@Override
public void forEach(Consumer<? super E> action) {
action.accept(element);
}
@Override
public boolean removeIf(Predicate<? super E> filter) {
throw new UnsupportedOperationException();
}
@Override
public void replaceAll(UnaryOperator<E> operator) {
throw new UnsupportedOperationException();
}
@Override
public void sort(Comparator<? super E> c) {
}
@Override
public Spliterator<E> spliterator() {
return singletonSpliterator(element);
}
}
}