Skip to content

Commit

Permalink
[JBMAR-172] Reserve instance cache entries in same order they are wri…
Browse files Browse the repository at this point in the history
…tten for empty object arrays
  • Loading branch information
dmlloyd committed Nov 4, 2014
1 parent 3d7f5d7 commit 0da3740
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
Expand Up @@ -309,9 +309,8 @@ Object doReadObject(int leadByte, final boolean unshared, final boolean discardM
}
final ArrayList<Object> instanceCache = this.instanceCache;
final int idx = instanceCache.size();
instanceCache.add(null);
final Object obj = Array.newInstance(doReadClassDescriptor(readUnsignedByte(), true).getType(), 0);
instanceCache.set(idx, obj);
instanceCache.add(obj);
final Object resolvedObject = objectResolver.readResolve(obj);
if (unshared) {
instanceCache.set(idx, null);
Expand Down
Expand Up @@ -51,6 +51,7 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import org.jboss.marshalling.AbstractClassResolver;
import org.jboss.marshalling.AnnotationClassExternalizerFactory;
import org.jboss.marshalling.ByteInput;
import org.jboss.marshalling.ByteOutput;
Expand Down Expand Up @@ -3501,4 +3502,59 @@ public void runRead(final Unmarshaller unmarshaller) throws Throwable {
}
});
}

// the trouble with empty arrays...

static class ArrayHolder implements Serializable {

private static final long serialVersionUID = 1L;

private final SerializableWithFinalFields[] array;

ArrayHolder(final SerializableWithFinalFields[] array) {
this.array = array;
}

public SerializableWithFinalFields[] getArray() {
return array;
}
}

@Test
public void testEmptyArray() throws Throwable {
runReadWriteTest(new ReadWriteTest() {
public void runWrite(final Marshaller marshaller) throws Throwable {
marshaller.writeObject(new SerializableWithFinalFields[0]);
}

public void runRead(final Unmarshaller unmarshaller) throws Throwable {
final SerializableWithFinalFields[] array = unmarshaller.readObject(SerializableWithFinalFields[].class);
assertNotNull(array);
assertArrayEquals(new SerializableWithFinalFields[0], array);
}
});
}

@Test
public void testEmptyArrayInObject() throws Throwable {
runReadWriteTest(new ReadWriteTest() {
public void configureRead(final MarshallingConfiguration configuration) throws Throwable {
configuration.setVersion(-1);
}

public void configureWrite(final MarshallingConfiguration configuration) throws Throwable {
configuration.setVersion(-1);
}

public void runWrite(final Marshaller marshaller) throws Throwable {
marshaller.writeObject(new ArrayHolder(new SerializableWithFinalFields[0]));
}

public void runRead(final Unmarshaller unmarshaller) throws Throwable {
final SerializableWithFinalFields[] array = unmarshaller.readObject(ArrayHolder.class).getArray();
assertNotNull(array);
assertArrayEquals(new SerializableWithFinalFields[0], array);
}
});
}
}

0 comments on commit 0da3740

Please sign in to comment.