avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1339381 - in /avro/trunk: CHANGES.txt lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java lang/java/avro/src/test/java/org/apache/avro/specific/ lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificData.java
Date Wed, 16 May 2012 21:47:23 GMT
Author: cutting
Date: Wed May 16 21:47:22 2012
New Revision: 1339381

URL: http://svn.apache.org/viewvc?rev=1339381&view=rev
Log:
AVRO-1051. Java: Fix specific RPC so that method is found when parameters are a union of a
primitive and null.  Contributed by Hamed Asghari.

Added:
    avro/trunk/lang/java/avro/src/test/java/org/apache/avro/specific/
    avro/trunk/lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificData.java
  (with props)
Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1339381&r1=1339380&r2=1339381&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Wed May 16 21:47:22 2012
@@ -58,6 +58,10 @@ Avro 1.6.3 (5 March 2012)
 
     AVRO-1078. ostreamOutputStream declaration missing in C++ API (thiru)
 
+    AVRO-1051. Java: Fix specific RPC so that method is found when
+    parameters are a union of a primitive and null.
+    (Hamed Asghari via cutting)
+
   IMPROVEMENTS
 
     AVRO-1030. Fix a broken link in the documentation.

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java?rev=1339381&r1=1339380&r2=1339381&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java Wed
May 16 21:47:22 2012
@@ -103,7 +103,7 @@ public class SpecificData extends Generi
     case UNION:
       List<Schema> types = schema.getTypes();     // elide unions with null
       if ((types.size() == 2) && types.contains(NULL_SCHEMA))
-        return getClass(types.get(types.get(0).equals(NULL_SCHEMA) ? 1 : 0));
+        return getWrapper(types.get(types.get(0).equals(NULL_SCHEMA) ? 1 : 0));
       return Object.class;
     case STRING:
       if (STRING_TYPE_STRING.equals(schema.getProp(STRING_PROP)))
@@ -120,6 +120,17 @@ public class SpecificData extends Generi
     }
   }
 
+  private Class getWrapper(Schema schema) {
+    switch (schema.getType()) {
+    case INT:     return Integer.class;
+    case LONG:    return Long.class;
+    case FLOAT:   return Float.class;
+    case DOUBLE:  return Double.class;
+    case BOOLEAN: return Boolean.class;
+    }
+    return getClass(schema);
+  }
+
   /** Returns the Java class name indicated by a schema's name and namespace. */
   public static String getClassName(Schema schema) {
     String namespace = schema.getNamespace();

Added: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificData.java?rev=1339381&view=auto
==============================================================================
--- avro/trunk/lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificData.java
(added)
+++ avro/trunk/lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificData.java
Wed May 16 21:47:22 2012
@@ -0,0 +1,62 @@
+package org.apache.avro.specific;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Arrays;
+
+import org.apache.avro.Schema;
+import org.apache.avro.Schema.Type;
+import org.junit.Before;
+import org.junit.Test;
+
+/*
+ * If integerClass is primitive, reflection to find method will
+ * result in a NoSuchMethodException in the case of a UNION schema
+ */
+public class TestSpecificData {
+
+  private Class<?> intClass;
+  private Class<?> integerClass;
+
+  @Before
+  public void setUp() {
+    Schema intSchema = Schema.create(Type.INT);
+    intClass = SpecificData.get().getClass(intSchema);
+    Schema nullSchema = Schema.create(Type.NULL);
+    Schema nullIntUnionSchema = Schema.createUnion(Arrays.asList(nullSchema, intSchema));
+    integerClass = SpecificData.get().getClass(nullIntUnionSchema);
+  }
+
+  @Test
+  public void testClassTypes() {
+    assertTrue(intClass.isPrimitive());
+    assertFalse(integerClass.isPrimitive());
+  }
+
+  @Test
+  public void testPrimitiveParam() throws Exception {
+    assertNotNull(Reflection.class.getMethod("primitive", intClass));
+  }
+
+  @Test(expected = NoSuchMethodException.class)
+  public void testPrimitiveParamError() throws Exception {
+    Reflection.class.getMethod("primitiveWrapper", intClass);
+  }
+
+  @Test
+  public void testPrimitiveWrapperParam() throws Exception {
+    assertNotNull(Reflection.class.getMethod("primitiveWrapper", integerClass));
+  }
+
+  @Test(expected = NoSuchMethodException.class)
+  public void testPrimitiveWrapperParamError() throws Exception {
+    Reflection.class.getMethod("primitive", integerClass);
+  }
+
+  static class Reflection {
+    public void primitive(int i) {}
+    public void primitiveWrapper(Integer i) {}
+  }
+}

Propchange: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificData.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message