avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1197765 - in /avro/trunk: ./ lang/java/avro/src/main/java/org/apache/avro/data/ lang/java/avro/src/main/java/org/apache/avro/generic/ lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/ lang/java/...
Date Fri, 04 Nov 2011 20:50:17 GMT
Author: cutting
Date: Fri Nov  4 20:50:17 2011
New Revision: 1197765

URL: http://svn.apache.org/viewvc?rev=1197765&view=rev
Log:
AVRO-951. Java: Fix generated code to not conflict with fields named 'data'.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/data/RecordBuilderBase.java
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericRecordBuilder.java
    avro/trunk/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm
    avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/compiler/specific/TestSpecificCompiler.java
    avro/trunk/lang/java/tools/src/test/compiler/output/Player.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1197765&r1=1197764&r2=1197765&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Fri Nov  4 20:50:17 2011
@@ -2,6 +2,12 @@ Avro Change Log
 
 Avro 1.6.1 (unreleased)
 
+  INCOMPATIBLE CHANGES
+
+    AVRO-951. Java: Fix generated code to not conflict with fields
+    named 'data'.  Code generated by the 1.6.0 compiler must be
+    re-generated to work correctly with the 1.6.1 runtime. (cutting)
+
   NEW FEATURES
 
     AVRO-821. PHP: Add support for parsing protocols. (Andy Wick,
@@ -18,8 +24,6 @@ Avro 1.6.1 (unreleased)
 
 Avro 1.6.0 (2 November 2011)
 
-  INCOMPATIBLE CHANGES
-
   NEW FEATURES
 
     AVRO-839. Java: Add accessor methods and builders to generated

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/data/RecordBuilderBase.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/data/RecordBuilderBase.java?rev=1197765&r1=1197764&r2=1197765&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/data/RecordBuilderBase.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/data/RecordBuilderBase.java Fri
Nov  4 20:50:17 2011
@@ -44,13 +44,18 @@ public abstract class RecordBuilderBase<
     DEFAULT_VALUE_CACHE = 
       new ConcurrentHashMap<String, ConcurrentMap<Integer, Object>>();
   private static final Field[] EMPTY_FIELDS = new Field[0];
-  protected final Schema schema;
-  protected final Field[] fields;
-  protected final boolean[] fieldSetFlags;
-  protected final GenericData data;
+  private final Schema schema;
+  private final Field[] fields;
+  private final boolean[] fieldSetFlags;
+  private final GenericData data;
   private BinaryEncoder encoder = null;
   private BinaryDecoder decoder = null;
   
+  protected final Schema schema() { return schema; }
+  protected final Field[] fields() { return fields; }
+  protected final boolean[] fieldSetFlags() { return fieldSetFlags; }
+  protected final GenericData data() { return data; }
+
   /**
    * Creates a RecordBuilderBase for building records of the given type.
    * @param schema the schema associated with the record class.
@@ -141,7 +146,7 @@ public abstract class RecordBuilderBase<
    * @throws IOException 
    */
   @SuppressWarnings({ "rawtypes", "unchecked" })
-  protected Object getDefaultValue(Field field) throws IOException {    
+  protected Object defaultValue(Field field) throws IOException {    
     if (field.schema().getType() == Type.NULL) {
       return null;
     }

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericRecordBuilder.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericRecordBuilder.java?rev=1197765&r1=1197764&r2=1197765&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericRecordBuilder.java
(original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericRecordBuilder.java
Fri Nov  4 20:50:17 2011
@@ -56,12 +56,12 @@ public class GenericRecordBuilder extend
     record = new GenericData.Record(other, /* deepCopy = */ true);
     
     // Set all fields in the RecordBuilder that are set in the record
-    for (Field f : schema.getFields()) {
+    for (Field f : schema().getFields()) {
       Object value = other.get(f.pos());
       // Only set the value if it is not null, if the schema type is null, 
       // or if the schema type is a union that accepts nulls.
       if (isValidValue(f, value)) {
-        set(f, data.deepCopy(f.schema(), value));
+        set(f, data().deepCopy(f.schema(), value));
       }
     }
   }
@@ -72,7 +72,7 @@ public class GenericRecordBuilder extend
    * @return the value of the field with the given name, or null if not set.
    */
   public Object get(String fieldName) {
-    return get(schema.getField(fieldName));
+    return get(schema().getField(fieldName));
   }
   
   /**
@@ -100,7 +100,7 @@ public class GenericRecordBuilder extend
    * @return a reference to the RecordBuilder.
    */
   public GenericRecordBuilder set(String fieldName, Object value) {
-    return set(schema.getField(fieldName), value);
+    return set(schema().getField(fieldName), value);
   }
   
   /**
@@ -120,7 +120,7 @@ public class GenericRecordBuilder extend
    * @return a reference to the RecordBuilder.
    */
   protected GenericRecordBuilder set(int pos, Object value) {
-    return set(fields[pos], pos, value);
+    return set(fields()[pos], pos, value);
   }
   
   /**
@@ -133,7 +133,7 @@ public class GenericRecordBuilder extend
   private GenericRecordBuilder set(Field field, int pos, Object value) {
     validate(field, value);
     record.put(pos, value);
-    fieldSetFlags[pos] = true;
+    fieldSetFlags()[pos] = true;
     return this;
   }
   
@@ -143,7 +143,7 @@ public class GenericRecordBuilder extend
    * @return true if the given field is non-null; false otherwise.
    */
   public boolean has(String fieldName) {
-    return has(schema.getField(fieldName));
+    return has(schema().getField(fieldName));
   }
   
   /**
@@ -161,7 +161,7 @@ public class GenericRecordBuilder extend
    * @return true if the given field is non-null; false otherwise.
    */
   protected boolean has(int pos) {
-    return fieldSetFlags[pos];
+    return fieldSetFlags()[pos];
   }
   
   /**
@@ -170,7 +170,7 @@ public class GenericRecordBuilder extend
    * @return a reference to the RecordBuilder.
    */
   public GenericRecordBuilder clear(String fieldName) {
-    return clear(schema.getField(fieldName));
+    return clear(schema().getField(fieldName));
   }
   
   /**
@@ -189,7 +189,7 @@ public class GenericRecordBuilder extend
    */
   protected GenericRecordBuilder clear(int pos) {
     record.put(pos, null);
-    fieldSetFlags[pos] = false;
+    fieldSetFlags()[pos] = false;
     return this;
   }
   
@@ -197,12 +197,12 @@ public class GenericRecordBuilder extend
   public Record build() {
     Record record;
     try {
-      record = new GenericData.Record(schema);
+      record = new GenericData.Record(schema());
     } catch (Exception e) {
       throw new AvroRuntimeException(e);
     }
     
-    for (Field field : fields) {
+    for (Field field : fields()) {
       Object value;
       try {
         value = getWithDefault(field);
@@ -229,8 +229,8 @@ public class GenericRecordBuilder extend
    * @throws IOException
    */
   private Object getWithDefault(Field field) throws IOException {
-    return fieldSetFlags[field.pos()] ? 
-        record.get(field.pos()) : getDefaultValue(field);
+    return fieldSetFlags()[field.pos()] ? 
+        record.get(field.pos()) : defaultValue(field);
   }
 
   @Override

Modified: avro/trunk/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm?rev=1197765&r1=1197764&r2=1197765&view=diff
==============================================================================
--- avro/trunk/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm
(original)
+++ avro/trunk/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm
Fri Nov  4 20:50:17 2011
@@ -134,9 +134,9 @@ public class ${this.mangle($schema.getNa
       #if ($schema.isError())super(other)#else
       super(#if ($schema.getNamespace())$schema.getNamespace().#end${this.mangle($schema.getName())}.SCHEMA$)#end;
 #foreach ($field in $schema.getFields())
-      if (isValidValue(fields[$field.pos()], other.${this.mangle($field.name(), $schema.isError())}))
{
-        ${this.mangle($field.name(), $schema.isError())} = (${this.javaType($field.schema())})
data.deepCopy(fields[$field.pos()].schema(), other.${this.mangle($field.name(), $schema.isError())});
-        fieldSetFlags[$field.pos()] = true;
+      if (isValidValue(fields()[$field.pos()], other.${this.mangle($field.name(), $schema.isError())}))
{
+        this.${this.mangle($field.name(), $schema.isError())} = (${this.javaType($field.schema())})
data().deepCopy(fields()[$field.pos()].schema(), other.${this.mangle($field.name(), $schema.isError())});
+        fieldSetFlags()[$field.pos()] = true;
       }
 #end
     }
@@ -175,15 +175,15 @@ public class ${this.mangle($schema.getNa
     
     /** Sets the value of the '${this.mangle($field.name(), $schema.isError())}' field */
     public #if ($schema.getNamespace())$schema.getNamespace().#end${this.mangle($schema.getName())}.Builder
${this.generateSetMethod($schema, $field)}(${this.javaUnbox($field.schema())} value) {
-      validate(fields[$field.pos()], value);
+      validate(fields()[$field.pos()], value);
       this.${this.mangle($field.name(), $schema.isError())} = value;
-      fieldSetFlags[$field.pos()] = true;
+      fieldSetFlags()[$field.pos()] = true;
       return this; 
     }
     
     /** Checks whether the '${this.mangle($field.name(), $schema.isError())}' field has been
set */
     public boolean ${this.generateHasMethod($schema, $field)}() {
-      return fieldSetFlags[$field.pos()];
+      return fieldSetFlags()[$field.pos()];
     }
     
     /** Clears the value of the '${this.mangle($field.name(), $schema.isError())}' field
*/
@@ -191,7 +191,7 @@ public class ${this.mangle($schema.getNa
 #if (${this.isUnboxedJavaTypeNullable($field.schema())})
       ${this.mangle($field.name(), $schema.isError())} = null;
 #end
-      fieldSetFlags[$field.pos()] = false;
+      fieldSetFlags()[$field.pos()] = false;
       return this;
     }
 
@@ -201,7 +201,7 @@ public class ${this.mangle($schema.getNa
       try {
         ${this.mangle($schema.getName())} record = new ${this.mangle($schema.getName())}(#if
($schema.isError())getValue(), getCause()#end);
 #foreach ($field in $schema.getFields())
-        record.${this.mangle($field.name(), $schema.isError())} = fieldSetFlags[$field.pos()]
? this.${this.mangle($field.name(), $schema.isError())} : (${this.javaType($field.schema())})
getDefaultValue(fields[$field.pos()]);
+        record.${this.mangle($field.name(), $schema.isError())} = fieldSetFlags()[$field.pos()]
? this.${this.mangle($field.name(), $schema.isError())} : (${this.javaType($field.schema())})
defaultValue(fields()[$field.pos()]);
 #end
         return record;
       } catch (Exception e) {

Modified: avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/compiler/specific/TestSpecificCompiler.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/compiler/specific/TestSpecificCompiler.java?rev=1197765&r1=1197764&r2=1197765&view=diff
==============================================================================
--- avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/compiler/specific/TestSpecificCompiler.java
(original)
+++ avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/compiler/specific/TestSpecificCompiler.java
Fri Nov  4 20:50:17 2011
@@ -120,6 +120,10 @@ public class TestSpecificCompiler {
     String schema = "" +
       "{ \"name\": \"volatile\", \"type\": \"record\", " +
       "  \"fields\": [ {\"name\": \"package\", \"type\": \"string\" }," +
+      "                {\"name\": \"data\", \"type\": \"int\" }," +
+      "                {\"name\": \"value\", \"type\": \"int\" }," +
+      "                {\"name\": \"defaultValue\", \"type\": \"int\" }," +
+      "                {\"name\": \"other\", \"type\": \"int\" }," +
       "                {\"name\": \"short\", \"type\": \"volatile\" } ] }";
     Collection<OutputFile> c =
       new SpecificCompiler(Schema.parse(schema)).compile();

Modified: avro/trunk/lang/java/tools/src/test/compiler/output/Player.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/tools/src/test/compiler/output/Player.java?rev=1197765&r1=1197764&r2=1197765&view=diff
==============================================================================
--- avro/trunk/lang/java/tools/src/test/compiler/output/Player.java (original)
+++ avro/trunk/lang/java/tools/src/test/compiler/output/Player.java Fri Nov  4 20:50:17 2011
@@ -133,21 +133,21 @@ public class Player extends org.apache.a
     /** Creates a Builder by copying an existing Player instance */
     private Builder(avro.examples.baseball.Player other) {
             super(avro.examples.baseball.Player.SCHEMA$);
-      if (isValidValue(fields[0], other.number)) {
-        number = (java.lang.Integer) data.deepCopy(fields[0].schema(), other.number);
-        fieldSetFlags[0] = true;
+      if (isValidValue(fields()[0], other.number)) {
+        this.number = (java.lang.Integer) data().deepCopy(fields()[0].schema(), other.number);
+        fieldSetFlags()[0] = true;
       }
-      if (isValidValue(fields[1], other.first_name)) {
-        first_name = (java.lang.CharSequence) data.deepCopy(fields[1].schema(), other.first_name);
-        fieldSetFlags[1] = true;
+      if (isValidValue(fields()[1], other.first_name)) {
+        this.first_name = (java.lang.CharSequence) data().deepCopy(fields()[1].schema(),
other.first_name);
+        fieldSetFlags()[1] = true;
       }
-      if (isValidValue(fields[2], other.last_name)) {
-        last_name = (java.lang.CharSequence) data.deepCopy(fields[2].schema(), other.last_name);
-        fieldSetFlags[2] = true;
+      if (isValidValue(fields()[2], other.last_name)) {
+        this.last_name = (java.lang.CharSequence) data().deepCopy(fields()[2].schema(), other.last_name);
+        fieldSetFlags()[2] = true;
       }
-      if (isValidValue(fields[3], other.position)) {
-        position = (java.util.List<avro.examples.baseball.Position>) data.deepCopy(fields[3].schema(),
other.position);
-        fieldSetFlags[3] = true;
+      if (isValidValue(fields()[3], other.position)) {
+        this.position = (java.util.List<avro.examples.baseball.Position>) data().deepCopy(fields()[3].schema(),
other.position);
+        fieldSetFlags()[3] = true;
       }
     }
 
@@ -158,20 +158,20 @@ public class Player extends org.apache.a
     
     /** Sets the value of the 'number' field */
     public avro.examples.baseball.Player.Builder setNumber(int value) {
-      validate(fields[0], value);
+      validate(fields()[0], value);
       this.number = value;
-      fieldSetFlags[0] = true;
+      fieldSetFlags()[0] = true;
       return this; 
     }
     
     /** Checks whether the 'number' field has been set */
     public boolean hasNumber() {
-      return fieldSetFlags[0];
+      return fieldSetFlags()[0];
     }
     
     /** Clears the value of the 'number' field */
     public avro.examples.baseball.Player.Builder clearNumber() {
-      fieldSetFlags[0] = false;
+      fieldSetFlags()[0] = false;
       return this;
     }
 
@@ -182,21 +182,21 @@ public class Player extends org.apache.a
     
     /** Sets the value of the 'first_name' field */
     public avro.examples.baseball.Player.Builder setFirstName(java.lang.CharSequence value)
{
-      validate(fields[1], value);
+      validate(fields()[1], value);
       this.first_name = value;
-      fieldSetFlags[1] = true;
+      fieldSetFlags()[1] = true;
       return this; 
     }
     
     /** Checks whether the 'first_name' field has been set */
     public boolean hasFirstName() {
-      return fieldSetFlags[1];
+      return fieldSetFlags()[1];
     }
     
     /** Clears the value of the 'first_name' field */
     public avro.examples.baseball.Player.Builder clearFirstName() {
       first_name = null;
-      fieldSetFlags[1] = false;
+      fieldSetFlags()[1] = false;
       return this;
     }
 
@@ -207,21 +207,21 @@ public class Player extends org.apache.a
     
     /** Sets the value of the 'last_name' field */
     public avro.examples.baseball.Player.Builder setLastName(java.lang.CharSequence value)
{
-      validate(fields[2], value);
+      validate(fields()[2], value);
       this.last_name = value;
-      fieldSetFlags[2] = true;
+      fieldSetFlags()[2] = true;
       return this; 
     }
     
     /** Checks whether the 'last_name' field has been set */
     public boolean hasLastName() {
-      return fieldSetFlags[2];
+      return fieldSetFlags()[2];
     }
     
     /** Clears the value of the 'last_name' field */
     public avro.examples.baseball.Player.Builder clearLastName() {
       last_name = null;
-      fieldSetFlags[2] = false;
+      fieldSetFlags()[2] = false;
       return this;
     }
 
@@ -232,21 +232,21 @@ public class Player extends org.apache.a
     
     /** Sets the value of the 'position' field */
     public avro.examples.baseball.Player.Builder setPosition(java.util.List<avro.examples.baseball.Position>
value) {
-      validate(fields[3], value);
+      validate(fields()[3], value);
       this.position = value;
-      fieldSetFlags[3] = true;
+      fieldSetFlags()[3] = true;
       return this; 
     }
     
     /** Checks whether the 'position' field has been set */
     public boolean hasPosition() {
-      return fieldSetFlags[3];
+      return fieldSetFlags()[3];
     }
     
     /** Clears the value of the 'position' field */
     public avro.examples.baseball.Player.Builder clearPosition() {
       position = null;
-      fieldSetFlags[3] = false;
+      fieldSetFlags()[3] = false;
       return this;
     }
 
@@ -254,10 +254,10 @@ public class Player extends org.apache.a
     public Player build() {
       try {
         Player record = new Player();
-        record.number = fieldSetFlags[0] ? this.number : (java.lang.Integer) getDefaultValue(fields[0]);
-        record.first_name = fieldSetFlags[1] ? this.first_name : (java.lang.CharSequence)
getDefaultValue(fields[1]);
-        record.last_name = fieldSetFlags[2] ? this.last_name : (java.lang.CharSequence) getDefaultValue(fields[2]);
-        record.position = fieldSetFlags[3] ? this.position : (java.util.List<avro.examples.baseball.Position>)
getDefaultValue(fields[3]);
+        record.number = fieldSetFlags()[0] ? this.number : (java.lang.Integer) defaultValue(fields()[0]);
+        record.first_name = fieldSetFlags()[1] ? this.first_name : (java.lang.CharSequence)
defaultValue(fields()[1]);
+        record.last_name = fieldSetFlags()[2] ? this.last_name : (java.lang.CharSequence)
defaultValue(fields()[2]);
+        record.position = fieldSetFlags()[3] ? this.position : (java.util.List<avro.examples.baseball.Position>)
defaultValue(fields()[3]);
         return record;
       } catch (Exception e) {
         throw new org.apache.avro.AvroRuntimeException(e);



Mime
View raw message