Skip to content

Commit

Permalink
Find correct enum type in class hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
ngmr committed May 25, 2015
1 parent 6f3ff67 commit 40a57b1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 17 deletions.
17 changes: 17 additions & 0 deletions yoko-core/src/test/java/test/rmi/ClientMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.TimeUnit;

import javax.rmi.PortableRemoteObject;

Expand Down Expand Up @@ -217,6 +218,20 @@ public void testEnumArray() throws RemoteException {
assertTrue(Arrays.deepEquals(sa, oa));
}

public void testTimeUnit() throws RemoteException {
TimeUnit tu = TimeUnit.NANOSECONDS;
sample.setSerializable(tu);
Serializable s = sample.getSerializable();
assertSame(tu, s);
}

public void testTimeUnitArray() throws RemoteException {
TimeUnit[] tua = { TimeUnit.NANOSECONDS, TimeUnit.HOURS, TimeUnit.NANOSECONDS };
sample.setSerializable(tua);
Object[] oa = (Object[])sample.getSerializable();
assertTrue(Arrays.deepEquals(tua, oa));
}

public void testRemoteAttributeOnServer() throws RemoteException {
SampleSerializable ser = new SampleSerializable();
ser.setRemote(sample);
Expand Down Expand Up @@ -374,6 +389,8 @@ public static void main(String[] args) throws Exception {
test.testHashMap();
test.testEnum();
test.testEnumArray();
test.testTimeUnit();
test.testTimeUnitArray();
test.testCmsfv2Data();
//myORB.destroy();
System.out.println("Testing complete");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.apache.yoko.rmi.impl;

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 {
@SuppressWarnings("rawtypes")
private final Class enumType;

EnumDescriptor(Class<?> type, TypeRepository repository) {
super(type, repository);
enumType = getEnumType(type);
}

private static Class<?> getEnumType(Class<?> type) {
while (!!!type.isEnum()) {
type = type.getSuperclass();
}
return type;
}

@Override
long getSerialVersionUID() {
return 0L;
}

@Override
public Serializable readValue(InputStream in, Map<Integer, Object> offsetMap, Integer offset) {
try {
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")
final Enum<?> value = (Enum<?>) Enum.valueOf(enumType, name);
offsetMap.put(offset, value);
return value;
} catch (IndirectionException ex) {
return (Serializable) offsetMap.get(ex.offset);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ protected TypeDescriptor computeValue(Class<?> type) {
return new IDLEntityDescriptor(type, repo);
} 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);
} else if (type.isArray()) {
return ArrayDescriptor.get(type, repo);
} else if (!type.isInterface()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
public class ValueDescriptor extends TypeDescriptor {
static final Logger logger = Logger.getLogger(ValueDescriptor.class.getName());

private boolean _is_enum;
private boolean _is_externalizable;

private boolean _is_serializable;
Expand Down Expand Up @@ -144,9 +143,7 @@ public String getCustomRepositoryID() {
return _custom_repid;
}

private long getSerialVersionUID() {
if (_is_enum)
return 0L;
long getSerialVersionUID() {
if (_serial_version_uid_field != null) {

try {
Expand Down Expand Up @@ -177,7 +174,6 @@ private void init0() {
final Class<?> type = getJavaClass();
final Class<?> superClass = type.getSuperclass();

_is_enum = Enum.class.isAssignableFrom(type);
_is_rmi_stub = RMIStub.class.isAssignableFrom(type);
_is_externalizable = Externalizable.class.isAssignableFrom(type);
_is_serializable = Serializable.class.isAssignableFrom(type);
Expand Down Expand Up @@ -582,18 +578,6 @@ private Serializable createBlankInstance() {
}

public Serializable readValue(final InputStream in, final Map<Integer, Object> offsetMap, final Integer offset) {
if (_is_enum) {
try {
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);
final Enum value = Enum.valueOf(getJavaClass(), name);
offsetMap.put(offset, value);
return value;
} catch (IndirectionException ex) {
return (Serializable) offsetMap.get(ex.offset);
}
}

final Serializable value = createBlankInstance();

offsetMap.put(offset, value);
Expand Down

0 comments on commit 40a57b1

Please sign in to comment.