diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ApexHandler.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ApexHandler.java index 182cac78e0..c71627bb43 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ApexHandler.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ApexHandler.java @@ -15,11 +15,9 @@ import net.sourceforge.pmd.lang.apex.ast.ApexNode; import net.sourceforge.pmd.lang.apex.ast.DumpFacade; import net.sourceforge.pmd.lang.apex.multifile.ApexMultifileVisitorFacade; import net.sourceforge.pmd.lang.apex.rule.ApexRuleViolationFactory; -import net.sourceforge.pmd.lang.ast.xpath.AbstractASTXPathHandler; +import net.sourceforge.pmd.lang.ast.xpath.DefaultASTXPathHandler; import net.sourceforge.pmd.lang.rule.RuleViolationFactory; -import net.sf.saxon.sxpath.IndependentContext; - public class ApexHandler extends AbstractLanguageVersionHandler { @Override @@ -30,13 +28,7 @@ public class ApexHandler extends AbstractLanguageVersionHandler { @Override public XPathHandler getXPathHandler() { - return new AbstractASTXPathHandler() { - public void initialize() { - } - - public void initialize(IndependentContext context) { - } - }; + return new DefaultASTXPathHandler(); } public RuleViolationFactory getRuleViolationFactory() { diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ApexParser.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ApexParser.java index 0ea88cb571..3cbd386de2 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ApexParser.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ApexParser.java @@ -40,8 +40,7 @@ public class ApexParser { Locations.useIndexFactory(); CompilerService.INSTANCE.visitAstFromString(sourceCode, visitor); - Compilation astRoot = visitor.getTopLevel(); - return astRoot; + return visitor.getTopLevel(); } public ApexNode parse(final Reader reader) { @@ -55,8 +54,7 @@ public class ApexParser { throw new ParseException("Couldn't parse the source - there is not root node - Syntax Error??"); } - ApexNode tree = treeBuilder.build(astRoot); - return tree; + return treeBuilder.build(astRoot); } catch (IOException e) { throw new ParseException(e); } diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ApexQualifiedName.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ApexQualifiedName.java index 9f1cc8eb05..0757b833f6 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ApexQualifiedName.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/ApexQualifiedName.java @@ -20,7 +20,7 @@ import apex.jorje.semantic.symbol.type.TypeInfo; * * @author Clément Fournier */ -public class ApexQualifiedName implements QualifiedName { +public final class ApexQualifiedName implements QualifiedName { private final String nameSpace; @@ -155,7 +155,7 @@ public class ApexQualifiedName implements QualifiedName { List paramTypes = node.getNode().getMethodInfo().getParameterTypes(); - if (paramTypes.size() > 0) { + if (!paramTypes.isEmpty()) { sb.append(paramTypes.get(0).getApexName()); for (int i = 1; i < paramTypes.size(); i++) { diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/EmptySymbolProvider.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/EmptySymbolProvider.java index f91a57fe30..3a01f8925b 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/EmptySymbolProvider.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/EmptySymbolProvider.java @@ -29,11 +29,11 @@ import apex.jorje.semantic.symbol.type.TypeInfo; /** * @author jspagnola */ -public class EmptySymbolProvider implements SymbolProvider { +public final class EmptySymbolProvider implements SymbolProvider { private static final EmptySymbolProvider INSTANCE = new EmptySymbolProvider(); - EmptySymbolProvider() { + private EmptySymbolProvider() { } public static EmptySymbolProvider get() { diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/TestQueryValidators.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/TestQueryValidators.java index 96ee968a92..5396c5bb34 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/TestQueryValidators.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/TestQueryValidators.java @@ -34,6 +34,7 @@ import apex.jorje.semantic.symbol.resolver.SymbolResolver; * * @author jspagnola */ +@SuppressWarnings("PMD.MissingStaticMethodInNonInstantiatableClass") // this class provides utility classes public final class TestQueryValidators { private TestQueryValidators() { diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/multifile/ApexProjectMirror.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/multifile/ApexProjectMirror.java index 1ec5634d9a..3283b6de55 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/multifile/ApexProjectMirror.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/multifile/ApexProjectMirror.java @@ -16,14 +16,13 @@ import net.sourceforge.pmd.lang.apex.metrics.signature.ApexOperationSigMask; * * @author Clément Fournier */ -class ApexProjectMirror implements ApexSignatureMatcher { +final class ApexProjectMirror implements ApexSignatureMatcher { static final ApexProjectMirror INSTANCE = new ApexProjectMirror(); private final Map classes = new HashMap<>(); - ApexProjectMirror() { - + private ApexProjectMirror() { } diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/AbstractStatisticalApexRule.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/AbstractStatisticalApexRule.java index facfe0f7e5..0709b52054 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/AbstractStatisticalApexRule.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/AbstractStatisticalApexRule.java @@ -21,7 +21,7 @@ public abstract class AbstractStatisticalApexRule extends AbstractApexRule imple } public Object[] getViolationParameters(DataPoint point) { - return null; + return new Object[0]; } @Override diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/ApexRuleViolation.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/ApexRuleViolation.java index 56df612f46..fd44350f5d 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/ApexRuleViolation.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/ApexRuleViolation.java @@ -22,6 +22,7 @@ import net.sourceforge.pmd.lang.rule.ParametricRuleViolation; * * @param */ +@SuppressWarnings("PMD.UseUtilityClass") // we inherit non-static methods... public class ApexRuleViolation extends ParametricRuleViolation { public ApexRuleViolation(Rule rule, RuleContext ctx, Node node, String message, int beginLine, int endLine) { diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexUnitTestClassShouldHaveAssertsRule.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexUnitTestClassShouldHaveAssertsRule.java index 5976bd92d5..76e6023fd6 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexUnitTestClassShouldHaveAssertsRule.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/bestpractices/ApexUnitTestClassShouldHaveAssertsRule.java @@ -7,6 +7,7 @@ package net.sourceforge.pmd.lang.apex.rule.bestpractices; import java.util.ArrayList; import java.util.HashSet; import java.util.List; +import java.util.Locale; import java.util.Set; import net.sourceforge.pmd.lang.apex.ast.ASTBlockStatement; @@ -51,7 +52,7 @@ public class ApexUnitTestClassShouldHaveAssertsRule extends AbstractApexUnitTest boolean isAssertFound = false; for (final ASTMethodCallExpression methodCallExpression : methodCalls) { - if (ASSERT_METHODS.contains(methodCallExpression.getFullMethodName().toLowerCase())) { + if (ASSERT_METHODS.contains(methodCallExpression.getFullMethodName().toLowerCase(Locale.ROOT))) { isAssertFound = true; break; } diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/codestyle/VariableNamingConventionsRule.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/codestyle/VariableNamingConventionsRule.java index 5ad80d822b..0961401fc4 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/codestyle/VariableNamingConventionsRule.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/codestyle/VariableNamingConventionsRule.java @@ -8,6 +8,7 @@ import static apex.jorje.semantic.symbol.type.ModifierTypeInfos.FINAL; import static apex.jorje.semantic.symbol.type.ModifierTypeInfos.STATIC; import java.util.List; +import java.util.Locale; import net.sourceforge.pmd.lang.apex.ast.ASTField; import net.sourceforge.pmd.lang.apex.ast.ASTParameter; @@ -152,7 +153,7 @@ public class VariableNamingConventionsRule extends AbstractApexRule { // Static finals should be uppercase if (isStatic && isFinal) { - if (!varName.equals(varName.toUpperCase())) { + if (!varName.equals(varName.toUpperCase(Locale.ROOT))) { addViolationWithMessage(data, node, "Variables that are final and static should be all capitals, ''{0}'' is not all capitals.", new Object[] { varName }); diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/design/AbstractNcssCountRule.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/design/AbstractNcssCountRule.java index 382585c833..a20f72307f 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/design/AbstractNcssCountRule.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/design/AbstractNcssCountRule.java @@ -108,9 +108,7 @@ public abstract class AbstractNcssCountRule extends AbstractStatisticalApexRule @Override public Object visit(ASTIfBlockStatement node, Object data) { - - Integer lineCount = countNodeChildren(node, data); - return lineCount; + return countNodeChildren(node, data); } @Override diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/security/ApexCRUDViolationRule.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/security/ApexCRUDViolationRule.java index 37328bdb0b..7e208cd8cf 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/security/ApexCRUDViolationRule.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/security/ApexCRUDViolationRule.java @@ -10,6 +10,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.WeakHashMap; @@ -47,6 +48,7 @@ import apex.jorje.data.Identifier; import apex.jorje.data.ast.TypeRef; import apex.jorje.data.ast.TypeRefs.ArrayTypeRef; import apex.jorje.data.ast.TypeRefs.ClassTypeRef; +import com.google.common.base.Objects; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ListMultimap; @@ -188,7 +190,7 @@ public class ApexCRUDViolationRule extends AbstractApexRule { } sb.deleteCharAt(sb.length() - 1); - switch (sb.toString().toLowerCase()) { + switch (sb.toString().toLowerCase(Locale.ROOT)) { case "list": case "map": addParametersToMapping(node, typeArgs); @@ -244,12 +246,13 @@ public class ApexCRUDViolationRule extends AbstractApexRule { } private void addVariableToMapping(final String variableName, final String type) { - switch (type.toLowerCase()) { + switch (type.toLowerCase(Locale.ROOT)) { case "list": case "map": - return; + break; default: varToTypeMapping.put(variableName, getSimpleType(type)); + break; } } @@ -421,7 +424,7 @@ public class ApexCRUDViolationRule extends AbstractApexRule { } AbstractApexNode match = n.getFirstDescendantOfType(self.getClass()); - if (match == self) { + if (Objects.equal(match, self)) { break; } ASTMethodCallExpression methodCall = n.getFirstDescendantOfType(ASTMethodCallExpression.class); @@ -436,7 +439,7 @@ public class ApexCRUDViolationRule extends AbstractApexRule { private void mapCallToMethodDecl(final AbstractApexNode self, final Set innerMethodCalls, final List nodes) { for (ASTMethodCallExpression node : nodes) { - if (node == self) { + if (Objects.equal(node, self)) { break; } diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/security/ApexSOQLInjectionRule.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/security/ApexSOQLInjectionRule.java index 01eb4e7403..ced42d6b7e 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/security/ApexSOQLInjectionRule.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/security/ApexSOQLInjectionRule.java @@ -7,6 +7,7 @@ package net.sourceforge.pmd.lang.apex.rule.security; import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.regex.Pattern; @@ -108,7 +109,7 @@ public class ApexSOQLInjectionRule extends AbstractApexRule { private void findSafeVariablesInSignature(ASTMethod m) { List parameters = m.getNode().getMethodInfo().getParameters(); for (Parameter p : parameters) { - switch (p.getType().getApexName().toLowerCase()) { + switch (p.getType().getApexName().toLowerCase(Locale.ROOT)) { case ID: case INTEGER: case BOOLEAN: @@ -159,7 +160,7 @@ public class ApexSOQLInjectionRule extends AbstractApexRule { if (node instanceof ASTVariableDeclaration) { VariableDeclaration o = (VariableDeclaration) node.getNode(); - switch (o.getLocalInfo().getType().getApexName().toLowerCase()) { + switch (o.getLocalInfo().getType().getApexName().toLowerCase(Locale.ROOT)) { case INTEGER: case ID: case BOOLEAN: diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/security/ApexSuggestUsingNamedCredRule.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/security/ApexSuggestUsingNamedCredRule.java index 234a023d16..ed17d50002 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/security/ApexSuggestUsingNamedCredRule.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/security/ApexSuggestUsingNamedCredRule.java @@ -67,7 +67,7 @@ public class ApexSuggestUsingNamedCredRule extends AbstractApexRule { private void findFieldLiterals(final ASTField fDecl) { Object f = fDecl.getNode().getFieldInfo().getValue(); - if (f != null && f instanceof String) { + if (f instanceof String) { final String fieldValue = (String) f; if (AUTHORIZATION.equalsIgnoreCase(fieldValue)) { listOfAuthorizationVariables.add(Helper.getFQVariableName(fDecl)); diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/security/Helper.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/security/Helper.java index d56a31c510..7a21a1b825 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/security/Helper.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/security/Helper.java @@ -6,6 +6,7 @@ package net.sourceforge.pmd.lang.apex.rule.security; import java.util.Arrays; import java.util.List; +import java.util.Locale; import net.sourceforge.pmd.lang.apex.ast.ASTDmlDeleteStatement; import net.sourceforge.pmd.lang.apex.ast.ASTDmlInsertStatement; @@ -43,7 +44,7 @@ import apex.jorje.semantic.ast.statement.VariableDeclaration; * */ public final class Helper { - protected static final String ANY_METHOD = "*"; + static final String ANY_METHOD = "*"; private Helper() { throw new AssertionError("Can't instantiate helper classes"); @@ -58,22 +59,14 @@ public final class Helper { } final String className = node.getNode().getDefiningType().getApexName(); - if (className.endsWith("Test")) { - return true; - } - - return false; + return className.endsWith("Test"); } static boolean foundAnySOQLorSOSL(final ApexNode node) { final List dmlSoqlExpression = node.findDescendantsOfType(ASTSoqlExpression.class); final List dmlSoslExpression = node.findDescendantsOfType(ASTSoslExpression.class); - if (dmlSoqlExpression.isEmpty() && dmlSoslExpression.isEmpty()) { - return false; - } - - return true; + return !dmlSoqlExpression.isEmpty() || !dmlSoslExpression.isEmpty(); } /** @@ -93,27 +86,17 @@ public final class Helper { final List dmlInsertStatement = node.findDescendantsOfType(ASTDmlInsertStatement.class); final List dmlDeleteStatement = node.findDescendantsOfType(ASTDmlDeleteStatement.class); - if (dmlUpsertStatement.isEmpty() && dmlUpdateStatement.isEmpty() && dmlUndeleteStatement.isEmpty() - && dmlMergeStatement.isEmpty() && dmlInsertStatement.isEmpty() && dmlDeleteStatement.isEmpty()) { - return false; - } - - return true; + return !dmlUpsertStatement.isEmpty() || !dmlUpdateStatement.isEmpty() || !dmlUndeleteStatement.isEmpty() + || !dmlMergeStatement.isEmpty() || !dmlInsertStatement.isEmpty() || !dmlDeleteStatement.isEmpty(); } static boolean isMethodName(final ASTMethodCallExpression methodNode, final String className, final String methodName) { final ASTReferenceExpression reference = methodNode.getFirstChildOfType(ASTReferenceExpression.class); - if (reference != null && reference.getNode().getNames().size() == 1) { - if (reference.getNode().getNames().get(0).getValue().equalsIgnoreCase(className)) { - if (methodName.equals(ANY_METHOD) || isMethodName(methodNode, methodName)) { - return true; - } - } - } - - return false; + return reference != null && reference.getNode().getNames().size() == 1 + && reference.getNode().getNames().get(0).getValue().equalsIgnoreCase(className) + && (methodName.equals(ANY_METHOD) || isMethodName(methodNode, methodName)); } static boolean isMethodName(final ASTMethodCallExpression m, final String methodName) { @@ -234,7 +217,7 @@ public final class Helper { } } - switch (sb.toString().toLowerCase()) { + switch (sb.toString().toLowerCase(Locale.ROOT)) { case "queueable": case "database.batchable": case "installhandler": diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/PMD.java b/pmd-core/src/main/java/net/sourceforge/pmd/PMD.java index 1fdcd13796..1375afdf84 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/PMD.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/PMD.java @@ -226,6 +226,7 @@ public class PMD { @Override public void metricAdded(Metric metric) { + // ignored - not needed for counting violations } }); 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 6b294a7930..1b0574b8ea 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSet.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSet.java @@ -225,9 +225,7 @@ public class RuleSet implements ChecksumAware { ruleReference = (RuleReference) rule; } else { final RuleSetReference ruleSetReference = new RuleSetReference(ruleSetFileName); - ruleReference = new RuleReference(); - ruleReference.setRule(rule); - ruleReference.setRuleSetReference(ruleSetReference); + ruleReference = new RuleReference(rule, ruleSetReference); } rules.add(ruleReference); return this; diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java index e9cd461253..ec47ae5a54 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetFactory.java @@ -503,9 +503,7 @@ public class RuleSetFactory { for (Rule rule : otherRuleSet.getRules()) { excludedRulesCheck.remove(rule.getName()); if (!ruleSetReference.getExcludes().contains(rule.getName())) { - RuleReference ruleReference = new RuleReference(); - ruleReference.setRuleSetReference(ruleSetReference); - ruleReference.setRule(rule); + RuleReference ruleReference = new RuleReference(rule, ruleSetReference); // override the priority if (priority != null) { ruleReference.setPriority(RulePriority.valueOf(Integer.parseInt(priority))); @@ -644,8 +642,7 @@ public class RuleSetFactory { RuleSetReference ruleSetReference = new RuleSetReference(otherRuleSetReferenceId.getRuleSetFileName(), false); - RuleReference ruleReference = new RuleFactory().decorateRule(referencedRule, ruleElement); - ruleReference.setRuleSetReference(ruleSetReference); + RuleReference ruleReference = new RuleFactory().decorateRule(referencedRule, ruleSetReference, ruleElement); if (warnDeprecated && ruleReference.isDeprecated()) { if (LOG.isLoggable(Level.WARNING)) { diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetReferenceId.java b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetReferenceId.java index 87d916a6ad..e5ee6c47cc 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetReferenceId.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetReferenceId.java @@ -293,11 +293,7 @@ public class RuleSetReferenceId { return false; } - if (stripped.startsWith("http://") || stripped.startsWith("https://")) { - return true; - } - - return false; + return stripped.startsWith("http://") || stripped.startsWith("https://"); } private static boolean isValidUrl(String name) { diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetWriter.java b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetWriter.java index 714ec7082b..3ec55cc3e6 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetWriter.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSetWriter.java @@ -170,8 +170,7 @@ public class RuleSetWriter { if (ruleSetReference.isAllRules()) { if (!ruleSetFileNames.contains(ruleSetReference.getRuleSetFileName())) { ruleSetFileNames.add(ruleSetReference.getRuleSetFileName()); - Element ruleSetReferenceElement = createRuleSetReferenceElement(ruleSetReference); - return ruleSetReferenceElement; + return createRuleSetReferenceElement(ruleSetReference); } else { return null; } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSets.java b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSets.java index ada471db07..9016830858 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/RuleSets.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/RuleSets.java @@ -35,8 +35,9 @@ public class RuleSets { * Public constructor. */ public RuleSets() { + // default constructor } - + /** * Copy constructor. Deep copies RuleSets. * @param ruleSets The RuleSets to copy. @@ -54,7 +55,6 @@ public class RuleSets { * the RuleSet */ public RuleSets(RuleSet ruleSet) { - this(); addRuleSet(ruleSet); } @@ -77,7 +77,7 @@ public class RuleSets { * @return RuleSet[] */ public RuleSet[] getAllRuleSets() { - return ruleSets.toArray(new RuleSet[ruleSets.size()]); + return ruleSets.toArray(new RuleSet[0]); } public Iterator getRuleSetsIterator() { diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/benchmark/Benchmarker.java b/pmd-core/src/main/java/net/sourceforge/pmd/benchmark/Benchmarker.java index 708e230275..8dd9762681 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/benchmark/Benchmarker.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/benchmark/Benchmarker.java @@ -40,7 +40,7 @@ import net.sourceforge.pmd.util.datasource.DataSource; * * */ -public class Benchmarker { +public final class Benchmarker { private static final Map BENCHMARKS_BY_NAME = new HashMap<>(); diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/cli/PMDCommandLineInterface.java b/pmd-core/src/main/java/net/sourceforge/pmd/cli/PMDCommandLineInterface.java index 64362a33de..fc45da1b19 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/cli/PMDCommandLineInterface.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/cli/PMDCommandLineInterface.java @@ -20,7 +20,7 @@ import com.beust.jcommander.ParameterException; * @author Romain Pelisse <belaran@gmail.com> * */ -public class PMDCommandLineInterface { +public final class PMDCommandLineInterface { public static final String PROG_NAME = "pmd"; diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/AbstractTokenizer.java b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/AbstractTokenizer.java index 296deb8347..7309fd2f4c 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/AbstractTokenizer.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/AbstractTokenizer.java @@ -5,6 +5,7 @@ package net.sourceforge.pmd.cpd; import java.util.List; +import java.util.Locale; /** * @@ -51,7 +52,7 @@ public abstract class AbstractTokenizer implements Tokenizer { loc = getTokenFromLine(token, loc); if (token.length() > 0 && !isIgnorableString(token.toString())) { if (downcaseString) { - token = new StringBuilder(token.toString().toLowerCase()); + token = new StringBuilder(token.toString().toLowerCase(Locale.ROOT)); } // need to re-think how to link this // if ( CPD.debugEnable ) { diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDCommandLineInterface.java b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDCommandLineInterface.java index 8eaf7ff75e..5a2f9d80fa 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDCommandLineInterface.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDCommandLineInterface.java @@ -23,7 +23,7 @@ import net.sourceforge.pmd.util.database.DBURI; import com.beust.jcommander.JCommander; import com.beust.jcommander.ParameterException; -public class CPDCommandLineInterface { +public final class CPDCommandLineInterface { private static final Logger LOGGER = Logger.getLogger(CPDCommandLineInterface.class.getName()); private static final int DUPLICATE_CODE_FOUND = 4; diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDNullListener.java b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDNullListener.java index dd0a132bf1..64b6060166 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDNullListener.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/CPDNullListener.java @@ -9,9 +9,11 @@ import java.io.File; public class CPDNullListener implements CPDListener { @Override public void addedFile(int fileCount, File file) { + // does nothing - override it if necessary } @Override public void phaseUpdate(int phase) { + // does nothing - override it if necessary } } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/GUI.java b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/GUI.java index 62b6f53f96..3f568c430f 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/GUI.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/GUI.java @@ -58,7 +58,7 @@ import javax.swing.SwingConstants; import javax.swing.Timer; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; -import javax.swing.event.TableModelListener; +import javax.swing.table.AbstractTableModel; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.JTableHeader; import javax.swing.table.TableColumn; @@ -138,7 +138,7 @@ public class GUI implements CPDListener { @Override public String[] extensions() { List exts = lang.getExtensions(); - return exts.toArray(new String[exts.size()]); + return exts.toArray(new String[0]); } @Override @@ -748,16 +748,16 @@ public class GUI implements CPDListener { return sb.toString(); } - private interface SortingTableModel extends TableModel { - int sortColumn(); + private abstract class SortingTableModel extends AbstractTableModel { + abstract int sortColumn(); - void sortColumn(int column); + abstract void sortColumn(int column); - boolean sortDescending(); + abstract boolean sortDescending(); - void sortDescending(boolean flag); + abstract void sortDescending(boolean flag); - void sort(Comparator comparator); + abstract void sort(Comparator comparator); } private TableModel tableModelFrom(final List items) { @@ -804,23 +804,11 @@ public class GUI implements CPDListener { return Object.class; } - @Override - public void setValueAt(Object aValue, int rowIndex, int columnIndex) { - } - @Override public String getColumnName(int i) { return matchColumns[i].label(); } - @Override - public void addTableModelListener(TableModelListener l) { - } - - @Override - public void removeTableModelListener(TableModelListener l) { - } - @Override public int sortColumn() { return sortColumn; diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/LanguageFactory.java b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/LanguageFactory.java index 66559a5326..336a49a00c 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/LanguageFactory.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/LanguageFactory.java @@ -72,7 +72,7 @@ public final class LanguageFactory { if (BY_EXTENSION.equals(language)) { implementation = instance.getLanguageByExtension(properties.getProperty(EXTENSION)); } else { - implementation = instance.languages.get(instance.languageAliases(language).toLowerCase()); + implementation = instance.languages.get(instance.languageAliases(language).toLowerCase(Locale.ROOT)); } if (implementation == null) { // No proper implementation diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/dcd/ClassLoaderUtil.java b/pmd-core/src/main/java/net/sourceforge/pmd/dcd/ClassLoaderUtil.java index 80ec239faf..9534861216 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/dcd/ClassLoaderUtil.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/dcd/ClassLoaderUtil.java @@ -12,7 +12,7 @@ import java.lang.reflect.Method; * ClassLoader utilities. Useful for extracting additional details from a class * hierarchy beyond the basic standard Java Reflection APIs. */ -public class ClassLoaderUtil { +public final class ClassLoaderUtil { public static final String CLINIT = ""; diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/dcd/DCD.java b/pmd-core/src/main/java/net/sourceforge/pmd/dcd/DCD.java index eddc9a266e..a4a0de4a8d 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/dcd/DCD.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/dcd/DCD.java @@ -46,7 +46,7 @@ import net.sourceforge.pmd.util.filter.Filters; * * @author Ryan Gustafson <ryan.gustafson@gmail.com> */ -public class DCD { +public final class DCD { // // TODO Implement the direct users, indirect users, and dead code // candidate sets. Use the pmd.util.filter.Filter APIs. Need to come up diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/dcd/asm/TypeSignatureVisitor.java b/pmd-core/src/main/java/net/sourceforge/pmd/dcd/asm/TypeSignatureVisitor.java index 1d34c106a8..0d95e544c6 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/dcd/asm/TypeSignatureVisitor.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/dcd/asm/TypeSignatureVisitor.java @@ -94,7 +94,7 @@ public class TypeSignatureVisitor extends SignatureVisitor { throw new RuntimeException(); } if (parameterTypes != null) { - return parameterTypes.toArray(new Class[parameterTypes.size()]); + return parameterTypes.toArray(new Class[0]); } else { return null; } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/LanguageFilenameFilter.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/LanguageFilenameFilter.java index 7a7ca02169..6635ac6663 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/LanguageFilenameFilter.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/LanguageFilenameFilter.java @@ -8,6 +8,7 @@ import java.io.File; import java.io.FilenameFilter; import java.util.Collections; import java.util.List; +import java.util.Locale; import java.util.Set; /** @@ -52,7 +53,7 @@ public class LanguageFilenameFilter implements FilenameFilter { return false; } - String extension = name.substring(1 + lastDotIndex).toUpperCase(); + String extension = name.substring(1 + lastDotIndex).toUpperCase(Locale.ROOT); for (Language language : languages) { for (String ext : language.getExtensions()) { if (extension.equalsIgnoreCase(ext)) { diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/XPathHandler.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/XPathHandler.java index c6407e1a20..d3d7ca82b6 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/XPathHandler.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/XPathHandler.java @@ -19,10 +19,12 @@ public interface XPathHandler { XPathHandler DUMMY = new XPathHandler() { @Override public void initialize() { + // empty handler - does nothing } @Override public void initialize(IndependentContext context) { + // empty handler - does nothing } @Override diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/AttributeAxisIterator.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/AttributeAxisIterator.java index 570631d35a..27a4800b7d 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/AttributeAxisIterator.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/AttributeAxisIterator.java @@ -65,7 +65,7 @@ public class AttributeAxisIterator implements Iterator { postFilter.add(new MethodWrapper(element)); } } - methodCache.putIfAbsent(contextNode.getClass(), postFilter.toArray(new MethodWrapper[postFilter.size()])); + methodCache.putIfAbsent(contextNode.getClass(), postFilter.toArray(new MethodWrapper[0])); } this.methodWrappers = methodCache.get(contextNode.getClass()); diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/DefaultASTXPathHandler.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/DefaultASTXPathHandler.java new file mode 100644 index 0000000000..4bf033c437 --- /dev/null +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/DefaultASTXPathHandler.java @@ -0,0 +1,19 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.ast.xpath; + +import net.sf.saxon.sxpath.IndependentContext; + +public class DefaultASTXPathHandler extends AbstractASTXPathHandler { + @Override + public void initialize() { + // override if needed + } + + @Override + public void initialize(IndependentContext context) { + // override if needed + } +} diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/metrics/MetricKeyUtil.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/metrics/MetricKeyUtil.java index d5391b3ae0..14cae5c13c 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/metrics/MetricKeyUtil.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/metrics/MetricKeyUtil.java @@ -14,7 +14,7 @@ import net.sourceforge.pmd.lang.ast.Node; * @author Clément Fournier * @since 6.0.0 */ -public class MetricKeyUtil { +public final class MetricKeyUtil { private MetricKeyUtil() { diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/metrics/ParameterizedMetricKey.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/metrics/ParameterizedMetricKey.java index f916a07af5..e0a8dbb799 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/metrics/ParameterizedMetricKey.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/metrics/ParameterizedMetricKey.java @@ -63,6 +63,7 @@ public final class ParameterizedMetricKey { * * @return An instance of parameterized metric key corresponding to the parameters */ + @SuppressWarnings("PMD.SingletonClassReturningNewInstance") public static ParameterizedMetricKey getInstance(MetricKey key, MetricOptions options) { ParameterizedMetricKey tmp = new ParameterizedMetricKey<>(key, options); if (!POOL.containsKey(tmp)) { diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/MockRule.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/MockRule.java index a4501f9d39..8c0e15ea9d 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/MockRule.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/MockRule.java @@ -41,5 +41,6 @@ public class MockRule extends AbstractRule { @Override public void apply(List nodes, RuleContext ctx) { + // the mock rule does nothing. Usually you would start here to analyze the AST. } } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/RuleReference.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/RuleReference.java index 2871a7b610..a8b8aaa959 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/RuleReference.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/RuleReference.java @@ -45,9 +45,18 @@ public class RuleReference extends AbstractDelegateRule { private static final List> EMPTY_DESCRIPTORS = Collections.emptyList(); + @Deprecated // to be removed with PMD 7.0.0 + // when creating a rule reference, always provide the rule and the ruleset, see + // the constructor RuleReference(Rule, RuleSetReference) public RuleReference() { + // default constructor } + /** + * + * @param theRule the referenced rule + * @param theRuleSetReference the rule set, where the rule is defined + */ public RuleReference(Rule theRule, RuleSetReference theRuleSetReference) { setRule(theRule); ruleSetReference = theRuleSetReference; @@ -317,11 +326,7 @@ public class RuleReference extends AbstractDelegateRule { } } - if (!getRule().usesDefaultValues()) { - return false; - } - - return true; + return getRule().usesDefaultValues(); } @Override diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/xpath/Initializer.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/xpath/Initializer.java index bb69fd43f8..54d28699c6 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/xpath/Initializer.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/xpath/Initializer.java @@ -17,7 +17,7 @@ import net.sf.saxon.sxpath.IndependentContext; * Initialization should be performed before any XPath related operations are * performed. */ -public class Initializer { +public final class Initializer { private Initializer() { } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/xpath/PMDFunctions.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/xpath/PMDFunctions.java index 2619c3e1e5..394bd230d1 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/xpath/PMDFunctions.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/xpath/PMDFunctions.java @@ -4,7 +4,7 @@ package net.sourceforge.pmd.lang.xpath; -public class PMDFunctions { +public final class PMDFunctions { private PMDFunctions() { } public static boolean matches(String s, String pattern1) { diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/processor/MultiThreadProcessor.java b/pmd-core/src/main/java/net/sourceforge/pmd/processor/MultiThreadProcessor.java index 68a68b6f71..9855299e62 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/processor/MultiThreadProcessor.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/processor/MultiThreadProcessor.java @@ -12,7 +12,6 @@ import java.util.concurrent.ExecutorCompletionService; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; -import java.util.concurrent.ThreadFactory; import net.sourceforge.pmd.PMDConfiguration; import net.sourceforge.pmd.Report; @@ -24,7 +23,6 @@ import net.sourceforge.pmd.renderers.Renderer; */ public class MultiThreadProcessor extends AbstractPMDProcessor { - private ThreadFactory factory; private ExecutorService executor; private CompletionService completionService; private List> tasks = new ArrayList<>(); @@ -32,8 +30,7 @@ public class MultiThreadProcessor extends AbstractPMDProcessor { public MultiThreadProcessor(final PMDConfiguration configuration) { super(configuration); - factory = new PmdThreadFactory(); - executor = Executors.newFixedThreadPool(configuration.getThreads(), factory); + executor = Executors.newFixedThreadPool(configuration.getThreads(), new PmdThreadFactory()); completionService = new ExecutorCompletionService<>(executor); } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/AbstractAccumulatingRenderer.java b/pmd-core/src/main/java/net/sourceforge/pmd/renderers/AbstractAccumulatingRenderer.java index 9ffdbf50f4..5ffb4e8fd5 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/AbstractAccumulatingRenderer.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/renderers/AbstractAccumulatingRenderer.java @@ -39,6 +39,7 @@ public abstract class AbstractAccumulatingRenderer extends AbstractRenderer { @Override public void startFileAnalysis(DataSource dataSource) { + // does nothing - override if necessary } @Override diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/AbstractIncrementingRenderer.java b/pmd-core/src/main/java/net/sourceforge/pmd/renderers/AbstractIncrementingRenderer.java index 1149891b1c..6e60d9d119 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/AbstractIncrementingRenderer.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/renderers/AbstractIncrementingRenderer.java @@ -47,10 +47,12 @@ public abstract class AbstractIncrementingRenderer extends AbstractRenderer { @Override public void start() throws IOException { + // does nothing - override if necessary } @Override public void startFileAnalysis(DataSource dataSource) { + // does nothing - override if necessary } @Override @@ -85,5 +87,6 @@ public abstract class AbstractIncrementingRenderer extends AbstractRenderer { @Override public void end() throws IOException { + // does nothing - override if necessary } } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/CodeClimateIssue.java b/pmd-core/src/main/java/net/sourceforge/pmd/renderers/CodeClimateIssue.java index aec04f9ab1..7d66f5ba0d 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/CodeClimateIssue.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/renderers/CodeClimateIssue.java @@ -11,7 +11,7 @@ import net.sourceforge.pmd.PMD; * (https://github.com/codeclimate/spec/blob/master/SPEC.md#issues) */ public class CodeClimateIssue { - public final String type = "issue"; + public String type; public String check_name; // SUPPRESS CHECKSTYLE underscore is required per codeclimate format public String description; public Content content; @@ -20,6 +20,10 @@ public class CodeClimateIssue { public String severity; public int remediation_points; // SUPPRESS CHECKSTYLE underscore is required per codeclimate format + public CodeClimateIssue() { + type = "issue"; // the default type for PMD violations when reporting as code climate + } + /** * Location structure */ diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/EmptyRenderer.java b/pmd-core/src/main/java/net/sourceforge/pmd/renderers/EmptyRenderer.java index 5a2c60b16f..bf1207f7e1 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/EmptyRenderer.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/renderers/EmptyRenderer.java @@ -26,17 +26,21 @@ public class EmptyRenderer extends AbstractRenderer { @Override public void start() throws IOException { + // deliberately does nothing } @Override public void startFileAnalysis(DataSource dataSource) { + // deliberately does nothing } @Override public void renderFileReport(Report report) throws IOException { + // deliberately does nothing } @Override public void end() throws IOException { + // deliberately does nothing } } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/RendererFactory.java b/pmd-core/src/main/java/net/sourceforge/pmd/renderers/RendererFactory.java index ab7e2cbdbd..48631ab33f 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/RendererFactory.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/renderers/RendererFactory.java @@ -21,7 +21,7 @@ import net.sourceforge.pmd.properties.PropertyDescriptor; * * @see Renderer */ -public class RendererFactory { +public final class RendererFactory { private static final Logger LOG = Logger.getLogger(RendererFactory.class.getName()); diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/TextRenderer.java b/pmd-core/src/main/java/net/sourceforge/pmd/renderers/TextRenderer.java index 696ee9e780..aaf03fdb11 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/TextRenderer.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/renderers/TextRenderer.java @@ -28,10 +28,6 @@ public class TextRenderer extends AbstractIncrementingRenderer { return "txt"; } - @Override - public void start() throws IOException { - } - @Override public void renderFileViolations(Iterator violations) throws IOException { Writer writer = getWriter(); diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/rules/RuleFactory.java b/pmd-core/src/main/java/net/sourceforge/pmd/rules/RuleFactory.java index bdca4f45a2..65eeee6283 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/rules/RuleFactory.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/rules/RuleFactory.java @@ -25,6 +25,7 @@ import org.w3c.dom.NodeList; import net.sourceforge.pmd.Rule; import net.sourceforge.pmd.RulePriority; +import net.sourceforge.pmd.RuleSetReference; import net.sourceforge.pmd.lang.rule.RuleReference; import net.sourceforge.pmd.properties.PropertyDescriptor; import net.sourceforge.pmd.properties.PropertyDescriptorField; @@ -59,19 +60,19 @@ public class RuleFactory { private static final List REQUIRED_ATTRIBUTES = Collections.unmodifiableList(Arrays.asList(NAME, CLASS)); /** - * Decorates a referenced rule with the metadata that are overriden in the given rule element. + * Decorates a referenced rule with the metadata that are overridden in the given rule element. * *

Declaring a property in the overriding element throws an exception (the property must exist in the referenced * rule). * * @param referencedRule Referenced rule + * @param ruleSetReference the ruleset, where the referenced rule is defined * @param ruleElement Element overriding some metadata about the rule * * @return A rule reference to the referenced rule */ - public RuleReference decorateRule(Rule referencedRule, Element ruleElement) { - RuleReference ruleReference = new RuleReference(); - ruleReference.setRule(referencedRule); + public RuleReference decorateRule(Rule referencedRule, RuleSetReference ruleSetReference, Element ruleElement) { + RuleReference ruleReference = new RuleReference(referencedRule, ruleSetReference); if (ruleElement.hasAttribute(DEPRECATED)) { ruleReference.setDeprecated(Boolean.parseBoolean(ruleElement.getAttribute(DEPRECATED))); diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/util/ClasspathClassLoader.java b/pmd-core/src/main/java/net/sourceforge/pmd/util/ClasspathClassLoader.java index 0fc37fb4a2..d9adf73bc5 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/util/ClasspathClassLoader.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/util/ClasspathClassLoader.java @@ -50,7 +50,7 @@ public class ClasspathClassLoader extends URLClassLoader { // Treat as classpath addClasspathURLs(urls, classpath); } - return urls.toArray(new URL[urls.size()]); + return urls.toArray(new URL[0]); } private static void addClasspathURLs(final List urls, final String classpath) throws MalformedURLException { diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/util/NumericConstants.java b/pmd-core/src/main/java/net/sourceforge/pmd/util/NumericConstants.java index 8f42b4f95c..e472814db8 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/util/NumericConstants.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/util/NumericConstants.java @@ -4,7 +4,7 @@ package net.sourceforge.pmd.util; -public class NumericConstants { +public final class NumericConstants { public static final Integer ZERO = 0; diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/util/StringUtil.java b/pmd-core/src/main/java/net/sourceforge/pmd/util/StringUtil.java index e96e737d16..9a254412d7 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/util/StringUtil.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/util/StringUtil.java @@ -370,7 +370,7 @@ public final class StringUtil { index = str.indexOf(separator, currPos); } list.add(str.substring(currPos)); - return list.toArray(new String[list.size()]); + return list.toArray(new String[0]); } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/util/database/DBMSMetadata.java b/pmd-core/src/main/java/net/sourceforge/pmd/util/database/DBMSMetadata.java index 634c3154f9..4dfde3d00b 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/util/database/DBMSMetadata.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/util/database/DBMSMetadata.java @@ -332,7 +332,6 @@ public class DBMSMetadata { public List getSourceObjectList(List languages, List schemas, List sourceCodeTypes, List sourceCodeNames) { - ResultSet sourceCodeObjects = null; List sourceObjectsList = new ArrayList<>(); List searchLanguages = languages; @@ -388,57 +387,14 @@ public class DBMSMetadata { if (null != returnSourceCodeObjectsStatement) { LOGGER.log(Level.FINE, "Have bespoke returnSourceCodeObjectsStatement from DBURI: \"{0}\"", returnSourceCodeObjectsStatement); - PreparedStatement sourceCodeObjectsStatement = getConnection() - .prepareStatement(returnSourceCodeObjectsStatement); - - for (String language : searchLanguages) { - for (String schema : searchSchemas) { - for (String sourceCodeType : searchSourceCodeTypes) { - for (String sourceCodeName : searchSourceCodeNames) { - sourceCodeObjectsStatement.setString(1, language); - sourceCodeObjectsStatement.setString(2, schema); - sourceCodeObjectsStatement.setString(3, sourceCodeType); - sourceCodeObjectsStatement.setString(4, sourceCodeName); - LOGGER.finer(String.format( - "searching for language=\"%s\", schema=\"%s\", sourceCodeType=\"%s\", sourceCodeNames=\"%s\" ", - language, schema, sourceCodeType, sourceCodeName)); - - /* - * public ResultSet getProcedures(String catalog - * , String schemaPattern , String - * procedureNamePattern) throws SQLException - */ - - sourceCodeObjects = sourceCodeObjectsStatement.executeQuery(); - - /* - * From Javadoc .... Each procedure description - * has the the following columns: PROCEDURE_CAT - * String => procedure catalog (may be null) - * PROCEDURE_SCHEM String => procedure schema - * (may be null) PROCEDURE_NAME String => - * procedure name reserved for future use - * reserved for future use reserved for future - * use REMARKS String => explanatory comment on - * the procedure PROCEDURE_TYPE short => kind of - * procedure: procedureResultUnknown - Cannot - * determine if a return value will be returned - * procedureNoResult - Does not return a return - * value procedureReturnsResult - Returns a - * return value SPECIFIC_NAME String => The name - * which uniquely identifies this procedure - * within its schema. - */ - while (sourceCodeObjects.next()) { - LOGGER.finest(String.format("Found schema=%s,object_type=%s,object_name=%s", - sourceCodeObjects.getString("PROCEDURE_SCHEM"), - sourceCodeObjects.getString("PROCEDURE_TYPE"), - sourceCodeObjects.getString("PROCEDURE_NAME"))); - - sourceObjectsList - .add(new SourceObject(sourceCodeObjects.getString("PROCEDURE_SCHEM"), - sourceCodeObjects.getString("PROCEDURE_TYPE"), - sourceCodeObjects.getString("PROCEDURE_NAME"), null)); + try (PreparedStatement sourceCodeObjectsStatement = getConnection() + .prepareStatement(returnSourceCodeObjectsStatement)) { + for (String language : searchLanguages) { + for (String schema : searchSchemas) { + for (String sourceCodeType : searchSourceCodeTypes) { + for (String sourceCodeName : searchSourceCodeNames) { + sourceObjectsList.addAll(findSourceObjects(sourceCodeObjectsStatement, language, schema, + sourceCodeType, sourceCodeName)); } } } @@ -453,74 +409,7 @@ public class DBMSMetadata { List schemasList = dburi.getSchemasList(); for (String schema : schemasList) { for (String sourceCodeName : dburi.getSourceCodeNamesList()) { - /* - * public ResultSet getProcedures(String catalog , - * String schemaPattern , String procedureNamePattern) - * throws SQLException - */ - sourceCodeObjects = metadata.getProcedures(null, schema, sourceCodeName); - /* - * From Javadoc .... Each procedure description has the - * the following columns: PROCEDURE_CAT String => - * procedure catalog (may be null) PROCEDURE_SCHEM - * String => procedure schema (may be null) - * PROCEDURE_NAME String => procedure name reserved for - * future use reserved for future use reserved for - * future use REMARKS String => explanatory comment on - * the procedure PROCEDURE_TYPE short => kind of - * procedure: procedureResultUnknown - Cannot determine - * if a return value will be returned procedureNoResult - * - Does not return a return value - * procedureReturnsResult - Returns a return value - * SPECIFIC_NAME String => The name which uniquely - * identifies this procedure within its schema. - * - * Oracle getProcedures actually returns these 8 - * columns:- ResultSet "Matched Procedures" has 8 - * columns and contains ... - * [PROCEDURE_CAT,PROCEDURE_SCHEM,PROCEDURE_NAME,NULL, - * NULL,NULL,REMARKS,PROCEDURE_TYPE - * ,null,PHPDEMO,ADD_JOB_HISTORY,null,null,null, - * Standalone procedure or function,1 - * ,FETCHPERFPKG,PHPDEMO,BULKSELECTPRC,null,null,null, - * Packaged function,2 - * ,FETCHPERFPKG,PHPDEMO,BULKSELECTPRC,null,null,null, - * Packaged procedure,1 - * ,null,PHPDEMO,CITY_LIST,null,null,null,Standalone - * procedure or function,1 - * ,null,PHPDEMO,EDDISCOUNT,null,null,null,Standalone - * procedure or function,2 - * ,SELPKG_BA,PHPDEMO,EMPSELBULK,null,null,null,Packaged - * function,2 - * ,SELPKG_BA,PHPDEMO,EMPSELBULK,null,null,null,Packaged - * procedure,1 - * ,INSPKG,PHPDEMO,INSFORALL,null,null,null,Packaged - * procedure,1 - * ,null,PHPDEMO,MYDOFETCH,null,null,null,Standalone - * procedure or function,2 - * ,null,PHPDEMO,MYPROC1,null,null,null,Standalone - * procedure or function,1 - * ,null,PHPDEMO,MYPROC2,null,null,null,Standalone - * procedure or function,1 - * ,null,PHPDEMO,MYXAQUERY,null,null,null,Standalone - * procedure or function,1 - * ,null,PHPDEMO,POLICY_VPDPARTS,null,null,null, - * Standalone procedure or function,2 - * ,FETCHPERFPKG,PHPDEMO,REFCURPRC,null,null,null, - * Packaged procedure,1 - * ,null,PHPDEMO,SECURE_DML,null,null,null,Standalone - * procedure or function,1 ... ] - */ - while (sourceCodeObjects.next()) { - LOGGER.finest(String.format("Located schema=%s,object_type=%s,object_name=%s\n", - sourceCodeObjects.getString("PROCEDURE_SCHEM"), - sourceCodeObjects.getString("PROCEDURE_TYPE"), - sourceCodeObjects.getString("PROCEDURE_NAME"))); - - sourceObjectsList.add(new SourceObject(sourceCodeObjects.getString("PROCEDURE_SCHEM"), - sourceCodeObjects.getString("PROCEDURE_TYPE"), - sourceCodeObjects.getString("PROCEDURE_NAME"), null)); - } + sourceObjectsList.addAll(findSourceObjectFromMetaData(metadata, schema, sourceCodeName)); } } } @@ -532,4 +421,130 @@ public class DBMSMetadata { throw new RuntimeException("Problem collecting list of source code objects", sqle); } } + + private List findSourceObjectFromMetaData(DatabaseMetaData metadata, + String schema, String sourceCodeName) throws SQLException { + List sourceObjectsList = new ArrayList<>(); + /* + * public ResultSet getProcedures(String catalog , + * String schemaPattern , String procedureNamePattern) + * throws SQLException + */ + try (ResultSet sourceCodeObjects = metadata.getProcedures(null, schema, sourceCodeName)) { + /* + * From Javadoc .... Each procedure description has the + * the following columns: PROCEDURE_CAT String => + * procedure catalog (may be null) PROCEDURE_SCHEM + * String => procedure schema (may be null) + * PROCEDURE_NAME String => procedure name reserved for + * future use reserved for future use reserved for + * future use REMARKS String => explanatory comment on + * the procedure PROCEDURE_TYPE short => kind of + * procedure: procedureResultUnknown - Cannot determine + * if a return value will be returned procedureNoResult + * - Does not return a return value + * procedureReturnsResult - Returns a return value + * SPECIFIC_NAME String => The name which uniquely + * identifies this procedure within its schema. + * + * Oracle getProcedures actually returns these 8 + * columns:- ResultSet "Matched Procedures" has 8 + * columns and contains ... + * [PROCEDURE_CAT,PROCEDURE_SCHEM,PROCEDURE_NAME,NULL, + * NULL,NULL,REMARKS,PROCEDURE_TYPE + * ,null,PHPDEMO,ADD_JOB_HISTORY,null,null,null, + * Standalone procedure or function,1 + * ,FETCHPERFPKG,PHPDEMO,BULKSELECTPRC,null,null,null, + * Packaged function,2 + * ,FETCHPERFPKG,PHPDEMO,BULKSELECTPRC,null,null,null, + * Packaged procedure,1 + * ,null,PHPDEMO,CITY_LIST,null,null,null,Standalone + * procedure or function,1 + * ,null,PHPDEMO,EDDISCOUNT,null,null,null,Standalone + * procedure or function,2 + * ,SELPKG_BA,PHPDEMO,EMPSELBULK,null,null,null,Packaged + * function,2 + * ,SELPKG_BA,PHPDEMO,EMPSELBULK,null,null,null,Packaged + * procedure,1 + * ,INSPKG,PHPDEMO,INSFORALL,null,null,null,Packaged + * procedure,1 + * ,null,PHPDEMO,MYDOFETCH,null,null,null,Standalone + * procedure or function,2 + * ,null,PHPDEMO,MYPROC1,null,null,null,Standalone + * procedure or function,1 + * ,null,PHPDEMO,MYPROC2,null,null,null,Standalone + * procedure or function,1 + * ,null,PHPDEMO,MYXAQUERY,null,null,null,Standalone + * procedure or function,1 + * ,null,PHPDEMO,POLICY_VPDPARTS,null,null,null, + * Standalone procedure or function,2 + * ,FETCHPERFPKG,PHPDEMO,REFCURPRC,null,null,null, + * Packaged procedure,1 + * ,null,PHPDEMO,SECURE_DML,null,null,null,Standalone + * procedure or function,1 ... ] + */ + while (sourceCodeObjects.next()) { + LOGGER.finest(String.format("Located schema=%s,object_type=%s,object_name=%s\n", + sourceCodeObjects.getString("PROCEDURE_SCHEM"), + sourceCodeObjects.getString("PROCEDURE_TYPE"), + sourceCodeObjects.getString("PROCEDURE_NAME"))); + + sourceObjectsList.add(new SourceObject(sourceCodeObjects.getString("PROCEDURE_SCHEM"), + sourceCodeObjects.getString("PROCEDURE_TYPE"), + sourceCodeObjects.getString("PROCEDURE_NAME"), null)); + } + } + return sourceObjectsList; + } + + private List findSourceObjects(PreparedStatement sourceCodeObjectsStatement, + String language, String schema, String sourceCodeType, String sourceCodeName) throws SQLException { + List sourceObjectsList = new ArrayList<>(); + sourceCodeObjectsStatement.setString(1, language); + sourceCodeObjectsStatement.setString(2, schema); + sourceCodeObjectsStatement.setString(3, sourceCodeType); + sourceCodeObjectsStatement.setString(4, sourceCodeName); + LOGGER.finer(String.format( + "searching for language=\"%s\", schema=\"%s\", sourceCodeType=\"%s\", sourceCodeNames=\"%s\" ", + language, schema, sourceCodeType, sourceCodeName)); + + /* + * public ResultSet getProcedures(String catalog + * , String schemaPattern , String + * procedureNamePattern) throws SQLException + */ + try (ResultSet sourceCodeObjects = sourceCodeObjectsStatement.executeQuery()) { + + /* + * From Javadoc .... Each procedure description + * has the the following columns: PROCEDURE_CAT + * String => procedure catalog (may be null) + * PROCEDURE_SCHEM String => procedure schema + * (may be null) PROCEDURE_NAME String => + * procedure name reserved for future use + * reserved for future use reserved for future + * use REMARKS String => explanatory comment on + * the procedure PROCEDURE_TYPE short => kind of + * procedure: procedureResultUnknown - Cannot + * determine if a return value will be returned + * procedureNoResult - Does not return a return + * value procedureReturnsResult - Returns a + * return value SPECIFIC_NAME String => The name + * which uniquely identifies this procedure + * within its schema. + */ + while (sourceCodeObjects.next()) { + LOGGER.finest(String.format("Found schema=%s,object_type=%s,object_name=%s", + sourceCodeObjects.getString("PROCEDURE_SCHEM"), + sourceCodeObjects.getString("PROCEDURE_TYPE"), + sourceCodeObjects.getString("PROCEDURE_NAME"))); + + sourceObjectsList + .add(new SourceObject(sourceCodeObjects.getString("PROCEDURE_SCHEM"), + sourceCodeObjects.getString("PROCEDURE_TYPE"), + sourceCodeObjects.getString("PROCEDURE_NAME"), null)); + } + } + return sourceObjectsList; + } } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/util/database/SourceObject.java b/pmd-core/src/main/java/net/sourceforge/pmd/util/database/SourceObject.java index 7b1d6f2025..5f73974de8 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/util/database/SourceObject.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/util/database/SourceObject.java @@ -4,6 +4,7 @@ package net.sourceforge.pmd.util.database; +import java.util.Locale; import java.util.logging.Logger; import net.sourceforge.pmd.cpd.SourceCode; @@ -130,21 +131,21 @@ public class SourceObject { LOG.entering(CLASS_NAME, "getSuffixFromType", this); if (null == type || type.isEmpty()) { return ""; - } else if (type.toUpperCase().indexOf("JAVA") >= 0) { + } else if (type.toUpperCase(Locale.ROOT).indexOf("JAVA") >= 0) { return ".java"; - } else if (type.toUpperCase().indexOf("TRIGGER") >= 0) { + } else if (type.toUpperCase(Locale.ROOT).indexOf("TRIGGER") >= 0) { return ".trg"; - } else if (type.toUpperCase().indexOf("FUNCTION") >= 0) { + } else if (type.toUpperCase(Locale.ROOT).indexOf("FUNCTION") >= 0) { return ".fnc"; - } else if (type.toUpperCase().indexOf("PROCEDURE") >= 0) { + } else if (type.toUpperCase(Locale.ROOT).indexOf("PROCEDURE") >= 0) { return ".prc"; - } else if (type.toUpperCase().indexOf("PACKAGE_BODY") >= 0) { + } else if (type.toUpperCase(Locale.ROOT).indexOf("PACKAGE_BODY") >= 0) { return ".pkb"; - } else if (type.toUpperCase().indexOf("PACKAGE") >= 0) { + } else if (type.toUpperCase(Locale.ROOT).indexOf("PACKAGE") >= 0) { return ".pks"; - } else if (type.toUpperCase().indexOf("TYPE_BODY") >= 0) { + } else if (type.toUpperCase(Locale.ROOT).indexOf("TYPE_BODY") >= 0) { return ".tpb"; - } else if (type.toUpperCase().indexOf("TYPE") >= 0) { + } else if (type.toUpperCase(Locale.ROOT).indexOf("TYPE") >= 0) { return ".tps"; } else { return ""; @@ -157,8 +158,7 @@ public class SourceObject { * parser is used. */ public String getPseudoFileName() { - String falseFilePath = String.format("/Database/%s/%s/%s%s", getSchema(), getType(), getName(), + return String.format("/Database/%s/%s/%s%s", getSchema(), getType(), getName(), getSuffixFromType()); - return falseFilePath; } } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/util/designer/Designer.java b/pmd-core/src/main/java/net/sourceforge/pmd/util/designer/Designer.java index ce3bd31dc2..a70f3fbc67 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/util/designer/Designer.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/util/designer/Designer.java @@ -222,7 +222,7 @@ public class Designer implements ClipboardOwner { } } } - return languageVersions.toArray(new LanguageVersion[languageVersions.size()]); + return languageVersions.toArray(new LanguageVersion[0]); } private LanguageVersion getLanguageVersion() { @@ -990,6 +990,7 @@ public class Designer implements ClipboardOwner { @Override public void lostOwnership(Clipboard clipboard, Transferable contents) { + // ignored } private void loadSettings() { diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/util/filter/AbstractDelegateFilter.java b/pmd-core/src/main/java/net/sourceforge/pmd/util/filter/AbstractDelegateFilter.java index 96ad9cb7c4..eea8ddf9a3 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/util/filter/AbstractDelegateFilter.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/util/filter/AbstractDelegateFilter.java @@ -15,6 +15,7 @@ public abstract class AbstractDelegateFilter implements Filter { protected Filter filter; public AbstractDelegateFilter() { + // default constructor } public AbstractDelegateFilter(Filter filter) { diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/util/filter/FileExtensionFilter.java b/pmd-core/src/main/java/net/sourceforge/pmd/util/filter/FileExtensionFilter.java index f0b18afc68..ef6e5bae51 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/util/filter/FileExtensionFilter.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/util/filter/FileExtensionFilter.java @@ -5,6 +5,7 @@ package net.sourceforge.pmd.util.filter; import java.io.File; +import java.util.Locale; public class FileExtensionFilter implements Filter { protected final String[] extensions; @@ -25,7 +26,7 @@ public class FileExtensionFilter implements Filter { this.ignoreCase = ignoreCase; if (ignoreCase) { for (int i = 0; i < this.extensions.length; i++) { - this.extensions[i] = this.extensions[i].toUpperCase(); + this.extensions[i] = this.extensions[i].toUpperCase(Locale.ROOT); } } } @@ -36,7 +37,7 @@ public class FileExtensionFilter implements Filter { if (!accept) { for (String extension : extensions) { String name = file.getName(); - if (ignoreCase ? name.toUpperCase().endsWith(extension) : name.endsWith(extension)) { + if (ignoreCase ? name.toUpperCase(Locale.ROOT).endsWith(extension) : name.endsWith(extension)) { accept = true; break; } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/util/filter/Filters.java b/pmd-core/src/main/java/net/sourceforge/pmd/util/filter/Filters.java index 9e8fecd309..7bd9b0c630 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/util/filter/Filters.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/util/filter/Filters.java @@ -14,7 +14,7 @@ import java.util.List; * Utility class for working with Filters. Contains builder style methods, apply * methods, as well as mechanisms for adapting Filters and FilenameFilters. */ -public class Filters { +public final class Filters { private Filters() { } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/util/log/AntLogHandler.java b/pmd-core/src/main/java/net/sourceforge/pmd/util/log/AntLogHandler.java index fe15dba444..8151be66bd 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/util/log/AntLogHandler.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/util/log/AntLogHandler.java @@ -62,9 +62,11 @@ public class AntLogHandler extends Handler { @Override public void close() throws SecurityException { + // nothing to do } @Override public void flush() { + // nothing to do } } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/util/log/ConsoleLogHandler.java b/pmd-core/src/main/java/net/sourceforge/pmd/util/log/ConsoleLogHandler.java index 1092dddb5d..42c0389f90 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/util/log/ConsoleLogHandler.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/util/log/ConsoleLogHandler.java @@ -35,9 +35,11 @@ public class ConsoleLogHandler extends Handler { @Override public void close() throws SecurityException { + // nothing to do } @Override public void flush() { + System.out.flush(); } } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/util/viewer/Viewer.java b/pmd-core/src/main/java/net/sourceforge/pmd/util/viewer/Viewer.java index 0b3bf0a531..a040cef667 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/util/viewer/Viewer.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/util/viewer/Viewer.java @@ -14,7 +14,7 @@ import net.sourceforge.pmd.util.viewer.gui.MainFrame; * @version $Id$ */ @Deprecated // to be removed with PMD 7.0.0 -public class Viewer { +public final class Viewer { private Viewer() { } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/util/viewer/model/AttributeToolkit.java b/pmd-core/src/main/java/net/sourceforge/pmd/util/viewer/model/AttributeToolkit.java index 74760679dd..aedb293b71 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/util/viewer/model/AttributeToolkit.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/util/viewer/model/AttributeToolkit.java @@ -13,7 +13,7 @@ import net.sourceforge.pmd.lang.ast.xpath.Attribute; * @version $Id$ */ @Deprecated // to be removed with PMD 7.0.0 -public class AttributeToolkit { +public final class AttributeToolkit { private AttributeToolkit() { } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/util/viewer/util/NLS.java b/pmd-core/src/main/java/net/sourceforge/pmd/util/viewer/util/NLS.java index 807832b40b..5848281e39 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/util/viewer/util/NLS.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/util/viewer/util/NLS.java @@ -13,7 +13,7 @@ import java.util.ResourceBundle; * @version $Id$ */ @Deprecated // to be removed with PMD 7.0.0 -public class NLS { +public final class NLS { private static final ResourceBundle BUNDLE; static { diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/lang/DummyLanguageModule.java b/pmd-core/src/test/java/net/sourceforge/pmd/lang/DummyLanguageModule.java index 4f5312fa0b..dc88588965 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/lang/DummyLanguageModule.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/lang/DummyLanguageModule.java @@ -107,9 +107,9 @@ public class DummyLanguageModule extends BaseLanguageModule { protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String message, int beginLine, int endLine) { ParametricRuleViolation rv = new ParametricRuleViolation(rule, ruleContext, node, message) { - { - this.packageName = "foo"; // just for testing variable - // expansion + public String getPackageName() { + this.packageName = "foo"; // just for testing variable expansion + return super.getPackageName(); } }; rv.setLines(beginLine, endLine); diff --git a/pmd-doc/src/main/java/net/sourceforge/pmd/docs/GenerateRuleDocsCmd.java b/pmd-doc/src/main/java/net/sourceforge/pmd/docs/GenerateRuleDocsCmd.java index db02d6c2c5..e58f1d72a7 100644 --- a/pmd-doc/src/main/java/net/sourceforge/pmd/docs/GenerateRuleDocsCmd.java +++ b/pmd-doc/src/main/java/net/sourceforge/pmd/docs/GenerateRuleDocsCmd.java @@ -23,7 +23,7 @@ import net.sourceforge.pmd.RuleSet; import net.sourceforge.pmd.RuleSetFactory; import net.sourceforge.pmd.RuleSetNotFoundException; -public class GenerateRuleDocsCmd { +public final class GenerateRuleDocsCmd { private GenerateRuleDocsCmd() { // Utility class } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/cpd/JavaTokenizer.java b/pmd-java/src/main/java/net/sourceforge/pmd/cpd/JavaTokenizer.java index 0b62ca6ade..505c150996 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/cpd/JavaTokenizer.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/cpd/JavaTokenizer.java @@ -179,9 +179,8 @@ public class JavaTokenizer implements Tokenizer { } public boolean isDiscarding() { - boolean result = discardingSemicolon || discardingKeywords || discardingAnnotations + return discardingSemicolon || discardingKeywords || discardingAnnotations || discardingSuppressing; - return result; } private void detectAnnotations(Token currentToken) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/AbstractJavaHandler.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/AbstractJavaHandler.java index 6e35293ff9..56d9b1f1dd 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/AbstractJavaHandler.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/AbstractJavaHandler.java @@ -12,7 +12,7 @@ import net.sourceforge.pmd.lang.LanguageRegistry; import net.sourceforge.pmd.lang.VisitorStarter; import net.sourceforge.pmd.lang.XPathHandler; import net.sourceforge.pmd.lang.ast.Node; -import net.sourceforge.pmd.lang.ast.xpath.AbstractASTXPathHandler; +import net.sourceforge.pmd.lang.ast.xpath.DefaultASTXPathHandler; import net.sourceforge.pmd.lang.dfa.DFAGraphRule; import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit; import net.sourceforge.pmd.lang.java.ast.DumpFacade; @@ -46,13 +46,15 @@ public abstract class AbstractJavaHandler extends AbstractLanguageVersionHandler @Override public XPathHandler getXPathHandler() { - return new AbstractASTXPathHandler() { + return new DefaultASTXPathHandler() { + @Override public void initialize() { TypeOfFunction.registerSelfInSimpleContext(); GetCommentOnFunction.registerSelfInSimpleContext(); MetricFunction.registerSelfInSimpleContext(); } + @Override public void initialize(IndependentContext context) { super.initialize(context, LanguageRegistry.getLanguage(JavaLanguageModule.NAME), JavaFunctions.class); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAllocationExpression.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAllocationExpression.java index 3d7bb093e8..70ddf58b1c 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAllocationExpression.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAllocationExpression.java @@ -5,8 +5,6 @@ package net.sourceforge.pmd.lang.java.ast; -import net.sourceforge.pmd.lang.ast.Node; - public class ASTAllocationExpression extends AbstractJavaTypeNode { public ASTAllocationExpression(int id) { super(id); @@ -25,8 +23,8 @@ public class ASTAllocationExpression extends AbstractJavaTypeNode { public boolean isAnonymousClass() { if (jjtGetNumChildren() > 1) { - Node lastChild = jjtGetChild(jjtGetNumChildren() - 1); - return lastChild instanceof ASTClassOrInterfaceBody; + // check the last child + return jjtGetChild(jjtGetNumChildren() - 1) instanceof ASTClassOrInterfaceBody; } return false; } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExpression.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExpression.java index 4b55bb7151..0c19fc0c51 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExpression.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTExpression.java @@ -40,12 +40,9 @@ public class ASTExpression extends AbstractJavaTypeNode { ASTLiteral literal = primaryPrefix.getFirstChildOfType(ASTLiteral.class); - if (literal == null || literal.isStringLiteral() - || literal.jjtGetNumChildren() != 0 && literal.jjtGetChild(0) instanceof ASTNullLiteral) { - return false; - } - + // if it is not a string literal and not a null, then it is one of // byte, short, char, int, long, float, double, boolean - return true; + return literal != null && !literal.isStringLiteral() + && (literal.jjtGetNumChildren() == 0 || !(literal.jjtGetChild(0) instanceof ASTNullLiteral)); } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTFieldDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTFieldDeclaration.java index f23a3e4e80..1f87f5a7af 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTFieldDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTFieldDeclaration.java @@ -90,14 +90,11 @@ public class ASTFieldDeclaration extends AbstractJavaAccessTypeNode implements D } public boolean isAnnotationMember() { - if (jjtGetParent().jjtGetParent() instanceof ASTAnnotationTypeBody) { - return true; - } - return false; + return getNthParent(2) instanceof ASTAnnotationTypeBody; } public boolean isInterfaceMember() { - if (jjtGetParent().jjtGetParent() instanceof ASTEnumBody) { + if (getNthParent(2) instanceof ASTEnumBody) { return false; } ASTClassOrInterfaceDeclaration n = getFirstParentOfType(ASTClassOrInterfaceDeclaration.class); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTLiteral.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTLiteral.java index 75bc981532..f1b2f104ab 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTLiteral.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTLiteral.java @@ -5,6 +5,7 @@ package net.sourceforge.pmd.lang.java.ast; +import java.util.Locale; import java.util.regex.Pattern; public class ASTLiteral extends AbstractJavaTypeNode { @@ -98,7 +99,7 @@ public class ASTLiteral extends AbstractJavaTypeNode { } private String stripIntValue() { - String image = getImage().toLowerCase().replaceAll("_", ""); + String image = getImage().toLowerCase(Locale.ROOT).replaceAll("_", ""); boolean isNegative = false; if (image.charAt(0) == '-') { @@ -126,11 +127,11 @@ public class ASTLiteral extends AbstractJavaTypeNode { } private String stripFloatValue() { - return getImage().toLowerCase().replaceAll("_", ""); + return getImage().toLowerCase(Locale.ROOT).replaceAll("_", ""); } private int getIntBase() { - final String image = getImage().toLowerCase(); + final String image = getImage().toLowerCase(Locale.ROOT); final int offset = image.charAt(0) == '-' ? 1 : 0; if (image.startsWith("0x", offset)) { return 16; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/Comment.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/Comment.java index 26479840e2..fc3f3ed0ba 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/Comment.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/Comment.java @@ -42,7 +42,7 @@ public abstract class Comment extends AbstractNode { entry.getValue() + 1, entry.getValue() + tag.label.length() + 1, tag)); } - children = kids.toArray(new Node[kids.size()]); + children = kids.toArray(new Node[0]); } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/JavaMetricsComputer.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/JavaMetricsComputer.java index 63287a7d8a..3b98fc6118 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/JavaMetricsComputer.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/JavaMetricsComputer.java @@ -17,13 +17,12 @@ import net.sourceforge.pmd.lang.metrics.AbstractMetricsComputer; * * @author Clément Fournier */ -public class JavaMetricsComputer extends AbstractMetricsComputer { +public final class JavaMetricsComputer extends AbstractMetricsComputer { static final JavaMetricsComputer INSTANCE = new JavaMetricsComputer(); private JavaMetricsComputer() { - } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/impl/visitors/CycloBaseVisitor.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/impl/visitors/CycloBaseVisitor.java index 007ffe9424..85b31f0921 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/impl/visitors/CycloBaseVisitor.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/impl/visitors/CycloBaseVisitor.java @@ -25,12 +25,12 @@ import net.sourceforge.pmd.lang.java.ast.JavaParserControllessVisitorAdapter; * @author Clément Fournier * @see net.sourceforge.pmd.lang.java.metrics.impl.CycloMetric */ -public class CycloBaseVisitor extends JavaParserControllessVisitorAdapter { +public final class CycloBaseVisitor extends JavaParserControllessVisitorAdapter { /** Instance. */ public static final CycloBaseVisitor INSTANCE = new CycloBaseVisitor(); - protected CycloBaseVisitor() { + private CycloBaseVisitor() { } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/impl/visitors/NcssBaseVisitor.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/impl/visitors/NcssBaseVisitor.java index ac07057af6..1a4806227a 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/impl/visitors/NcssBaseVisitor.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/impl/visitors/NcssBaseVisitor.java @@ -41,14 +41,13 @@ import net.sourceforge.pmd.lang.java.ast.JavaParserControllessVisitorAdapter; * @author Clément Fournier * @see net.sourceforge.pmd.lang.java.metrics.impl.NcssMetric */ -public class NcssBaseVisitor extends JavaParserControllessVisitorAdapter { +public final class NcssBaseVisitor extends JavaParserControllessVisitorAdapter { /** Instance. */ public static final NcssBaseVisitor INSTANCE = new NcssBaseVisitor(); - protected NcssBaseVisitor() { - + private NcssBaseVisitor() { } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/impl/visitors/NpathBaseVisitor.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/impl/visitors/NpathBaseVisitor.java index 7cd0bbdaf3..490897ddbe 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/impl/visitors/NpathBaseVisitor.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/impl/visitors/NpathBaseVisitor.java @@ -28,13 +28,12 @@ import net.sourceforge.pmd.lang.java.metrics.impl.CycloMetric; * @author Clément Fournier * @author Jason Bennett */ -public class NpathBaseVisitor extends JavaParserVisitorReducedAdapter { +public final class NpathBaseVisitor extends JavaParserVisitorReducedAdapter { /** Instance. */ public static final NpathBaseVisitor INSTANCE = new NpathBaseVisitor(); - protected NpathBaseVisitor() { - + private NpathBaseVisitor() { } /* Multiplies the complexity of the children of this node. */ diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/impl/visitors/TccAttributeAccessCollector.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/impl/visitors/TccAttributeAccessCollector.java index 115f1f2b3c..06cf13cf3f 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/impl/visitors/TccAttributeAccessCollector.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/impl/visitors/TccAttributeAccessCollector.java @@ -8,6 +8,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration; @@ -57,7 +58,7 @@ public class TccAttributeAccessCollector extends JavaParserVisitorReducedAdapter @Override public Object visit(ASTAnyTypeDeclaration node, Object data) { - if (node == exploredClass) { + if (Objects.equals(node, exploredClass)) { methodAttributeAccess = new HashMap<>(); super.visit(node, data); } else if (node instanceof ASTClassOrInterfaceDeclaration diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/multifile/PackageStats.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/multifile/PackageStats.java index f236d3860f..749a915b18 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/multifile/PackageStats.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/multifile/PackageStats.java @@ -32,8 +32,7 @@ final class PackageStats implements ProjectMirror { /** * Default constructor. */ - /* default */ PackageStats() { - + private PackageStats() { } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/AbstractStatisticalJavaRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/AbstractStatisticalJavaRule.java index ae43ab56d7..c645afef57 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/AbstractStatisticalJavaRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/AbstractStatisticalJavaRule.java @@ -21,7 +21,7 @@ public abstract class AbstractStatisticalJavaRule extends AbstractJavaRule imple } public Object[] getViolationParameters(DataPoint point) { - return null; + return new Object[0]; } @Override diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/ArrayIsStoredDirectlyRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/ArrayIsStoredDirectlyRule.java index 109366b172..55fe45f9b5 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/ArrayIsStoredDirectlyRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/ArrayIsStoredDirectlyRule.java @@ -44,12 +44,10 @@ public class ArrayIsStoredDirectlyRule extends AbstractSunSecureRule { @Override public Object visit(ASTConstructorDeclaration node, Object data) { ASTFormalParameter[] arrs = getArrays(node.getParameters()); - if (arrs != null) { - // TODO check if one of these arrays is stored in a non local - // variable - List bs = node.findDescendantsOfType(ASTBlockStatement.class); - checkAll(data, arrs, bs); - } + // TODO check if one of these arrays is stored in a non local + // variable + List bs = node.findDescendantsOfType(ASTBlockStatement.class); + checkAll(data, arrs, bs); return data; } @@ -57,9 +55,7 @@ public class ArrayIsStoredDirectlyRule extends AbstractSunSecureRule { public Object visit(ASTMethodDeclaration node, Object data) { final ASTFormalParameters params = node.getFirstDescendantOfType(ASTFormalParameters.class); ASTFormalParameter[] arrs = getArrays(params); - if (arrs != null) { - checkAll(data, arrs, node.findDescendantsOfType(ASTBlockStatement.class)); - } + checkAll(data, arrs, node.findDescendantsOfType(ASTBlockStatement.class)); return data; } @@ -160,9 +156,9 @@ public class ArrayIsStoredDirectlyRule extends AbstractSunSecureRule { l2.add(fp); } } - return l2.toArray(new ASTFormalParameter[l2.size()]); + return l2.toArray(new ASTFormalParameter[0]); } - return null; + return new ASTFormalParameter[0]; } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/ForLoopCanBeForeachRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/ForLoopCanBeForeachRule.java index 19cb7ab9c8..a14f8d4874 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/ForLoopCanBeForeachRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/ForLoopCanBeForeachRule.java @@ -8,6 +8,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Objects; import org.jaxen.JaxenException; @@ -126,7 +127,7 @@ public class ForLoopCanBeForeachRule extends AbstractJavaRule { for (Entry> e : decls.entrySet()) { ASTForInit declInit = e.getKey().getNode().getFirstParentOfType(ASTForInit.class); - if (declInit == init) { + if (Objects.equals(declInit, init)) { indexVarAndOccurrences = e; break; } @@ -417,7 +418,7 @@ public class ForLoopCanBeForeachRule extends AbstractJavaRule { String iterableName = iterableInfo.getKey().getName(); for (NameOccurrence occ : iterableInfo.getValue()) { ASTForStatement forParent = occ.getLocation().getFirstParentOfType(ASTForStatement.class); - if (forParent == stmt) { + if (Objects.equals(forParent, stmt)) { String image = occ.getLocation().getImage(); if (image.startsWith(iterableName + ".remove")) { return true; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/PreserveStackTraceRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/PreserveStackTraceRule.java index ffdf1da4e3..dbff360e20 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/PreserveStackTraceRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/PreserveStackTraceRule.java @@ -6,6 +6,7 @@ package net.sourceforge.pmd.lang.java.rule.bestpractices; import java.util.List; import java.util.Map; +import java.util.Objects; import net.sourceforge.pmd.RuleContext; import net.sourceforge.pmd.lang.ast.Node; @@ -160,7 +161,7 @@ public class PreserveStackTraceRule extends AbstractJavaRule { */ private boolean isStringConcat(Node childNode, Node baseNode) { Node currentNode = childNode; - while (currentNode != baseNode) { + while (!Objects.equals(currentNode, baseNode)) { currentNode = currentNode.jjtGetParent(); if (currentNode instanceof ASTAdditiveExpression) { return true; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedImportsRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedImportsRule.java index 3c9e8691ec..d370a5b66a 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedImportsRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/bestpractices/UnusedImportsRule.java @@ -172,7 +172,6 @@ public class UnusedImportsRule extends AbstractJavaRule { } else { name = node.getImage().substring(0, node.getImage().indexOf('.')); } - ImportWrapper candidate = new ImportWrapper(node.getImage(), name); - return candidate; + return new ImportWrapper(node.getImage(), name); } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/DuplicateImportsRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/DuplicateImportsRule.java index 667d73f156..8c6c19dcb3 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/DuplicateImportsRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/DuplicateImportsRule.java @@ -79,11 +79,8 @@ public class DuplicateImportsRule extends AbstractJavaRule { } String fullyQualifiedClassName = "java.lang." + singleTypeName; - if (node.getClassTypeResolver().classNameExists(fullyQualifiedClassName)) { - return true; // Class exists in another imported package - } - - return false; // This really is a duplicate import + // Class might exist in another imported package + return node.getClassTypeResolver().classNameExists(fullyQualifiedClassName); } public Object visit(ASTImportDeclaration node, Object data) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryFullyQualifiedNameRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryFullyQualifiedNameRule.java index 02a6cd1a5c..c464ac5e3b 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryFullyQualifiedNameRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryFullyQualifiedNameRule.java @@ -7,6 +7,7 @@ package net.sourceforge.pmd.lang.java.rule.codestyle; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.Set; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType; @@ -141,7 +142,7 @@ public class UnnecessaryFullyQualifiedNameRule extends AbstractJavaRule { // Is there any other static import conflictive? for (final ASTImportDeclaration importDeclaration : imports) { - if (importDeclaration != firstMatch && importDeclaration.isStatic()) { + if (!Objects.equals(importDeclaration, firstMatch) && importDeclaration.isStatic()) { if (importDeclaration.getImportedName().startsWith(firstMatch.getImportedName()) && importDeclaration.getImportedName().lastIndexOf('.') == firstMatch.getImportedName() .length()) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/VariableNamingConventionsRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/VariableNamingConventionsRule.java index 7b6a48c774..67fe9b19fe 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/VariableNamingConventionsRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/VariableNamingConventionsRule.java @@ -5,6 +5,7 @@ package net.sourceforge.pmd.lang.java.rule.codestyle; import java.util.List; +import java.util.Locale; import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.java.ast.ASTAnnotationTypeDeclaration; @@ -177,7 +178,7 @@ public class VariableNamingConventionsRule extends AbstractJavaRule { // Static finals should be uppercase if (isStatic && isFinal) { - if (!varName.equals(varName.toUpperCase())) { + if (!varName.equals(varName.toUpperCase(Locale.ROOT))) { addViolationWithMessage(data, variableDeclaratorId, "Variables that are final and static should be all capitals, ''{0}'' is not all capitals.", new Object[] { varName }); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/CyclomaticComplexityRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/CyclomaticComplexityRule.java index dc26c0f5d7..2af6b4c184 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/CyclomaticComplexityRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/CyclomaticComplexityRule.java @@ -6,6 +6,7 @@ package net.sourceforge.pmd.lang.java.rule.design; import java.util.Collections; import java.util.HashMap; +import java.util.Locale; import java.util.Map; import java.util.logging.Logger; @@ -125,7 +126,7 @@ public class CyclomaticComplexityRule extends AbstractJavaMetricsRule { if (classWmc >= classReportLevel) { int classHighest = (int) JavaMetrics.get(JavaOperationMetricKey.CYCLO, node, cycloOptions, ResultOption.HIGHEST); - String[] messageParams = {node.getTypeKind().name().toLowerCase(), + String[] messageParams = {node.getTypeKind().name().toLowerCase(Locale.ROOT), node.getImage(), " total", classWmc + " (highest " + classHighest + ")", }; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/LawOfDemeterRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/LawOfDemeterRule.java index 2b493180a6..9a791ad494 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/LawOfDemeterRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/LawOfDemeterRule.java @@ -8,6 +8,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; +import java.util.Locale; import java.util.Set; import net.sourceforge.pmd.RuleContext; @@ -383,7 +384,7 @@ public class LawOfDemeterRule extends AbstractJavaRule { boolean factory = false; List names = declarator.findDescendantsOfType(ASTName.class); for (ASTName name : names) { - if (name.getImage().toLowerCase().contains("factory")) { + if (name.getImage().toLowerCase(Locale.ROOT).contains("factory")) { factory = true; break; } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/NcssCountRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/NcssCountRule.java index 2db89d7a62..caebcfe5a4 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/NcssCountRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/NcssCountRule.java @@ -6,6 +6,7 @@ package net.sourceforge.pmd.lang.java.rule.design; import java.util.Collections; import java.util.HashMap; +import java.util.Locale; import java.util.Map; import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration; @@ -83,7 +84,7 @@ public final class NcssCountRule extends AbstractJavaMetricsRule { int classHighest = (int) JavaMetrics.get(JavaOperationMetricKey.NCSS, node, ncssOptions, ResultOption.HIGHEST); if (classSize >= classReportLevel) { - String[] messageParams = {node.getTypeKind().name().toLowerCase(), + String[] messageParams = {node.getTypeKind().name().toLowerCase(Locale.ROOT), node.getImage(), classSize + " (Highest = " + classHighest + ")", }; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/AbstractCommentRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/AbstractCommentRule.java index 2277e8b0fe..5fd1da2575 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/AbstractCommentRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/AbstractCommentRule.java @@ -35,9 +35,6 @@ import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule; */ public abstract class AbstractCommentRule extends AbstractJavaRule { - protected AbstractCommentRule() { - } - protected List tagsIndicesIn(String comments) { int atPos = comments.indexOf('@'); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/CommentContentRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/CommentContentRule.java index 58fc3dea0f..bdbbcf5576 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/CommentContentRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/CommentContentRule.java @@ -8,6 +8,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; +import java.util.Locale; import java.util.Set; import org.apache.commons.lang3.StringUtils; @@ -68,7 +69,7 @@ public class CommentContentRule extends AbstractCommentRule { } else { currentBadWords = new ArrayList<>(); for (String badWord : originalBadWords) { - currentBadWords.add(badWord.toUpperCase()); + currentBadWords.add(badWord.toUpperCase(Locale.ROOT)); } } } @@ -94,7 +95,7 @@ public class CommentContentRule extends AbstractCommentRule { } if (!caseSensitive) { - commentText = commentText.toUpperCase(); + commentText = commentText.toUpperCase(Locale.ROOT); } List foundWords = new ArrayList<>(); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/CommentRequiredRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/CommentRequiredRule.java index 1d98e41d9d..77b4870feb 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/CommentRequiredRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/CommentRequiredRule.java @@ -9,6 +9,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; @@ -97,7 +98,9 @@ public class CommentRequiredRule extends AbstractCommentRule { addViolationWithMessage(data, node, - DESCRIPTOR_NAME_TO_COMMENT_TYPE.get(descriptor.name()) + " are " + getProperty(descriptor).label.toLowerCase()); + DESCRIPTOR_NAME_TO_COMMENT_TYPE.get(descriptor.name()) + + " are " + + getProperty(descriptor).label.toLowerCase(Locale.ROOT)); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/AvoidFieldNameMatchingMethodNameRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/AvoidFieldNameMatchingMethodNameRule.java index 1920b21c1c..77fe17fc1e 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/AvoidFieldNameMatchingMethodNameRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/AvoidFieldNameMatchingMethodNameRule.java @@ -7,6 +7,7 @@ package net.sourceforge.pmd.lang.java.rule.errorprone; import java.util.ArrayList; import java.util.HashSet; import java.util.List; +import java.util.Locale; import java.util.Set; import net.sourceforge.pmd.lang.ast.Node; @@ -40,11 +41,11 @@ public class AvoidFieldNameMatchingMethodNameRule extends AbstractJavaRule { if (child instanceof ASTFieldDeclaration) { fields.add((ASTFieldDeclaration) child); } else if (child instanceof ASTMethodDeclaration) { - methodNames.add(((ASTMethodDeclaration) child).getMethodName().toLowerCase()); + methodNames.add(((ASTMethodDeclaration) child).getMethodName().toLowerCase(Locale.ROOT)); } } for (ASTFieldDeclaration field : fields) { - String varName = field.getVariableName().toLowerCase(); + String varName = field.getVariableName().toLowerCase(Locale.ROOT); if (methodNames.contains(varName)) { addViolation(data, field, field.getVariableName()); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/BeanMembersShouldSerializeRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/BeanMembersShouldSerializeRule.java index c809b0dfb8..6345ae4fda 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/BeanMembersShouldSerializeRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/BeanMembersShouldSerializeRule.java @@ -7,6 +7,7 @@ package net.sourceforge.pmd.lang.java.rule.errorprone; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Locale; import java.util.Map; import net.sourceforge.pmd.lang.ast.Node; @@ -80,7 +81,7 @@ public class BeanMembersShouldSerializeRule extends AbstractJavaRule { continue; } String varName = trimIfPrefix(decl.getImage()); - varName = varName.substring(0, 1).toUpperCase() + varName.substring(1, varName.length()); + varName = varName.substring(0, 1).toUpperCase(Locale.ROOT) + varName.substring(1, varName.length()); boolean hasGetMethod = Arrays.binarySearch(methNameArray, "get" + varName) >= 0 || Arrays.binarySearch(methNameArray, "is" + varName) >= 0; boolean hasSetMethod = Arrays.binarySearch(methNameArray, "set" + varName) >= 0; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/CloneMethodMustImplementCloneableRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/CloneMethodMustImplementCloneableRule.java index f2517f2188..e528fac6ac 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/CloneMethodMustImplementCloneableRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/CloneMethodMustImplementCloneableRule.java @@ -6,6 +6,7 @@ package net.sourceforge.pmd.lang.java.rule.errorprone; import java.util.HashSet; import java.util.List; +import java.util.Objects; import java.util.Set; import net.sourceforge.pmd.lang.ast.Node; @@ -15,7 +16,6 @@ import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType; import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit; import net.sourceforge.pmd.lang.java.ast.ASTExtendsList; -import net.sourceforge.pmd.lang.java.ast.ASTFormalParameters; import net.sourceforge.pmd.lang.java.ast.ASTImplementsList; import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator; @@ -152,21 +152,17 @@ public class CloneMethodMustImplementCloneableRule extends AbstractJavaRule { .findDescendantsOfType(ASTClassOrInterfaceDeclaration.class); final Set classesNames = new HashSet(); for (final ASTClassOrInterfaceDeclaration c : classes) { - if (c != currentClass && extendsOrImplementsCloneable(c)) { + if (!Objects.equals(c, currentClass) && extendsOrImplementsCloneable(c)) { classesNames.add(c.getImage()); } } return classesNames; } - public boolean isCloneMethod(final ASTMethodDeclarator node) { - if (!"clone".equals(node.getImage())) { + public boolean isCloneMethod(final ASTMethodDeclarator method) { + if (!"clone".equals(method.getImage())) { return false; } - final int countParams = ((ASTFormalParameters) node.jjtGetChild(0)).jjtGetNumChildren(); - if (countParams != 0) { - return false; - } - return true; + return method.getParameterCount() == 0; } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/ConstructorCallsOverridableMethodRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/ConstructorCallsOverridableMethodRule.java index a2211caab7..1de99a9493 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/ConstructorCallsOverridableMethodRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/ConstructorCallsOverridableMethodRule.java @@ -596,7 +596,7 @@ public final class ConstructorCallsOverridableMethodRule extends AbstractJavaRul public List calledConstructors; public Map> allPrivateConstructorsOfClass; - EvalPackage() { + private EvalPackage() { } EvalPackage(String className) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/InvalidSlf4jMessageFormatRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/InvalidSlf4jMessageFormatRule.java index d4dda736bc..86ab80b76c 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/InvalidSlf4jMessageFormatRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/InvalidSlf4jMessageFormatRule.java @@ -29,6 +29,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId; import net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode; import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule; import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration; +import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper; import net.sourceforge.pmd.lang.symboltable.NameDeclaration; public class InvalidSlf4jMessageFormatRule extends AbstractJavaRule { @@ -45,7 +46,7 @@ public class InvalidSlf4jMessageFormatRule extends AbstractJavaRule { public Object visit(final ASTName node, final Object data) { final NameDeclaration nameDeclaration = node.getNameDeclaration(); // ignore imports or methods - if (nameDeclaration == null || !(nameDeclaration instanceof VariableNameDeclaration)) { + if (!(nameDeclaration instanceof VariableNameDeclaration)) { return super.visit(node, data); } @@ -100,19 +101,13 @@ public class InvalidSlf4jMessageFormatRule extends AbstractJavaRule { // in case a new exception is created or the exception class is // mentioned. ASTClassOrInterfaceType classOrInterface = last.getFirstDescendantOfType(ASTClassOrInterfaceType.class); - if (classOrInterface != null && classOrInterface.getType() != null - && Throwable.class.isAssignableFrom(classOrInterface.getType())) { - return true; - } - return false; + return classOrInterface != null && classOrInterface.getType() != null + && TypeHelper.isA(classOrInterface, Throwable.class); } private boolean hasTypeThrowable(ASTPrimaryExpression last) { // if the type could be determined already - if (last.getType() != null && Throwable.class.isAssignableFrom(last.getType())) { - return true; - } - return false; + return last.getType() != null && TypeHelper.isA(last, Throwable.class); } private boolean isReferencingThrowable(ASTPrimaryExpression last) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/SingletonClassReturningNewInstanceRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/SingletonClassReturningNewInstanceRule.java index 0a9f9f9ba4..10c96c3672 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/SingletonClassReturningNewInstanceRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/SingletonClassReturningNewInstanceRule.java @@ -62,7 +62,7 @@ public class SingletonClassReturningNewInstanceRule extends AbstractJavaRule { List astBlockStatements = node.findDescendantsOfType(ASTBlockStatement.class); returnVariableName = getReturnVariableName(node); - if (astBlockStatements.size() != 0) { + if (!astBlockStatements.isEmpty()) { for (ASTBlockStatement blockStatement : astBlockStatements) { if (blockStatement.hasDescendantOfType(ASTLocalVariableDeclaration.class)) { List lVarList = blockStatement diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/ConsecutiveAppendsShouldReuseRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/ConsecutiveAppendsShouldReuseRule.java index 7561f7b304..5ab97ff034 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/ConsecutiveAppendsShouldReuseRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/ConsecutiveAppendsShouldReuseRule.java @@ -165,9 +165,6 @@ public class ConsecutiveAppendsShouldReuseRule extends AbstractJavaRule { } private boolean isFirstChild(Node node, Class clazz) { - if (node.jjtGetNumChildren() == 1 && clazz.isAssignableFrom(node.jjtGetChild(0).getClass())) { - return true; - } - return false; + return node.jjtGetNumChildren() == 1 && clazz.isAssignableFrom(node.jjtGetChild(0).getClass()); } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/InefficientStringBufferingRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/InefficientStringBufferingRule.java index a1392f069f..77c739baa4 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/InefficientStringBufferingRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/InefficientStringBufferingRule.java @@ -121,10 +121,7 @@ public class InefficientStringBufferingRule extends AbstractJavaRule { private boolean isPrimitiveType(ASTName name) { ASTType type = getTypeNode(name); - if (type != null && !type.findChildrenOfType(ASTPrimitiveType.class).isEmpty()) { - return true; - } - return false; + return type != null && !type.findChildrenOfType(ASTPrimitiveType.class).isEmpty(); } private ASTType getTypeNode(ASTName name) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/UseStringBufferForStringAppendsRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/UseStringBufferForStringAppendsRule.java index cecea5fb16..0b04df4054 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/UseStringBufferForStringAppendsRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/performance/UseStringBufferForStringAppendsRule.java @@ -4,6 +4,8 @@ package net.sourceforge.pmd.lang.java.rule.performance; +import java.util.Objects; + import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.java.ast.ASTArgumentList; import net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator; @@ -48,8 +50,9 @@ public class UseStringBufferForStringAppendsRule extends AbstractJavaRule { ASTConditionalExpression conditional = name.getFirstParentOfType(ASTConditionalExpression.class); if (conditional != null) { - Node thirdParent = name.jjtGetParent().jjtGetParent().jjtGetParent(); - if ((thirdParent == conditional || thirdParent.jjtGetParent() == conditional) + Node thirdParent = name.getNthParent(3); + Node fourthParent = name.getNthParent(4); + if ((Objects.equals(thirdParent, conditional) || Objects.equals(fourthParent, conditional)) && conditional.getFirstParentOfType(ASTStatementExpression.class) == statement) { // is used in ternary as only option (not appended to other // string) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/JavaNameOccurrence.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/JavaNameOccurrence.java index 02b1b0d770..d0f15177c7 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/JavaNameOccurrence.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/JavaNameOccurrence.java @@ -109,11 +109,7 @@ public class JavaNameOccurrence implements NameOccurrence { return false; } - if (isCompoundAssignment(primaryExpression)) { - return false; - } - - return true; + return !isCompoundAssignment(primaryExpression); } private boolean isCompoundAssignment(Node primaryExpression) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/SimpleTypedNameDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/SimpleTypedNameDeclaration.java index 19978187c0..559358d4c3 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/SimpleTypedNameDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/SimpleTypedNameDeclaration.java @@ -5,6 +5,7 @@ package net.sourceforge.pmd.lang.java.symboltable; import java.util.HashSet; +import java.util.Locale; import java.util.Set; /** @@ -150,8 +151,8 @@ public class SimpleTypedNameDeclaration implements TypedNameDeclaration { } else if (!typeImage.equals(other.typeImage)) { // consider auto-boxing if (other.typeImage != null) { - String lcType = typeImage.toLowerCase(); - String otherLcType = other.typeImage.toLowerCase(); + String lcType = typeImage.toLowerCase(Locale.ROOT); + String otherLcType = other.typeImage.toLowerCase(Locale.ROOT); if (primitiveTypes.contains(lcType) && primitiveTypes.contains(otherLcType)) { if (lcType.equals(otherLcType)) { return true; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/MethodType.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/MethodType.java index f274de6a39..1e77b1744b 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/MethodType.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/MethodType.java @@ -14,7 +14,7 @@ import net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefin /** * This is really just a POJO. */ -public class MethodType { +public final class MethodType { private final JavaTypeDefinition returnType; private final List argTypes; private final Method method; diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/MethodTypeResolution.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/MethodTypeResolution.java index f7e280f354..ff8b6eac0a 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/MethodTypeResolution.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/MethodTypeResolution.java @@ -525,7 +525,7 @@ public final class MethodTypeResolution { public static boolean isMethodApplicable(Method method, String methodName, int argArity, Class accessingClass, List typeArguments) { - if (method.getName().equals(methodName) // name matches + return method.getName().equals(methodName) // name matches // is visible && isMemberVisibleFromClass(method.getDeclaringClass(), method.getModifiers(), accessingClass) // if method is vararg with arity n, then the invocation's arity >= n - 1 @@ -534,12 +534,7 @@ public final class MethodTypeResolution { && (method.isVarArgs() || argArity == getArity(method)) // isn't generic or arity of type arguments matches that of parameters && (!isGeneric(method) || typeArguments.isEmpty() - || method.getTypeParameters().length == typeArguments.size())) { - - return true; - } - - return false; + || method.getTypeParameters().length == typeArguments.size()); } @@ -631,15 +626,10 @@ public final class MethodTypeResolution { // covers unboxing int indexInBoxed = BOXED_PRIMITIVE_SUBTYPE_ORDER.indexOf(argument.getType()); - if (indexInBoxed != -1 // arg is boxed primitive + return indexInBoxed != -1 // arg is boxed primitive && isSubtypeable(parameter, - JavaTypeDefinition.forClass(PRIMITIVE_SUBTYPE_ORDER.get(indexInBoxed)))) { - return true; - } - + JavaTypeDefinition.forClass(PRIMITIVE_SUBTYPE_ORDER.get(indexInBoxed))); // TODO: add raw unchecked conversion part - - return false; } public static boolean isSubtypeable(JavaTypeDefinition parameter, ASTExpression argument) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/typedefinition/JavaTypeDefinitionSimple.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/typedefinition/JavaTypeDefinitionSimple.java index f8063918a3..2a684371fd 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/typedefinition/JavaTypeDefinitionSimple.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/typedefinition/JavaTypeDefinitionSimple.java @@ -20,6 +20,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; +import java.util.Objects; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; @@ -264,7 +265,7 @@ import java.util.logging.Logger; @Override public boolean equals(Object obj) { - if (obj == null || !(obj instanceof JavaTypeDefinitionSimple)) { + if (!(obj instanceof JavaTypeDefinitionSimple)) { return false; } @@ -341,7 +342,7 @@ import java.util.logging.Logger; } public JavaTypeDefinition getAsSuper(Class superClazz) { - if (clazz == superClazz) { // optimize for same class calls + if (Objects.equals(clazz, superClazz)) { // optimize for same class calls return this; } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/typedefinition/JavaTypeDefinitionUpper.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/typedefinition/JavaTypeDefinitionUpper.java index 22a7a61784..3276302a09 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/typedefinition/JavaTypeDefinitionUpper.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/typedefinition/JavaTypeDefinitionUpper.java @@ -145,11 +145,7 @@ import java.util.Set; } // we assume that the typeList list cannot contain duplicates, then indeed, this will prove equality - if (!Arrays.deepEquals(typeList, otherTypeDef.typeList)) { - return false; - } - - return true; + return Arrays.deepEquals(typeList, otherTypeDef.typeList); } @Override diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/typeinference/TypeInferenceResolver.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/typeinference/TypeInferenceResolver.java index d6a07ea584..5ee07c7237 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/typeinference/TypeInferenceResolver.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/typeinference/TypeInferenceResolver.java @@ -15,6 +15,7 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import net.sourceforge.pmd.lang.java.typeresolution.MethodTypeResolution; @@ -184,7 +185,7 @@ public final class TypeInferenceResolver { outter: for (Class candidate : erasedSet) { for (Class erasedSetMember : erasedSet) { - if (candidate != erasedSetMember + if (!Objects.equals(candidate, erasedSetMember) && MethodTypeResolution.isSubtypeable(candidate, erasedSetMember)) { continue outter; // skip candidate from result set } @@ -276,7 +277,7 @@ public final class TypeInferenceResolver { for (Variable unresolvedVariable : variables) { for (Variable dependency : dependencies.get(unresolvedVariable)) { if (!instantiations.containsKey(dependency) - && unresolvedVariable != dependency + && !Objects.equals(unresolvedVariable, dependency) && !boundsHaveAnEqualityBetween(variables, dependency, bounds)) { return false; } @@ -324,58 +325,60 @@ public final class TypeInferenceResolver { @Override public Iterator> iterator() { - return new Iterator>() { - private BitSet nextBitSet = new BitSet(n); + return new CombinationsIterator(); + } - { + private class CombinationsIterator implements Iterator> { + private BitSet nextBitSet = new BitSet(n); + + private CombinationsIterator() { + advanceToNextK(); + } + + private void advanceToNextK() { + k++; + if (k > n) { + nextBitSet = null; + } else { + nextBitSet.clear(); + nextBitSet.set(0, k); + } + } + + @Override + public void remove() { + throw new UnsupportedOperationException("remove"); + } + + @Override + public boolean hasNext() { + return nextBitSet != null; + } + + @Override + public List next() { + BitSet resultBitSet = (BitSet) nextBitSet.clone(); + + int b = nextBitSet.previousClearBit(n - 1); + int b1 = nextBitSet.previousSetBit(b); + + if (b1 == -1) { advanceToNextK(); + } else { + nextBitSet.clear(b1); + nextBitSet.set(b1 + 1, b1 + (n - b) + 1); + nextBitSet.clear(b1 + (n - b) + 1, n); } - @Override - public void remove() { - - } - - private void advanceToNextK() { - k++; - if (k > n) { - nextBitSet = null; - } else { - nextBitSet.clear(); - nextBitSet.set(0, k); + resultList.clear(); + for (int i = 0; i < n; ++i) { + if (resultBitSet.get(i)) { + resultList.add(permuteThis.get(i)); } } - @Override - public boolean hasNext() { - return nextBitSet != null; - } - - @Override - public List next() { - BitSet resultBitSet = (BitSet) nextBitSet.clone(); - - int b = nextBitSet.previousClearBit(n - 1); - int b1 = nextBitSet.previousSetBit(b); - - if (b1 == -1) { - advanceToNextK(); - } else { - nextBitSet.clear(b1); - nextBitSet.set(b1 + 1, b1 + (n - b) + 1); - nextBitSet.clear(b1 + (n - b) + 1, n); - } - - resultList.clear(); - for (int i = 0; i < n; ++i) { - if (resultBitSet.get(i)) { - resultList.add(permuteThis.get(i)); - } - } - - return unmodifyableViewOfResult; - } - }; + return unmodifyableViewOfResult; + } } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/visitors/PMDASMVisitor.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/visitors/PMDASMVisitor.java index 649295e095..fe29dd3d50 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/visitors/PMDASMVisitor.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/typeresolution/visitors/PMDASMVisitor.java @@ -10,7 +10,6 @@ import java.util.List; import java.util.Map; import org.objectweb.asm.AnnotationVisitor; -import org.objectweb.asm.Attribute; import org.objectweb.asm.ClassVisitor; import org.objectweb.asm.FieldVisitor; import org.objectweb.asm.Label; @@ -126,10 +125,6 @@ public class PMDASMVisitor extends ClassVisitor { return methodVisitor; } - @Override - public void visitSource(String source, String debug) { - } - @Override public void visitInnerClass(String name, String outerName, String innerName, int access) { if (!this.outerName.replace('.', '/').equals(outerName)) { @@ -147,14 +142,6 @@ public class PMDASMVisitor extends ClassVisitor { packages.put(innerName, name.replace('/', '.')); } - @Override - public void visitOuterClass(String owner, String name, String desc) { - } - - @Override - public void visitEnd() { - } - private void addMethodDesc(String desc) { addTypes(desc); addType(Type.getReturnType(desc)); @@ -181,10 +168,6 @@ public class PMDASMVisitor extends ClassVisitor { } } - @Override - public void visitAttribute(Attribute attr) { - } - /* * Start visitors */ @@ -203,14 +186,6 @@ public class PMDASMVisitor extends ClassVisitor { parent.addType(Type.getType(desc)); return parent.annotationVisitor; } - - @Override - public void visitAttribute(Attribute attr) { - } - - @Override - public void visitEnd() { - } } private static class PMDAnnotationVisitor extends AnnotationVisitor { @@ -237,10 +212,6 @@ public class PMDASMVisitor extends ClassVisitor { return this; } - @Override - public void visitEnd() { - } - @Override public void visit(String name, Object value) { if (value instanceof Type) { @@ -257,10 +228,6 @@ public class PMDASMVisitor extends ClassVisitor { this.parent = visitor; } - @Override - public void visitFormalTypeParameter(String name) { - } - @Override public SignatureVisitor visitClassBound() { return this; @@ -296,14 +263,6 @@ public class PMDASMVisitor extends ClassVisitor { return this; } - @Override - public void visitBaseType(char descriptor) { - } - - @Override - public void visitTypeVariable(String name) { - } - @Override public SignatureVisitor visitArrayType() { return this; @@ -319,18 +278,10 @@ public class PMDASMVisitor extends ClassVisitor { // parent.parseClassName(name); } - @Override - public void visitTypeArgument() { - } - @Override public SignatureVisitor visitTypeArgument(char wildcard) { return this; } - - @Override - public void visitEnd() { - } } private static class PMDMethodVisitor extends MethodVisitor { @@ -347,11 +298,6 @@ public class PMDASMVisitor extends ClassVisitor { return parent.annotationVisitor; } - public AnnotationVisitor visitAnnotation(String name, String desc) { - parent.addType(Type.getType(desc)); - return parent.annotationVisitor; - } - @Override public void visitTypeInsn(int opcode, String desc) { if (desc.charAt(0) == '[') { @@ -399,59 +345,11 @@ public class PMDASMVisitor extends ClassVisitor { parent.extractSignature(sig); } - @Override - public void visitCode() { - } - - @Override - public void visitFrame(int type, int nLocal, Object[] local, int nStack, Object[] stack) { - } - - @Override - public void visitInsn(int opcode) { - } - - @Override - public void visitIntInsn(int opcode, int operand) { - } - - @Override - public void visitVarInsn(int opcode, int var) { - } - - @Override - public void visitJumpInsn(int opcode, Label label) { - } - - @Override - public void visitLabel(Label label) { - } - - @Override - public void visitIincInsn(int var, int increment) { - } - - @Override - public void visitTableSwitchInsn(int min, int max, Label dflt, Label... labels) { - } - - @Override - public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) { - } - @Override public void visitTryCatchBlock(Label start, Label end, Label handler, String type) { parent.parseClassName(type); } - @Override - public void visitLineNumber(int line, Label start) { - } - - @Override - public void visitMaxs(int maxStack, int maxLocals) { - } - @Override public AnnotationVisitor visitAnnotationDefault() { return parent.annotationVisitor; @@ -462,14 +360,5 @@ public class PMDASMVisitor extends ClassVisitor { parent.addType(Type.getType(desc)); return parent.annotationVisitor; } - - @Override - public void visitEnd() { - } - - @Override - public void visitAttribute(Attribute attr) { - } - } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/xpath/MetricFunction.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/xpath/MetricFunction.java index bb795bf46c..b59b68a760 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/xpath/MetricFunction.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/xpath/MetricFunction.java @@ -5,6 +5,7 @@ package net.sourceforge.pmd.lang.java.xpath; import java.util.List; +import java.util.Locale; import java.util.Map; import org.apache.commons.lang3.EnumUtils; @@ -82,7 +83,7 @@ public class MetricFunction implements Function { private static JavaClassMetricKey getClassMetricKey(String s) { - String constantName = s.toUpperCase(); + String constantName = s.toUpperCase(Locale.ROOT); if (!CLASS_METRIC_KEY_MAP.containsKey(constantName)) { throw new IllegalArgumentException(badClassMetricKeyMessage()); } @@ -91,7 +92,7 @@ public class MetricFunction implements Function { private static JavaOperationMetricKey getOperationMetricKey(String s) { - String constantName = s.toUpperCase(); + String constantName = s.toUpperCase(Locale.ROOT); if (!OPERATION_METRIC_KEY_MAP.containsKey(constantName)) { throw new IllegalArgumentException(badOperationMetricKeyMessage()); } diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/multifile/PackageStatsTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/multifile/PackageStatsTest.java index 8b17a99d26..f5464a1a11 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/multifile/PackageStatsTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/multifile/PackageStatsTest.java @@ -34,7 +34,8 @@ public class PackageStatsTest { @Before public void setUp() { - pack = new PackageStats(); + pack = PackageStats.INSTANCE; + pack.reset(); } diff --git a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/Ecmascript3Handler.java b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/Ecmascript3Handler.java index cf21d6fca8..37de975367 100644 --- a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/Ecmascript3Handler.java +++ b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/Ecmascript3Handler.java @@ -12,14 +12,12 @@ import net.sourceforge.pmd.lang.ParserOptions; import net.sourceforge.pmd.lang.VisitorStarter; import net.sourceforge.pmd.lang.XPathHandler; import net.sourceforge.pmd.lang.ast.Node; -import net.sourceforge.pmd.lang.ast.xpath.AbstractASTXPathHandler; +import net.sourceforge.pmd.lang.ast.xpath.DefaultASTXPathHandler; import net.sourceforge.pmd.lang.ecmascript.ast.DumpFacade; import net.sourceforge.pmd.lang.ecmascript.ast.EcmascriptNode; import net.sourceforge.pmd.lang.ecmascript.rule.EcmascriptRuleViolationFactory; import net.sourceforge.pmd.lang.rule.RuleViolationFactory; -import net.sf.saxon.sxpath.IndependentContext; - /** * Implementation of LanguageVersionHandler for the ECMAScript Version 3. */ @@ -27,13 +25,7 @@ public class Ecmascript3Handler extends AbstractLanguageVersionHandler { @Override public XPathHandler getXPathHandler() { - return new AbstractASTXPathHandler() { - public void initialize() { - } - - public void initialize(IndependentContext context) { - } - }; + return new DefaultASTXPathHandler(); } public RuleViolationFactory getRuleViolationFactory() { diff --git a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTKeywordLiteral.java b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTKeywordLiteral.java index d69ce8034d..9d797fe6d4 100644 --- a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTKeywordLiteral.java +++ b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTKeywordLiteral.java @@ -4,13 +4,15 @@ package net.sourceforge.pmd.lang.ecmascript.ast; +import java.util.Locale; + import org.mozilla.javascript.Token; import org.mozilla.javascript.ast.KeywordLiteral; public class ASTKeywordLiteral extends AbstractEcmascriptNode { public ASTKeywordLiteral(KeywordLiteral keywordLiteral) { super(keywordLiteral); - super.setImage(Token.typeToName(keywordLiteral.getType()).toLowerCase()); + super.setImage(Token.typeToName(keywordLiteral.getType()).toLowerCase(Locale.ROOT)); } /** diff --git a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTVariableDeclaration.java b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTVariableDeclaration.java index c3f4fa4cc4..98236f0e38 100644 --- a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTVariableDeclaration.java +++ b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript/ast/ASTVariableDeclaration.java @@ -4,13 +4,15 @@ package net.sourceforge.pmd.lang.ecmascript.ast; +import java.util.Locale; + import org.mozilla.javascript.Token; import org.mozilla.javascript.ast.VariableDeclaration; public class ASTVariableDeclaration extends AbstractEcmascriptNode { public ASTVariableDeclaration(VariableDeclaration variableDeclaration) { super(variableDeclaration); - super.setImage(Token.typeToName(variableDeclaration.getType()).toLowerCase()); + super.setImage(Token.typeToName(variableDeclaration.getType()).toLowerCase(Locale.ROOT)); } /** diff --git a/pmd-jsp/src/main/java/net/sourceforge/pmd/lang/jsp/JspHandler.java b/pmd-jsp/src/main/java/net/sourceforge/pmd/lang/jsp/JspHandler.java index 33d07840d1..094d8da62f 100644 --- a/pmd-jsp/src/main/java/net/sourceforge/pmd/lang/jsp/JspHandler.java +++ b/pmd-jsp/src/main/java/net/sourceforge/pmd/lang/jsp/JspHandler.java @@ -12,14 +12,12 @@ import net.sourceforge.pmd.lang.ParserOptions; import net.sourceforge.pmd.lang.VisitorStarter; import net.sourceforge.pmd.lang.XPathHandler; import net.sourceforge.pmd.lang.ast.Node; -import net.sourceforge.pmd.lang.ast.xpath.AbstractASTXPathHandler; +import net.sourceforge.pmd.lang.ast.xpath.DefaultASTXPathHandler; import net.sourceforge.pmd.lang.jsp.ast.DumpFacade; import net.sourceforge.pmd.lang.jsp.ast.JspNode; import net.sourceforge.pmd.lang.jsp.rule.JspRuleViolationFactory; import net.sourceforge.pmd.lang.rule.RuleViolationFactory; -import net.sf.saxon.sxpath.IndependentContext; - /** * Implementation of LanguageVersionHandler for the JSP parser. * @@ -29,13 +27,7 @@ public class JspHandler extends AbstractLanguageVersionHandler { @Override public XPathHandler getXPathHandler() { - return new AbstractASTXPathHandler() { - public void initialize() { - } - - public void initialize(IndependentContext context) { - } - }; + return new DefaultASTXPathHandler(); } public RuleViolationFactory getRuleViolationFactory() { diff --git a/pmd-jsp/src/main/java/net/sourceforge/pmd/lang/jsp/rule/design/NoInlineStyleInformationRule.java b/pmd-jsp/src/main/java/net/sourceforge/pmd/lang/jsp/rule/design/NoInlineStyleInformationRule.java index 39c2a37db8..77d520a1af 100644 --- a/pmd-jsp/src/main/java/net/sourceforge/pmd/lang/jsp/rule/design/NoInlineStyleInformationRule.java +++ b/pmd-jsp/src/main/java/net/sourceforge/pmd/lang/jsp/rule/design/NoInlineStyleInformationRule.java @@ -4,6 +4,7 @@ package net.sourceforge.pmd.lang.jsp.rule.design; +import java.util.Locale; import java.util.Set; import net.sourceforge.pmd.lang.jsp.ast.ASTAttribute; @@ -64,7 +65,7 @@ public class NoInlineStyleInformationRule extends AbstractJspRule { * @return boolean */ private boolean isStyleElement(ASTElement elementNode) { - return STYLE_ELEMENT_NAMES.contains(elementNode.getName().toUpperCase()); + return STYLE_ELEMENT_NAMES.contains(elementNode.getName().toUpperCase(Locale.ROOT)); } /** @@ -77,10 +78,10 @@ public class NoInlineStyleInformationRule extends AbstractJspRule { * otherwise. */ private boolean isStyleAttribute(ASTAttribute attributeNode) { - if (STYLE_ATTRIBUTES.contains(attributeNode.getName().toUpperCase())) { + if (STYLE_ATTRIBUTES.contains(attributeNode.getName().toUpperCase(Locale.ROOT))) { if (attributeNode.jjtGetParent() instanceof ASTElement) { ASTElement parent = (ASTElement) attributeNode.jjtGetParent(); - if (ELEMENT_NAMES_THAT_CAN_HAVE_STYLE_ATTRIBUTES.contains(parent.getName().toUpperCase())) { + if (ELEMENT_NAMES_THAT_CAN_HAVE_STYLE_ATTRIBUTES.contains(parent.getName().toUpperCase(Locale.ROOT))) { return true; } } diff --git a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/PLSQLHandler.java b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/PLSQLHandler.java index a467272921..5a8d8388c8 100644 --- a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/PLSQLHandler.java +++ b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/PLSQLHandler.java @@ -6,8 +6,6 @@ package net.sourceforge.pmd.lang.plsql; import java.io.Writer; -import org.jaxen.Navigator; - import net.sourceforge.pmd.lang.AbstractLanguageVersionHandler; import net.sourceforge.pmd.lang.DataFlowHandler; import net.sourceforge.pmd.lang.Parser; @@ -15,7 +13,7 @@ import net.sourceforge.pmd.lang.ParserOptions; import net.sourceforge.pmd.lang.VisitorStarter; import net.sourceforge.pmd.lang.XPathHandler; import net.sourceforge.pmd.lang.ast.Node; -import net.sourceforge.pmd.lang.ast.xpath.DocumentNavigator; +import net.sourceforge.pmd.lang.ast.xpath.DefaultASTXPathHandler; import net.sourceforge.pmd.lang.dfa.DFAGraphRule; import net.sourceforge.pmd.lang.plsql.ast.ASTInput; import net.sourceforge.pmd.lang.plsql.ast.DumpFacade; @@ -26,8 +24,6 @@ import net.sourceforge.pmd.lang.plsql.rule.PLSQLRuleViolationFactory; import net.sourceforge.pmd.lang.plsql.symboltable.SymbolFacade; import net.sourceforge.pmd.lang.rule.RuleViolationFactory; -import net.sf.saxon.sxpath.IndependentContext; - /** * Implementation of LanguageVersionHandler for the PLSQL AST. It uses anonymous * classes as adapters of the visitors to the VisitorStarter interface. @@ -81,21 +77,11 @@ public class PLSQLHandler extends AbstractLanguageVersionHandler { }; } - @Override /** * Return minimal XPathHandler to cope with Jaxen XPath Rules. */ + @Override public XPathHandler getXPathHandler() { - return new XPathHandler() { - public void initialize() { - } - - public void initialize(IndependentContext context) { - } - - public Navigator getNavigator() { - return new DocumentNavigator(); - } - }; + return new DefaultASTXPathHandler(); } } diff --git a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/PLSQLParser.java b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/PLSQLParser.java index d828abd947..40e4d1f6e2 100644 --- a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/PLSQLParser.java +++ b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/PLSQLParser.java @@ -35,8 +35,7 @@ public class PLSQLParser extends AbstractParser { protected net.sourceforge.pmd.lang.plsql.ast.PLSQLParser createPLSQLParser(Reader source) throws ParseException { Reader in = IOUtil.skipBOM(source); // Wrapped PLSQL AST Parser - net.sourceforge.pmd.lang.plsql.ast.PLSQLParser parser = new net.sourceforge.pmd.lang.plsql.ast.PLSQLParser(in); - return parser; + return new net.sourceforge.pmd.lang.plsql.ast.PLSQLParser(in); } public boolean canParse() { diff --git a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/rule/AbstractStatisticalPLSQLRule.java b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/rule/AbstractStatisticalPLSQLRule.java index 4971f0e6a7..d83ac18789 100644 --- a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/rule/AbstractStatisticalPLSQLRule.java +++ b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/rule/AbstractStatisticalPLSQLRule.java @@ -21,7 +21,7 @@ public abstract class AbstractStatisticalPLSQLRule extends AbstractPLSQLRule imp } public Object[] getViolationParameters(DataPoint point) { - return null; + return new Object[0]; } @Override diff --git a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/rule/design/AbstractNcssCountRule.java b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/rule/design/AbstractNcssCountRule.java index 4585a16e62..180fb22d66 100644 --- a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/rule/design/AbstractNcssCountRule.java +++ b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/rule/design/AbstractNcssCountRule.java @@ -119,26 +119,17 @@ public abstract class AbstractNcssCountRule extends AbstractStatisticalPLSQLRule @Override public Object visit(ASTIfStatement node, Object data) { - - Integer lineCount = countNodeChildren(node, data); - - return lineCount; + return countNodeChildren(node, data); } @Override public Object visit(ASTElsifClause node, Object data) { - - Integer lineCount = countNodeChildren(node, data); - - return lineCount; + return countNodeChildren(node, data); } @Override public Object visit(ASTElseClause node, Object data) { - - Integer lineCount = countNodeChildren(node, data); - - return lineCount; + return countNodeChildren(node, data); } @Override diff --git a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/symboltable/PLSQLNameOccurrence.java b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/symboltable/PLSQLNameOccurrence.java index f52529e067..fca9e59238 100644 --- a/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/symboltable/PLSQLNameOccurrence.java +++ b/pmd-plsql/src/main/java/net/sourceforge/pmd/lang/plsql/symboltable/PLSQLNameOccurrence.java @@ -94,15 +94,11 @@ public class PLSQLNameOccurrence implements NameOccurrence { * ASTAssignmentOperator)) { return false; } */ - if (isPartOfQualifiedName() /* or is an array type */) { - return false; - } - /* * if (isCompoundAssignment(primaryExpression)) { return false; } */ - return true; + return !isPartOfQualifiedName() /* and not is an array type */; } /* diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/test/lang/DummyLanguageModule.java b/pmd-test/src/main/java/net/sourceforge/pmd/test/lang/DummyLanguageModule.java index 4527f14b75..f78e1dd4f8 100644 --- a/pmd-test/src/main/java/net/sourceforge/pmd/test/lang/DummyLanguageModule.java +++ b/pmd-test/src/main/java/net/sourceforge/pmd/test/lang/DummyLanguageModule.java @@ -113,9 +113,9 @@ public class DummyLanguageModule extends BaseLanguageModule { protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String message, int beginLine, int endLine) { ParametricRuleViolation rv = new ParametricRuleViolation(rule, ruleContext, node, message) { - { - // just for testing variable expansion - this.packageName = "foo"; + public String getPackageName() { + this.packageName = "foo"; // just for testing variable expansion + return super.getPackageName(); } }; rv.setLines(beginLine, endLine); diff --git a/pmd-test/src/main/java/net/sourceforge/pmd/testframework/TestDescriptor.java b/pmd-test/src/main/java/net/sourceforge/pmd/testframework/TestDescriptor.java index 68b2e2a95e..dcb4ba6fd4 100644 --- a/pmd-test/src/main/java/net/sourceforge/pmd/testframework/TestDescriptor.java +++ b/pmd-test/src/main/java/net/sourceforge/pmd/testframework/TestDescriptor.java @@ -32,9 +32,8 @@ public class TestDescriptor { private boolean useAuxClasspath = true; private int numberInDocument = -1; - // Empty descriptor added to please mvn surefire plugin public TestDescriptor() { - + // Empty default descriptor added to please mvn surefire plugin } public TestDescriptor(String code, String description, int numberOfProblemsExpected, Rule rule) { diff --git a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/EditPropertyDialogController.java b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/EditPropertyDialogController.java index bfcdb8a5f9..1c692c8740 100644 --- a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/EditPropertyDialogController.java +++ b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/EditPropertyDialogController.java @@ -68,7 +68,7 @@ public class EditPropertyDialogController implements Initializable { public EditPropertyDialogController() { - + // default constructor } diff --git a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/SourceEditorController.java b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/SourceEditorController.java index c6c71c98a6..9e4731318c 100644 --- a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/SourceEditorController.java +++ b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/SourceEditorController.java @@ -45,8 +45,6 @@ import javafx.scene.control.TreeView; * @since 6.0.0 */ public class SourceEditorController implements Initializable, SettingsOwner { - - private final DesignerRoot designerRoot; private final MainDesignerController parent; @FXML @@ -60,9 +58,8 @@ public class SourceEditorController implements Initializable, SettingsOwner { public SourceEditorController(DesignerRoot owner, MainDesignerController mainController) { - this.designerRoot = owner; parent = mainController; - astManager = new ASTManager(designerRoot); + astManager = new ASTManager(owner); } @@ -100,7 +97,7 @@ public class SourceEditorController implements Initializable, SettingsOwner { invalidateAST(true); return; } - if (previous != current) { + if (!Objects.equals(previous, current)) { parent.invalidateAst(); setUpToDateCompilationUnit(current); } diff --git a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/XPathPanelController.java b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/XPathPanelController.java index 5c7a275ce0..236333426e 100644 --- a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/XPathPanelController.java +++ b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/XPathPanelController.java @@ -73,6 +73,7 @@ public class XPathPanelController implements Initializable, SettingsOwner { @FXML private ListView xpathResultListView; // Actually a child of the main view toolbar, but this controller is responsible for it + @SuppressWarnings("PMD.SingularField") private ChoiceBox xpathVersionChoiceBox; diff --git a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/DesignerUtil.java b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/DesignerUtil.java index ed48f91ef0..662fdff4b0 100644 --- a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/DesignerUtil.java +++ b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/DesignerUtil.java @@ -11,6 +11,7 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Optional; import java.util.function.Consumer; @@ -32,7 +33,7 @@ import javafx.util.StringConverter; * @author Clément Fournier * @since 6.0.0 */ -public class DesignerUtil { +public final class DesignerUtil { private static final Path PMD_SETTINGS_DIR = Paths.get(System.getProperty("user.home"), ".pmd"); @@ -98,7 +99,7 @@ public class DesignerUtil { public static StringConverter languageVersionStringConverter() { return DesignerUtil.stringConverter(LanguageVersion::getShortName, - s -> LanguageRegistry.findLanguageVersionByTerseName(s.toLowerCase())); + s -> LanguageRegistry.findLanguageVersionByTerseName(s.toLowerCase(Locale.ROOT))); } @@ -114,7 +115,7 @@ public class DesignerUtil { } - public static LanguageVersion getLanguageVersionFromExtension(String filename) { + public static synchronized LanguageVersion getLanguageVersionFromExtension(String filename) { if (extensionsToLanguage == null) { extensionsToLanguage = getExtensionsToLanguageMap(); } @@ -127,7 +128,7 @@ public class DesignerUtil { } - public static List getSupportedLanguageVersions() { + public static synchronized List getSupportedLanguageVersions() { if (supportedLanguageVersions == null) { List languageVersions = new ArrayList<>(); for (LanguageVersion languageVersion : LanguageRegistry.findAllVersions()) { diff --git a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/beans/SettingsPersistenceUtil.java b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/beans/SettingsPersistenceUtil.java index a47a9cabd8..e65fbca5f7 100644 --- a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/beans/SettingsPersistenceUtil.java +++ b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/beans/SettingsPersistenceUtil.java @@ -44,7 +44,7 @@ import net.sourceforge.pmd.util.fxdesigner.util.beans.converters.RulePriorityCon * @see SettingsOwner * @since 6.1.0 */ -public class SettingsPersistenceUtil { +public final class SettingsPersistenceUtil { static { // register converters for custom types diff --git a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/controls/ASTTreeItem.java b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/controls/ASTTreeItem.java index da12780fca..f6e7cd09a2 100644 --- a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/controls/ASTTreeItem.java +++ b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/controls/ASTTreeItem.java @@ -15,7 +15,7 @@ import javafx.scene.control.TreeItem; * @author Clément Fournier * @since 6.0.0 */ -public class ASTTreeItem extends TreeItem { +public final class ASTTreeItem extends TreeItem { private ASTTreeItem(Node n) { diff --git a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/controls/ScopeHierarchyTreeItem.java b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/controls/ScopeHierarchyTreeItem.java index 72faf47399..9b5c450560 100644 --- a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/controls/ScopeHierarchyTreeItem.java +++ b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/controls/ScopeHierarchyTreeItem.java @@ -20,7 +20,7 @@ import javafx.scene.control.TreeItem; * @author Clément Fournier * @since 6.0.0 */ -public class ScopeHierarchyTreeItem extends TreeItem { +public final class ScopeHierarchyTreeItem extends TreeItem { private ScopeHierarchyTreeItem(Object scopeOrDecl) { super(scopeOrDecl); diff --git a/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/VfHandler.java b/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/VfHandler.java index 986b44bd90..8bb6d8509f 100644 --- a/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/VfHandler.java +++ b/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/VfHandler.java @@ -12,25 +12,17 @@ import net.sourceforge.pmd.lang.ParserOptions; import net.sourceforge.pmd.lang.VisitorStarter; import net.sourceforge.pmd.lang.XPathHandler; import net.sourceforge.pmd.lang.ast.Node; -import net.sourceforge.pmd.lang.ast.xpath.AbstractASTXPathHandler; +import net.sourceforge.pmd.lang.ast.xpath.DefaultASTXPathHandler; import net.sourceforge.pmd.lang.rule.RuleViolationFactory; import net.sourceforge.pmd.lang.vf.ast.DumpFacade; import net.sourceforge.pmd.lang.vf.ast.VfNode; import net.sourceforge.pmd.lang.vf.rule.VfRuleViolationFactory; -import net.sf.saxon.sxpath.IndependentContext; - public class VfHandler extends AbstractLanguageVersionHandler { @Override public XPathHandler getXPathHandler() { - return new AbstractASTXPathHandler() { - public void initialize() { - } - - public void initialize(IndependentContext context) { - } - }; + return new DefaultASTXPathHandler(); } public RuleViolationFactory getRuleViolationFactory() { diff --git a/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/rule/security/VfCsrfRule.java b/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/rule/security/VfCsrfRule.java index 80877ac92b..447d0c0e9c 100644 --- a/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/rule/security/VfCsrfRule.java +++ b/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/rule/security/VfCsrfRule.java @@ -5,6 +5,7 @@ package net.sourceforge.pmd.lang.vf.rule.security; import java.util.List; +import java.util.Locale; import net.sourceforge.pmd.lang.vf.ast.ASTAttribute; import net.sourceforge.pmd.lang.vf.ast.ASTElExpression; @@ -29,7 +30,7 @@ public class VfCsrfRule extends AbstractVfRule { ASTElExpression valToReport = null; for (ASTAttribute attr : attribs) { - switch (attr.getName().toLowerCase()) { + switch (attr.getName().toLowerCase(Locale.ROOT)) { case "action": ASTElExpression value = attr.getFirstDescendantOfType(ASTElExpression.class); if (value != null) { diff --git a/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/rule/security/VfUnescapeElRule.java b/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/rule/security/VfUnescapeElRule.java index 402f9cd19e..ac1f2e72f9 100644 --- a/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/rule/security/VfUnescapeElRule.java +++ b/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/rule/security/VfUnescapeElRule.java @@ -7,6 +7,7 @@ package net.sourceforge.pmd.lang.vf.rule.security; import java.util.EnumSet; import java.util.HashSet; import java.util.List; +import java.util.Locale; import java.util.Set; import java.util.regex.Pattern; @@ -107,11 +108,7 @@ public class VfUnescapeElRule extends AbstractVfRule { final String text = prevText.getImage().endsWith("'") ? prevText.getImage().substring(0, prevText.getImage().length() - 1) : prevText.getImage(); - if (text.endsWith("JSON.parse(") || text.endsWith("jQuery.parseJSON(") || text.endsWith("$.parseJSON(")) { - return true; - } - - return false; + return text.endsWith("JSON.parse(") || text.endsWith("jQuery.parseJSON(") || text.endsWith("$.parseJSON("); } private boolean isUnbalanced(String image, char pattern) { @@ -125,11 +122,7 @@ public class VfUnescapeElRule extends AbstractVfRule { } if (array[i] == ';') { - if (foundPattern) { - return true; - } else { - return false; - } + return foundPattern; } } @@ -149,7 +142,7 @@ public class VfUnescapeElRule extends AbstractVfRule { } private void checkLimitedFlags(ASTElement node, Object data) { - switch (node.getName().toLowerCase()) { + switch (node.getName().toLowerCase(Locale.ROOT)) { case IFRAME_CONST: case APEXIFRAME_CONST: case A_CONST: @@ -163,7 +156,7 @@ public class VfUnescapeElRule extends AbstractVfRule { final Set toReport = new HashSet<>(); for (ASTAttribute attr : attributes) { - String name = attr.getName().toLowerCase(); + String name = attr.getName().toLowerCase(Locale.ROOT); // look for onevents if (HREF.equalsIgnoreCase(name) || SRC.equalsIgnoreCase(name)) { @@ -172,8 +165,9 @@ public class VfUnescapeElRule extends AbstractVfRule { final ASTText attrText = attr.getFirstDescendantOfType(ASTText.class); if (attrText != null) { if (0 == attrText.jjtGetChildIndex()) { - if (attrText.getImage().startsWith("/") || attrText.getImage().toLowerCase().startsWith("http") - || attrText.getImage().toLowerCase().startsWith("mailto")) { + String lowerCaseImage = attrText.getImage().toLowerCase(Locale.ROOT); + if (lowerCaseImage.startsWith("/") || lowerCaseImage.startsWith("http") + || lowerCaseImage.startsWith("mailto")) { startingWithSlashText = true; } } @@ -215,7 +209,7 @@ public class VfUnescapeElRule extends AbstractVfRule { final Set toReport = new HashSet<>(); for (ASTAttribute attr : attributes) { - String name = attr.getName().toLowerCase(); + String name = attr.getName().toLowerCase(Locale.ROOT); // look for onevents if (ON_EVENT.matcher(name).matches()) { @@ -253,14 +247,15 @@ public class VfUnescapeElRule extends AbstractVfRule { final ASTIdentifier id = expression.getFirstChildOfType(ASTIdentifier.class); if (id != null) { + String lowerCaseId = id.getImage().toLowerCase(Locale.ROOT); List args = expression.findChildrenOfType(ASTArguments.class); if (!args.isEmpty()) { - switch (id.getImage().toLowerCase()) { + switch (lowerCaseId) { case "urlfor": case "casesafeid": case "begins": case "contains": - case "len": + case "len": case "getrecordids": case "linkto": case "sqrt": @@ -292,7 +287,7 @@ public class VfUnescapeElRule extends AbstractVfRule { } } else { // has no arguments - switch (id.getImage().toLowerCase()) { + switch (lowerCaseId) { case "$action": case "$page": case "$site": @@ -318,9 +313,10 @@ public class VfUnescapeElRule extends AbstractVfRule { if (expression != null) { final ASTLiteral literal = expression.getFirstChildOfType(ASTLiteral.class); if (literal != null && literal.jjtGetChildIndex() == 0) { - if (literal.getImage().startsWith("'/") || literal.getImage().startsWith("\"/") - || literal.getImage().toLowerCase().startsWith("'http") - || literal.getImage().toLowerCase().startsWith("\"http")) { + String lowerCaseLiteral = literal.getImage().toLowerCase(Locale.ROOT); + if (lowerCaseLiteral.startsWith("'/") || lowerCaseLiteral.startsWith("\"/") + || lowerCaseLiteral.startsWith("'http") + || lowerCaseLiteral.startsWith("\"http")) { return true; } } @@ -338,7 +334,7 @@ public class VfUnescapeElRule extends AbstractVfRule { boolean hasPlaceholders = false; for (ASTAttribute attr : attributes) { - String name = attr.getName().toLowerCase(); + String name = attr.getName().toLowerCase(Locale.ROOT); switch (name) { case ESCAPE: case ITEM_ESCAPED: @@ -457,7 +453,7 @@ public class VfUnescapeElRule extends AbstractVfRule { Node child = expression.jjtGetChild(i); if (child instanceof ASTIdentifier) { - switch (child.getImage().toLowerCase()) { + switch (child.getImage().toLowerCase(Locale.ROOT)) { case "id": case "size": case "caseNumber": @@ -488,7 +484,7 @@ public class VfUnescapeElRule extends AbstractVfRule { return false; } - switch (node.getName().toLowerCase()) { // vf is case insensitive + switch (node.getName().toLowerCase(Locale.ROOT)) { // vf is case insensitive case APEX_OUTPUT_TEXT: case APEX_PAGE_MESSAGE: case APEX_PAGE_MESSAGES: diff --git a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/VmHandler.java b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/VmHandler.java index 084fd58ae9..7ef5796df7 100644 --- a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/VmHandler.java +++ b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/VmHandler.java @@ -12,13 +12,11 @@ import net.sourceforge.pmd.lang.ParserOptions; import net.sourceforge.pmd.lang.VisitorStarter; import net.sourceforge.pmd.lang.XPathHandler; import net.sourceforge.pmd.lang.ast.Node; -import net.sourceforge.pmd.lang.ast.xpath.AbstractASTXPathHandler; +import net.sourceforge.pmd.lang.ast.xpath.DefaultASTXPathHandler; import net.sourceforge.pmd.lang.rule.RuleViolationFactory; import net.sourceforge.pmd.lang.vm.ast.AbstractVmNode; import net.sourceforge.pmd.lang.vm.rule.VmRuleViolationFactory; -import net.sf.saxon.sxpath.IndependentContext; - /** * Implementation of LanguageVersionHandler for the VM parser. * @@ -27,13 +25,7 @@ public class VmHandler extends AbstractLanguageVersionHandler { @Override public XPathHandler getXPathHandler() { - return new AbstractASTXPathHandler() { - public void initialize() { - } - - public void initialize(final IndependentContext context) { - } - }; + return new DefaultASTXPathHandler(); } public RuleViolationFactory getRuleViolationFactory() { diff --git a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/NodeUtils.java b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/NodeUtils.java index 95e5f1e8ec..dd0267a6af 100644 --- a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/NodeUtils.java +++ b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/NodeUtils.java @@ -29,7 +29,7 @@ import org.apache.commons.lang3.text.StrBuilder; * @author Geir Magnusson Jr. * @version $Id: NodeUtils.java 687386 2008-08-20 16:57:07Z nbubna $ */ -public class NodeUtils { +public final class NodeUtils { private NodeUtils() { } /** diff --git a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/TokenMgrError.java b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/TokenMgrError.java index cb9d2dbd1b..288ef69405 100644 --- a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/TokenMgrError.java +++ b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/ast/TokenMgrError.java @@ -45,6 +45,7 @@ public class TokenMgrError extends RuntimeException { */ public TokenMgrError() { + // default constructor } public TokenMgrError(final String message, final int reason) { diff --git a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/rule/AbstractStatisticalVmRule.java b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/rule/AbstractStatisticalVmRule.java index 934f39dcb9..dc78c1a85e 100644 --- a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/rule/AbstractStatisticalVmRule.java +++ b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/rule/AbstractStatisticalVmRule.java @@ -23,7 +23,7 @@ public abstract class AbstractStatisticalVmRule extends AbstractVmRule implement @Override public Object[] getViolationParameters(final DataPoint point) { - return null; + return new Object[0]; } @Override diff --git a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/util/DirectiveMapper.java b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/util/DirectiveMapper.java index ed7eb9caaa..6b72a0d9a8 100644 --- a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/util/DirectiveMapper.java +++ b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/util/DirectiveMapper.java @@ -20,7 +20,7 @@ import net.sourceforge.pmd.lang.vm.directive.Macro; import net.sourceforge.pmd.lang.vm.directive.Parse; import net.sourceforge.pmd.lang.vm.directive.Stop; -public class DirectiveMapper { +public final class DirectiveMapper { private DirectiveMapper() { } private static final Map DIRECTIVE_MAP = new HashMap<>(); diff --git a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/util/LogUtil.java b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/util/LogUtil.java index b925c5b1fa..93a5a38da5 100644 --- a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/util/LogUtil.java +++ b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/util/LogUtil.java @@ -32,7 +32,7 @@ import net.sourceforge.pmd.lang.vm.directive.Directive; * @version $Id: Log.java 724825 2008-12-09 18:56:06Z nbubna $ * @since 1.5 */ -public class LogUtil { +public final class LogUtil { private LogUtil() { } /** @@ -40,7 +40,7 @@ public class LogUtil { * column of the given Directive. We use this routine to provide a cosistent * format for displaying file errors. */ - public static final String formatFileString(final Directive directive) { + public static String formatFileString(final Directive directive) { return formatFileString(directive.getTemplateName(), directive.getLine(), directive.getColumn()); } @@ -49,7 +49,7 @@ public class LogUtil { * column of the given Node. We use this routine to provide a cosistent * format for displaying file errors. */ - public static final String formatFileString(final AbstractVmNode node) { + public static String formatFileString(final AbstractVmNode node) { return formatFileString(node.getTemplateName(), node.getLine(), node.getColumn()); } @@ -65,7 +65,7 @@ public class LogUtil { * @param colnum * Column number withing the file at linenum */ - public static final String formatFileString(String template, final int linenum, final int colnum) { + public static String formatFileString(String template, final int linenum, final int colnum) { if (template == null || "".equals(template)) { template = ""; } diff --git a/pmd-xml/src/main/java/net/sourceforge/pmd/lang/xml/XmlHandler.java b/pmd-xml/src/main/java/net/sourceforge/pmd/lang/xml/XmlHandler.java index 0536fccc80..65d6c528cf 100644 --- a/pmd-xml/src/main/java/net/sourceforge/pmd/lang/xml/XmlHandler.java +++ b/pmd-xml/src/main/java/net/sourceforge/pmd/lang/xml/XmlHandler.java @@ -6,22 +6,18 @@ package net.sourceforge.pmd.lang.xml; import java.io.Writer; -import org.jaxen.Navigator; - import net.sourceforge.pmd.lang.AbstractLanguageVersionHandler; import net.sourceforge.pmd.lang.Parser; import net.sourceforge.pmd.lang.ParserOptions; import net.sourceforge.pmd.lang.VisitorStarter; import net.sourceforge.pmd.lang.XPathHandler; import net.sourceforge.pmd.lang.ast.Node; -import net.sourceforge.pmd.lang.ast.xpath.DocumentNavigator; +import net.sourceforge.pmd.lang.ast.xpath.DefaultASTXPathHandler; import net.sourceforge.pmd.lang.rule.RuleViolationFactory; import net.sourceforge.pmd.lang.xml.ast.DumpFacade; import net.sourceforge.pmd.lang.xml.ast.XmlNode; import net.sourceforge.pmd.lang.xml.rule.XmlRuleViolationFactory; -import net.sf.saxon.sxpath.IndependentContext; - /** * Implementation of LanguageVersionHandler for the XML. */ @@ -29,17 +25,7 @@ public class XmlHandler extends AbstractLanguageVersionHandler { @Override public XPathHandler getXPathHandler() { - return new XPathHandler() { - public void initialize() { - } - - public void initialize(IndependentContext context) { - } - - public Navigator getNavigator() { - return new DocumentNavigator(); - } - }; + return new DefaultASTXPathHandler(); } public RuleViolationFactory getRuleViolationFactory() { diff --git a/pmd-xml/src/main/java/net/sourceforge/pmd/lang/xml/ast/XmlNodeWrapper.java b/pmd-xml/src/main/java/net/sourceforge/pmd/lang/xml/ast/XmlNodeWrapper.java index 8fee8ccc8c..fef8309a0b 100644 --- a/pmd-xml/src/main/java/net/sourceforge/pmd/lang/xml/ast/XmlNodeWrapper.java +++ b/pmd-xml/src/main/java/net/sourceforge/pmd/lang/xml/ast/XmlNodeWrapper.java @@ -40,12 +40,6 @@ public class XmlNodeWrapper extends AbstractDomNodeProxy implements XmlNode { } - @Override - public void jjtOpen() { - - } - - @Override public void jjtClose() { throw new UnsupportedOperationException(); diff --git a/pmd-xml/src/main/java/net/sourceforge/pmd/lang/xml/rule/AbstractDomXmlRule.java b/pmd-xml/src/main/java/net/sourceforge/pmd/lang/xml/rule/AbstractDomXmlRule.java index a4fe974e57..13f3ebb65a 100644 --- a/pmd-xml/src/main/java/net/sourceforge/pmd/lang/xml/rule/AbstractDomXmlRule.java +++ b/pmd-xml/src/main/java/net/sourceforge/pmd/lang/xml/rule/AbstractDomXmlRule.java @@ -86,6 +86,7 @@ public class AbstractDomXmlRule extends AbstractXmlRule { } protected void visit(XmlNode node, Attr attr, RuleContext ctx) { + // does nothing by default since attributes are leaf nodes } protected void visit(XmlNode node, CharacterData characterData, RuleContext ctx) { diff --git a/pom.xml b/pom.xml index 5e9147992c..9bcfa28c5c 100644 --- a/pom.xml +++ b/pom.xml @@ -528,6 +528,7 @@ Additionally it includes CPD, the copy-paste-detector. CPD finds duplicated code target/generated-sources/javacc + target/generated-sources/antlr4 false true