finish superseding old base module class

This commit is contained in:
Clément Fournier
2022-07-21 03:46:08 +02:00
parent 3d5c2d1f70
commit 94c4acbcc8
5 changed files with 50 additions and 261 deletions

View File

@ -1,191 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.checkerframework.checker.nullness.qual.NonNull;
import net.sourceforge.pmd.util.CollectionUtil;
/**
* Created by christoferdutz on 21.09.14.
*/
public abstract class BaseLanguageModule implements Language {
protected String name;
protected String shortName;
protected String terseName;
protected List<String> extensions;
private final List<LanguageVersion> distinctVersions = new ArrayList<>();
protected Map<String, LanguageVersion> versions;
protected LanguageVersion defaultVersion;
public BaseLanguageModule(String name,
String shortName,
String terseName,
String firstExtension,
String... otherExtensions) {
this(name, shortName, terseName, CollectionUtil.listOf(firstExtension, otherExtensions));
}
public BaseLanguageModule(String name,
String shortName,
String terseName,
List<String> extensions) {
this.name = name;
this.shortName = shortName;
this.terseName = terseName;
this.extensions = CollectionUtil.defensiveUnmodifiableCopy(extensions);
}
private void addVersion(String version, LanguageVersionHandler languageVersionHandler, boolean isDefault, String... versionAliases) {
if (versions == null) {
versions = new HashMap<>();
}
LanguageVersion languageVersion = new LanguageVersion(this, version, null);
distinctVersions.add(languageVersion);
checkNotPresent(version);
versions.put(version, languageVersion);
for (String alias : versionAliases) {
checkNotPresent(alias);
versions.put(alias, languageVersion);
}
if (isDefault) {
if (defaultVersion != null) {
throw new IllegalStateException(
"Default version already set to " + defaultVersion + ", cannot set it to " + languageVersion);
}
defaultVersion = languageVersion;
}
}
private void checkNotPresent(String alias) {
if (versions.containsKey(alias)) {
throw new IllegalArgumentException("Version key '" + alias + "' is duplicated");
}
}
/**
* Adds a non-default version with the given identifier.
*
* @throws IllegalArgumentException If the string key or any of the
* aliases conflict with other already
* recorded versions
*/
protected void addVersion(String version, LanguageVersionHandler languageVersionHandler, String... versionAliases) {
addVersion(version, languageVersionHandler, false, versionAliases);
}
/**
* Adds a version with the given identifier, and sets it as the default.
*
* @throws IllegalStateException If the default version is already set
* @throws IllegalArgumentException If the string key or any of the
* aliases conflict with other already
* recorded versions
*/
protected void addDefaultVersion(String version, LanguageVersionHandler languageVersionHandler, String... versionAliases) {
addVersion(version, languageVersionHandler, true, versionAliases);
}
/**
* @deprecated use {@link #addVersion(String, LanguageVersionHandler, String...)} or {@link #addDefaultVersion(String, LanguageVersionHandler, String...)}
*/
@Deprecated
protected void addVersion(String version, LanguageVersionHandler languageVersionHandler, boolean isDefault) {
addVersion(version, languageVersionHandler, isDefault, new String[0]);
}
@Override
public String getName() {
return name;
}
@Override
public String getShortName() {
return (shortName != null) ? shortName : name;
}
@Override
public String getTerseName() {
return terseName;
}
@NonNull
@Override
public List<String> getExtensions() {
return Collections.unmodifiableList(extensions);
}
@Override
public boolean hasExtension(String extensionWithoutDot) {
return extensions != null && extensions.contains(extensionWithoutDot);
}
@Override
public List<LanguageVersion> getVersions() {
return new ArrayList<>(distinctVersions);
}
@Override
public boolean hasVersion(String version) {
return versions != null && versions.containsKey(version);
}
@Override
public LanguageVersion getVersion(String versionName) {
if (versions != null) {
return versions.get(versionName);
}
return null;
}
@Override
public LanguageVersion getDefaultVersion() {
assert defaultVersion != null : "Null default version for language " + this;
return defaultVersion;
}
@Override
public String toString() {
return getTerseName();
}
@Override
public int compareTo(Language o) {
return getName().compareTo(o.getName());
}
@Override
public int hashCode() {
return Objects.hash(name);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
BaseLanguageModule other = (BaseLanguageModule) obj;
return Objects.equals(name, other.name);
}
}

View File

@ -7,24 +7,19 @@ package net.sourceforge.pmd.lang;
import java.util.Objects;
import net.sourceforge.pmd.lang.DummyLanguageModule.Handler;
import net.sourceforge.pmd.processor.SimpleBatchLanguageProcessor;
import net.sourceforge.pmd.lang.impl.SimpleLanguageModuleBase;
/**
* A second dummy language used for testing PMD.
*/
public class Dummy2LanguageModule extends BaseLanguageModule {
public class Dummy2LanguageModule extends SimpleLanguageModuleBase {
public static final String NAME = "Dummy2";
public static final String TERSE_NAME = "dummy2";
public Dummy2LanguageModule() {
super(NAME, null, TERSE_NAME, "dummy2");
addVersion("1.0", new DummyLanguageModule.Handler(), true);
}
@Override
public LanguageProcessor createProcessor(LanguagePropertyBundle bundle) {
return new SimpleBatchLanguageProcessor(bundle, new Handler());
super(LanguageMetadata.withId(TERSE_NAME).name(NAME).extensions("dummy2")
.addVersion("1.0"), new Handler());
}
public static Dummy2LanguageModule getInstance() {

View File

@ -20,34 +20,29 @@ import net.sourceforge.pmd.lang.document.Chars;
import net.sourceforge.pmd.lang.document.FileLocation;
import net.sourceforge.pmd.lang.document.TextDocument;
import net.sourceforge.pmd.lang.document.TextRegion;
import net.sourceforge.pmd.lang.impl.SimpleLanguageModuleBase;
import net.sourceforge.pmd.lang.rule.ParametricRuleViolation;
import net.sourceforge.pmd.lang.rule.impl.DefaultRuleViolationFactory;
import net.sourceforge.pmd.processor.SimpleBatchLanguageProcessor;
/**
* Dummy language used for testing PMD.
*/
public class DummyLanguageModule extends BaseLanguageModule {
public class DummyLanguageModule extends SimpleLanguageModuleBase {
public static final String NAME = "Dummy";
public static final String TERSE_NAME = "dummy";
public DummyLanguageModule() {
super(NAME, null, TERSE_NAME, "dummy");
addVersion("1.0", new Handler());
addVersion("1.1", new Handler());
addVersion("1.2", new Handler());
addVersion("1.3", new Handler());
addVersion("1.4", new Handler());
addVersion("1.5", new Handler(), "5");
addVersion("1.6", new Handler(), "6");
addDefaultVersion("1.7", new Handler(), "7");
addVersion("1.8", new Handler(), "8");
}
@Override
public LanguageProcessor createProcessor(LanguagePropertyBundle bundle) {
return new SimpleBatchLanguageProcessor(bundle, new Handler());
super(LanguageMetadata.withId(TERSE_NAME).name(NAME).extensions("dummy")
.addVersion("1.0")
.addVersion("1.1")
.addVersion("1.2")
.addVersion("1.3")
.addVersion("1.4")
.addVersion("1.5", "5")
.addVersion("1.6", "6")
.addDefaultVersion("1.7", "7")
.addVersion("1.8", "8"), new Handler());
}
public static DummyLanguageModule getInstance() {

View File

@ -4,44 +4,43 @@
package net.sourceforge.pmd.lang.java;
import net.sourceforge.pmd.lang.BaseLanguageModule;
import net.sourceforge.pmd.lang.Language;
import net.sourceforge.pmd.lang.LanguageProcessor;
import net.sourceforge.pmd.lang.LanguagePropertyBundle;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.java.internal.JavaLanguageHandler;
import net.sourceforge.pmd.lang.impl.LanguageModuleBase;
import net.sourceforge.pmd.lang.java.internal.JavaLanguageProcessor;
import net.sourceforge.pmd.lang.java.internal.JavaLanguageProperties;
/**
* Created by christoferdutz on 20.09.14.
*/
public class JavaLanguageModule extends BaseLanguageModule {
public class JavaLanguageModule extends LanguageModuleBase {
public static final String NAME = "Java";
public static final String TERSE_NAME = "java";
public JavaLanguageModule() {
super(NAME, null, TERSE_NAME, "java");
addVersion("1.3", new JavaLanguageHandler(3));
addVersion("1.4", new JavaLanguageHandler(4));
addVersion("1.5", new JavaLanguageHandler(5), "5");
addVersion("1.6", new JavaLanguageHandler(6), "6");
addVersion("1.7", new JavaLanguageHandler(7), "7");
addVersion("1.8", new JavaLanguageHandler(8), "8");
addVersion("9", new JavaLanguageHandler(9), "1.9");
addVersion("10", new JavaLanguageHandler(10), "1.10");
addVersion("11", new JavaLanguageHandler(11));
addVersion("12", new JavaLanguageHandler(12));
addVersion("13", new JavaLanguageHandler(13));
addVersion("14", new JavaLanguageHandler(14));
addVersion("15", new JavaLanguageHandler(15));
addVersion("16", new JavaLanguageHandler(16));
addVersion("17", new JavaLanguageHandler(17));
addVersion("18", new JavaLanguageHandler(18));
addVersion("18-preview", new JavaLanguageHandler(18, true));
addDefaultVersion("19", new JavaLanguageHandler(19)); // 19 is the default
addVersion("19-preview", new JavaLanguageHandler(19, true));
super(LanguageMetadata.withId(TERSE_NAME).name(NAME).extensions("java")
.addVersion("1.3")
.addVersion("1.4")
.addVersion("1.5", "5")
.addVersion("1.6", "6")
.addVersion("1.7", "7")
.addVersion("1.8", "8")
.addVersion("9", "1.9")
.addVersion("10", "1.10")
.addVersion("11")
.addVersion("12")
.addVersion("13")
.addVersion("14")
.addVersion("15")
.addVersion("16")
.addVersion("17")
.addVersion("18")
.addVersion("18-preview")
.addDefaultVersion("19") // 19 is the default
.addVersion("19-preview"));
}

View File

@ -6,17 +6,14 @@ package net.sourceforge.pmd.test.lang;
import net.sourceforge.pmd.annotation.InternalApi;
import net.sourceforge.pmd.lang.AbstractPmdLanguageVersionHandler;
import net.sourceforge.pmd.lang.BaseLanguageModule;
import net.sourceforge.pmd.lang.LanguageProcessor;
import net.sourceforge.pmd.lang.LanguagePropertyBundle;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.ast.AstInfo;
import net.sourceforge.pmd.lang.ast.Parser;
import net.sourceforge.pmd.lang.ast.Parser.ParserTask;
import net.sourceforge.pmd.lang.ast.RootNode;
import net.sourceforge.pmd.lang.document.TextRegion;
import net.sourceforge.pmd.lang.impl.SimpleLanguageModuleBase;
import net.sourceforge.pmd.lang.rule.impl.DefaultRuleViolationFactory;
import net.sourceforge.pmd.processor.SimpleBatchLanguageProcessor;
import net.sourceforge.pmd.test.lang.ast.DummyNode;
/**
@ -27,28 +24,22 @@ import net.sourceforge.pmd.test.lang.ast.DummyNode;
*/
@Deprecated
@InternalApi
public class DummyLanguageModule extends BaseLanguageModule {
public class DummyLanguageModule extends SimpleLanguageModuleBase {
public static final String NAME = "Dummy";
public static final String TERSE_NAME = "dummy";
public DummyLanguageModule() {
super(NAME, null, TERSE_NAME, "dummy");
addVersion("1.0", new Handler(), false);
addVersion("1.1", new Handler(), false);
addVersion("1.2", new Handler(), false);
addVersion("1.3", new Handler(), false);
addVersion("1.4", new Handler(), false);
addVersion("1.5", new Handler(), false);
addVersion("1.6", new Handler(), false);
addVersion("1.7", new Handler(), true);
addVersion("1.8", new Handler(), false);
}
@Override
public LanguageProcessor createProcessor(LanguagePropertyBundle bundle) {
return new SimpleBatchLanguageProcessor(bundle, new Handler());
super(LanguageMetadata.withId(TERSE_NAME).name(NAME).extensions("dummy")
.addVersion("1.0")
.addVersion("1.1")
.addVersion("1.2")
.addVersion("1.3")
.addVersion("1.4")
.addVersion("1.5", "5")
.addVersion("1.6", "6")
.addDefaultVersion("1.7", "7")
.addVersion("1.8", "8"), new Handler());
}
public static DummyLanguageModule getInstance() {