Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AVRO-3641: Adds support for nullSafeAnnotations to java SpecificCompiler #2142

Merged
merged 13 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lang/java/compiler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
</dependency>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only see the dependency being used in the test code (which is perfect IMHO). Can we please reduce the scope to test?

<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ void addLogicalTypeConversions(SpecificData specificData) {
private boolean gettersReturnOptional = false;
private boolean optionalGettersForNullableFieldsOnly = false;
private boolean createSetters = true;
private boolean createNullSafeAnnotations = false;
private boolean createAllArgsConstructor = true;
private String outputCharacterEncoding;
private boolean enableDecimalLogicalType = false;
Expand Down Expand Up @@ -246,6 +247,17 @@ public void setCreateSetters(boolean createSetters) {
this.createSetters = createSetters;
}

public boolean isCreateNullSafeAnnotations() {
return this.createNullSafeAnnotations;
}

/**
* Set to true to add jetbrains @Nullable and @NotNull annotations
*/
public void setCreateNullSafeAnnotations(boolean createNullSafeAnnotations) {
this.createNullSafeAnnotations = createNullSafeAnnotations;
}

public boolean isCreateOptionalGetters() {
return this.createOptionalGetters;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import org.apache.avro.message.BinaryMessageDecoder;
import org.apache.avro.message.SchemaStore;
#end
#if (${this.gettersReturnOptional} || ${this.createOptionalGetters})import java.util.Optional;#end
#if (${this.createNullSafeAnnotations})import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;
#end
opwvhk marked this conversation as resolved.
Show resolved Hide resolved

#if ($schema.getDoc())
/** $schema.getDoc() */
Expand Down Expand Up @@ -161,7 +164,7 @@ public class ${this.mangleTypeIdentifier($schema.getName())}#if ($schema.isError
#end
#end
*/
public ${this.mangleTypeIdentifier($schema.getName())}(#foreach($field in $schema.getFields())${this.javaType($field.schema())} ${this.mangle($field.name())}#if($foreach.count < $schema.getFields().size()), #end#end) {
public ${this.mangleTypeIdentifier($schema.getName())}(#foreach($field in $schema.getFields())#if(${this.createNullSafeAnnotations})#if(${field.schema().isNullable()})@Nullable#else@NotNull#end #end${this.javaType($field.schema())} ${this.mangle($field.name())}#if($foreach.count < $schema.getFields().size()), #end#end) {
#foreach ($field in $schema.getFields())
${this.generateSetterCode($field.schema(), ${this.mangle($field.name())}, ${this.mangle($field.name())})}
#end
Expand Down Expand Up @@ -243,6 +246,13 @@ public class ${this.mangleTypeIdentifier($schema.getName())}#if ($schema.isError
#else * @return The value of the '${this.mangle($field.name(), $schema.isError())}' field.
#end
*/
#if(${this.createNullSafeAnnotations})
#if (${field.schema().isNullable()})
@Nullable
#else
@NotNull
#end
#end
public ${this.javaUnbox($field.schema(), false)} ${this.generateGetMethod($schema, $field)}() {
return ${this.mangle($field.name(), $schema.isError())};
}
Expand All @@ -267,7 +277,7 @@ public class ${this.mangleTypeIdentifier($schema.getName())}#if ($schema.isError
#end
* @param value the value to set.
*/
public void ${this.generateSetMethod($schema, $field)}(${this.javaUnbox($field.schema(), false)} value) {
public void ${this.generateSetMethod($schema, $field)}(#if(${this.createNullSafeAnnotations})#if(${field.schema().isNullable()})@Nullable#else@NotNull#end #end${this.javaUnbox($field.schema(), false)} value) {
${this.generateSetterCode($field.schema(), ${this.mangle($field.name(), $schema.isError())}, "value")}
}
#end
Expand Down Expand Up @@ -423,7 +433,7 @@ public class ${this.mangleTypeIdentifier($schema.getName())}#if ($schema.isError
* @param value The value of '${this.mangle($field.name(), $schema.isError())}'.
* @return This builder.
*/
public #if ($schema.getNamespace())$this.mangle($schema.getNamespace()).#end${this.mangleTypeIdentifier($schema.getName())}.Builder ${this.generateSetMethod($schema, $field)}(${this.javaUnbox($field.schema(), false)} value) {
public #if ($schema.getNamespace())$this.mangle($schema.getNamespace()).#end${this.mangleTypeIdentifier($schema.getName())}.Builder ${this.generateSetMethod($schema, $field)}(#if(${this.createNullSafeAnnotations})#if(${field.schema().isNullable()})@Nullable#else@NotNull#end #end${this.javaUnbox($field.schema(), false)} value) {
validate(fields()[$field.pos()], value);
#if (${this.hasBuilder($field.schema())})
this.${this.mangle($field.name(), $schema.isError())}Builder = null;
Expand Down
4 changes: 4 additions & 0 deletions lang/java/ipc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
</dependency>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this dependency actually used here?

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
Expand Down
6 changes: 6 additions & 0 deletions lang/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<hamcrest.version>2.2</hamcrest.version>
<grpc.version>1.53.0</grpc.version>
<zstd-jni.version>1.5.4-2</zstd-jni.version>
<jetbrains-annotations.version>23.0.0</jetbrains-annotations.version>
<!-- version properties for plugins -->
<archetype-plugin.version>3.2.1</archetype-plugin.version>
<bundle-plugin-version>5.1.8</bundle-plugin-version>
Expand Down Expand Up @@ -605,6 +606,11 @@
<artifactId>zstd-jni</artifactId>
<version>${zstd-jni.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>${jetbrains-annotations.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ public int run(InputStream in, PrintStream out, PrintStream err, List<String> or
if (origArgs.size() < 3) {
System.err
.println("Usage: [-encoding <outputencoding>] [-string] [-bigDecimal] [-fieldVisibility <visibilityType>] "
+ "[-noSetters] [-addExtraOptionalGetters] [-optionalGetters <optionalGettersType>] "
+ "[-noSetters] [-nullSafeAnnotations] [-addExtraOptionalGetters] [-optionalGetters <optionalGettersType>] "
+ "[-templateDir <templateDir>] (schema|protocol) input... outputdir");
System.err.println(" input - input files or directories");
System.err.println(" outputdir - directory to write generated java");
System.err.println(" -encoding <outputencoding> - set the encoding of " + "output file(s)");
System.err.println(" -string - use java.lang.String instead of Utf8");
System.err.println(" -fieldVisibility [private|public] - use either and default private");
System.err.println(" -noSetters - do not generate setters");
System.err.println(" -nullSafeAnnotations - add @Nullable and @NotNull annotations");
System.err
.println(" -addExtraOptionalGetters - generate extra getters with this format: 'getOptional<FieldName>'");
System.err.println(
Expand All @@ -72,6 +73,7 @@ public int run(InputStream in, PrintStream out, PrintStream err, List<String> or
compilerOpts.stringType = StringType.CharSequence;
compilerOpts.useLogicalDecimal = false;
compilerOpts.createSetters = true;
compilerOpts.createNullSafeAnnotations = false;
compilerOpts.optionalGettersType = Optional.empty();
compilerOpts.addExtraOptionalGetters = false;
compilerOpts.encoding = Optional.empty();
Expand All @@ -85,6 +87,11 @@ public int run(InputStream in, PrintStream out, PrintStream err, List<String> or
args.remove(args.indexOf("-noSetters"));
}

if (args.contains("-nullSafeAnnotations")) {
compilerOpts.createNullSafeAnnotations = true;
args.remove(args.indexOf("-nullSafeAnnotations"));
}

if (args.contains("-addExtraOptionalGetters")) {
compilerOpts.addExtraOptionalGetters = true;
args.remove(args.indexOf("-addExtraOptionalGetters"));
Expand Down Expand Up @@ -172,6 +179,7 @@ private void executeCompiler(SpecificCompiler compiler, CompilerOptions opts, Fi
throws IOException {
compiler.setStringType(opts.stringType);
compiler.setCreateSetters(opts.createSetters);
compiler.setCreateNullSafeAnnotations(opts.createNullSafeAnnotations);

opts.optionalGettersType.ifPresent(choice -> {
compiler.setGettersReturnOptional(true);
Expand Down Expand Up @@ -267,6 +275,7 @@ private static class CompilerOptions {
Optional<FieldVisibility> fieldVisibility;
boolean useLogicalDecimal;
boolean createSetters;
boolean createNullSafeAnnotations;
boolean addExtraOptionalGetters;
Optional<OptionalGettersType> optionalGettersType;
Optional<String> templateDir;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{"type":"record", "name":"NullSafeAnnotationsFieldsTest", "namespace": "avro.examples.baseball","doc":"Test that @Nullable and @NotNull annotations are created for all fields",
"fields": [
{"name": "name", "type": "string"},
{"name": "nullable_name", "type": ["string", "null"]},
{"name": "favorite_number", "type": "int"},
{"name": "nullable_favorite_number", "type": ["int", "null"]}
]
}
Loading