Forráskód Böngészése

Fix bug introduced yesterday. NullInstance really is still required!

git-svn-id: https://svn.apache.org/repos/asf/lucene/hadoop/trunk@397593 13f79535-47bb-0310-9956-ffa450edef68
Doug Cutting 19 éve
szülő
commit
be64989561

+ 33 - 9
src/java/org/apache/hadoop/io/ObjectWritable.java

@@ -77,13 +77,36 @@ public class ObjectWritable implements Writable, Configurable {
     PRIMITIVE_NAMES.put("void", Void.TYPE);
   }
 
+  private static class NullInstance implements Writable {
+    private Class declaredClass;
+    public NullInstance() {}
+    public NullInstance(Class declaredClass) {
+      this.declaredClass = declaredClass;
+    }
+    public void readFields(DataInput in) throws IOException {
+      String className = UTF8.readString(in);
+      declaredClass = (Class)PRIMITIVE_NAMES.get(className);
+      if (declaredClass == null) {
+        try {
+          declaredClass = Class.forName(className);
+        } catch (ClassNotFoundException e) {
+          throw new RuntimeException(e.toString());
+        }
+      }
+    }
+    public void write(DataOutput out) throws IOException {
+      UTF8.writeString(out, declaredClass.getName());
+    }
+  }
+
   /** Write a {@link Writable}, {@link String}, primitive type, or an array of
    * the preceding. */
   public static void writeObject(DataOutput out, Object instance,
                                  Class declaredClass) throws IOException {
 
     if (instance == null) {                       // null
-      instance = NullWritable.get();
+      instance = new NullInstance(declaredClass);
+      declaredClass = Writable.class;
     }
 
     UTF8.writeString(out, declaredClass.getName()); // always write declared
@@ -197,15 +220,16 @@ public class ObjectWritable implements Writable, Configurable {
         throw new RuntimeException(e.toString());
       }
       
-      if (instanceClass == NullWritable.class) {  // null
+      Writable writable = WritableFactories.newInstance(instanceClass);
+      if(writable instanceof Configurable) {
+        ((Configurable) writable).setConf(conf);
+      }
+      writable.readFields(in);
+      instance = writable;
+
+      if (instanceClass == NullInstance.class) {  // null
+        declaredClass = ((NullInstance)instance).declaredClass;
         instance = null;
-      } else {
-        Writable writable = WritableFactories.newInstance(instanceClass);
-        if(writable instanceof Configurable) {
-          ((Configurable) writable).setConf(conf);
-        }
-        writable.readFields(in);
-        instance = writable;
       }
     }
 

+ 9 - 0
src/test/org/apache/hadoop/ipc/TestRPC.java

@@ -109,12 +109,21 @@ public class TestRPC extends TestCase {
     String stringResult = proxy.echo("foo");
     assertEquals(stringResult, "foo");
 
+    stringResult = proxy.echo((String)null);
+    assertEquals(stringResult, null);
+
     String[] stringResults = proxy.echo(new String[]{"foo","bar"});
     assertTrue(Arrays.equals(stringResults, new String[]{"foo","bar"}));
 
+    stringResults = proxy.echo((String[])null);
+    assertTrue(Arrays.equals(stringResults, null));
+
     UTF8 utf8Result = (UTF8)proxy.echo(new UTF8("hello world"));
     assertEquals(utf8Result, new UTF8("hello world"));
 
+    utf8Result = (UTF8)proxy.echo((UTF8)null);
+    assertEquals(utf8Result, null);
+
     int intResult = proxy.add(1, 2);
     assertEquals(intResult, 3);