Skip to content

Commit 04a30f2

Browse files
AlexRiedleropwvhk
andcommitted
AVRO-4098: [maven-plugin]: make protocol-idl mojo support all compiler options [AVRO-4098] (#3261)
* AVRO-40980: [maven-plugin] make mojos support all compiler options Keep compiler settings DRY, in particular make idl-protocol support for createNullSafeAnnotations flag * AVRO-4098: Add test case --------- Co-authored-by: Oscar Westra van Holthe - Kind <opwvhk@apache.org> (cherry picked from commit 70d6e7c)
1 parent 9859228 commit 04a30f2

File tree

4 files changed

+14
-18
lines changed

4 files changed

+14
-18
lines changed

lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/AbstractAvroMojo.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,7 @@ protected void doCompile(File sourceFileForModificationDetection, Protocol proto
422422
doCompile(sourceFileForModificationDetection, new SpecificCompiler(protocol), outputDirectory);
423423
}
424424

425-
private void doCompile(File sourceFileForModificationDetection, SpecificCompiler compiler, File outputDirectory)
426-
throws IOException {
425+
protected void setCompilerProperties(SpecificCompiler compiler) {
427426
compiler.setTemplateDir(templateDirectory);
428427
compiler.setStringType(GenericData.StringType.valueOf(stringType));
429428
compiler.setFieldVisibility(getFieldVisibility());
@@ -435,17 +434,22 @@ private void doCompile(File sourceFileForModificationDetection, SpecificCompiler
435434
compiler.setNullSafeAnnotationNullable(nullSafeAnnotationNullable);
436435
compiler.setNullSafeAnnotationNotNull(nullSafeAnnotationNotNull);
437436
compiler.setEnableDecimalLogicalType(enableDecimalLogicalType);
437+
compiler.setOutputCharacterEncoding(project.getProperties().getProperty("project.build.sourceEncoding"));
438+
compiler.setAdditionalVelocityTools(instantiateAdditionalVelocityTools());
439+
compiler.setRecordSpecificClass(this.recordSpecificClass);
440+
compiler.setErrorSpecificClass(this.errorSpecificClass);
441+
}
442+
443+
private void doCompile(File sourceFileForModificationDetection, SpecificCompiler compiler, File outputDirectory)
444+
throws IOException {
445+
setCompilerProperties(compiler);
438446
try {
439447
for (String customConversion : customConversions) {
440448
compiler.addCustomConversion(Thread.currentThread().getContextClassLoader().loadClass(customConversion));
441449
}
442450
} catch (ClassNotFoundException e) {
443451
throw new IOException(e);
444452
}
445-
compiler.setOutputCharacterEncoding(project.getProperties().getProperty("project.build.sourceEncoding"));
446-
compiler.setAdditionalVelocityTools(instantiateAdditionalVelocityTools());
447-
compiler.setRecordSpecificClass(this.recordSpecificClass);
448-
compiler.setErrorSpecificClass(this.errorSpecificClass);
449453
compiler.compileToDestination(sourceFileForModificationDetection, outputDirectory);
450454
}
451455

lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/IDLMojo.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import org.apache.avro.Protocol;
2222
import org.apache.avro.compiler.specific.SpecificCompiler;
23-
import org.apache.avro.generic.GenericData;
2423
import org.apache.avro.idl.IdlFile;
2524
import org.apache.avro.idl.IdlReader;
2625
import org.apache.maven.artifact.DependencyResolutionRequiredException;
@@ -97,19 +96,10 @@ protected void doCompile(String filename, File sourceDirectory, File outputDirec
9796
} else {
9897
compiler = new SpecificCompiler(idlFile.getNamedSchemas().values());
9998
}
100-
compiler.setStringType(GenericData.StringType.valueOf(stringType));
101-
compiler.setTemplateDir(templateDirectory);
102-
compiler.setFieldVisibility(getFieldVisibility());
103-
compiler.setCreateOptionalGetters(createOptionalGetters);
104-
compiler.setGettersReturnOptional(gettersReturnOptional);
105-
compiler.setOptionalGettersForNullableFieldsOnly(optionalGettersForNullableFieldsOnly);
106-
compiler.setCreateSetters(createSetters);
107-
compiler.setAdditionalVelocityTools(instantiateAdditionalVelocityTools());
108-
compiler.setEnableDecimalLogicalType(enableDecimalLogicalType);
99+
setCompilerProperties(compiler);
109100
for (String customConversion : customConversions) {
110101
compiler.addCustomConversion(projPathLoader.loadClass(customConversion));
111102
}
112-
compiler.setOutputCharacterEncoding(project.getProperties().getProperty("project.build.sourceEncoding"));
113103
compiler.compileToDestination(sourceFilePath.toFile(), outputDirectory);
114104
} finally {
115105
Thread.currentThread().setContextClassLoader(contextClassLoader);

lang/java/maven-plugin/src/test/java/org/apache/avro/mojo/TestIDLMojo.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ public void testIdlProtocolMojo() throws Exception {
6161
assertFilesExist(outputDir, generatedFiles);
6262

6363
final String idlUserContent = FileUtils.fileRead(new File(outputDir, "IdlUser.java"));
64-
assertTrue(idlUserContent.contains("java.time.Instant"));
64+
assertTrue(idlUserContent.contains("@org.jetbrains.annotations.Nullable\n public java.lang.String getId"));
65+
assertTrue(idlUserContent.contains("@org.jetbrains.annotations.NotNull\n public java.time.Instant getModifiedOn"));
6566

6667
assertEquals(Collections.singletonList("[WARN] Line 22, char 1: Ignoring out-of-place documentation comment.\n"
6768
+ "Did you mean to use a multiline comment ( /* ... */ ) instead?"), log.getLogEntries());

lang/java/maven-plugin/src/test/resources/unit/idl/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<sourceDirectory>${basedir}/src/test</sourceDirectory>
4848
<outputDirectory>${basedir}/target/test-harness/idl</outputDirectory>
4949
<stringType>String</stringType>
50+
<createNullSafeAnnotations>true</createNullSafeAnnotations>
5051
<project implementation="org.apache.maven.plugin.testing.stubs.MavenProjectStub"/>
5152
</configuration>
5253
</plugin>

0 commit comments

Comments
 (0)