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 8 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
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 @@ -161,7 +161,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()})@org.jetbrains.annotations.Nullable#[email protected]#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 +243,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()})
@org.jetbrains.annotations.Nullable
#else
@org.jetbrains.annotations.NotNull
#end
#end
public ${this.javaUnbox($field.schema(), false)} ${this.generateGetMethod($schema, $field)}() {
return ${this.mangle($field.name(), $schema.isError())};
}
Expand All @@ -267,7 +274,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()})@org.jetbrains.annotations.Nullable#[email protected]#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 +430,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()})@org.jetbrains.annotations.Nullable#[email protected]#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
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ public abstract class AbstractAvroMojo extends AbstractMojo {
*/
protected boolean createSetters;

/**
* The createNullSafeAnnotations parameters adds @Nullable and @NotNull
* annotations for fhe fields of the record. The default is to not include
* annotations.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we please mention here that the annotations are JetBrains annotations?

Then it'll automatically be documented by the plugin help. Ideally, it also lists the dependency coordinates / package-url (pkg:maven/org.jetbrains/[email protected]).

Copy link
Contributor

Choose a reason for hiding this comment

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

At this point in time, there are no other places on the website that document the maven plugin, or code generation options. It's only mentioned in passing on the "Getting started" page for Java.

*
* @parameter property="createNullSafeAnnotations"
*/
protected boolean createNullSafeAnnotations = false;

/**
* A set of fully qualified class names of custom
* {@link org.apache.avro.Conversion} implementations to add to the compiler.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ protected void doCompile(String filename, File sourceDirectory, File outputDirec
compiler.setGettersReturnOptional(gettersReturnOptional);
compiler.setOptionalGettersForNullableFieldsOnly(optionalGettersForNullableFieldsOnly);
compiler.setCreateSetters(createSetters);
compiler.setCreateNullSafeAnnotations(createNullSafeAnnotations);
compiler.setAdditionalVelocityTools(instantiateAdditionalVelocityTools());
compiler.setEnableDecimalLogicalType(enableDecimalLogicalType);
for (String customConversion : customConversions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ protected void doCompile(String filename, File sourceDirectory, File outputDirec
compiler.setGettersReturnOptional(gettersReturnOptional);
compiler.setOptionalGettersForNullableFieldsOnly(optionalGettersForNullableFieldsOnly);
compiler.setCreateSetters(createSetters);
compiler.setCreateNullSafeAnnotations(createNullSafeAnnotations);
compiler.setAdditionalVelocityTools(instantiateAdditionalVelocityTools());
compiler.setEnableDecimalLogicalType(enableDecimalLogicalType);
final URLClassLoader classLoader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ protected void doCompile(String filename, File sourceDirectory, File outputDirec
compiler.setGettersReturnOptional(gettersReturnOptional);
compiler.setOptionalGettersForNullableFieldsOnly(optionalGettersForNullableFieldsOnly);
compiler.setCreateSetters(createSetters);
compiler.setCreateNullSafeAnnotations(createNullSafeAnnotations);
compiler.setEnableDecimalLogicalType(enableDecimalLogicalType);
try {
final URLClassLoader classLoader = createClassLoader();
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 @org.jetbrains.annotations.Nullable and @org.jetbrains.annotations.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