From 8db4906052852247e9275c8e8b02abfff3388d19 Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Fri, 18 Dec 2020 18:56:31 +0100 Subject: [PATCH] issue #90 pass encoding parameter to java compiler --- .../AbstractAnnotationProcessorMojo.java | 51 +++++++++++-------- .../AnnotationProcessorCompiler.java | 3 ++ .../maven/plugin/processor/ProcessorTest.java | 29 ++++++++--- 3 files changed, 54 insertions(+), 29 deletions(-) diff --git a/processor/src/main/java/org/bsc/maven/plugin/processor/AbstractAnnotationProcessorMojo.java b/processor/src/main/java/org/bsc/maven/plugin/processor/AbstractAnnotationProcessorMojo.java index 0002bc7..b7b175c 100644 --- a/processor/src/main/java/org/bsc/maven/plugin/processor/AbstractAnnotationProcessorMojo.java +++ b/processor/src/main/java/org/bsc/maven/plugin/processor/AbstractAnnotationProcessorMojo.java @@ -341,6 +341,26 @@ public abstract class AbstractAnnotationProcessorMojo extends AbstractMojo */ public abstract File getDefaultOutputDirectory(); + /** + * + * @return + */ + private Charset getCharsetFromEncoding() { + + return ofNullable(encoding).map( enc -> { + try { + return Charset.forName(encoding); + } + catch( IllegalCharsetNameException ex1 ) { + getLog().warn( format("the given charset name [%s] is illegal!. default is used", encoding )); + } + catch( UnsupportedCharsetException ex2 ) { + getLog().warn( format("the given charset name [%s] is unsupported!. default is used", encoding )); + } + return Charset.defaultCharset(); + }).orElseGet( () -> Charset.defaultCharset() ); + + } private String buildProcessor() { @@ -565,6 +585,11 @@ private List prepareOptions( JavaCompiler compiler ) { }); }); + ofNullable(encoding).ifPresent( enc -> { + options.add("-encoding"); + options.add( getCharsetFromEncoding().name() ); + }); + if( getLog().isDebugEnabled() ) { for (String option : options) { getLog().debug(format("javac option: %s", option)); @@ -803,28 +828,10 @@ private void executeWithExceptionsHandled() throws Exception return; } - - Charset charset = null; ; - - if( encoding != null ) { - try { - charset = Charset.forName(encoding); - } - catch( IllegalCharsetNameException ex1 ) { - getLog().warn( format("the given charset name [%s] is illegal!. default is used", encoding )); - charset = null; - } - catch( UnsupportedCharsetException ex2 ) { - getLog().warn( format("the given charset name [%s] is unsupported!. default is used", encoding )); - charset = null; - } - } - - final StandardJavaFileManager fileManager = - compiler.getStandardFileManager(null, null, - (charset==null) ? - Charset.defaultCharset() : - charset); + final StandardJavaFileManager fileManager = + compiler.getStandardFileManager(null, + null, + getCharsetFromEncoding()); if( files!=null && !files.isEmpty() ) { diff --git a/processor/src/main/java/org/bsc/maven/plugin/processor/AnnotationProcessorCompiler.java b/processor/src/main/java/org/bsc/maven/plugin/processor/AnnotationProcessorCompiler.java index 55ef8f1..17dbac0 100644 --- a/processor/src/main/java/org/bsc/maven/plugin/processor/AnnotationProcessorCompiler.java +++ b/processor/src/main/java/org/bsc/maven/plugin/processor/AnnotationProcessorCompiler.java @@ -342,6 +342,9 @@ else if( "-s".equals(option) ) { else if( "--release".equals(option) ) { javacConf.setReleaseVersion(ii.next()); } + else if("-encoding".equals(option)) { + javacConf.setSourceEncoding(ii.next()); + } else /*if( option.startsWith("-A") ) */ { // view pull #70 // Just pass through any other arguments javacConf.addCompilerCustomArgument(option, ""); diff --git a/processor/src/test/java/org/bsc/maven/plugin/processor/ProcessorTest.java b/processor/src/test/java/org/bsc/maven/plugin/processor/ProcessorTest.java index 1191e0c..548272b 100644 --- a/processor/src/test/java/org/bsc/maven/plugin/processor/ProcessorTest.java +++ b/processor/src/test/java/org/bsc/maven/plugin/processor/ProcessorTest.java @@ -4,10 +4,12 @@ */ package org.bsc.maven.plugin.processor; -import java.io.File; -import org.junit.Assert; import org.junit.Test; -import org.hamcrest.core.*; + +import java.nio.charset.Charset; + +import static org.junit.Assert.*; + /** * * @author softphone @@ -20,11 +22,11 @@ public void compareFile() { final java.io.File f = new java.io.File( "target/test-classes"); final java.io.File f2 = new java.io.File( "target/classes"); - Assert.assertThat( f.equals(f2), Is.is(false)); + assertFalse( f.equals(f2)); final java.io.File f3 = new java.io.File( "target/classes"); - Assert.assertThat( f3.equals(f2), Is.is(true)); + assertTrue( f3.equals(f2)); } @@ -46,12 +48,25 @@ public void testDuplicatePath() { fileSet.add( f2 ); - Assert.assertThat( fileSet.size(), IsEqual.equalTo(1) ); + assertEquals( 1, fileSet.size() ); fileSet.add( f2 ); fileSet.add(f3); - Assert.assertThat( fileSet.size(), IsEqual.equalTo(2) ); + assertEquals( 2, fileSet.size() ); } + + @Test + public void testEncoding() { + + Charset.availableCharsets().entrySet().forEach( e -> { + System.out.printf( "encoding { key:%s, name:%s, aliases:%s }\n", e.getKey(), e.getValue().name(), e.getValue().aliases()); + }); + + final Charset utf8 = Charset.forName("utf8"); + assertEquals( "UTF-8", utf8.name() ); + } + + }