Move out into own class

This commit is contained in:
Clément Fournier
2023-03-19 23:36:07 +01:00
parent a8c481e2ac
commit a09532e174
9 changed files with 73 additions and 49 deletions

View File

@ -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;

View File

@ -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<Language, LanguagePropertyBundle> 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<Path> 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}.
*
* <p>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<Path> 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<GlobalAnalysisListener> 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");

View File

@ -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.

View File

@ -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;

View File

@ -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<Path> 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}.
*
* <p>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();
}
}

View File

@ -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());
}

View File

@ -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;
/**

View File

@ -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

View File

@ -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));
}
}
}