avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1637493 - in /avro/trunk: ./ lang/java/avro/src/main/java/org/apache/avro/specific/ lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/ lang/java/ipc/src/test/java/org/apache/avro/specific/ share/test/schemas/
Date Sat, 08 Nov 2014 00:54:22 GMT
Author: cutting
Date: Sat Nov  8 00:54:22 2014
New Revision: 1637493

URL: http://svn.apache.org/r1637493
Log:
AVRO-1592. Java: Fix handling of Java reserved words as enum constants in generated code.
 Contributed by Lukas Steiblys.

Added:
    avro/trunk/share/test/schemas/reserved.avsc
Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java
    avro/trunk/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java
    avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificData.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1637493&r1=1637492&r2=1637493&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Sat Nov  8 00:54:22 2014
@@ -61,6 +61,9 @@ Trunk (not yet released)
     AVRO-1598. Java: Fix flakiness in TestFileSpanStorage.
     (Ryan Blue via cutting)
 
+    AVRO-1592. Java: Fix handling of Java reserved words as enum
+    constants in generated code. (Lukas Steiblys via cutting)
+
 Avro 1.7.7 (23 July 2014)
 
   NEW FEATURES

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=1637493&r1=1637492&r2=1637493&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 Sat
Nov  8 00:54:22 2014
@@ -17,6 +17,7 @@
  */
 package org.apache.avro.specific;
 
+import java.util.Arrays;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Collection;
@@ -59,6 +60,20 @@ public class SpecificData extends Generi
   public static final String KEY_CLASS_PROP = "java-key-class";
   public static final String ELEMENT_PROP = "java-element-class";
 
+  /** List of Java reserved words from
+   * http://java.sun.com/docs/books/jls/third_edition/html/lexical.html. */
+  public static final Set<String> RESERVED_WORDS = new HashSet<String>
+    (Arrays.asList(new String[] {
+        "abstract", "assert", "boolean", "break", "byte", "case", "catch",
+        "char", "class", "const", "continue", "default", "do", "double",
+        "else", "enum", "extends", "false", "final", "finally", "float",
+        "for", "goto", "if", "implements", "import", "instanceof", "int",
+        "interface", "long", "native", "new", "null", "package", "private",
+        "protected", "public", "return", "short", "static", "strictfp",
+        "super", "switch", "synchronized", "this", "throw", "throws",
+        "transient", "true", "try", "void", "volatile", "while"
+      }));
+
   /** Read/write some common builtin classes as strings.  Representing these as
    * strings isn't always best, as they aren't always ordered ideally, but at
    * least they're stored.  Also note that, for compatibility, only classes
@@ -109,6 +124,8 @@ public class SpecificData extends Generi
   public Object createEnum(String symbol, Schema schema) {
     Class c = getClass(schema);
     if (c == null) return super.createEnum(symbol, schema); // punt to generic
+    if (RESERVED_WORDS.contains(symbol))
+      symbol += "$";
     return Enum.valueOf(c, symbol);
   }
 

Modified: avro/trunk/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java?rev=1637493&r1=1637492&r2=1637493&view=diff
==============================================================================
--- avro/trunk/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java
(original)
+++ avro/trunk/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java
Sat Nov  8 00:54:22 2014
@@ -53,6 +53,8 @@ import org.apache.velocity.runtime.log.L
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static org.apache.avro.specific.SpecificData.RESERVED_WORDS;
+
 /**
  * Generate specific Java interfaces and classes for protocols and schemas.
  *
@@ -71,20 +73,6 @@ public class SpecificCompiler {
   private boolean createSetters = true;
   private String outputCharacterEncoding;
 
-  /* List of Java reserved words from
-   * http://java.sun.com/docs/books/jls/third_edition/html/lexical.html. */
-  private static final Set<String> RESERVED_WORDS = new HashSet<String>(
-      Arrays.asList(new String[] {
-          "abstract", "assert", "boolean", "break", "byte", "case", "catch",
-          "char", "class", "const", "continue", "default", "do", "double",
-          "else", "enum", "extends", "false", "final", "finally", "float",
-          "for", "goto", "if", "implements", "import", "instanceof", "int",
-          "interface", "long", "native", "new", "null", "package", "private",
-          "protected", "public", "return", "short", "static", "strictfp",
-          "super", "switch", "synchronized", "this", "throw", "throws",
-          "transient", "true", "try", "void", "volatile", "while"
-        }));
-  
   /* Reserved words for accessor/mutator methods */
   private static final Set<String> ACCESSOR_MUTATOR_RESERVED_WORDS = 
       new HashSet<String>(Arrays.asList(new String[] {

Modified: avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificData.java?rev=1637493&r1=1637492&r2=1637493&view=diff
==============================================================================
--- avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificData.java
(original)
+++ avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificData.java
Sat Nov  8 00:54:22 2014
@@ -44,6 +44,7 @@ import org.apache.avro.TestSchema;
 import org.apache.avro.test.TestRecord;
 import org.apache.avro.test.MD5;
 import org.apache.avro.test.Kind;
+import org.apache.avro.test.Reserved;
 
 public class TestSpecificData {
   
@@ -143,5 +144,11 @@ public class TestSpecificData {
 
   }
 
+  @Test public void testReservedEnumSymbol() throws Exception {
+    Assert.assertEquals(Reserved.default$,
+                        SpecificData.get().createEnum("default",
+                                                      Reserved.SCHEMA$));
+  }
+
 }
 

Added: avro/trunk/share/test/schemas/reserved.avsc
URL: http://svn.apache.org/viewvc/avro/trunk/share/test/schemas/reserved.avsc?rev=1637493&view=auto
==============================================================================
--- avro/trunk/share/test/schemas/reserved.avsc (added)
+++ avro/trunk/share/test/schemas/reserved.avsc Sat Nov  8 00:54:22 2014
@@ -0,0 +1,2 @@
+{"name": "org.apache.avro.test.Reserved", "type": "enum",
+ "symbols": ["default","class","int"]},



Mime
View raw message