Skip to content
Permalink
Browse files
Avoid writing out fields of Enum subclasses
  • Loading branch information
ngmr committed Mar 15, 2016
1 parent 788192a commit 49a219e3d915cd365ac106c3a4388e32b6e4941a
Showing 4 changed files with 85 additions and 3 deletions.
@@ -218,6 +218,13 @@ public void testEnumArray() throws RemoteException {
assertTrue(Arrays.deepEquals(sa, oa));
}

public void testData() throws RemoteException {
SampleData sd = new SampleData();
sample.setSerializable(sd);
Serializable s = sample.getSerializable();
assertEquals(sd, s);
}

public void testTimeUnit() throws RemoteException {
TimeUnit tu = TimeUnit.NANOSECONDS;
sample.setSerializable(tu);
@@ -403,6 +410,7 @@ public static void main(String[] args) throws Exception {
test.testHashMap();
test.testEnum();
test.testEnumArray();
test.testData();
test.testTimeUnit();
test.testTimeUnitArray();
test.testCmsfv2Data();
@@ -0,0 +1,54 @@
package test.rmi;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Objects;

/**
* Created by nrichard on 11/03/16.
*/
public class SampleData implements Serializable {
private static class Data2 implements Serializable {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Data2)) return false;
return true;
}
}

private static class MrBoom implements Serializable {
private void writeObject(ObjectOutputStream oos) throws IOException {
throw new IOException("*BOOM*!");
}
}

public static enum DataEnum {
E1(new Data2()), E2(new MrBoom());
public final Serializable s;

private DataEnum(Serializable s) {
this.s = s;
}
}

public final Serializable f1 = DataEnum.E1;
public final Serializable f2 = DataEnum.E2;
public final Serializable f3 = DataEnum.E1.s;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SampleData)) return false;
SampleData sd = (SampleData)o;
if (f1 != sd.f1) return false;
if (f2 != sd.f2) return false;
return f3.equals(sd.f3);
}

@Override
public int hashCode() {
return Objects.hash(f1, f2);
}
}
@@ -1,16 +1,17 @@
package org.apache.yoko.rmi.impl;

import java.io.IOException;
import java.io.Serializable;
import java.util.Map;

import org.omg.CORBA.portable.IndirectionException;
import org.omg.CORBA.portable.InputStream;

public final class EnumDescriptor extends ValueDescriptor {
public final class EnumSubclassDescriptor extends ValueDescriptor {
@SuppressWarnings("rawtypes")
private final Class enumType;

EnumDescriptor(Class<?> type, TypeRepository repository) {
EnumSubclassDescriptor(Class<?> type, TypeRepository repository) {
super(type, repository);
enumType = getEnumType(type);
}
@@ -30,6 +31,7 @@ long getSerialVersionUID() {
@Override
public Serializable readValue(InputStream in, Map<Integer, Object> offsetMap, Integer offset) {
try {
// Shortcut to reading in just the fields of java.lang.Enum - ordinal and name
in.read_long(); // read in and ignore Enum ordinal
final String name = (String) ((org.omg.CORBA_2_3.portable.InputStream) in).read_value(String.class);
@SuppressWarnings("unchecked")
@@ -40,4 +42,22 @@ public Serializable readValue(InputStream in, Map<Integer, Object> offsetMap, In
return (Serializable) offsetMap.get(ex.offset);
}
}

@Override
protected final void writeValue(ObjectWriter writer, Serializable val) throws IOException {
// Don't write out any fields in the Enum subclass
_super_descriptor.writeValue(writer, val);
}

@Override
public final boolean isChunked() {
// Always do chunking for subclasses of Enum - like it's custom marshalled
return true;
}

@Override
public final Serializable writeReplace(Serializable val) {
// Never allow the honoring of writeReplace on an Enum subclass
return val;
}
}
@@ -119,7 +119,7 @@ protected TypeDescriptor computeValue(Class<?> type) {
} else if (Throwable.class.isAssignableFrom(type)) {
return new ExceptionDescriptor(type, repo);
} else if (Enum.class.isAssignableFrom(type) && (Enum.class != type)) {
return new EnumDescriptor(type, repo);
return new EnumSubclassDescriptor(type, repo);
} else if (type.isArray()) {
return ArrayDescriptor.get(type, repo);
} else if (!type.isInterface()

0 comments on commit 49a219e

Please sign in to comment.