@Override /** Called to read an array instance. May be overridden for alternate array * representations.*/ protected Object readArray(Object old, Schema expected, ResolvingDecoder in) throws IOException { Schema expectedType = expected.getElementType(); long l = in.readArrayStart(); if (l <= 0) { return newArray(old, 0, expected); } Object array = newArray(old, (int) l, expected); if (array instanceof Collection) { @SuppressWarnings("unchecked") Collection<Object> c = (Collection<Object>) array; return readCollection(c, expectedType, l, in); } else { return readJavaArray(array, expectedType, l, in); } }
@Override @SuppressWarnings("unchecked") protected Object newArray(Object old, int size, Schema schema) { Class collectionClass = ScalaSafeReflectData.getClassProp(schema, ScalaSafeReflectData.CLASS_PROP); Class elementClass = ScalaSafeReflectData.getClassProp(schema, ScalaSafeReflectData.ELEMENT_PROP); if (collectionClass == null && elementClass == null) return super.newArray(old, size, schema); // use specific/generic ScalaSafeReflectData data = ScalaSafeReflectData.getInstance(); if (collectionClass != null && !collectionClass.isArray()) { if (old instanceof Collection) { ((Collection)old).clear(); return old; } if (scala.collection.Iterable.class.isAssignableFrom(collectionClass) || collectionClass.isAssignableFrom(ArrayList.class)) { return Lists.newArrayList(); } return data.newInstance(collectionClass, schema); } if (elementClass == null) { elementClass = data.getClass(schema.getElementType()); } return Array.newInstance(elementClass, size); } }