martin-g commented on code in PR #1680:
URL: https://github.com/apache/avro/pull/1680#discussion_r877998855
##########
lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java:
##########
@@ -61,6 +65,20 @@ public class SpecificData extends GenericData {
private static final Class<?>[] NO_ARG = new Class[] {};
private static final Class<?>[] SCHEMA_ARG = new Class[] { Schema.class };
+ private static final Method IS_RECORD;
Review Comment:
```suggestion
private static final Method IS_RECORD_METHOD;
```
##########
lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectDatumReader.java:
##########
@@ -70,6 +70,21 @@ public ReflectDatumReader(ReflectData data) {
super(data);
}
+ /** Called to read data. */
+ protected Object read(Object old, Schema expected, ResolvingDecoder in)
throws IOException {
Review Comment:
```suggestion
@Override
protected Object read(Object old, Schema expected, ResolvingDecoder in)
throws IOException {
```
##########
lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectRecordEncoding.java:
##########
@@ -0,0 +1,140 @@
+package org.apache.avro.reflect;
+
+import java.io.IOException;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import org.apache.avro.AvroMissingFieldException;
+import org.apache.avro.AvroRuntimeException;
+import org.apache.avro.Schema;
+import org.apache.avro.io.Decoder;
+import org.apache.avro.io.Encoder;
+import org.apache.avro.io.ResolvingDecoder;
+
+public class ReflectRecordEncoding extends CustomEncoding {
+
+ private final List<FieldWriter> writer;
+ private final Constructor<?> constructor;
+ private List<FieldReader> reader;
+
+ public ReflectRecordEncoding(Schema schema, Class<?> type) {
+ this.schema = schema;
+
+ this.writer = schema.getFields().stream().map(field -> {
+ try {
+ Method method = type.getMethod(field.name());
+ return new FieldWriter(method, field.schema());
+ } catch (ReflectiveOperationException e) {
+ throw new AvroMissingFieldException("Field does not exist", field);
+ }
+
+ }).collect(Collectors.toList());
+
+ // order of this matches default constructor find order mapping
+
+ Field[] fields = type.getDeclaredFields();
+
+ List<Class<?>> parameterTypes = new ArrayList<>(fields.length);
+
+ Map<String, Integer> offsets = new HashMap<>();
+
+ // need to know offset for mapping
+ for (Field field : fields) {
+ if (Modifier.isStatic(field.getModifiers())) {
+ continue;
+ }
+ offsets.put(field.getName(), parameterTypes.size());
+ parameterTypes.add(field.getType());
+ }
+
+ try {
+ this.constructor =
type.getDeclaredConstructor(parameterTypes.toArray(Class[]::new));
+
+ } catch (NoSuchMethodException e) {
+ throw new AvroRuntimeException(e);
+ }
+
+ this.reader = schema.getFields().stream().map(field -> {
+ int offset = offsets.get(field.name());
+ return new FieldReader(offset, field.schema());
+
+ }).collect(Collectors.toList());
+
+ }
+
+ @Override
+ protected void write(Object datum, Encoder out) throws IOException {
+ throw new UnsupportedOperationException("No writer specified");
+ }
+
+ @Override
+ protected void write(Object datum, Encoder out, ReflectDatumWriter writer)
throws IOException {
+ for (FieldWriter field : this.writer) {
+ field.write(datum, out, writer);
+ }
+ }
+
+ @Override
+ protected Object read(Object reuse, Decoder in) throws IOException {
+ throw new UnsupportedOperationException("No writer specified");
Review Comment:
```suggestion
throw new UnsupportedOperationException("No reader specified");
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]