Skip to content

Commit

Permalink
AVRO-1592. Java: Fix handling of Java reserved words as enum constant…
Browse files Browse the repository at this point in the history
…s in generated code. Contributed by Lukas Steiblys.

git-svn-id: https://svn.apache.org/repos/asf/avro/trunk@1637493 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
cutting committed Nov 8, 2014
1 parent d84eef2 commit 4210b05
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<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
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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<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[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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$));
}

}

2 changes: 2 additions & 0 deletions share/test/schemas/reserved.avsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"name": "org.apache.avro.test.Reserved", "type": "enum",
"symbols": ["default","class","int"]},

0 comments on commit 4210b05

Please sign in to comment.