diff --git a/pmd-ant/src/main/java/net/sourceforge/pmd/ant/Formatter.java b/pmd-ant/src/main/java/net/sourceforge/pmd/ant/Formatter.java index f90fcf7f6b..8201ef9878 100644 --- a/pmd-ant/src/main/java/net/sourceforge/pmd/ant/Formatter.java +++ b/pmd-ant/src/main/java/net/sourceforge/pmd/ant/Formatter.java @@ -29,7 +29,7 @@ import net.sourceforge.pmd.Report; import net.sourceforge.pmd.annotation.InternalApi; import net.sourceforge.pmd.internal.util.IOUtil; import net.sourceforge.pmd.lang.document.TextFile; -import net.sourceforge.pmd.renderers.FileNameRenderer; +import net.sourceforge.pmd.reporting.FileNameRenderer; import net.sourceforge.pmd.renderers.Renderer; import net.sourceforge.pmd.renderers.RendererFactory; import net.sourceforge.pmd.reporting.FileAnalysisListener; diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/PmdAnalysis.java b/pmd-core/src/main/java/net/sourceforge/pmd/PmdAnalysis.java index a5a06f4659..249c089df0 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/PmdAnalysis.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/PmdAnalysis.java @@ -12,7 +12,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -47,8 +46,8 @@ import net.sourceforge.pmd.lang.LanguageVersionDiscoverer; import net.sourceforge.pmd.lang.document.FileCollector; import net.sourceforge.pmd.lang.document.FileId; import net.sourceforge.pmd.lang.document.TextFile; -import net.sourceforge.pmd.renderers.FileNameRenderer; import net.sourceforge.pmd.renderers.Renderer; +import net.sourceforge.pmd.reporting.ConfigurableFileNameRenderer; import net.sourceforge.pmd.reporting.GlobalAnalysisListener; import net.sourceforge.pmd.reporting.ListenerInitializer; import net.sourceforge.pmd.reporting.ReportStats; @@ -100,6 +99,7 @@ public final class PmdAnalysis implements AutoCloseable { private final Map langProperties = new HashMap<>(); private boolean closed; + private final ConfigurableFileNameRenderer fileNameRenderer = new ConfigurableFileNameRenderer(); /** * Constructs a new instance. The files paths (input files, filelist, @@ -115,9 +115,6 @@ public final class PmdAnalysis implements AutoCloseable { reporter ); - for (Path path : config.getRelativizeRoots()) { - this.relativizeWith(path); - } } /** @@ -171,6 +168,10 @@ public final class PmdAnalysis implements AutoCloseable { } } + for (Path path : config.getRelativizeRoots()) { + pmd.fileNameRenderer.relativizeWith(path); + } + return pmd; } @@ -289,43 +290,9 @@ public final class PmdAnalysis implements AutoCloseable { return langProperties.computeIfAbsent(language, Language::newPropertyBundle); } - private final List relativizeRootPaths = new ArrayList<>(); - /** - * Add a prefix that is used to relativize file paths as their display name. - * For instance, when adding a file {@code /tmp/src/main/java/org/foo.java}, - * and relativizing with {@code /tmp/src/}, the registered {@link TextFile} - * will have a path id of {@code /tmp/src/main/java/org/foo.java}, and a - * display name of {@code main/java/org/foo.java}. - * - *

This only matters for files added from a {@link Path} object. - * - * @param path Path with which to relativize - */ - public void relativizeWith(Path path) { - this.relativizeRootPaths.add(Objects.requireNonNull(path)); - this.relativizeRootPaths.sort(Comparator.naturalOrder()); - } - public FileNameRenderer getFileNameRenderer() { - return new FileNameRenderer() { - private final List relativizeRootPaths = new ArrayList<>(PmdAnalysis.this.relativizeRootPaths); - - @Override - public String getDisplayName(FileId fileId) { - String localDisplayName = getLocalDisplayName(fileId); - if (fileId.getParentFsPath() != null) { - return getDisplayName(fileId.getParentFsPath()) + "!" + localDisplayName; - } - return localDisplayName; - } - - private String getLocalDisplayName(FileId file) { - if (!relativizeRootPaths.isEmpty()) { - return PmdAnalysis.getDisplayName(file, relativizeRootPaths); - } - return file.getOriginalPath(); - } - }; + public ConfigurableFileNameRenderer fileNameRenderer() { + return fileNameRenderer; } /** @@ -378,7 +345,7 @@ public final class PmdAnalysis implements AutoCloseable { // Initialize listeners try (ListenerInitializer initializer = listener.initializer()) { initializer.setNumberOfFilesToAnalyze(textFiles.size()); - initializer.setFileNameRenderer(getFileNameRenderer()); + initializer.setFileNameRenderer(fileNameRenderer()); } } catch (Exception e) { reporter.errorEx("Exception while initializing analysis listeners", e); @@ -449,7 +416,7 @@ public final class PmdAnalysis implements AutoCloseable { List rendererListeners = new ArrayList<>(renderers.size()); for (Renderer renderer : renderers) { try { - renderer.setFileNameRenderer(getFileNameRenderer()); + renderer.setFileNameRenderer(fileNameRenderer()); @SuppressWarnings("PMD.CloseResource") GlobalAnalysisListener listener = Objects.requireNonNull(renderer.newListener(), "Renderer should provide non-null listener"); diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/AbstractRenderer.java b/pmd-core/src/main/java/net/sourceforge/pmd/renderers/AbstractRenderer.java index 9f532e623b..57f4a69b45 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/AbstractRenderer.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/renderers/AbstractRenderer.java @@ -11,6 +11,7 @@ import net.sourceforge.pmd.annotation.Experimental; import net.sourceforge.pmd.internal.util.IOUtil; import net.sourceforge.pmd.lang.document.FileId; import net.sourceforge.pmd.properties.AbstractPropertySource; +import net.sourceforge.pmd.reporting.FileNameRenderer; /** * Abstract base class for {@link Renderer} implementations. diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/Renderer.java b/pmd-core/src/main/java/net/sourceforge/pmd/renderers/Renderer.java index 91ce73223f..ff84c4eff5 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/Renderer.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/renderers/Renderer.java @@ -22,6 +22,7 @@ import net.sourceforge.pmd.lang.document.TextFile; import net.sourceforge.pmd.properties.PropertyDescriptor; import net.sourceforge.pmd.properties.PropertySource; import net.sourceforge.pmd.reporting.FileAnalysisListener; +import net.sourceforge.pmd.reporting.FileNameRenderer; import net.sourceforge.pmd.reporting.GlobalAnalysisListener; import net.sourceforge.pmd.reporting.ListenerInitializer; diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/reporting/ConfigurableFileNameRenderer.java b/pmd-core/src/main/java/net/sourceforge/pmd/reporting/ConfigurableFileNameRenderer.java new file mode 100644 index 0000000000..87fb306440 --- /dev/null +++ b/pmd-core/src/main/java/net/sourceforge/pmd/reporting/ConfigurableFileNameRenderer.java @@ -0,0 +1,55 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.reporting; + +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Objects; + +import net.sourceforge.pmd.PmdAnalysis; +import net.sourceforge.pmd.lang.document.FileId; +import net.sourceforge.pmd.lang.document.TextFile; + +/** + * @author Clément Fournier + */ +public class ConfigurableFileNameRenderer implements FileNameRenderer { + + private final List relativizeRootPaths = new ArrayList<>(); + + /** + * Add a prefix that is used to relativize file paths as their display name. + * For instance, when adding a file {@code /tmp/src/main/java/org/foo.java}, + * and relativizing with {@code /tmp/src/}, the registered {@link TextFile} + * will have a path id of {@code /tmp/src/main/java/org/foo.java}, and a + * display name of {@code main/java/org/foo.java}. + * + *

This only matters for files added from a {@link Path} object. + * + * @param path Path with which to relativize + */ + public void relativizeWith(Path path) { + this.relativizeRootPaths.add(Objects.requireNonNull(path)); + this.relativizeRootPaths.sort(Comparator.naturalOrder()); + } + + @Override + public String getDisplayName(FileId fileId) { + String localDisplayName = getLocalDisplayName(fileId); + if (fileId.getParentFsPath() != null) { + return getDisplayName(fileId.getParentFsPath()) + "!" + localDisplayName; + } + return localDisplayName; + } + + private String getLocalDisplayName(FileId file) { + if (!relativizeRootPaths.isEmpty()) { + return PmdAnalysis.getDisplayName(file, relativizeRootPaths); + } + return file.getOriginalPath(); + } +} diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/FileNameRenderer.java b/pmd-core/src/main/java/net/sourceforge/pmd/reporting/FileNameRenderer.java similarity index 80% rename from pmd-core/src/main/java/net/sourceforge/pmd/renderers/FileNameRenderer.java rename to pmd-core/src/main/java/net/sourceforge/pmd/reporting/FileNameRenderer.java index 72a9c0634f..98b3c2c79a 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/FileNameRenderer.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/reporting/FileNameRenderer.java @@ -2,13 +2,13 @@ * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ -package net.sourceforge.pmd.renderers; +package net.sourceforge.pmd.reporting; import net.sourceforge.pmd.lang.document.FileId; import net.sourceforge.pmd.lang.document.TextFile; /** - * Renders a {@link FileId} into a display name. + * Renders a {@link FileId} into a display name for the file. * * @author Clément Fournier */ @@ -17,6 +17,7 @@ public interface FileNameRenderer { String getDisplayName(FileId fileId); + default String getDisplayName(TextFile textFile) { return getDisplayName(textFile.getPathId()); } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/reporting/ListenerInitializer.java b/pmd-core/src/main/java/net/sourceforge/pmd/reporting/ListenerInitializer.java index 397a7da08d..56a6d2e68f 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/reporting/ListenerInitializer.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/reporting/ListenerInitializer.java @@ -9,7 +9,6 @@ import java.util.Collection; import java.util.List; import net.sourceforge.pmd.internal.util.IOUtil; -import net.sourceforge.pmd.renderers.FileNameRenderer; import net.sourceforge.pmd.util.AssertionUtil; /** diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/cli/PMDFilelistTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/cli/PMDFilelistTest.java index d61b9d552c..cb30ddc4b8 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/cli/PMDFilelistTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/cli/PMDFilelistTest.java @@ -93,7 +93,7 @@ class PMDFilelistTest { } public static void assertHasName(TextFile textFile, String expected, PmdAnalysis pmd) { - assertThat(pmd.getFileNameRenderer().getDisplayName(textFile), equalTo(expected)); + assertThat(pmd.fileNameRenderer().getDisplayName(textFile), equalTo(expected)); } @Test diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/lang/document/NioTextFileTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/lang/document/NioTextFileTest.java index 7c73f69feb..f2b4c4264b 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/lang/document/NioTextFileTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/lang/document/NioTextFileTest.java @@ -42,7 +42,7 @@ class NioTextFileTest { assertEquals(1, collectedFiles.size()); TextFile textFile = collectedFiles.get(0); assertEquals(zipArchive.toAbsolutePath() + "!/path/inside/someSource.dummy", - pmd.getFileNameRenderer().getDisplayName(textFile)); + pmd.fileNameRenderer().getDisplayName(textFile)); } } }