You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by rs...@apache.org on 2020/05/19 14:56:07 UTC

[avro] branch master updated: AVRO-2808: provide details while encountering non-static inner classes (#874)

This is an automated email from the ASF dual-hosted git repository.

rskraba pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/master by this push:
     new efd6cda  AVRO-2808: provide details while encountering non-static inner classes (#874)
efd6cda is described below

commit efd6cda8bf9a025ca2847fe8900ac590305facf6
Author: Anh Le (Andy) <an...@vng.com.vn>
AuthorDate: Tue May 19 21:56:00 2020 +0700

    AVRO-2808: provide details while encountering non-static inner classes (#874)
    
    * AVRO-2808: ReflectData to provide details while encountering non-static inner classes
    
    * AVRO-2808: provide details while encountering non-static inner classes
    
    * AVRO-2808: improve tests
    
    Thank you @Fokko for reviewing my code
    
    * AVRO-2808: improve code readability
    
    * AVRO-2808: fix Checkstyle violation for member variables
---
 .../main/java/org/apache/avro/reflect/ReflectData.java |  6 ++++++
 .../java/org/apache/avro/reflect/TestReflectData.java  | 18 ++++++++++++++++--
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java b/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
index bbba622..ebe94e7 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
@@ -65,6 +65,9 @@ import java.util.concurrent.ConcurrentHashMap;
 
 /** Utilities to use existing Java classes and interfaces via reflection. */
 public class ReflectData extends SpecificData {
+
+  private static final String STRING_OUTER_PARENT_REFERENCE = "this$0";
+
   @Override
   public boolean useCustomCoders() {
     return false;
@@ -737,6 +740,9 @@ public class ReflectData extends SpecificData {
 
               AvroName annotatedName = field.getAnnotation(AvroName.class); // Rename fields
               String fieldName = (annotatedName != null) ? annotatedName.value() : field.getName();
+              if (STRING_OUTER_PARENT_REFERENCE.equals(fieldName)) {
+                throw new AvroTypeException("Class " + fullName + " must be a static inner class");
+              }
               Schema.Field recordField = new Schema.Field(fieldName, fieldSchema, doc, defaultValue);
 
               AvroMeta[] metadata = field.getAnnotationsByType(AvroMeta.class); // add metadata
diff --git a/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflectData.java b/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflectData.java
index 4cee431..5900988 100644
--- a/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflectData.java
+++ b/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflectData.java
@@ -18,6 +18,7 @@
 
 package org.apache.avro.reflect;
 
+import org.apache.avro.AvroTypeException;
 import org.apache.avro.Protocol;
 import org.apache.avro.Schema;
 import org.apache.avro.util.internal.JacksonUtils;
@@ -29,8 +30,7 @@ import java.util.List;
 import java.util.Map;
 
 import static org.hamcrest.Matchers.*;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertThat;
+import static org.junit.Assert.*;
 
 public class TestReflectData {
   @Test
@@ -130,4 +130,18 @@ public class TestReflectData {
       assertEquals("Invalid field " + field.name(), field.defaultVal(), testCases.get(field.name()));
     }
   }
+
+  public class Definition {
+    public Map<String, String> tokens;
+  }
+
+  @Test(expected = AvroTypeException.class)
+  public void testNonStaticInnerClasses() {
+    ReflectData.get().getSchema(Definition.class);
+  }
+
+  @Test
+  public void testStaticInnerClasses() {
+    ReflectData.get().getSchema(Meta.class);
+  }
 }