From 4210b05960b6657b4f0ca98f97c49649e1de7139 Mon Sep 17 00:00:00 2001 From: Doug Cutting Date: Sat, 8 Nov 2014 00:54:22 +0000 Subject: [PATCH] AVRO-1592. Java: Fix handling of Java reserved words as enum constants in generated code. Contributed by Lukas Steiblys. git-svn-id: https://svn.apache.org/repos/asf/avro/trunk@1637493 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 3 +++ .../org/apache/avro/specific/SpecificData.java | 17 +++++++++++++++++ .../compiler/specific/SpecificCompiler.java | 16 ++-------------- .../apache/avro/specific/TestSpecificData.java | 7 +++++++ share/test/schemas/reserved.avsc | 2 ++ 5 files changed, 31 insertions(+), 14 deletions(-) create mode 100644 share/test/schemas/reserved.avsc diff --git a/CHANGES.txt b/CHANGES.txt index 45eb8f0a815..44fdd7806cb 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -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 diff --git a/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java b/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java index 03537761e35..1fa3e52fb0b 100644 --- a/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java +++ b/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificData.java @@ -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 GenericData { 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 RESERVED_WORDS = new HashSet + (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 @@ protected boolean isEnum(Object datum) { 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); } diff --git a/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java b/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java index fb3118ed989..ea388ef54ec 100644 --- a/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java +++ b/lang/java/compiler/src/main/java/org/apache/avro/compiler/specific/SpecificCompiler.java @@ -53,6 +53,8 @@ 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 static enum FieldVisibility { 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 RESERVED_WORDS = new HashSet( - 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 ACCESSOR_MUTATOR_RESERVED_WORDS = new HashSet(Arrays.asList(new String[] { diff --git a/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificData.java b/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificData.java index d7d81517c16..d1a03777fd7 100644 --- a/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificData.java +++ b/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificData.java @@ -44,6 +44,7 @@ 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 void testSpecificRecordToString() throws IOException { } + @Test public void testReservedEnumSymbol() throws Exception { + Assert.assertEquals(Reserved.default$, + SpecificData.get().createEnum("default", + Reserved.SCHEMA$)); + } + } diff --git a/share/test/schemas/reserved.avsc b/share/test/schemas/reserved.avsc new file mode 100644 index 00000000000..40f4849e5c9 --- /dev/null +++ b/share/test/schemas/reserved.avsc @@ -0,0 +1,2 @@ +{"name": "org.apache.avro.test.Reserved", "type": "enum", + "symbols": ["default","class","int"]},