Fix build
This commit is contained in:
parent
cd2e95ff7b
commit
4cd1fa0a1d
@ -11,7 +11,6 @@ import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import net.sourceforge.pmd.lang.ast.AstInfo;
|
||||
import net.sourceforge.pmd.lang.ast.Parser.ParserTask;
|
||||
import net.sourceforge.pmd.lang.ast.RootNode;
|
||||
import net.sourceforge.pmd.util.document.TextDocument;
|
||||
import net.sourceforge.pmd.util.document.TextRegion;
|
||||
|
||||
import apex.jorje.semantic.ast.AstNode;
|
||||
|
@ -35,8 +35,7 @@ public final class ASTBlockStatement extends AbstractApexNode<BlockStatement> {
|
||||
// check, whether the this block statement really begins with a curly brace
|
||||
// unfortunately, for-loop and if-statements always contain a block statement,
|
||||
// regardless whether curly braces where present or not.
|
||||
char firstChar = positioner.getText().charAt(node.getLoc().getStartIndex());
|
||||
curlyBrace = firstChar == '{';
|
||||
curlyBrace = positioner.getText().startsWith('{', node.getLoc().getStartIndex());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -20,6 +20,7 @@ import java.util.logging.ConsoleHandler;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import net.sourceforge.pmd.annotation.DeprecatedUntil700;
|
||||
import net.sourceforge.pmd.benchmark.TextTimingReportRenderer;
|
||||
import net.sourceforge.pmd.benchmark.TimeTracker;
|
||||
import net.sourceforge.pmd.benchmark.TimedOperation;
|
||||
@ -78,8 +79,6 @@ public final class PMD {
|
||||
final RuleSetLoader ruleSetFactory = RuleSetLoader.fromPmdConfig(configuration);
|
||||
|
||||
final List<RuleSet> ruleSets;
|
||||
try (TimedOperation ignored = TimeTracker.startOperation(TimedOperationCategory.LOAD_RULES)) {
|
||||
ruleSets;
|
||||
try (TimedOperation ignored = TimeTracker.startOperation(TimedOperationCategory.LOAD_RULES)) {
|
||||
ruleSets = RulesetsFactoryUtils.getRuleSets(configuration.getRuleSets(), ruleSetFactory);
|
||||
}
|
||||
@ -142,8 +141,13 @@ public final class PMD {
|
||||
*
|
||||
* @throws Exception If an exception occurs while closing the data sources
|
||||
* Todo wrap that into a known exception type
|
||||
*
|
||||
* @deprecated Use {@link #processTextFiles(PMDConfiguration, List, List, GlobalAnalysisListener)},
|
||||
* which uses a list of {@link TextFile} and not the deprecated {@link DataSource}.
|
||||
*
|
||||
*/
|
||||
@Deprecated
|
||||
@DeprecatedUntil700
|
||||
public static void processFiles(PMDConfiguration configuration,
|
||||
List<RuleSet> ruleSets,
|
||||
List<DataSource> files,
|
||||
@ -153,6 +157,21 @@ public final class PMD {
|
||||
processTextFiles(configuration, ruleSets, inputFiles, listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run PMD on a list of files using the number of threads specified
|
||||
* by the configuration.
|
||||
*
|
||||
* TODO rulesets should be validated more strictly upon construction.
|
||||
* We shouldn't be removing rules after construction.
|
||||
*
|
||||
* @param configuration Configuration (the input files and rulesets are ignored)
|
||||
* @param ruleSets RuleSetFactory
|
||||
* @param inputFiles List of input files to process
|
||||
* @param listener RuleContext
|
||||
*
|
||||
* @throws Exception If an exception occurs while closing the data sources
|
||||
* Todo wrap that into a known exception type
|
||||
*/
|
||||
public static void processTextFiles(PMDConfiguration configuration,
|
||||
List<RuleSet> ruleSets,
|
||||
List<TextFile> inputFiles,
|
||||
|
@ -6,11 +6,18 @@ package net.sourceforge.pmd;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
import net.sourceforge.pmd.annotation.DeprecatedUntil700;
|
||||
import net.sourceforge.pmd.cache.AnalysisCache;
|
||||
import net.sourceforge.pmd.cache.FileAnalysisCache;
|
||||
import net.sourceforge.pmd.cache.NoopAnalysisCache;
|
||||
@ -89,7 +96,7 @@ public class PMDConfiguration extends AbstractConfiguration {
|
||||
// Rule and source file options
|
||||
private String ruleSets;
|
||||
private RulePriority minimumPriority = RulePriority.LOW;
|
||||
private String inputPaths;
|
||||
private @NonNull List<String> inputPaths = Collections.emptyList();
|
||||
private String inputUri;
|
||||
private String inputFilePath;
|
||||
private String ignoreFilePath;
|
||||
@ -298,9 +305,20 @@ public class PMDConfiguration extends AbstractConfiguration {
|
||||
* Get the comma separated list of input paths to process for source files.
|
||||
*
|
||||
* @return A comma separated list.
|
||||
*
|
||||
* @deprecated Use {@link #getAllInputPaths()}
|
||||
*/
|
||||
public String getInputPaths() {
|
||||
return inputPaths;
|
||||
@Deprecated
|
||||
@DeprecatedUntil700
|
||||
public @Nullable String getInputPaths() {
|
||||
return inputPaths.isEmpty() ? null : String.join(",", inputPaths);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an unmodifiable list.
|
||||
*/
|
||||
public @NonNull List<String> getAllInputPaths() {
|
||||
return Collections.unmodifiableList(inputPaths);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -309,8 +327,17 @@ public class PMDConfiguration extends AbstractConfiguration {
|
||||
* @param inputPaths
|
||||
* The comma separated list.
|
||||
*/
|
||||
public void setInputPaths(String inputPaths) {
|
||||
this.inputPaths = inputPaths;
|
||||
public void setInputPaths(@NonNull String inputPaths) {
|
||||
List<String> paths = new ArrayList<>();
|
||||
Collections.addAll(paths, inputPaths.split(","));
|
||||
paths.removeIf(StringUtils::isBlank);
|
||||
this.inputPaths = paths;
|
||||
}
|
||||
|
||||
public void setInputPaths(@NonNull List<String> inputPaths) {
|
||||
List<String> paths = new ArrayList<>(inputPaths);
|
||||
paths.removeIf(StringUtils::isBlank);
|
||||
this.inputPaths = paths;
|
||||
}
|
||||
|
||||
public String getInputFilePath() {
|
||||
|
@ -69,7 +69,7 @@ public final class RuleContext {
|
||||
Objects.requireNonNull(message, "Message was null");
|
||||
Objects.requireNonNull(formatArgs, "Format arguments were null, use an empty array");
|
||||
|
||||
RuleViolationFactory fact = node.getAstInfo().getLanguageVersion().getLanguageVersionHandler().getRuleViolationFactory();
|
||||
RuleViolationFactory fact = node.getTextDocument().getLanguageVersion().getLanguageVersionHandler().getRuleViolationFactory();
|
||||
|
||||
|
||||
FileLocation location = node.getReportLocation();
|
||||
|
@ -9,7 +9,6 @@ import static java.util.Arrays.asList;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.tools.ant.AntClassLoader;
|
||||
@ -40,6 +39,7 @@ import net.sourceforge.pmd.util.ClasspathClassLoader;
|
||||
import net.sourceforge.pmd.util.FileUtil;
|
||||
import net.sourceforge.pmd.util.IOUtil;
|
||||
import net.sourceforge.pmd.util.document.TextFile;
|
||||
import net.sourceforge.pmd.util.document.TextFileBuilder;
|
||||
import net.sourceforge.pmd.util.log.AntLogHandler;
|
||||
import net.sourceforge.pmd.util.log.ScopedLogHandlersManager;
|
||||
|
||||
@ -125,24 +125,7 @@ public class PMDTaskImpl {
|
||||
@SuppressWarnings("PMD.CloseResource")
|
||||
ViolationCounterListener reportSizeListener = new ViolationCounterListener();
|
||||
|
||||
final List<TextFile> files = new ArrayList<>();
|
||||
final List<String> reportShortNamesPaths = new ArrayList<>();
|
||||
StringJoiner fullInputPath = new StringJoiner(",");
|
||||
for (FileSet fs : filesets) {
|
||||
DirectoryScanner ds = fs.getDirectoryScanner(project);
|
||||
java.nio.file.Path baseDir = ds.getBasedir().toPath();
|
||||
for (String srcFile : ds.getIncludedFiles()) {
|
||||
java.nio.file.Path file = baseDir.resolve(srcFile);
|
||||
files.add(FileUtil.createNioTextFile(configuration, file, null));
|
||||
}
|
||||
|
||||
final String commonInputPath = ds.getBasedir().getPath();
|
||||
fullInputPath.add(commonInputPath);
|
||||
if (configuration.isReportShortNames()) {
|
||||
reportShortNamesPaths.add(commonInputPath);
|
||||
}
|
||||
}
|
||||
configuration.setInputPaths(fullInputPath.toString());
|
||||
final List<TextFile> files = collectFiles(filesets, project, configuration.isReportShortNames());
|
||||
|
||||
try (GlobalAnalysisListener listener = getListener(reportSizeListener)) {
|
||||
PMD.processTextFiles(configuration, rules, files, listener);
|
||||
@ -163,6 +146,24 @@ public class PMDTaskImpl {
|
||||
}
|
||||
}
|
||||
|
||||
private List<TextFile> collectFiles(List<FileSet> filesets, Project project, boolean reportShortNames) {
|
||||
final List<TextFile> files = new ArrayList<>();
|
||||
for (FileSet fs : filesets) {
|
||||
DirectoryScanner ds = fs.getDirectoryScanner(project);
|
||||
java.nio.file.Path baseDir = ds.getBasedir().toPath();
|
||||
|
||||
for (String srcFile : ds.getIncludedFiles()) {
|
||||
java.nio.file.Path filePath = baseDir.resolve(srcFile);
|
||||
TextFileBuilder builder = FileUtil.buildNioTextFile(configuration, filePath);
|
||||
if (reportShortNames) {
|
||||
builder = builder.withDisplayName(srcFile);
|
||||
}
|
||||
files.add(builder.build());
|
||||
}
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
private @NonNull GlobalAnalysisListener getListener(ViolationCounterListener reportSizeListener) {
|
||||
List<GlobalAnalysisListener> renderers = new ArrayList<>(formatters.size() + 1);
|
||||
try {
|
||||
|
@ -129,10 +129,11 @@ public final class CachedRuleViolation implements RuleViolation {
|
||||
stream.writeUTF(getValueOrEmpty(violation.getRule().getRuleClass()));
|
||||
stream.writeUTF(getValueOrEmpty(violation.getRule().getName()));
|
||||
stream.writeUTF(getValueOrEmpty(violation.getRule().getLanguage().getTerseName()));
|
||||
stream.writeInt(violation.getBeginLine());
|
||||
stream.writeInt(violation.getBeginColumn());
|
||||
stream.writeInt(violation.getEndLine());
|
||||
stream.writeInt(violation.getEndColumn());
|
||||
FileLocation location = violation.getLocation();
|
||||
stream.writeInt(location.getBeginLine());
|
||||
stream.writeInt(location.getBeginColumn());
|
||||
stream.writeInt(location.getEndLine());
|
||||
stream.writeInt(location.getEndColumn());
|
||||
stream.writeUTF(getValueOrEmpty(violation.getPackageName()));
|
||||
stream.writeUTF(getValueOrEmpty(violation.getClassName()));
|
||||
stream.writeUTF(getValueOrEmpty(violation.getMethodName()));
|
||||
|
@ -18,8 +18,8 @@ import net.sourceforge.pmd.cpd.Tokens;
|
||||
import net.sourceforge.pmd.cpd.token.AntlrTokenFilter;
|
||||
import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrToken;
|
||||
import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrTokenManager;
|
||||
import net.sourceforge.pmd.util.document.TextDocument;
|
||||
import net.sourceforge.pmd.util.document.CpdCompat;
|
||||
import net.sourceforge.pmd.util.document.TextDocument;
|
||||
|
||||
/**
|
||||
* Generic implementation of a {@link Tokenizer} useful to any Antlr grammar.
|
||||
|
@ -6,8 +6,8 @@ package net.sourceforge.pmd.internal.util;
|
||||
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
@ -11,8 +11,8 @@ import java.util.function.Function;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import net.sourceforge.pmd.lang.ast.CharStream;
|
||||
import net.sourceforge.pmd.util.document.TextDocument;
|
||||
import net.sourceforge.pmd.util.document.CpdCompat;
|
||||
import net.sourceforge.pmd.util.document.TextDocument;
|
||||
|
||||
public final class CharStreamFactory {
|
||||
|
||||
|
@ -38,7 +38,7 @@ final class MonoThreadProcessor extends AbstractPMDProcessor {
|
||||
|
||||
private final RuleSets ruleSets;
|
||||
|
||||
MonothreadRunnable(RuleSets ruleSets, DataSource dataSource, GlobalAnalysisListener ruleContext, PMDConfiguration configuration) {
|
||||
MonothreadRunnable(RuleSets ruleSets, TextFile dataSource, GlobalAnalysisListener ruleContext, PMDConfiguration configuration) {
|
||||
super(dataSource, ruleContext, configuration);
|
||||
this.ruleSets = ruleSets;
|
||||
}
|
||||
|
@ -51,6 +51,7 @@ import net.sourceforge.pmd.util.database.SourceObject;
|
||||
import net.sourceforge.pmd.util.datasource.DataSource;
|
||||
import net.sourceforge.pmd.util.document.ReferenceCountedCloseable;
|
||||
import net.sourceforge.pmd.util.document.TextFile;
|
||||
import net.sourceforge.pmd.util.document.TextFileBuilder;
|
||||
|
||||
/**
|
||||
* This is a utility class for working with Files.
|
||||
@ -225,10 +226,8 @@ public final class FileUtil {
|
||||
Predicate<Path> fileFilter = PredicateUtil.toFileFilter(new LanguageFilenameFilter(languages));
|
||||
fileFilter = fileFilter.and(path -> !ignoredFiles.contains(path.toString()));
|
||||
|
||||
if (null != configuration.getInputPaths()) {
|
||||
for (String root : configuration.getInputPaths().split(",")) {
|
||||
collect(files, root, configuration, fileFilter);
|
||||
}
|
||||
for (String root : configuration.getAllInputPaths()) {
|
||||
collect(files, root, configuration, fileFilter);
|
||||
}
|
||||
|
||||
if (null != configuration.getInputUri()) {
|
||||
@ -300,19 +299,25 @@ public final class FileUtil {
|
||||
}
|
||||
|
||||
private static @Nullable String displayName(PMDConfiguration config, Path file) {
|
||||
if (config.isReportShortNames() && config.getInputPaths() != null) {
|
||||
return ShortFilenameUtil.determineFileName(Arrays.asList(config.getInputPaths().split(",")), file.toString());
|
||||
if (config.isReportShortNames()) {
|
||||
return ShortFilenameUtil.determineFileName(config.getAllInputPaths(), file.toString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static TextFile createNioTextFile(PMDConfiguration config, Path file, @Nullable ReferenceCountedCloseable fsCloseable) {
|
||||
return buildNioTextFile(config, file).belongingTo(fsCloseable).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a builder that uses the configuration's encoding, and pre-fills the display name
|
||||
* using the input paths ({@link PMDConfiguration#getAllInputPaths()}) if {@link PMDConfiguration#isReportShortNames()}).
|
||||
*/
|
||||
public static TextFileBuilder buildNioTextFile(PMDConfiguration config, Path file) {
|
||||
LanguageVersion langVersion = config.getLanguageVersionOfFile(file.toString());
|
||||
|
||||
return TextFile.forPath(file, config.getSourceEncoding(), langVersion)
|
||||
.withDisplayName(displayName(config, file))
|
||||
.belongingTo(fsCloseable)
|
||||
.build();
|
||||
.withDisplayName(displayName(config, file));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -172,7 +172,7 @@ public final class Chars implements CharSequence {
|
||||
* See {@link String#startsWith(String, int)}.
|
||||
*/
|
||||
public boolean startsWith(String prefix, int fromIndex) {
|
||||
if (fromIndex < 0 || fromIndex >= len || prefix.length() > len) {
|
||||
if (fromIndex < 0 || fromIndex + prefix.length() > len) {
|
||||
return false;
|
||||
}
|
||||
return str.startsWith(prefix, idx(fromIndex));
|
||||
@ -185,6 +185,14 @@ public final class Chars implements CharSequence {
|
||||
return startsWith(prefix, 0);
|
||||
}
|
||||
|
||||
|
||||
public boolean startsWith(char prefix, int fromIndex) {
|
||||
if (fromIndex < 0 || fromIndex + 1 > len) {
|
||||
return false;
|
||||
}
|
||||
return str.charAt(start + fromIndex) == prefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a subsequence which does not start with control characters ({@code <= 32}).
|
||||
* This is consistent with {@link String#trim()}.
|
||||
|
@ -115,4 +115,9 @@ public final class FileLocation {
|
||||
return new FileLocation(fileName, beginLine, beginColumn, endLine, endColumn);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "!debug only! " + startPosToStringWithFile();
|
||||
}
|
||||
}
|
||||
|
@ -216,7 +216,7 @@ final class SourceCodePositioner {
|
||||
buf = new int[Math.max(1, bufSize)];
|
||||
}
|
||||
|
||||
public Builder() {
|
||||
Builder() {
|
||||
this(400);
|
||||
}
|
||||
|
||||
|
@ -109,6 +109,12 @@ public interface TextDocument extends Closeable {
|
||||
*/
|
||||
FileLocation toLocation(TextRegion region);
|
||||
|
||||
|
||||
// todo doc
|
||||
default FileLocation createLocation(int bline, int bcol, int eline, int ecol) {
|
||||
return FileLocation.location(getDisplayName(), bline, bcol, eline, ecol);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines the line number at the given offset (inclusive).
|
||||
*
|
||||
@ -157,6 +163,7 @@ public interface TextDocument extends Closeable {
|
||||
*
|
||||
* @see TextFile#forCharSeq(CharSequence, String, LanguageVersion)
|
||||
*/
|
||||
@SuppressWarnings("PMD.CloseResource")
|
||||
static TextDocument readOnlyString(@NonNull CharSequence source, @NonNull String filename, @NonNull LanguageVersion lv) {
|
||||
TextFile textFile = TextFile.forCharSeq(source, filename, lv);
|
||||
try {
|
||||
|
@ -21,7 +21,7 @@ public abstract class TextFileBuilder {
|
||||
protected final LanguageVersion languageVersion;
|
||||
protected @Nullable String displayName;
|
||||
|
||||
private TextFileBuilder(LanguageVersion languageVersion) {
|
||||
TextFileBuilder(LanguageVersion languageVersion) {
|
||||
this.languageVersion = AssertionUtil.requireParamNotNull("language version", languageVersion);
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,8 @@
|
||||
|
||||
package net.sourceforge.pmd;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@ -34,6 +36,8 @@ public class ReportTest {
|
||||
r.addRuleViolation(new ParametricRuleViolation(rule2, s1, rule2.getMessage()));
|
||||
Renderer rend = new XMLRenderer();
|
||||
String result = render(rend, r);
|
||||
assertThat(result, containsString("bar"));
|
||||
assertThat(result, containsString("foo"));
|
||||
assertTrue("sort order wrong", result.indexOf("bar") < result.indexOf("foo"));
|
||||
}
|
||||
|
||||
|
@ -7,7 +7,6 @@ package net.sourceforge.pmd.ant;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@ -71,7 +70,7 @@ public class PMDTaskTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithShortFilenames() throws FileNotFoundException, IOException {
|
||||
public void testWithShortFilenames() throws IOException {
|
||||
buildRule.executeTarget("testWithShortFilenames");
|
||||
|
||||
try (InputStream in = new FileInputStream("target/pmd-ant-test.txt")) {
|
||||
|
@ -36,6 +36,7 @@ import net.sourceforge.pmd.RuleViolation;
|
||||
import net.sourceforge.pmd.lang.Language;
|
||||
import net.sourceforge.pmd.lang.LanguageRegistry;
|
||||
import net.sourceforge.pmd.lang.LanguageVersion;
|
||||
import net.sourceforge.pmd.util.document.FileLocation;
|
||||
import net.sourceforge.pmd.util.document.TextDocument;
|
||||
import net.sourceforge.pmd.util.document.TextFile;
|
||||
import net.sourceforge.pmd.util.document.TextFileContent;
|
||||
@ -113,6 +114,7 @@ public class FileAnalysisCacheTest {
|
||||
|
||||
final RuleViolation rv = mock(RuleViolation.class);
|
||||
when(rv.getFilename()).thenReturn(sourceFile.getDisplayName());
|
||||
when(rv.getLocation()).thenReturn(FileLocation.location(sourceFile.getDisplayName(), 1, 2, 3, 4));
|
||||
final net.sourceforge.pmd.Rule rule = mock(net.sourceforge.pmd.Rule.class, Mockito.RETURNS_SMART_NULLS);
|
||||
when(rule.getLanguage()).thenReturn(mock(Language.class));
|
||||
when(rv.getRule()).thenReturn(rule);
|
||||
|
@ -50,7 +50,7 @@ public class PMDFilelistTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetApplicatbleFilesWithIgnores() throws IOException {
|
||||
public void testGetApplicableFilesWithIgnores() throws IOException {
|
||||
Set<Language> languages = new HashSet<>();
|
||||
languages.add(new DummyLanguageModule());
|
||||
|
||||
@ -65,7 +65,7 @@ public class PMDFilelistTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetApplicatbleFilesWithDirAndIgnores() throws IOException {
|
||||
public void testGetApplicableFilesWithDirAndIgnores() throws IOException {
|
||||
Set<Language> languages = new HashSet<>();
|
||||
languages.add(new DummyLanguageModule());
|
||||
|
||||
|
@ -4,10 +4,17 @@
|
||||
|
||||
package net.sourceforge.pmd.lang;
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import net.sourceforge.pmd.Rule;
|
||||
import net.sourceforge.pmd.RuleViolation;
|
||||
import net.sourceforge.pmd.lang.ast.DummyAstStages;
|
||||
import net.sourceforge.pmd.lang.ast.DummyRoot;
|
||||
import net.sourceforge.pmd.lang.ast.Node;
|
||||
import net.sourceforge.pmd.lang.ast.Parser;
|
||||
import net.sourceforge.pmd.lang.rule.ParametricRuleViolation;
|
||||
import net.sourceforge.pmd.lang.rule.impl.DefaultRuleViolationFactory;
|
||||
import net.sourceforge.pmd.util.document.FileLocation;
|
||||
|
||||
/**
|
||||
* Dummy language used for testing PMD.
|
||||
@ -67,5 +74,14 @@ public class DummyLanguageModule extends BaseLanguageModule {
|
||||
|
||||
public static class RuleViolationFactory extends DefaultRuleViolationFactory {
|
||||
|
||||
@Override
|
||||
public RuleViolation createViolation(Rule rule, @NonNull Node node, FileLocation location, @NonNull String formattedMessage) {
|
||||
return new ParametricRuleViolation(rule, location, formattedMessage) {
|
||||
{
|
||||
this.packageName = "foo"; // just for testing variable expansion
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,10 @@ public class DummyNode extends AbstractNode<DummyNode, DummyNode> implements Gen
|
||||
private final Map<String, String> userData = new HashMap<>();
|
||||
private String image;
|
||||
|
||||
private FileLocation location;
|
||||
private int bline = 1;
|
||||
private int bcol = 1;
|
||||
private int eline = 1;
|
||||
private int ecol = 1;
|
||||
|
||||
public DummyNode(String xpathName) {
|
||||
super();
|
||||
@ -45,14 +48,17 @@ public class DummyNode extends AbstractNode<DummyNode, DummyNode> implements Gen
|
||||
}
|
||||
}
|
||||
|
||||
public void setCoords(int bline, int bcol, int eline, int ecol) {
|
||||
this.location = FileLocation.location(":dummyFile:", bline, bcol, eline, ecol);
|
||||
public DummyNode setCoords(int bline, int bcol, int eline, int ecol) {
|
||||
this.bline = bline;
|
||||
this.bcol = bcol;
|
||||
this.eline = eline;
|
||||
this.ecol = ecol;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileLocation getReportLocation() {
|
||||
assert location != null : "Should have called setCoords";
|
||||
return location;
|
||||
return getTextDocument().createLocation(bline, bcol, eline, ecol);
|
||||
}
|
||||
|
||||
public void setImage(String image) {
|
||||
|
@ -11,6 +11,7 @@ import net.sourceforge.pmd.lang.DummyLanguageModule;
|
||||
import net.sourceforge.pmd.lang.LanguageRegistry;
|
||||
import net.sourceforge.pmd.lang.LanguageVersion;
|
||||
import net.sourceforge.pmd.lang.ast.impl.GenericNode;
|
||||
import net.sourceforge.pmd.util.document.TextDocument;
|
||||
|
||||
public class DummyRoot extends DummyNode implements GenericNode<DummyNode>, RootNode {
|
||||
|
||||
@ -45,9 +46,7 @@ public class DummyRoot extends DummyNode implements GenericNode<DummyNode>, Root
|
||||
@Override
|
||||
public AstInfo<DummyRoot> getAstInfo() {
|
||||
return new AstInfo<>(
|
||||
filename,
|
||||
languageVersion,
|
||||
sourceText,
|
||||
TextDocument.readOnlyString(sourceText, filename, languageVersion),
|
||||
this,
|
||||
suppressMap
|
||||
);
|
||||
|
@ -95,8 +95,8 @@ public class XPathRuleTest {
|
||||
public DummyNode newNode() {
|
||||
DummyRoot root = new DummyRoot();
|
||||
DummyNode dummy = new DummyNodeWithDeprecatedAttribute();
|
||||
dummy.setCoords(1, 1, 1, 2);
|
||||
root.addChild(dummy, 0);
|
||||
dummy.setCoords(1, 1, 1, 2);
|
||||
return root;
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,6 @@
|
||||
package net.sourceforge.pmd.processor;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.contrib.java.lang.system.RestoreSystemProperties;
|
||||
import org.junit.rules.TestRule;
|
||||
@ -25,54 +24,55 @@ import net.sourceforge.pmd.lang.LanguageVersion;
|
||||
import net.sourceforge.pmd.lang.ast.Node;
|
||||
import net.sourceforge.pmd.lang.rule.AbstractRule;
|
||||
import net.sourceforge.pmd.processor.MonoThreadProcessor.MonothreadRunnable;
|
||||
import net.sourceforge.pmd.util.datasource.DataSource;
|
||||
import net.sourceforge.pmd.util.document.TextFile;
|
||||
|
||||
public class PmdRunnableTest {
|
||||
|
||||
@org.junit.Rule
|
||||
public TestRule restoreSystemProperties = new RestoreSystemProperties();
|
||||
|
||||
private LanguageVersion dummyThrows;
|
||||
private LanguageVersion dummyDefault;
|
||||
private PMDConfiguration configuration;
|
||||
private PmdRunnable pmdRunnable;
|
||||
private GlobalReportBuilderListener reportBuilder;
|
||||
private static final LanguageVersion DUMMY_THROWS;
|
||||
private static final LanguageVersion DUMMY_DEFAULT;
|
||||
|
||||
@Before
|
||||
public void prepare() {
|
||||
|
||||
static {
|
||||
Language dummyLanguage = LanguageRegistry.findLanguageByTerseName(DummyLanguageModule.TERSE_NAME);
|
||||
dummyDefault = dummyLanguage.getDefaultVersion();
|
||||
dummyThrows = dummyLanguage.getVersion("1.9-throws");
|
||||
DataSource dataSource = DataSource.forString("test", "test.dummy");
|
||||
DUMMY_DEFAULT = dummyLanguage.getDefaultVersion();
|
||||
DUMMY_THROWS = dummyLanguage.getVersion("1.9-throws");
|
||||
}
|
||||
|
||||
|
||||
private Report process(LanguageVersion lv) {
|
||||
TextFile dataSource = TextFile.forCharSeq("test", "test.dummy", lv);
|
||||
|
||||
Rule rule = new RuleThatThrows();
|
||||
configuration = new PMDConfiguration();
|
||||
reportBuilder = new GlobalReportBuilderListener();
|
||||
pmdRunnable = new MonothreadRunnable(new RuleSets(RuleSet.forSingleRule(rule)),
|
||||
dataSource,
|
||||
reportBuilder,
|
||||
configuration);
|
||||
PMDConfiguration configuration = new PMDConfiguration();
|
||||
GlobalReportBuilderListener reportBuilder = new GlobalReportBuilderListener();
|
||||
PmdRunnable pmdRunnable = new MonothreadRunnable(new RuleSets(RuleSet.forSingleRule(rule)),
|
||||
dataSource,
|
||||
reportBuilder,
|
||||
configuration);
|
||||
|
||||
pmdRunnable.run();
|
||||
reportBuilder.close();
|
||||
return reportBuilder.getResult();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void inErrorRecoveryModeErrorsShouldBeLoggedByParser() {
|
||||
System.setProperty(SystemProps.PMD_ERROR_RECOVERY, "");
|
||||
configuration.setDefaultLanguageVersion(dummyThrows);
|
||||
|
||||
pmdRunnable.run();
|
||||
reportBuilder.close();
|
||||
Assert.assertEquals(1, reportBuilder.getResult().getProcessingErrors().size());
|
||||
Report report = process(DUMMY_THROWS);
|
||||
|
||||
Assert.assertEquals(1, report.getProcessingErrors().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void inErrorRecoveryModeErrorsShouldBeLoggedByRule() {
|
||||
System.setProperty(SystemProps.PMD_ERROR_RECOVERY, "");
|
||||
configuration.setDefaultLanguageVersion(dummyDefault);
|
||||
|
||||
pmdRunnable.run();
|
||||
reportBuilder.close();
|
||||
Report report = reportBuilder.getResult();
|
||||
Report report = process(DUMMY_DEFAULT);
|
||||
|
||||
Assert.assertEquals(1, report.getProcessingErrors().size());
|
||||
Assert.assertSame(AssertionError.class, report.getProcessingErrors().get(0).getError().getClass());
|
||||
}
|
||||
@ -80,17 +80,16 @@ public class PmdRunnableTest {
|
||||
@Test
|
||||
public void withoutErrorRecoveryModeProcessingShouldBeAbortedByParser() {
|
||||
Assert.assertNull(System.getProperty(SystemProps.PMD_ERROR_RECOVERY));
|
||||
configuration.setDefaultLanguageVersion(dummyThrows);
|
||||
|
||||
Assert.assertThrows(AssertionError.class, pmdRunnable::run);
|
||||
Assert.assertThrows(AssertionError.class, () -> process(DUMMY_THROWS));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withoutErrorRecoveryModeProcessingShouldBeAbortedByRule() {
|
||||
Assert.assertNull(System.getProperty(SystemProps.PMD_ERROR_RECOVERY));
|
||||
configuration.setDefaultLanguageVersion(dummyDefault);
|
||||
|
||||
Assert.assertThrows(AssertionError.class, pmdRunnable::run);
|
||||
|
||||
Assert.assertThrows(AssertionError.class, () -> process(DUMMY_DEFAULT));
|
||||
}
|
||||
|
||||
private static class RuleThatThrows extends AbstractRule {
|
||||
|
@ -13,8 +13,8 @@ import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import net.sourceforge.pmd.lang.ast.impl.javacc.CharStreamFactory;
|
||||
import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccTokenDocument;
|
||||
import net.sourceforge.pmd.lang.ast.impl.javacc.SimpleCharStream;
|
||||
import net.sourceforge.pmd.util.document.TextDocument;
|
||||
import net.sourceforge.pmd.util.document.CpdCompat;
|
||||
import net.sourceforge.pmd.util.document.TextDocument;
|
||||
|
||||
/**
|
||||
* A SimpleCharStream, that supports the continuation of lines via backslash+newline,
|
||||
|
@ -17,7 +17,6 @@ import net.sourceforge.pmd.lang.ast.impl.GenericNode;
|
||||
import net.sourceforge.pmd.lang.java.symbols.table.JSymbolTable;
|
||||
import net.sourceforge.pmd.lang.java.typeresolution.ClassTypeResolver;
|
||||
import net.sourceforge.pmd.lang.java.types.TypeSystem;
|
||||
import net.sourceforge.pmd.util.document.TextDocument;
|
||||
|
||||
// FUTURE Change this class to extend from SimpleJavaNode, as TypeNode is not appropriate (unless I'm wrong)
|
||||
public final class ASTCompilationUnit extends AbstractJavaTypeNode implements JavaNode, GenericNode<JavaNode>, RootNode {
|
||||
|
@ -36,7 +36,7 @@ public final class ASTLocalVariableDeclaration extends AbstractJavaNode
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable FileLocation getReportLocation() {
|
||||
public @Nullable FileLocation getReportLocation() {
|
||||
return getVarIds().firstOrThrow().getFirstToken().getReportLocation();
|
||||
}
|
||||
|
||||
|
@ -7,7 +7,6 @@ package net.sourceforge.pmd.lang.jsp.ast;
|
||||
import net.sourceforge.pmd.lang.ast.AstInfo;
|
||||
import net.sourceforge.pmd.lang.ast.Parser.ParserTask;
|
||||
import net.sourceforge.pmd.lang.ast.RootNode;
|
||||
import net.sourceforge.pmd.util.document.TextDocument;
|
||||
|
||||
public final class ASTCompilationUnit extends AbstractJspNode implements RootNode {
|
||||
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user