Delete RulesetNotFoundException

This commit is contained in:
Clément Fournier committed 2020-12-12 19:21:18 +01:00
1 parent 75e4537d0d
commit 3cf619e8e4
8 files changed
+39 -128

No files matched your search

@@ -77,52 +77,16 @@ final class RuleSetFactory {
* @param ruleSetReferenceId
* The RuleSetReferenceId of the RuleSet to create.
* @return A new RuleSet.
* @throws RuleSetNotFoundException
* if unable to find a resource.
*/
RuleSet createRuleSet(RuleSetReferenceId ruleSetReferenceId) throws RuleSetNotFoundException {
RuleSet createRuleSet(RuleSetReferenceId ruleSetReferenceId) {
return createRuleSet(ruleSetReferenceId, includeDeprecatedRuleReferences);
}
private RuleSet createRuleSet(RuleSetReferenceId ruleSetReferenceId, boolean withDeprecatedRuleReferences)
throws RuleSetNotFoundException {
throws RuleSetLoadException {
return parseRuleSetNode(ruleSetReferenceId, withDeprecatedRuleReferences);
}
/**
* Creates a copy of the given ruleset. All properties like name, description, fileName
* and exclude/include patterns are copied.
*
* <p><strong>Note:</strong> The rule instances are shared between the original
* and the new ruleset (copy-by-reference). This might lead to concurrency issues,
* if the original ruleset and the new ruleset are used in different threads.
* </p>
*
* @param original the original rule set to copy from
* @return the copy
*
* @deprecated Use {@link RuleSet#copy(RuleSet)}
*/
@Deprecated
public RuleSet createRuleSetCopy(RuleSet original) {
RuleSetBuilder builder = new RuleSetBuilder(original);
return builder.build();
}
/**
* Creates a new RuleSet containing a single rule.
*
* @param rule The rule being created
*
* @return The newly created RuleSet
*
* @deprecated Use {@link RuleSet#forSingleRule(Rule)}
*/
@Deprecated
public RuleSet createSingleRuleRuleSet(final Rule rule) {
return RuleSet.forSingleRule(rule);
}
/**
* Create a Rule from a RuleSet created from a file name resource. The
* currently configured ResourceLoader is used.
@@ -137,14 +101,11 @@ final class RuleSetFactory {
* Whether RuleReferences that are deprecated should be ignored
* or not
* @return A new Rule.
* @throws RuleSetNotFoundException
* if unable to find a resource.
*/
private Rule createRule(RuleSetReferenceId ruleSetReferenceId, boolean withDeprecatedRuleReferences)
throws RuleSetNotFoundException {
private Rule createRule(RuleSetReferenceId ruleSetReferenceId, boolean withDeprecatedRuleReferences) {
if (ruleSetReferenceId.isAllRules()) {
throw new IllegalArgumentException(
"Cannot parse a single Rule from an all Rule RuleSet reference: <" + ruleSetReferenceId + ">.");
"Cannot parse a single Rule from an all Rule RuleSet reference: <" + ruleSetReferenceId + ">.");
}
RuleSet ruleSet;
// java8: computeIfAbsent
@@ -160,20 +121,18 @@ final class RuleSetFactory {
/**
* Parse a ruleset node to construct a RuleSet.
*
* @param ruleSetReferenceId
* The RuleSetReferenceId of the RuleSet being parsed.
* @param withDeprecatedRuleReferences
* whether rule references that are deprecated should be ignored
* or not
* @param ruleSetReferenceId The RuleSetReferenceId of the RuleSet being parsed.
* @param withDeprecatedRuleReferences whether rule references that are deprecated should be ignored
* or not
*
* @return The new RuleSet.
*/
private RuleSet parseRuleSetNode(RuleSetReferenceId ruleSetReferenceId, boolean withDeprecatedRuleReferences)
throws RuleSetNotFoundException {
private RuleSet parseRuleSetNode(RuleSetReferenceId ruleSetReferenceId, boolean withDeprecatedRuleReferences) {
try (CheckedInputStream inputStream = new CheckedInputStream(
ruleSetReferenceId.getInputStream(resourceLoader), new Adler32());) {
ruleSetReferenceId.getInputStream(resourceLoader), new Adler32());) {
if (!ruleSetReferenceId.isExternal()) {
throw new IllegalArgumentException(
"Cannot parse a RuleSet from a non-external reference: <" + ruleSetReferenceId + ">.");
"Cannot parse a RuleSet from a non-external reference: <" + ruleSetReferenceId + ">.");
}
DocumentBuilder builder = createDocumentBuilder();
InputSource inputSource = new InputSource(inputStream);
@@ -299,8 +258,7 @@ final class RuleSetFactory {
* @param rulesetReferences keeps track of already processed complete ruleset references in order to log a warning
*/
private void parseRuleNode(RuleSetReferenceId ruleSetReferenceId, RuleSetBuilder ruleSetBuilder, Node ruleNode,
boolean withDeprecatedRuleReferences, Set<String> rulesetReferences)
throws RuleSetNotFoundException {
boolean withDeprecatedRuleReferences, Set<String> rulesetReferences) {
Element ruleElement = (Element) ruleNode;
String ref = ruleElement.getAttribute("ref");
ref = compatibilityFilter.applyRef(ref, this.warnDeprecated);
@@ -330,8 +288,7 @@ final class RuleSetFactory {
* The RuleSet reference.
* @param rulesetReferences keeps track of already processed complete ruleset references in order to log a warning
*/
private void parseRuleSetReferenceNode(RuleSetBuilder ruleSetBuilder, Element ruleElement, String ref, Set<String> rulesetReferences)
throws RuleSetNotFoundException {
private void parseRuleSetReferenceNode(RuleSetBuilder ruleSetBuilder, Element ruleElement, String ref, Set<String> rulesetReferences) {
String priority = null;
NodeList childNodes = ruleElement.getChildNodes();
Set<String> excludedRulesCheck = new HashSet<>();
@@ -455,13 +412,13 @@ final class RuleSetFactory {
* or not
*/
private void parseRuleReferenceNode(RuleSetReferenceId ruleSetReferenceId, RuleSetBuilder ruleSetBuilder,
Node ruleNode, String ref, boolean withDeprecatedRuleReferences) throws RuleSetNotFoundException {
Node ruleNode, String ref, boolean withDeprecatedRuleReferences) {
Element ruleElement = (Element) ruleNode;
// Stop if we're looking for a particular Rule, and this element is not
// it.
if (StringUtils.isNotBlank(ruleSetReferenceId.getRuleName())
&& !isRuleName(ruleElement, ruleSetReferenceId.getRuleName())) {
&& !isRuleName(ruleElement, ruleSetReferenceId.getRuleName())) {
return;
}
@@ -565,7 +522,7 @@ final class RuleSetFactory {
}
}
} catch (Exception e) {
throw new RuntimeException(e);
throw new RuleSetLoadException("Cannot load " + ruleSetReferenceId, e);
}
return found;
@@ -22,6 +22,7 @@ import org.checkerframework.checker.nullness.qual.Nullable;
final class RuleSetFactoryCompatibility {
static final RuleSetFactoryCompatibility EMPTY = new RuleSetFactoryCompatibility();
/** The instance with the built-in filters for the modified PMD rules. */
static final RuleSetFactoryCompatibility DEFAULT = new RuleSetFactoryCompatibility();
@@ -79,13 +80,6 @@ final class RuleSetFactoryCompatibility {
private final List<RuleSetFilter> filters = new ArrayList<>();
/**
* Creates a new instance of the compatibility filter with the built-in
* filters for the modified PMD rules.
*/
RuleSetFactoryCompatibility() {
}
void addFilterRuleMovedAndRenamed(String language, String oldRuleset, String oldName, String newRuleset, String newName) {
filters.add(RuleSetFilter.ruleMoved(language, oldRuleset, newRuleset, oldName));
@@ -11,9 +11,6 @@ import net.sourceforge.pmd.annotation.InternalApi;
* {@linkplain RuleSetLoader loading rulesets}. This may be because the
* XML is not well-formed, does not respect the ruleset schema, is
* not a valid ruleset or is otherwise unparsable.
*
* <p>In the new {@link RuleSetLoader} API, this is thrown instead of
* {@link RuleSetNotFoundException}.
*/
public final class RuleSetLoadException extends RuntimeException {
@@ -9,10 +9,10 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -29,8 +29,6 @@ import net.sourceforge.pmd.util.ResourceLoader;
*/
public final class RuleSetLoader {
private static final Logger LOG = Logger.getLogger(RuleSetLoader.class.getName());
private ResourceLoader resourceLoader = new ResourceLoader(RuleSetLoader.class.getClassLoader());
private RulePriority minimumPriority = RulePriority.LOW;
private boolean warnDeprecated = true;
@@ -220,7 +218,7 @@ public final class RuleSetLoader {
*/
public List<RuleSet> getStandardRuleSets() {
String rulesetsProperties;
List<RuleSetReferenceId> ruleSetReferenceIds = new ArrayList<>();
List<String> ruleSetReferenceIds = new ArrayList<>();
for (Language language : LanguageRegistry.getLanguages()) {
Properties props = new Properties();
rulesetsProperties = "category/" + language.getTerseName() + "/categories.properties";
@@ -228,11 +226,9 @@ public final class RuleSetLoader {
props.load(inputStream);
String rulesetFilenames = props.getProperty("rulesets.filenames");
if (rulesetFilenames != null) {
ruleSetReferenceIds.addAll(RuleSetReferenceId.parse(rulesetFilenames));
ruleSetReferenceIds.addAll(Arrays.asList(rulesetFilenames.split(",")));
}
} catch (RuleSetNotFoundException e) {
LOG.warning("The language " + language.getTerseName() + " provides no " + rulesetsProperties + ".");
} catch (IOException ioe) {
} catch (IOException e) {
throw new RuntimeException("Couldn't find " + rulesetsProperties
+ "; please ensure that the directory is on the classpath. The current classpath is: "
+ System.getProperty("java.class.path"));
@@ -240,7 +236,7 @@ public final class RuleSetLoader {
}
List<RuleSet> ruleSets = new ArrayList<>();
for (RuleSetReferenceId id : ruleSetReferenceIds) {
for (String id : ruleSetReferenceIds) {
ruleSets.add(loadFromResource(id)); // may throw
}
return ruleSets;
@@ -1,23 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd;
/**
* @deprecated This is now only thrown by deprecated apis. {@link RuleSetLoader}
* throws {@link RuleSetLoadException} instead
*/
@Deprecated
public class RuleSetNotFoundException extends Exception {
private static final long serialVersionUID = -4617033110919250810L;
public RuleSetNotFoundException(String msg) {
super(msg);
}
public RuleSetNotFoundException(String msg, Throwable cause) {
super(msg, cause);
}
}
@@ -5,6 +5,7 @@
package net.sourceforge.pmd;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
@@ -397,19 +398,15 @@ public class RuleSetReferenceId {
*
* @param rl The {@link ResourceLoader} to use.
* @return An InputStream to that resource.
* @throws RuleSetNotFoundException
* if unable to find a resource.
*/
public InputStream getInputStream(final ResourceLoader rl) throws RuleSetNotFoundException {
public InputStream getInputStream(final ResourceLoader rl) throws IOException {
if (externalRuleSetReferenceId == null) {
InputStream in = StringUtils.isBlank(ruleSetFileName) ? null
: rl.loadResourceAsStream(ruleSetFileName);
if (in == null) {
throw new RuleSetNotFoundException("Can't find resource '" + ruleSetFileName + "' for rule '" + ruleName
+ "'" + ". Make sure the resource is a valid file or URL and is on the CLASSPATH. "
+ "Here's the current classpath: " + System.getProperty("java.class.path"));
if (StringUtils.isBlank(ruleSetFileName)) {
throw new FileNotFoundException("Can't find resource '" + ruleSetFileName + "' for rule '" + ruleName
+ "'" + ". Make sure the resource is a valid file or URL and is on the CLASSPATH. "
+ "Here's the current classpath: " + System.getProperty("java.class.path"));
}
return in;
return rl.loadResourceAsStream(ruleSetFileName);
} else {
return externalRuleSetReferenceId.getInputStream(rl);
}
@@ -5,6 +5,7 @@
package net.sourceforge.pmd.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
@@ -14,7 +15,6 @@ import java.nio.file.Files;
import java.util.Objects;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleSetNotFoundException;
import net.sourceforge.pmd.annotation.InternalApi;
/**
@@ -58,10 +58,10 @@ public class ResourceLoader {
* Caller is responsible for closing the {@link InputStream}.
*
* @param name The resource to attempt and load
*
* @return InputStream
* @throws RuleSetNotFoundException
*/
public InputStream loadResourceAsStream(final String name) throws RuleSetNotFoundException {
public InputStream loadResourceAsStream(final String name) throws IOException {
// Search file locations first
final File file = new File(name);
if (file.exists()) {
@@ -69,7 +69,8 @@ public class ResourceLoader {
return Files.newInputStream(file.toPath());
} catch (final IOException e) {
// if the file didn't exist, we wouldn't be here
throw new RuntimeException(e); // somehow the file vanished between checking for existence and opening
// somehow the file vanished between checking for existence and opening
throw new IOException("File was checked to exist", e);
}
}
@@ -82,13 +83,11 @@ public class ResourceLoader {
} catch (final Exception e) {
try {
return loadClassPathResourceAsStream(name);
} catch (final IOException ignored) {
// We will throw our own exception, with a different message
} catch (final IOException ioe) {
throw new IOException("Can't find resource " + name + ". Make sure the resource is a valid file or URL or is on the CLASSPATH", ioe);
}
}
throw new RuleSetNotFoundException("Can't find resource " + name
+ ". Make sure the resource is a valid file or URL or is on the CLASSPATH");
}
public InputStream loadClassPathResourceAsStream(final String name) throws IOException {
@@ -110,7 +109,7 @@ public class ResourceLoader {
}
}
public InputStream loadClassPathResourceAsStreamOrThrow(final String name) throws RuleSetNotFoundException {
public InputStream loadClassPathResourceAsStreamOrThrow(final String name) throws IOException {
InputStream is = null;
try {
is = loadClassPathResourceAsStream(name);
@@ -119,7 +118,7 @@ public class ResourceLoader {
}
if (is == null) {
throw new RuleSetNotFoundException("Can't find resource " + name
throw new FileNotFoundException("Can't find resource " + name
+ ". Make sure the resource is on the CLASSPATH");
}
@@ -128,12 +127,6 @@ public class ResourceLoader {
/**
* Load the rule from the classloader from resource loader, consistent with the ruleset
*
* @param clazz
* @return
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public Rule loadRuleFromClassPath(final String clazz) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
return (Rule) classLoader.loadClass(clazz).newInstance();
@@ -579,7 +579,7 @@ public class RuleSetFactoryTest {
}
@Test
public void testIncorrectExternalRef() {
public void testIncorrectExternalRef() {
assertCannotParse(REF_MISSPELLED_XREF);
}