geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From djen...@apache.org
Subject [16/51] [abbrv] geronimo-yoko git commit: Avoid writing out fields of Enum subclasses
Date Sun, 19 Feb 2017 01:49:33 GMT
Avoid writing out fields of Enum subclasses


Project: http://git-wip-us.apache.org/repos/asf/geronimo-yoko/repo
Commit: http://git-wip-us.apache.org/repos/asf/geronimo-yoko/commit/49a219e3
Tree: http://git-wip-us.apache.org/repos/asf/geronimo-yoko/tree/49a219e3
Diff: http://git-wip-us.apache.org/repos/asf/geronimo-yoko/diff/49a219e3

Branch: refs/heads/master
Commit: 49a219e3d915cd365ac106c3a4388e32b6e4941a
Parents: 788192a
Author: Neil GM Richards <neil_richards@uk.ibm.com>
Authored: Tue Mar 15 17:18:01 2016 +0000
Committer: Neil GM Richards <neil_richards@uk.ibm.com>
Committed: Tue Mar 15 17:24:09 2016 +0000

----------------------------------------------------------------------
 .../src/test/java/test/rmi/ClientMain.java      |  8 +++
 .../src/test/java/test/rmi/SampleData.java      | 54 +++++++++++++++++
 .../apache/yoko/rmi/impl/EnumDescriptor.java    | 43 -------------
 .../yoko/rmi/impl/EnumSubclassDescriptor.java   | 63 ++++++++++++++++++++
 .../apache/yoko/rmi/impl/TypeRepository.java    |  2 +-
 5 files changed, 126 insertions(+), 44 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/geronimo-yoko/blob/49a219e3/yoko-core/src/test/java/test/rmi/ClientMain.java
----------------------------------------------------------------------
diff --git a/yoko-core/src/test/java/test/rmi/ClientMain.java b/yoko-core/src/test/java/test/rmi/ClientMain.java
index 97e1f34..ff12805 100755
--- a/yoko-core/src/test/java/test/rmi/ClientMain.java
+++ b/yoko-core/src/test/java/test/rmi/ClientMain.java
@@ -218,6 +218,13 @@ public class ClientMain extends Assert {
             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 class ClientMain extends Assert {
         test.testHashMap();
         test.testEnum();
         test.testEnumArray();
+        test.testData();
         test.testTimeUnit();
         test.testTimeUnitArray();
         test.testCmsfv2Data();

http://git-wip-us.apache.org/repos/asf/geronimo-yoko/blob/49a219e3/yoko-core/src/test/java/test/rmi/SampleData.java
----------------------------------------------------------------------
diff --git a/yoko-core/src/test/java/test/rmi/SampleData.java b/yoko-core/src/test/java/test/rmi/SampleData.java
new file mode 100644
index 0000000..fe4c5a1
--- /dev/null
+++ b/yoko-core/src/test/java/test/rmi/SampleData.java
@@ -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);
+    }
+}

http://git-wip-us.apache.org/repos/asf/geronimo-yoko/blob/49a219e3/yoko-rmi-impl/src/main/java/org/apache/yoko/rmi/impl/EnumDescriptor.java
----------------------------------------------------------------------
diff --git a/yoko-rmi-impl/src/main/java/org/apache/yoko/rmi/impl/EnumDescriptor.java b/yoko-rmi-impl/src/main/java/org/apache/yoko/rmi/impl/EnumDescriptor.java
deleted file mode 100644
index 1e89d28..0000000
--- a/yoko-rmi-impl/src/main/java/org/apache/yoko/rmi/impl/EnumDescriptor.java
+++ /dev/null
@@ -1,43 +0,0 @@
-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);
-        }
-    }
-}

http://git-wip-us.apache.org/repos/asf/geronimo-yoko/blob/49a219e3/yoko-rmi-impl/src/main/java/org/apache/yoko/rmi/impl/EnumSubclassDescriptor.java
----------------------------------------------------------------------
diff --git a/yoko-rmi-impl/src/main/java/org/apache/yoko/rmi/impl/EnumSubclassDescriptor.java
b/yoko-rmi-impl/src/main/java/org/apache/yoko/rmi/impl/EnumSubclassDescriptor.java
new file mode 100644
index 0000000..d9254b7
--- /dev/null
+++ b/yoko-rmi-impl/src/main/java/org/apache/yoko/rmi/impl/EnumSubclassDescriptor.java
@@ -0,0 +1,63 @@
+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 EnumSubclassDescriptor extends ValueDescriptor {
+    @SuppressWarnings("rawtypes")
+    private final Class enumType;
+
+    EnumSubclassDescriptor(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 {
+            // 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")
+            final Enum<?> value = (Enum<?>) Enum.valueOf(enumType, name);
+            offsetMap.put(offset, value);
+            return value;
+        } catch (IndirectionException ex) {
+            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;
+    }
+}

http://git-wip-us.apache.org/repos/asf/geronimo-yoko/blob/49a219e3/yoko-rmi-impl/src/main/java/org/apache/yoko/rmi/impl/TypeRepository.java
----------------------------------------------------------------------
diff --git a/yoko-rmi-impl/src/main/java/org/apache/yoko/rmi/impl/TypeRepository.java b/yoko-rmi-impl/src/main/java/org/apache/yoko/rmi/impl/TypeRepository.java
index ea1afce..aa5f95e 100755
--- a/yoko-rmi-impl/src/main/java/org/apache/yoko/rmi/impl/TypeRepository.java
+++ b/yoko-rmi-impl/src/main/java/org/apache/yoko/rmi/impl/TypeRepository.java
@@ -119,7 +119,7 @@ public class TypeRepository {
                 } 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()


Mime
View raw message