You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by cu...@apache.org on 2011/12/13 19:56:48 UTC

svn commit: r1213860 - in /avro/trunk: ./ lang/java/avro/src/main/java/org/apache/avro/reflect/ lang/java/avro/src/main/java/org/apache/avro/specific/ lang/java/avro/src/test/java/org/apache/avro/

Author: cutting
Date: Tue Dec 13 18:56:48 2011
New Revision: 1213860

URL: http://svn.apache.org/viewvc?rev=1213860&view=rev
Log:
AVRO-978. Java: Fix reflect to better handle Byte type.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectDatumReader.java
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectDatumWriter.java
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java
    avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestReflect.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1213860&r1=1213859&r2=1213860&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Tue Dec 13 18:56:48 2011
@@ -40,6 +40,8 @@ Avro 1.6.2 (unreleased)
     AVRO-977. Java: Fix codegen to not generate deprecated code. 
     (Hamed Asghari via cutting)
 
+    AVRO-978. Java: Fix reflect to better handle Byte type.  (cutting)
+
 Avro 1.6.1 (8 November 2011)
 
   INCOMPATIBLE CHANGES

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java?rev=1213860&r1=1213859&r2=1213860&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java Tue Dec 13 18:56:48 2011
@@ -206,8 +206,9 @@ public class ReflectData extends Specifi
     case STRING:  return String.class;
     case BYTES:   return BYTES_CLASS;
     case INT:
-      if (Short.class.getName().equals(schema.getProp(CLASS_PROP)))
-        return Short.TYPE;
+      String intClass = schema.getProp(CLASS_PROP);
+      if (Byte.class.getName().equals(intClass))  return Byte.TYPE;
+      if (Short.class.getName().equals(intClass)) return Short.TYPE;
     default:
       return super.getClass(schema);
     }
@@ -240,6 +241,10 @@ public class ReflectData extends Specifi
         schema.addProp(CLASS_PROP, raw.getName());
         return schema;
       }
+    } else if ((type == Byte.class) || (type == Byte.TYPE)) {
+      Schema result = Schema.create(Schema.Type.INT);
+      result.addProp(CLASS_PROP, Byte.class.getName());
+      return result;
     } else if ((type == Short.class) || (type == Short.TYPE)) {
       Schema result = Schema.create(Schema.Type.INT);
       result.addProp(CLASS_PROP, Short.class.getName());

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectDatumReader.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectDatumReader.java?rev=1213860&r1=1213859&r2=1213860&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectDatumReader.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectDatumReader.java Tue Dec 13 18:56:48 2011
@@ -132,7 +132,10 @@ public class ReflectDatumReader<T> exten
   protected Object readInt(Object old,
                            Schema expected, Decoder in) throws IOException {
     Object value = in.readInt();
-    if (Short.class.getName().equals(expected.getProp(ReflectData.CLASS_PROP)))
+    String intClass = expected.getProp(ReflectData.CLASS_PROP);
+    if (Byte.class.getName().equals(intClass))
+      value = ((Integer)value).byteValue();
+    else if (Short.class.getName().equals(intClass))
       value = ((Integer)value).shortValue();
     return value;
   }

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectDatumWriter.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectDatumWriter.java?rev=1213860&r1=1213859&r2=1213860&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectDatumWriter.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectDatumWriter.java Tue Dec 13 18:56:48 2011
@@ -96,7 +96,9 @@ public class ReflectDatumWriter<T> exten
   @Override
   protected void write(Schema schema, Object datum, Encoder out)
     throws IOException {
-    if (datum instanceof Short)
+    if (datum instanceof Byte)
+      datum = ((Byte)datum).intValue();
+    else if (datum instanceof Short)
       datum = ((Short)datum).intValue();
     try {
       super.write(schema, datum, out);

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=1213860&r1=1213859&r2=1213860&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 Tue Dec 13 18:56:48 2011
@@ -190,7 +190,7 @@ public class SpecificData extends Generi
               (schema.toString().replace(schema.getNamespace(),
                                          c.getPackage().getName()));
         } catch (NoSuchFieldException e) {
-          throw new AvroRuntimeException(e);
+          throw new AvroRuntimeException("Not a Specific class: "+c);
         } catch (IllegalAccessException e) {
           throw new AvroRuntimeException(e);
         }
@@ -210,7 +210,7 @@ public class SpecificData extends Generi
                                                 iface.getPackage().getName()));
       return p;
    } catch (NoSuchFieldException e) {
-      throw new AvroRuntimeException(e);
+      throw new AvroRuntimeException("Not a Specific protocol: "+iface);
     } catch (IllegalAccessException e) {
       throw new AvroRuntimeException(e);
     }

Modified: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestReflect.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestReflect.java?rev=1213860&r1=1213859&r2=1213860&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestReflect.java (original)
+++ avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestReflect.java Tue Dec 13 18:56:48 2011
@@ -71,6 +71,16 @@ public class TestReflect {
     check(Integer.class, "\"int\"");
   }
 
+  @Test public void testByte() {
+    check(Byte.TYPE, "{\"type\":\"int\",\"java-class\":\"java.lang.Byte\"}");
+    check(Byte.class, "{\"type\":\"int\",\"java-class\":\"java.lang.Byte\"}");
+  }
+
+  @Test public void testShort() {
+    check(Short.TYPE, "{\"type\":\"int\",\"java-class\":\"java.lang.Short\"}");
+    check(Short.class, "{\"type\":\"int\",\"java-class\":\"java.lang.Short\"}");
+  }
+
   @Test public void testLong() {
     check(Long.TYPE, "\"long\"");
     check(Long.class, "\"long\"");
@@ -188,12 +198,14 @@ public class TestReflect {
   public static class R4 {
     public short value;
     public short[] shorts;
+    public byte b;
     
     public boolean equals(Object o) {
       if (!(o instanceof R4)) return false;
       R4 that = (R4)o;
       return this.value == that.value
-        && Arrays.equals(this.shorts, that.shorts);
+        && Arrays.equals(this.shorts, that.shorts)
+        && this.b == that.b;
     }
   }
 
@@ -203,6 +215,7 @@ public class TestReflect {
     R5 r5 = new R5();
     r5.value = 1;
     r5.shorts = new short[] {3,255,256,Short.MAX_VALUE,Short.MIN_VALUE};
+    r5.b = 99;
     checkReadWrite(r5);
   }