From c225a84539edf431e41330f5a1ad4255b3cb4ad5 Mon Sep 17 00:00:00 2001 From: Alex Riedler Date: Sun, 8 Dec 2024 16:18:36 -0500 Subject: [PATCH] AVRO-40980: [maven-plugin] make mojos support all compiler options Keep compiler settings DRY, in particular make idl-protocol support for createNullSafeAnnotations flag --- .../org/apache/avro/mojo/AbstractAvroMojo.java | 16 ++++++++++------ .../main/java/org/apache/avro/mojo/IDLMojo.java | 11 +---------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/AbstractAvroMojo.java b/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/AbstractAvroMojo.java index 7dc203b0357..08a758818a9 100644 --- a/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/AbstractAvroMojo.java +++ b/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/AbstractAvroMojo.java @@ -395,8 +395,7 @@ protected void doCompile(File sourceFileForModificationDetection, Protocol proto doCompile(sourceFileForModificationDetection, new SpecificCompiler(protocol), outputDirectory); } - private void doCompile(File sourceFileForModificationDetection, SpecificCompiler compiler, File outputDirectory) - throws IOException { + protected void setCompilerProperties(SpecificCompiler compiler) { compiler.setTemplateDir(templateDirectory); compiler.setStringType(GenericData.StringType.valueOf(stringType)); compiler.setFieldVisibility(getFieldVisibility()); @@ -406,6 +405,15 @@ private void doCompile(File sourceFileForModificationDetection, SpecificCompiler compiler.setCreateSetters(createSetters); compiler.setCreateNullSafeAnnotations(createNullSafeAnnotations); compiler.setEnableDecimalLogicalType(enableDecimalLogicalType); + compiler.setOutputCharacterEncoding(project.getProperties().getProperty("project.build.sourceEncoding")); + compiler.setAdditionalVelocityTools(instantiateAdditionalVelocityTools()); + compiler.setRecordSpecificClass(this.recordSpecificClass); + compiler.setErrorSpecificClass(this.errorSpecificClass); + } + + private void doCompile(File sourceFileForModificationDetection, SpecificCompiler compiler, File outputDirectory) + throws IOException { + setCompilerProperties(compiler); try { for (String customConversion : customConversions) { compiler.addCustomConversion(Thread.currentThread().getContextClassLoader().loadClass(customConversion)); @@ -413,10 +421,6 @@ private void doCompile(File sourceFileForModificationDetection, SpecificCompiler } catch (ClassNotFoundException e) { throw new IOException(e); } - compiler.setOutputCharacterEncoding(project.getProperties().getProperty("project.build.sourceEncoding")); - compiler.setAdditionalVelocityTools(instantiateAdditionalVelocityTools()); - compiler.setRecordSpecificClass(this.recordSpecificClass); - compiler.setErrorSpecificClass(this.errorSpecificClass); compiler.compileToDestination(sourceFileForModificationDetection, outputDirectory); } diff --git a/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/IDLMojo.java b/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/IDLMojo.java index 632ce7f955c..3b4ad5d02ee 100644 --- a/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/IDLMojo.java +++ b/lang/java/maven-plugin/src/main/java/org/apache/avro/mojo/IDLMojo.java @@ -97,19 +97,10 @@ protected void doCompile(String filename, File sourceDirectory, File outputDirec } else { compiler = new SpecificCompiler(idlFile.getNamedSchemas().values()); } - compiler.setStringType(GenericData.StringType.valueOf(stringType)); - compiler.setTemplateDir(templateDirectory); - compiler.setFieldVisibility(getFieldVisibility()); - compiler.setCreateOptionalGetters(createOptionalGetters); - compiler.setGettersReturnOptional(gettersReturnOptional); - compiler.setOptionalGettersForNullableFieldsOnly(optionalGettersForNullableFieldsOnly); - compiler.setCreateSetters(createSetters); - compiler.setAdditionalVelocityTools(instantiateAdditionalVelocityTools()); - compiler.setEnableDecimalLogicalType(enableDecimalLogicalType); + setCompilerProperties(compiler); for (String customConversion : customConversions) { compiler.addCustomConversion(projPathLoader.loadClass(customConversion)); } - compiler.setOutputCharacterEncoding(project.getProperties().getProperty("project.build.sourceEncoding")); compiler.compileToDestination(sourceFilePath.toFile(), outputDirectory); } finally { Thread.currentThread().setContextClassLoader(contextClassLoader);