Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid calling hashCode on arbitrary objects #202

Merged
merged 1 commit into from
Feb 9, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.rmi.RemoteException;
import java.util.Collections;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.concurrent.Future;

Expand Down Expand Up @@ -92,6 +93,7 @@ final class ProtocolV3ObjectTable implements ObjectTable {
static final ProtocolV3ObjectTable INSTANCE = new ProtocolV3ObjectTable();

private static final Map<Object, AbstractWritingExternalizer> objectWriters;
private static final Map<Object, AbstractWritingExternalizer> stringWriters;
private static final Map<Class<?>, AbstractWritingExternalizer> classWriters;

private static final AbstractWritingExternalizer[] extById;
Expand Down Expand Up @@ -181,23 +183,37 @@ final class ProtocolV3ObjectTable implements ObjectTable {
"ejbStore",
};
final AbstractWritingExternalizer[] extByIdTmp = new AbstractWritingExternalizer[simpleObjects.length];
final Map<Object, AbstractWritingExternalizer> objMap = new HashMap<>();
final Map<Object, AbstractWritingExternalizer> objMap = new IdentityHashMap<>();
final Map<Object, AbstractWritingExternalizer> stringMap = new HashMap<>();
for (int i = 0, simpleObjectsLength = simpleObjects.length; i < simpleObjectsLength; i++) {
ByteExternalizer ext = new ByteExternalizer(simpleObjects[i], i);
extByIdTmp[i] = ext;
objMap.put(ext.getObject(), ext);
if(ext.getObject() instanceof String) {
stringMap.put(ext.getObject(), ext);
} else {
objMap.put(ext.getObject(), ext);
}
}
// TODO: add class-based ext for LocalTransaction
// TODO: add class-based ext for RemoteTransaction
// TODO: add class-based ext for InputStream
// TODO: add class-based ext for OutputStream
extById = extByIdTmp;
//we have a separate map for string objects, as we don't want to call hashCode on arbitrary objects
//There are tests we need to pass that involve serializing a Vector that contains itself, which will
//fail if we call hashCode
objectWriters = objMap;
stringWriters = stringMap;
classWriters = Collections.emptyMap();
}

public Writer getObjectWriter(final Object object) throws IOException {
Writer writer = objectWriters.get(object);
Writer writer;
if(object instanceof String) {
writer = stringWriters.get(object);
} else {
writer = objectWriters.get(object);
}
if (writer == null) {
writer = classWriters.get(object.getClass());
}
Expand Down