From 90138b00c648c8d90dea234631a6e292cb0b1afc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Mon, 22 Aug 2022 10:12:20 -0300 Subject: [PATCH] Extract encoding to a mixin --- .../AbstractAnalysisPmdSubcommand.java | 9 ++++---- .../pmd/cli/commands/internal/CpdCommand.java | 4 ++-- .../pmd/cli/commands/internal/PmdCommand.java | 2 +- .../mixins/internal/EncodingMixin.java | 23 +++++++++++++++++++ 4 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/mixins/internal/EncodingMixin.java diff --git a/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/AbstractAnalysisPmdSubcommand.java b/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/AbstractAnalysisPmdSubcommand.java index c42dc45a01..de9ccd8eac 100644 --- a/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/AbstractAnalysisPmdSubcommand.java +++ b/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/AbstractAnalysisPmdSubcommand.java @@ -5,18 +5,19 @@ package net.sourceforge.pmd.cli.commands.internal; import java.net.URI; -import java.nio.charset.Charset; import java.nio.file.Path; import java.util.List; +import net.sourceforge.pmd.cli.commands.mixins.internal.EncodingMixin; + +import picocli.CommandLine.Mixin; import picocli.CommandLine.Option; import picocli.CommandLine.ParameterException; public abstract class AbstractAnalysisPmdSubcommand extends AbstractPmdSubcommand { - @Option(names = { "--encoding", "-e" }, description = "Specifies the character set encoding of the source code files", - defaultValue = "UTF-8") - protected Charset encoding; + @Mixin + protected EncodingMixin encoding; @Option(names = { "--dir", "-d" }, description = "Path to a source file, or directory containing source files to analyze. " diff --git a/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/CpdCommand.java b/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/CpdCommand.java index 011407c62c..54fcc04b3a 100644 --- a/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/CpdCommand.java +++ b/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/CpdCommand.java @@ -112,10 +112,10 @@ public class CpdCommand extends AbstractAnalysisPmdSubcommand { configuration.setSkipBlocksPattern(skipBlocksPattern); configuration.setSkipDuplicates(skipDuplicates); configuration.setSkipLexicalErrors(skipLexicalErrors); - configuration.setSourceEncoding(encoding.name()); + configuration.setSourceEncoding(encoding.getEncoding().name()); configuration.setURI(uri == null ? null : uri.toString()); - configuration.setCPDRenderer(CPDConfiguration.getCPDRendererFromString(rendererName, encoding.name())); + configuration.setCPDRenderer(CPDConfiguration.getCPDRendererFromString(rendererName, encoding.getEncoding().name())); // TODO // Setup CLI message reporter diff --git a/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/PmdCommand.java b/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/PmdCommand.java index d0c70bbcf8..1ce72fe46c 100644 --- a/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/PmdCommand.java +++ b/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/internal/PmdCommand.java @@ -268,7 +268,7 @@ public class PmdCommand extends AbstractAnalysisPmdSubcommand { configuration.setInputUri(uri != null ? uri.toString() : null); configuration.setReportFormat(format); configuration.setDebug(debug); - configuration.setSourceEncoding(encoding.name()); + configuration.setSourceEncoding(encoding.getEncoding().name()); configuration.setMinimumPriority(minimumPriority); configuration.setReportFile(reportFile != null ? reportFile.toString() : null); configuration.setReportProperties(properties); diff --git a/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/mixins/internal/EncodingMixin.java b/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/mixins/internal/EncodingMixin.java new file mode 100644 index 0000000000..e6086bb46f --- /dev/null +++ b/pmd-cli/src/main/java/net/sourceforge/pmd/cli/commands/mixins/internal/EncodingMixin.java @@ -0,0 +1,23 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.cli.commands.mixins.internal; + +import java.nio.charset.Charset; + +import picocli.CommandLine.Option; + +/** + * A mixin for source code encoding. Used to ensure consistency among commands. + */ +public class EncodingMixin { + + @Option(names = { "--encoding", "-e" }, description = "Specifies the character set encoding of the source code files", + defaultValue = "UTF-8") + private Charset encoding; + + public Charset getEncoding() { + return encoding; + } +}