0

I am building source code of Kaa project and they used Apache avro for generating source code.

I used avro library as instruction from here but I got only "@Deprecated" and "public" attribute, I think they should be "private".

Here is my .avsc file:

{
   "type":"record",
   "name":"User",
   "namespace":"com.example",
   "fields":[
      {
         "name":"username",
         "type":"string"
      },
      {
         "name":"password",
         "type":"string"
      }
   ]
}

Here is my User.java:

/**
 * Autogenerated by Avro
 * 
 * DO NOT EDIT DIRECTLY
 */
package com.example;  
@SuppressWarnings("all")
@org.apache.avro.specific.AvroGenerated
public class User extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord {
  public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"User\",\"namespace\":\"com.example\",\"fields\":[{\"name\":\"username\",\"type\":\"string\"},{\"name\":\"password\",\"type\":\"string\"}]}");
  public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
  @Deprecated public java.lang.CharSequence username;
  @Deprecated public java.lang.CharSequence password;

  /**
   * Default constructor.  Note that this does not initialize fields
   * to their default values from the schema.  If that is desired then
   * one should use <code>newBuilder()</code>. 
   */
  public User() {}

  /**
   * All-args constructor.
   */
  public User(java.lang.CharSequence username, java.lang.CharSequence password) {
    this.username = username;
    this.password = password;
  }

  public org.apache.avro.Schema getSchema() { return SCHEMA$; }
  // Used by DatumWriter.  Applications should not call. 
  public java.lang.Object get(int field$) {
    switch (field$) {
    case 0: return username;
    case 1: return password;
    default: throw new org.apache.avro.AvroRuntimeException("Bad index");
    }
  }
  // Used by DatumReader.  Applications should not call. 
  @SuppressWarnings(value="unchecked")
  public void put(int field$, java.lang.Object value$) {
    switch (field$) {
    case 0: username = (java.lang.CharSequence)value$; break;
    case 1: password = (java.lang.CharSequence)value$; break;
    default: throw new org.apache.avro.AvroRuntimeException("Bad index");
    }
  }

  /**
   * Gets the value of the 'username' field.
   */
  public java.lang.CharSequence getUsername() {
    return username;
  }

  /**
   * Sets the value of the 'username' field.
   * @param value the value to set.
   */
  public void setUsername(java.lang.CharSequence value) {
    this.username = value;
  }

  /**
   * Gets the value of the 'password' field.
   */
  public java.lang.CharSequence getPassword() {
    return password;
  }

  /**
   * Sets the value of the 'password' field.
   * @param value the value to set.
   */
  public void setPassword(java.lang.CharSequence value) {
    this.password = value;
  }

  /** Creates a new User RecordBuilder */
  public static com.example.User.Builder newBuilder() {
    return new com.example.User.Builder();
  }

  /** Creates a new User RecordBuilder by copying an existing Builder */
  public static com.example.User.Builder newBuilder(com.example.User.Builder other) {
    return new com.example.User.Builder(other);
  }

  /** Creates a new User RecordBuilder by copying an existing User instance */
  public static com.example.User.Builder newBuilder(com.example.User other) {
    return new com.example.User.Builder(other);
  }

  /**
   * RecordBuilder for User instances.
   */
  public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase<User>
    implements org.apache.avro.data.RecordBuilder<User> {

    private java.lang.CharSequence username;
    private java.lang.CharSequence password;

    /** Creates a new Builder */
    private Builder() {
      super(com.example.User.SCHEMA$);
    }

    /** Creates a Builder by copying an existing Builder */
    private Builder(com.example.User.Builder other) {
      super(other);
      if (isValidValue(fields()[0], other.username)) {
        this.username = data().deepCopy(fields()[0].schema(), other.username);
        fieldSetFlags()[0] = true;
      }
      if (isValidValue(fields()[1], other.password)) {
        this.password = data().deepCopy(fields()[1].schema(), other.password);
        fieldSetFlags()[1] = true;
      }
    }

    /** Creates a Builder by copying an existing User instance */
    private Builder(com.example.User other) {
            super(com.example.User.SCHEMA$);
      if (isValidValue(fields()[0], other.username)) {
        this.username = data().deepCopy(fields()[0].schema(), other.username);
        fieldSetFlags()[0] = true;
      }
      if (isValidValue(fields()[1], other.password)) {
        this.password = data().deepCopy(fields()[1].schema(), other.password);
        fieldSetFlags()[1] = true;
      }
    }

    /** Gets the value of the 'username' field */
    public java.lang.CharSequence getUsername() {
      return username;
    }

    /** Sets the value of the 'username' field */
    public com.example.User.Builder setUsername(java.lang.CharSequence value) {
      validate(fields()[0], value);
      this.username = value;
      fieldSetFlags()[0] = true;
      return this; 
    }

    /** Checks whether the 'username' field has been set */
    public boolean hasUsername() {
      return fieldSetFlags()[0];
    }

    /** Clears the value of the 'username' field */
    public com.example.User.Builder clearUsername() {
      username = null;
      fieldSetFlags()[0] = false;
      return this;
    }

    /** Gets the value of the 'password' field */
    public java.lang.CharSequence getPassword() {
      return password;
    }

    /** Sets the value of the 'password' field */
    public com.example.User.Builder setPassword(java.lang.CharSequence value) {
      validate(fields()[1], value);
      this.password = value;
      fieldSetFlags()[1] = true;
      return this; 
    }

    /** Checks whether the 'password' field has been set */
    public boolean hasPassword() {
      return fieldSetFlags()[1];
    }

    /** Clears the value of the 'password' field */
    public com.example.User.Builder clearPassword() {
      password = null;
      fieldSetFlags()[1] = false;
      return this;
    }

    @Override
    public User build() {
      try {
        User record = new User();
        record.username = fieldSetFlags()[0] ? this.username : (java.lang.CharSequence) defaultValue(fields()[0]);
        record.password = fieldSetFlags()[1] ? this.password : (java.lang.CharSequence) defaultValue(fields()[1]);
        return record;
      } catch (Exception e) {
        throw new org.apache.avro.AvroRuntimeException(e);
      }
    }
  }
}

I'm new to java, so please show me where I'm not correct.

2 Answers 2

0

I don't think there is a way to mark them as private, but you're supposed to use the Builder objects to create your classes and the getter methods to retrieve the fields rather than directly accessing the public fields anyway

3
  • Yes, that's what I want. And I see in source code of Kaa project, they can generate with access modifiers are "private". I'm just wondering how they can do that.
    – ktcl
    Jun 17, 2019 at 2:42
  • I think the issue is this library uses an old version of Avro (1.7.5). When I use 1.8.1 or newer, then I see the fields as private Jun 18, 2019 at 21:55
  • I used all versions from 1.7.7 to the latest and it didn't work correctly :-(
    – ktcl
    Jun 21, 2019 at 7:24
0

In your pom.xml, you can specify the fieldVisibility (documentation)

<!-- ... -->
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.avro</groupId>
            <artifactId>avro-maven-plugin</artifactId>
            <version>1.9.2</version>
            <configuration>
                <fieldVisibility>private</fieldVisibility>
            </configuration>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>schema</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

In the resulting generated code, the field username and password are now private:

@AvroGenerated
public class User extends SpecificRecordBase implements SpecificRecord {
    private static final long serialVersionUID = -203188480172108080L;
    public static final Schema SCHEMA$ = (new Parser()).parse("{\"type\":\"record\",\"name\":\"User\",\"namespace\":\"com.example\",\"fields\":[{\"name\":\"username\",\"type\":\"string\"},{\"name\":\"password\",\"type\":\"string\"}]}");
    private static SpecificData MODEL$ = new SpecificData();
    private static final BinaryMessageEncoder<User> ENCODER;
    private static final BinaryMessageDecoder<User> DECODER;
    private CharSequence username;
    private CharSequence password;
    private static final DatumWriter<User> WRITER$;
    private static final DatumReader<User> READER$;

    // ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.