Author: cutting
Date: Thu Jul 16 20:34:13 2009
New Revision: 794837
URL: http://svn.apache.org/viewvc?rev=794837&view=rev
Log:
AVRO-78. Fix Java reflect to work on non-public fields.
Modified:
hadoop/avro/trunk/CHANGES.txt
hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectData.java
hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectDatumReader.java
hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectDatumWriter.java
hadoop/avro/trunk/src/test/java/org/apache/avro/TestReflect.java
Modified: hadoop/avro/trunk/CHANGES.txt
URL:
http://svn.apache.org/viewvc/hadoop/avro/trunk/CHANGES.txt?rev=794837&r1=794836&r2=794837&view=diff
==============================================================================
--- hadoop/avro/trunk/CHANGES.txt (original)
+++ hadoop/avro/trunk/CHANGES.txt Thu Jul 16 20:34:13 2009
@@ -18,6 +18,8 @@
BUG FIXES
+ AVRO-78. Fix Java reflect to work on non-public fields. (cutting)
+
Avro 1.0.0 -- 9 July 2009
INCOMPATIBLE CHANGES
Modified: hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectData.java
URL:
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectData.java?rev=794837&r1=794836&r2=794837&view=diff
==============================================================================
--- hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectData.java
(original)
+++ hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectData.java Thu Jul
16 20:34:13 2009
@@ -51,14 +51,12 @@
public static boolean validate(Schema schema, Object datum) {
switch (schema.getType()) {
case RECORD:
- Class recordClass = datum.getClass();
+ Class c = datum.getClass();
if (!(datum instanceof Object)) return false;
for (Map.Entry<String, Schema> entry : schema.getFieldSchemas()) {
try {
if (!validate(entry.getValue(),
- recordClass.getField(entry.getKey()).get(datum)))
- return false;
- } catch (NoSuchFieldException e) {
+ ReflectData.getField(c, entry.getKey()).get(datum)))
return false;
} catch (IllegalAccessException e) {
throw new AvroRuntimeException(e);
@@ -92,6 +90,16 @@
}
}
+ static Field getField(Class c, String name) {
+ try {
+ Field f = c.getDeclaredField(name);
+ f.setAccessible(true);
+ return f;
+ } catch (NoSuchFieldException e) {
+ throw new AvroRuntimeException(e);
+ }
+ }
+
private static final WeakHashMap<java.lang.reflect.Type,Schema> SCHEMA_CACHE
=
new WeakHashMap<java.lang.reflect.Type,Schema>();
Modified:
hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectDatumReader.java
URL:
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectDatumReader.java?rev=794837&r1=794836&r2=794837&view=diff
==============================================================================
--- hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectDatumReader.java
(original)
+++ hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectDatumReader.java
Thu Jul 16 20:34:13 2009
@@ -47,22 +47,20 @@
protected void addField(Object record, String name, int position, Object o) {
try {
- Field field = record.getClass().getField(name);
- field.setAccessible(true);
- field.set(record, o);
- } catch (Exception e) {
+ ReflectData.getField(record.getClass(), name).set(record, o);
+ } catch (IllegalAccessException e) {
throw new AvroRuntimeException(e);
}
}
+
protected Object getField(Object record, String name, int position) {
try {
- Field field = record.getClass().getField(name);
- field.setAccessible(true);
- return field.get(record);
- } catch (Exception e) {
+ return ReflectData.getField(record.getClass(), name).get(record);
+ } catch (IllegalAccessException e) {
throw new AvroRuntimeException(e);
}
}
+
protected void removeField(Object record, String name, int position) {
addField(record, name, position, null);
}
Modified:
hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectDatumWriter.java
URL:
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectDatumWriter.java?rev=794837&r1=794836&r2=794837&view=diff
==============================================================================
--- hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectDatumWriter.java
(original)
+++ hadoop/avro/trunk/src/java/org/apache/avro/reflect/ReflectDatumWriter.java
Thu Jul 16 20:34:13 2009
@@ -37,9 +37,8 @@
protected Object getField(Object record, String name, int position) {
try {
- Field field = record.getClass().getField(name);
- return field.get(record);
- } catch (Exception e) {
+ return ReflectData.getField(record.getClass(), name).get(record);
+ } catch (IllegalAccessException e) {
throw new AvroRuntimeException(e);
}
}
Modified: hadoop/avro/trunk/src/test/java/org/apache/avro/TestReflect.java
URL:
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/test/java/org/apache/avro/TestReflect.java?rev=794837&r1=794836&r2=794837&view=diff
==============================================================================
--- hadoop/avro/trunk/src/test/java/org/apache/avro/TestReflect.java (original)
+++ hadoop/avro/trunk/src/test/java/org/apache/avro/TestReflect.java Thu Jul 16
20:34:13 2009
@@ -17,7 +17,8 @@
*/
package org.apache.avro;
-import org.apache.avro.reflect.ReflectData;
+import org.apache.avro.reflect.*;
+import org.apache.avro.io.*;
import org.apache.avro.test.Simple;
import org.apache.avro.test.Simple.TestRecord;
import org.slf4j.Logger;
@@ -25,8 +26,7 @@
import static org.testng.AssertJUnit.assertEquals;
import org.testng.annotations.Test;
-import java.io.File;
-import java.io.IOException;
+import java.io.*;
public class TestReflect {
private static final Logger LOG
@@ -43,7 +43,7 @@
}
@Test
- public void testRecord() throws IOException {
+ public void testSchema() throws IOException {
assertEquals(PROTOCOL.getTypes().get("TestRecord"),
ReflectData.getSchema(TestRecord.class));
}
@@ -52,4 +52,50 @@
public void testProtocol() throws IOException {
assertEquals(PROTOCOL, ReflectData.getProtocol(Simple.class));
}
+
+ @Test
+ public void testRecord() throws IOException {
+ Schema schm = ReflectData.getSchema(SampleRecord.class);
+ Class<?> c = SampleRecord.class;
+ String prefix =
+ ((c.getEnclosingClass() == null
+ || "null".equals(c.getEnclosingClass())) ?
+ c.getPackage().getName() + "."
+ : (c.getEnclosingClass().getName() + "$"));
+ ReflectDatumWriter writer = new ReflectDatumWriter(schm);
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ SampleRecord record = new SampleRecord();
+ record.x = 5;
+ record.y = 10;
+ writer.write(record, new BinaryEncoder(out));
+ ReflectDatumReader reader = new ReflectDatumReader(schm, prefix);
+ Object decoded =
+ reader.read(null, new BinaryDecoder
+ (new ByteArrayInputStream(out.toByteArray())));
+ assertEquals(record, decoded);
+ }
+
+ public static class SampleRecord {
+ public int x = 1;
+ private int y = 2;
+
+ public int hashCode() {
+ return x + y;
+ }
+
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ final SampleRecord other = (SampleRecord)obj;
+ if (x != other.x)
+ return false;
+ if (y != other.y)
+ return false;
+ return true;
+ }
+ }
}