WIP needs xml messages PR

This commit is contained in:
Clément Fournier
2022-04-09 21:50:12 +02:00
parent 7e8b5e37fc
commit 9f163ed9a1
14 changed files with 52 additions and 50 deletions

View File

@ -339,7 +339,7 @@ public class PMDConfiguration extends AbstractConfiguration {
// FUTURE Delete this? I can't think of a good reason to keep it around.
// Failure to determine the LanguageVersion for a file should be a hard
// error, or simply cause the file to be skipped?
public LanguageVersion getLanguageVersionOfFile(String fileName) {
public @Nullable LanguageVersion getLanguageVersionOfFile(String fileName) {
LanguageVersion forcedVersion = getForceLanguageVersion();
if (forcedVersion != null) {
// use force language if given
@ -347,13 +347,11 @@ public class PMDConfiguration extends AbstractConfiguration {
}
// otherwise determine by file extension
LanguageVersion languageVersion = languageVersionDiscoverer.getDefaultLanguageVersionForFile(fileName);
if (languageVersion == null) {
// For compatibility with older code that does not always pass in
// a correct filename.
languageVersion = languageVersionDiscoverer.getDefaultLanguageVersion(LanguageRegistry.getLanguage("Java"));
}
return languageVersion;
return languageVersionDiscoverer.getDefaultLanguageVersionForFile(fileName);
}
public LanguageRegistry languages() {
return langRegistry;
}
/**

View File

@ -26,6 +26,7 @@ import net.sourceforge.pmd.cache.AnalysisCacheListener;
import net.sourceforge.pmd.internal.util.AssertionUtil;
import net.sourceforge.pmd.internal.util.FileCollectionUtil;
import net.sourceforge.pmd.lang.Language;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersion;
import net.sourceforge.pmd.lang.LanguageVersionDiscoverer;
import net.sourceforge.pmd.lang.document.FileCollector;
@ -157,6 +158,13 @@ public final class PmdAnalysis implements AutoCloseable {
return collector; // todo user can close collector programmatically
}
/**
* Returns the language registry for this analysis.
*/
public LanguageRegistry languages() {
return configuration.languages();
}
/**
* Returns a new ruleset loader, which can be used to create new
* rulesets (add them then with {@link #addRuleSet(RuleSet)}).

View File

@ -79,7 +79,7 @@ public class PMDTaskImpl {
SourceLanguage version = task.getSourceLanguage();
if (version != null) {
Language lang = LanguageRegistry.findLanguageByTerseName(version.getName());
Language lang = LanguageRegistry.PMD.getLanguageById(version.getName());
LanguageVersion languageVersion = lang == null ? null : lang.getVersion(version.getVersion());
if (languageVersion == null) {
throw new BuildException("The following language is not supported:" + version + '.');

View File

@ -114,6 +114,24 @@ public final class LanguageRegistry implements Iterable<Language> {
return languagesById.get(langId);
}
/**
* Returns a language version from its {@linkplain Language#getId() language ID}
* (eg {@code "java"}). This is case-sensitive.
*
* @param langId Language ID
* @param version Version ID
*
* @return A language, or null if the name is unknown
*/
public @Nullable LanguageVersion getLanguageVersionById(@Nullable String langId, @Nullable String version) {
Language lang = languagesById.get(langId);
if (lang == null) {
return null;
}
return version == null ? lang.getDefaultVersion()
: lang.getVersion(version);
}
/**
* Returns a language from its {@linkplain Language#getName() full name}
* (eg {@code "Java"}). This is case sensitive.
@ -126,17 +144,6 @@ public final class LanguageRegistry implements Iterable<Language> {
return languagesByFullName.get(languageName);
}
/**
* Returns a "default language" known to the service loader. This
* is the Java language if available, otherwise an arbitrary one.
* If no languages are loaded, returns null.
*
* @return A language, or null if the name is unknown
*/
public static @Nullable Language getDefaultLanguage() {
return null;
}
/**
* Returns a language from its {@linkplain Language#getTerseName() terse name}
* (eg {@code "java"}). This is case sensitive.
@ -168,7 +175,7 @@ public final class LanguageRegistry implements Iterable<Language> {
}
public @NonNull String commaSeparatedList(Function<Language, String> getter) {
return getLanguages().stream().map(getter).collect(Collectors.joining(", "));
return getLanguages().stream().map(getter).sorted().collect(Collectors.joining(", "));
}

View File

@ -11,6 +11,7 @@ import java.util.Map;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.nullness.qual.Nullable;
import net.sourceforge.pmd.internal.util.AssertionUtil;
@ -99,7 +100,7 @@ public class LanguageVersionDiscoverer {
* <code>null</code> if there are no supported Languages for the
* file.
*/
public LanguageVersion getDefaultLanguageVersionForFile(String fileName) {
public @Nullable LanguageVersion getDefaultLanguageVersionForFile(String fileName) {
List<Language> languages = getLanguagesForFile(fileName);
LanguageVersion languageVersion = null;
if (!languages.isEmpty()) {

View File

@ -72,11 +72,12 @@ public class RuleBuilder {
return;
}
Language lang = LanguageRegistry.findLanguageByTerseName(languageName);
LanguageRegistry registry = LanguageRegistry.PMD;
Language lang = registry.findLanguageByTerseName(languageName);
if (lang == null) {
throw new IllegalArgumentException(
"Unknown Language '" + languageName + "' for rule " + name + ", supported Languages are "
+ LanguageRegistry.getLanguages().stream().map(Language::getTerseName).collect(Collectors.joining(", "))
+ registry.commaSeparatedList(Language::getId)
);
}
language = lang;

View File

@ -15,7 +15,7 @@ import java.util.function.Consumer;
import org.junit.Test;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.DummyLanguageModule;
import net.sourceforge.pmd.lang.LanguageVersion;
import net.sourceforge.pmd.lang.ast.DummyNode;
import net.sourceforge.pmd.lang.ast.DummyRoot;
@ -95,7 +95,7 @@ public class ReportTest {
public static String render(Renderer renderer, Consumer<? super FileAnalysisListener> listenerEffects) throws IOException {
return renderGlobal(renderer, globalListener -> {
LanguageVersion dummyVersion = LanguageRegistry.getDefaultLanguage().getDefaultVersion();
LanguageVersion dummyVersion = DummyLanguageModule.INSTANCE.getDefaultVersion();
TextFile dummyFile = TextFile.forCharSeq("dummyText", "file", dummyVersion);
try (FileAnalysisListener fal = globalListener.startFileAnalysis(dummyFile)) {

View File

@ -34,6 +34,7 @@ import org.mockito.Mockito;
import net.sourceforge.pmd.RuleSets;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.lang.DummyLanguageModule;
import net.sourceforge.pmd.lang.Language;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersion;
@ -59,7 +60,7 @@ public class FileAnalysisCacheTest {
private TextDocument sourceFile;
private TextFile sourceFileBackend;
private final LanguageVersion dummyVersion = LanguageRegistry.getDefaultLanguage().getDefaultVersion();
private final LanguageVersion dummyVersion = DummyLanguageModule.INSTANCE.getDefaultVersion();
@Before

View File

@ -11,14 +11,6 @@ import net.sourceforge.pmd.PmdContextualizedTest;
public class LanguageRegistryTest extends PmdContextualizedTest {
@Test
public void getDefaultLanguageTest() {
Language defaultLanguage = languageRegistry().getDefaultLanguage();
Assert.assertNotNull(defaultLanguage);
// as we don't have java language in this test, we get the first
// available language now -> DummyLanguage
Assert.assertSame(DummyLanguageModule.class, defaultLanguage.getClass());
}
@Test
public void getDefaultVersionLanguageTest() {

View File

@ -11,6 +11,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import net.sourceforge.pmd.lang.DummyLanguageModule;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersion;
@ -18,7 +19,7 @@ public class TextDocumentTest {
@Rule
public ExpectedException expect = ExpectedException.none();
private final LanguageVersion dummyVersion = LanguageRegistry.getDefaultLanguage().getDefaultVersion();
private final LanguageVersion dummyVersion = DummyLanguageModule.INSTANCE.getDefaultVersion();
@Test
public void testSingleLineRegion() {

View File

@ -18,6 +18,7 @@ import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.RuleSetLoader;
import net.sourceforge.pmd.RuleSets;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.lang.DummyLanguageModule;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersion;
import net.sourceforge.pmd.lang.ast.Node;
@ -37,7 +38,7 @@ public class MultiThreadProcessorTest {
public RuleSets setUpForTest(final String ruleset) {
configuration = new PMDConfiguration();
configuration.setThreads(2);
LanguageVersion lv = LanguageRegistry.getDefaultLanguage().getDefaultVersion();
LanguageVersion lv = DummyLanguageModule.INSTANCE.getDefaultVersion();
files = listOf(
TextFile.forCharSeq("abc", "file1-violation.dummy", lv),
TextFile.forCharSeq("DEF", "file2-foo.dummy", lv)

View File

@ -44,7 +44,7 @@ public class PmdRunnableTest {
static {
Language dummyLanguage = LanguageRegistry.findLanguageByTerseName(DummyLanguageModule.TERSE_NAME);
Language dummyLanguage = DummyLanguageModule.INSTANCE;
DUMMY_DEFAULT = dummyLanguage.getDefaultVersion();
DUMMY_THROWS = dummyLanguage.getVersion("1.9-throws");
}
@ -104,7 +104,7 @@ public class PmdRunnableTest {
private static class RuleThatThrows extends AbstractRule {
RuleThatThrows() {
Language dummyLanguage = LanguageRegistry.findLanguageByTerseName(DummyLanguageModule.TERSE_NAME);
Language dummyLanguage = DummyLanguageModule.INSTANCE;
setLanguage(dummyLanguage);
}

View File

@ -41,8 +41,8 @@ public class SidebarGeneratorTest {
Map<Language, List<RuleSet>> rulesets = new HashMap<>();
RuleSet ruleSet1 = RuleSet.create("test", "test", "bestpractices.xml", Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
RuleSet ruleSet2 = RuleSet.create("test2", "test", "codestyle.xml", Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
rulesets.put(LanguageRegistry.findLanguageByTerseName("java"), Arrays.asList(ruleSet1, ruleSet2));
rulesets.put(LanguageRegistry.findLanguageByTerseName("ecmascript"), Arrays.asList(ruleSet1));
rulesets.put(LanguageRegistry.PMD.getLanguageById("java"), Arrays.asList(ruleSet1, ruleSet2));
rulesets.put(LanguageRegistry.PMD.getLanguageById("ecmascript"), Arrays.asList(ruleSet1));
SidebarGenerator generator = new SidebarGenerator(writer, FileSystems.getDefault().getPath(".."));
List<Map<String, Object>> result = generator.generateRuleReferenceSection(rulesets);

View File

@ -521,15 +521,7 @@ public abstract class RuleTst extends PmdContextualizedTest {
version = null;
terseName = terseNameAndVersion;
}
Language language = languageRegistry().findLanguageByTerseName(terseName);
if (language != null) {
if (version == null) {
return language.getDefaultVersion();
} else {
return language.getVersion(version);
}
}
return null;
return languageRegistry().getLanguageVersionById(terseName, version);
}
private String getNodeValue(Element parentElm, String nodeName, boolean required) {