From 4be7616c15da351570f15ffc40f4c47bc26292ee Mon Sep 17 00:00:00 2001 From: oowekyala Date: Sun, 28 May 2017 18:13:19 +0200 Subject: [PATCH] Fixed formatter chaos --- .../java/net/sourceforge/pmd/RuleSet.java | 679 +++++++++--------- 1 file changed, 355 insertions(+), 324 deletions(-) diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSet.java b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSet.java index 66e9b6d697..9c007581a0 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSet.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSet.java @@ -54,10 +54,12 @@ public class RuleSet implements ChecksumAware { /** * Creates a new RuleSet with the given checksum. - * - * @param checksum A checksum of the ruleset, should change only if the ruleset - * was configured differently - * @param rules The rules to be applied as part of this ruleset + * + * @param checksum + * A checksum of the ruleset, should change only if the ruleset + * was configured differently + * @param rules + * The rules to be applied as part of this ruleset */ private RuleSet(final RuleSetBuilder builder) { checksum = builder.checksum; @@ -73,22 +75,294 @@ public class RuleSet implements ChecksumAware { filter = Filters.toNormalizedFileFilter(regexFilter); } - /** - * Does the given Rule apply to the given LanguageVersion? If so, the - * Language must be the same and be between the minimum and maximums - * versions on the Rule. - * - * @param rule The rule. - * @param languageVersion The language version. - * @return true if the given rule matches the given language, - * which means, that the rule would be executed. - */ - public static boolean applies(Rule rule, LanguageVersion languageVersion) { - final LanguageVersion min = rule.getMinimumLanguageVersion(); - final LanguageVersion max = rule.getMaximumLanguageVersion(); - return rule.getLanguage().equals(languageVersion.getLanguage()) - && (min == null || min.compareTo(languageVersion) <= 0) - && (max == null || max.compareTo(languageVersion) >= 0); + /* package */ static class RuleSetBuilder { + public String description = ""; + public String name = ""; + public String fileName; + private final List rules = new ArrayList<>(); + private final List excludePatterns = new ArrayList<>(0); + private final List includePatterns = new ArrayList<>(0); + private final long checksum; + + /* package */ RuleSetBuilder(final long checksum) { + this.checksum = checksum; + } + + /** Copy constructor. Takes the same checksum as the original ruleset. */ + /* package */ RuleSetBuilder(final RuleSet original) { + checksum = original.getChecksum(); + this.withName(original.getName()) + .withDescription(original.getDescription()) + .withFileName(original.getFileName()) + .setExcludePatterns(original.getExcludePatterns()) + .setIncludePatterns(original.getIncludePatterns()); + addRuleSet(original); + } + + /** + * Add a new rule to this ruleset. Note that this method does not check + * for duplicates. + * + * @param rule + * the rule to be added + * @return The same builder, for a fluid programming interface + */ + public RuleSetBuilder addRule(final Rule rule) { + if (rule == null) { + throw new IllegalArgumentException(MISSING_RULE); + } + rules.add(rule); + return this; + } + + /** + * Adds a rule. If a rule with the same name and language already + * existed before in the ruleset, then the new rule will replace it. + * This makes sure that the rule configured is overridden. + * + * @param rule + * the new rule to add + * @return The same builder, for a fluid programming interface + */ + public RuleSetBuilder addRuleReplaceIfExists(final Rule rule) { + if (rule == null) { + throw new IllegalArgumentException(MISSING_RULE); + } + + for (final Iterator it = rules.iterator(); it.hasNext();) { + final Rule r = it.next(); + if (r.getName().equals(rule.getName()) && r.getLanguage() == rule.getLanguage()) { + it.remove(); + } + } + addRule(rule); + return this; + } + + /** + * Only adds a rule to the ruleset if no rule with the same name for the + * same language was added before, so that the existent rule + * configuration won't be overridden. + * + * @param rule + * the new rule to add + * @return The same builder, for a fluid programming interface + */ + public RuleSetBuilder addRuleIfNotExists(final Rule rule) { + if (rule == null) { + throw new IllegalArgumentException(MISSING_RULE); + } + + boolean exists = false; + for (final Rule r : rules) { + if (r.getName().equals(rule.getName()) && r.getLanguage() == rule.getLanguage()) { + exists = true; + break; + } + } + if (!exists) { + addRule(rule); + } + return this; + } + + /** + * Add a new rule by reference to this ruleset. + * + * @param ruleSetFileName + * the ruleset which contains the rule + * @param rule + * the rule to be added + * @return The same builder, for a fluid programming interface + */ + public RuleSetBuilder addRuleByReference(final String ruleSetFileName, final Rule rule) { + if (StringUtil.isEmpty(ruleSetFileName)) { + throw new RuntimeException( + "Adding a rule by reference is not allowed with an empty rule set file name."); + } + if (rule == null) { + throw new IllegalArgumentException("Cannot add a null rule reference to a RuleSet"); + } + final RuleReference ruleReference; + if (rule instanceof RuleReference) { + ruleReference = (RuleReference) rule; + } else { + final RuleSetReference ruleSetReference = new RuleSetReference(); + ruleSetReference.setRuleSetFileName(ruleSetFileName); + ruleReference = new RuleReference(); + ruleReference.setRule(rule); + ruleReference.setRuleSetReference(ruleSetReference); + } + rules.add(ruleReference); + return this; + } + + /** + * Add all rules of a whole RuleSet to this RuleSet + * + * @param ruleSet + * the RuleSet to add + * @return The same builder, for a fluid programming interface + */ + public RuleSetBuilder addRuleSet(final RuleSet ruleSet) { + rules.addAll(rules.size(), ruleSet.getRules()); + return this; + } + + /** + * Add all rules by reference from one RuleSet to this RuleSet. The + * rules can be added as individual references, or collectively as an + * all rule reference. + * + * @param ruleSet + * the RuleSet to add + * @param allRules + * true if the ruleset should be added + * collectively or false to add individual + * references for each rule. + * @return The same builder, for a fluid programming interface + */ + public RuleSetBuilder addRuleSetByReference(final RuleSet ruleSet, final boolean allRules) { + return addRuleSetByReference(ruleSet, allRules, (String[]) null); + } + + /** + * Add all rules by reference from one RuleSet to this RuleSet. The + * rules can be added as individual references, or collectively as an + * all rule reference. + * + * @param ruleSet + * the RuleSet to add + * @param allRules + * true if the ruleset should be added + * collectively or false to add individual + * references for each rule. + * @param excludes + * names of the rules that should be excluded. + * @return The same builder, for a fluid programming interface + */ + public RuleSetBuilder addRuleSetByReference(final RuleSet ruleSet, final boolean allRules, + final String... excludes) { + if (StringUtil.isEmpty(ruleSet.getFileName())) { + throw new RuntimeException( + "Adding a rule by reference is not allowed with an empty rule set file name."); + } + final RuleSetReference ruleSetReference = new RuleSetReference(ruleSet.getFileName()); + ruleSetReference.setAllRules(allRules); + if (excludes != null) { + ruleSetReference.setExcludes(new HashSet<>(Arrays.asList(excludes))); + } + for (final Rule rule : ruleSet.getRules()) { + final RuleReference ruleReference = new RuleReference(rule, ruleSetReference); + rules.add(ruleReference); + } + return this; + } + + /** + * Adds a new file exclusion pattern. + * + * @param aPattern + * the pattern + * @return The same builder, for a fluid programming interface + */ + public RuleSetBuilder addExcludePattern(final String aPattern) { + if (!excludePatterns.contains(aPattern)) { + excludePatterns.add(aPattern); + } + return this; + } + + /** + * Adds new file exclusion patterns. + * + * @param someExcludePatterns + * the patterns + * @return The same builder, for a fluid programming interface + */ + public RuleSetBuilder addExcludePatterns(final Collection someExcludePatterns) { + CollectionUtil.addWithoutDuplicates(someExcludePatterns, excludePatterns); + return this; + } + + /** + * Replaces the existing exclusion patterns with the given patterns. + * + * @param theExcludePatterns + * the new patterns + */ + public RuleSetBuilder setExcludePatterns(final Collection theExcludePatterns) { + if (!excludePatterns.equals(theExcludePatterns)) { + excludePatterns.clear(); + CollectionUtil.addWithoutDuplicates(theExcludePatterns, excludePatterns); + } + return this; + } + + /** + * Adds new inclusion patterns. + * + * @param someIncludePatterns + * the patterns + * @return The same builder, for a fluid programming interface + */ + public RuleSetBuilder addIncludePatterns(final Collection someIncludePatterns) { + CollectionUtil.addWithoutDuplicates(someIncludePatterns, includePatterns); + return this; + } + + /** + * Replaces the existing inclusion patterns with the given patterns. + * + * @param theIncludePatterns + * the new patterns + * @return The same builder, for a fluid programming interface + */ + public RuleSetBuilder setIncludePatterns(final Collection theIncludePatterns) { + if (!includePatterns.equals(theIncludePatterns)) { + includePatterns.clear(); + CollectionUtil.addWithoutDuplicates(theIncludePatterns, includePatterns); + } + + return this; + } + + /** + * Adds a new inclusion pattern. + * + * @param aPattern + * the pattern + * @return The same builder, for a fluid programming interface + */ + public RuleSetBuilder addIncludePattern(final String aPattern) { + if (!includePatterns.contains(aPattern)) { + includePatterns.add(aPattern); + } + return this; + } + + public RuleSetBuilder withFileName(final String fileName) { + this.fileName = fileName; + return this; + } + + public RuleSetBuilder withName(final String name) { + this.name = name; + return this; + } + + public RuleSetBuilder withDescription(final String description) { + this.description = description; + return this; + } + + public String getName() { + return name; + } + + public RuleSet build() { + return new RuleSet(this); + } } /** @@ -109,29 +383,15 @@ public class RuleSet implements ChecksumAware { return rules; } - /** - * Does any Rule for the given Language use the DFA layer? - * - * @param language The Language. - * @return true if a Rule for the Language uses the DFA layer, - * false otherwise. - */ - public boolean usesDFA(Language language) { - for (Rule r : rules) { - if (r.getLanguage().equals(language) && r.usesDFA()) { - return true; - } - } - return false; - } /** * Returns the first Rule found with the given name (case-sensitive). - *

+ * * Note: Since we support multiple languages, rule names are not expected to * be unique within any specific ruleset. * - * @param ruleName the exact name of the rule to find + * @param ruleName + * the exact name of the rule to find * @return the rule or null if not found */ public Rule getRuleByName(String ruleName) { @@ -151,9 +411,10 @@ public class RuleSet implements ChecksumAware { * which also matches the file. In other words, include * patterns override exclude patterns. * - * @param file the source file to check + * @param file + * the source file to check * @return true if the file should be checked, - * false otherwise + * false otherwise */ public boolean applies(File file) { return file != null ? filter.filter(file) : true; @@ -163,7 +424,8 @@ public class RuleSet implements ChecksumAware { * Triggers that start lifecycle event on each rule in this ruleset. Some * rules perform initialization tasks on start. * - * @param ctx the current context + * @param ctx + * the current context */ public void start(RuleContext ctx) { for (Rule rule : rules) { @@ -174,8 +436,10 @@ public class RuleSet implements ChecksumAware { /** * Executes the rules in this ruleset against each of the given nodes. * - * @param acuList the node list, usually the root nodes like compilation units - * @param ctx the current context + * @param acuList + * the node list, usually the root nodes like compilation units + * @param ctx + * the current context */ public void apply(List acuList, RuleContext ctx) { long start = System.nanoTime(); @@ -200,11 +464,33 @@ public class RuleSet implements ChecksumAware { } } + /** + * Does the given Rule apply to the given LanguageVersion? If so, the + * Language must be the same and be between the minimum and maximums + * versions on the Rule. + * + * @param rule + * The rule. + * @param languageVersion + * The language version. + * + * @return true if the given rule matches the given language, + * which means, that the rule would be executed. + */ + public static boolean applies(Rule rule, LanguageVersion languageVersion) { + final LanguageVersion min = rule.getMinimumLanguageVersion(); + final LanguageVersion max = rule.getMaximumLanguageVersion(); + return rule.getLanguage().equals(languageVersion.getLanguage()) + && (min == null || min.compareTo(languageVersion) <= 0) + && (max == null || max.compareTo(languageVersion) >= 0); + } + /** * Triggers the end lifecycle event on each rule in the ruleset. Some rules * perform a final summary calculation or cleanup in the end. * - * @param ctx the current context + * @param ctx + * the current context */ public void end(RuleContext ctx) { for (Rule rule : rules) { @@ -216,9 +502,10 @@ public class RuleSet implements ChecksumAware { * Two rulesets are equals, if they have the same name and contain the same * rules. * - * @param o the other ruleset to compare with + * @param o + * the other ruleset to compare with * @return true if o is a ruleset with the same name and rules, - * false otherwise + * false otherwise */ @Override public boolean equals(Object o) { @@ -259,12 +546,30 @@ public class RuleSet implements ChecksumAware { return includePatterns; } + /** + * Does any Rule for the given Language use the DFA layer? + * + * @param language + * The Language. + * @return true if a Rule for the Language uses the DFA layer, + * false otherwise. + */ + public boolean usesDFA(Language language) { + for (Rule r : rules) { + if (r.getLanguage().equals(language) && r.usesDFA()) { + return true; + } + } + return false; + } + /** * Does any Rule for the given Language use Type Resolution? * - * @param language The Language. + * @param language + * The Language. * @return true if a Rule for the Language uses Type - * Resolution, false otherwise. + * Resolution, false otherwise. */ public boolean usesTypeResolution(Language language) { for (Rule r : rules) { @@ -276,7 +581,7 @@ public class RuleSet implements ChecksumAware { } /** - * Does any Rule for the given Language use Metrics? + * Does any Rule for the given Language use the Metrics Framework? * * @param language The Language. * @return true if a Rule for the Language uses the Metrics @@ -294,7 +599,8 @@ public class RuleSet implements ChecksumAware { /** * Remove and collect any misconfigured rules. * - * @param collector the removed rules will be added to this collection + * @param collector + * the removed rules will be added to this collection */ public void removeDysfunctionalRules(Collection collector) { Iterator iter = rules.iterator(); @@ -312,279 +618,4 @@ public class RuleSet implements ChecksumAware { public long getChecksum() { return checksum; } - - /* package */ static class RuleSetBuilder { - private final List rules = new ArrayList<>(); - private final List excludePatterns = new ArrayList<>(0); - private final List includePatterns = new ArrayList<>(0); - private final long checksum; - public String description = ""; - public String name = ""; - public String fileName; - - /* package */ RuleSetBuilder(final long checksum) { - this.checksum = checksum; - } - - /** - * Copy constructor. Takes the same checksum as the original ruleset. - */ - /* package */ RuleSetBuilder(final RuleSet original) { - checksum = original.getChecksum(); - this.withName(original.getName()) - .withDescription(original.getDescription()) - .withFileName(original.getFileName()) - .setExcludePatterns(original.getExcludePatterns()) - .setIncludePatterns(original.getIncludePatterns()); - addRuleSet(original); - } - - /** - * Add a new rule to this ruleset. Note that this method does not check - * for duplicates. - * - * @param rule the rule to be added - * @return The same builder, for a fluid programming interface - */ - public RuleSetBuilder addRule(final Rule rule) { - if (rule == null) { - throw new IllegalArgumentException(MISSING_RULE); - } - rules.add(rule); - return this; - } - - /** - * Adds a rule. If a rule with the same name and language already - * existed before in the ruleset, then the new rule will replace it. - * This makes sure that the rule configured is overridden. - * - * @param rule the new rule to add - * @return The same builder, for a fluid programming interface - */ - public RuleSetBuilder addRuleReplaceIfExists(final Rule rule) { - if (rule == null) { - throw new IllegalArgumentException(MISSING_RULE); - } - - for (final Iterator it = rules.iterator(); it.hasNext();) { - final Rule r = it.next(); - if (r.getName().equals(rule.getName()) && r.getLanguage() == rule.getLanguage()) { - it.remove(); - } - } - addRule(rule); - return this; - } - - /** - * Only adds a rule to the ruleset if no rule with the same name for the - * same language was added before, so that the existent rule - * configuration won't be overridden. - * - * @param rule the new rule to add - * @return The same builder, for a fluid programming interface - */ - public RuleSetBuilder addRuleIfNotExists(final Rule rule) { - if (rule == null) { - throw new IllegalArgumentException(MISSING_RULE); - } - - boolean exists = false; - for (final Rule r : rules) { - if (r.getName().equals(rule.getName()) && r.getLanguage() == rule.getLanguage()) { - exists = true; - break; - } - } - if (!exists) { - addRule(rule); - } - return this; - } - - /** - * Add a new rule by reference to this ruleset. - * - * @param ruleSetFileName the ruleset which contains the rule - * @param rule the rule to be added - * @return The same builder, for a fluid programming interface - */ - public RuleSetBuilder addRuleByReference(final String ruleSetFileName, final Rule rule) { - if (StringUtil.isEmpty(ruleSetFileName)) { - throw new RuntimeException( - "Adding a rule by reference is not allowed with an empty rule set file name."); - } - if (rule == null) { - throw new IllegalArgumentException("Cannot add a null rule reference to a RuleSet"); - } - final RuleReference ruleReference; - if (rule instanceof RuleReference) { - ruleReference = (RuleReference) rule; - } else { - final RuleSetReference ruleSetReference = new RuleSetReference(); - ruleSetReference.setRuleSetFileName(ruleSetFileName); - ruleReference = new RuleReference(); - ruleReference.setRule(rule); - ruleReference.setRuleSetReference(ruleSetReference); - } - rules.add(ruleReference); - return this; - } - - /** - * Add all rules of a whole RuleSet to this RuleSet - * - * @param ruleSet the RuleSet to add - * @return The same builder, for a fluid programming interface - */ - public RuleSetBuilder addRuleSet(final RuleSet ruleSet) { - rules.addAll(rules.size(), ruleSet.getRules()); - return this; - } - - /** - * Add all rules by reference from one RuleSet to this RuleSet. The - * rules can be added as individual references, or collectively as an - * all rule reference. - * - * @param ruleSet the RuleSet to add - * @param allRules true if the ruleset should be added - * collectively or false to add individual - * references for each rule. - * @return The same builder, for a fluid programming interface - */ - public RuleSetBuilder addRuleSetByReference(final RuleSet ruleSet, final boolean allRules) { - return addRuleSetByReference(ruleSet, allRules, (String[]) null); - } - - /** - * Add all rules by reference from one RuleSet to this RuleSet. The - * rules can be added as individual references, or collectively as an - * all rule reference. - * - * @param ruleSet the RuleSet to add - * @param allRules true if the ruleset should be added - * collectively or false to add individual - * references for each rule. - * @param excludes names of the rules that should be excluded. - * @return The same builder, for a fluid programming interface - */ - public RuleSetBuilder addRuleSetByReference(final RuleSet ruleSet, final boolean allRules, - final String... excludes) { - if (StringUtil.isEmpty(ruleSet.getFileName())) { - throw new RuntimeException( - "Adding a rule by reference is not allowed with an empty rule set file name."); - } - final RuleSetReference ruleSetReference = new RuleSetReference(ruleSet.getFileName()); - ruleSetReference.setAllRules(allRules); - if (excludes != null) { - ruleSetReference.setExcludes(new HashSet<>(Arrays.asList(excludes))); - } - for (final Rule rule : ruleSet.getRules()) { - final RuleReference ruleReference = new RuleReference(rule, ruleSetReference); - rules.add(ruleReference); - } - return this; - } - - /** - * Adds a new file exclusion pattern. - * - * @param aPattern the pattern - * @return The same builder, for a fluid programming interface - */ - public RuleSetBuilder addExcludePattern(final String aPattern) { - if (!excludePatterns.contains(aPattern)) { - excludePatterns.add(aPattern); - } - return this; - } - - /** - * Adds new file exclusion patterns. - * - * @param someExcludePatterns the patterns - * @return The same builder, for a fluid programming interface - */ - public RuleSetBuilder addExcludePatterns(final Collection someExcludePatterns) { - CollectionUtil.addWithoutDuplicates(someExcludePatterns, excludePatterns); - return this; - } - - /** - * Replaces the existing exclusion patterns with the given patterns. - * - * @param theExcludePatterns the new patterns - */ - public RuleSetBuilder setExcludePatterns(final Collection theExcludePatterns) { - if (!excludePatterns.equals(theExcludePatterns)) { - excludePatterns.clear(); - CollectionUtil.addWithoutDuplicates(theExcludePatterns, excludePatterns); - } - return this; - } - - /** - * Adds new inclusion patterns. - * - * @param someIncludePatterns the patterns - * @return The same builder, for a fluid programming interface - */ - public RuleSetBuilder addIncludePatterns(final Collection someIncludePatterns) { - CollectionUtil.addWithoutDuplicates(someIncludePatterns, includePatterns); - return this; - } - - /** - * Replaces the existing inclusion patterns with the given patterns. - * - * @param theIncludePatterns the new patterns - * @return The same builder, for a fluid programming interface - */ - public RuleSetBuilder setIncludePatterns(final Collection theIncludePatterns) { - if (!includePatterns.equals(theIncludePatterns)) { - includePatterns.clear(); - CollectionUtil.addWithoutDuplicates(theIncludePatterns, includePatterns); - } - - return this; - } - - /** - * Adds a new inclusion pattern. - * - * @param aPattern the pattern - * @return The same builder, for a fluid programming interface - */ - public RuleSetBuilder addIncludePattern(final String aPattern) { - if (!includePatterns.contains(aPattern)) { - includePatterns.add(aPattern); - } - return this; - } - - public RuleSetBuilder withFileName(final String fileName) { - this.fileName = fileName; - return this; - } - - public RuleSetBuilder withName(final String name) { - this.name = name; - return this; - } - - public RuleSetBuilder withDescription(final String description) { - this.description = description; - return this; - } - - public String getName() { - return name; - } - - public RuleSet build() { - return new RuleSet(this); - } - } }