From eaceefc39e97df247667ccf1990a7fe1b414f19c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Thu, 21 Feb 2019 16:51:07 +0100 Subject: [PATCH 01/42] Update XPath rules to 2.0 --- .../resources/category/apex/codestyle.xml | 12 ++-- .../resources/category/apex/errorprone.xml | 2 +- .../pmd/lang/ast/xpath/saxon/ElementNode.java | 7 +++ .../sourceforge/pmd/lang/rule/XPathRule.java | 6 +- .../net/sourceforge/pmd/xml/j2ee.xml | 4 +- .../resources/category/java/bestpractices.xml | 12 ++-- .../resources/category/java/codestyle.xml | 57 ++++++++--------- .../main/resources/category/java/design.xml | 20 +++--- .../resources/category/java/documentation.xml | 6 +- .../resources/category/java/errorprone.xml | 63 +++++++++---------- .../category/java/multithreading.xml | 8 +-- .../resources/category/java/performance.xml | 4 +- .../category/ecmascript/bestpractices.xml | 2 +- .../category/ecmascript/codestyle.xml | 16 ++--- .../category/ecmascript/errorprone.xml | 6 +- .../resources/category/plsql/codestyle.xml | 4 +- .../resources/category/xsl/performance.xml | 2 +- 17 files changed, 117 insertions(+), 114 deletions(-) diff --git a/pmd-apex/src/main/resources/category/apex/codestyle.xml b/pmd-apex/src/main/resources/category/apex/codestyle.xml index 262b6f9757..72c22c83ce 100644 --- a/pmd-apex/src/main/resources/category/apex/codestyle.xml +++ b/pmd-apex/src/main/resources/category/apex/codestyle.xml @@ -48,9 +48,9 @@ from the rest. 0] +//IfBlockStatement/BlockStatement[@CurlyBrace= false()][count(child::*) > 0] | -//IfElseBlockStatement/BlockStatement[@CurlyBrace='false'][count(child::*) > 0] +//IfElseBlockStatement/BlockStatement[@CurlyBrace= false()][count(child::*) > 0] ]]> @@ -85,7 +85,7 @@ controlled from the rest. @@ -142,9 +142,9 @@ from the rest. @@ -348,7 +348,7 @@ controlled from the rest. diff --git a/pmd-apex/src/main/resources/category/apex/errorprone.xml b/pmd-apex/src/main/resources/category/apex/errorprone.xml index c96110f960..17c9182938 100644 --- a/pmd-apex/src/main/resources/category/apex/errorprone.xml +++ b/pmd-apex/src/main/resources/category/apex/errorprone.xml @@ -189,7 +189,7 @@ Empty block statements serve no purpose and should be removed. diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/saxon/ElementNode.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/saxon/ElementNode.java index 4663e7f1bf..94bec1fbb1 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/saxon/ElementNode.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/saxon/ElementNode.java @@ -122,6 +122,13 @@ public class ElementNode extends AbstractNodeInfo { return result; } + + @Override + public String getDisplayName() { + return getLocalPart(); + } + + @SuppressWarnings("PMD.MissingBreakInSwitch") @Override public AxisIterator iterateAxis(byte axisNumber) { diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/XPathRule.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/XPathRule.java index 773d64a5c0..55257976f9 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/XPathRule.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/XPathRule.java @@ -41,8 +41,8 @@ public class XPathRule extends AbstractRule { static { Map tmp = new HashMap<>(); - tmp.put(XPATH_1_0, XPATH_1_0); - tmp.put(XPATH_1_0_COMPATIBILITY, XPATH_1_0_COMPATIBILITY); + tmp.put(XPATH_1_0, XPATH_2_0); + tmp.put(XPATH_1_0_COMPATIBILITY, XPATH_2_0); tmp.put(XPATH_2_0, XPATH_2_0); XPATH_VERSIONS = Collections.unmodifiableMap(tmp); } @@ -51,7 +51,7 @@ public class XPathRule extends AbstractRule { public static final EnumeratedProperty VERSION_DESCRIPTOR = EnumeratedProperty.named("version") .desc("XPath specification version") .mappings(XPATH_VERSIONS) - .defaultValue(XPATH_1_0) + .defaultValue(XPATH_2_0) .type(String.class) .uiOrder(2.0f) .build(); diff --git a/pmd-core/src/test/resources/net/sourceforge/pmd/xml/j2ee.xml b/pmd-core/src/test/resources/net/sourceforge/pmd/xml/j2ee.xml index f158927f95..607967f6fd 100644 --- a/pmd-core/src/test/resources/net/sourceforge/pmd/xml/j2ee.xml +++ b/pmd-core/src/test/resources/net/sourceforge/pmd/xml/j2ee.xml @@ -324,9 +324,9 @@ behavior especially when instances are distributed by the container on several J ) and (./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration[ - (./FieldDeclaration[@Static = 'true']) + (./FieldDeclaration[@Static = true()]) and - (./FieldDeclaration[@Final = 'false']) + (./FieldDeclaration[@Final = false()]) ]) ] ]]> diff --git a/pmd-java/src/main/resources/category/java/bestpractices.xml b/pmd-java/src/main/resources/category/java/bestpractices.xml index 5c089e46d7..7dd321aee1 100644 --- a/pmd-java/src/main/resources/category/java/bestpractices.xml +++ b/pmd-java/src/main/resources/category/java/bestpractices.xml @@ -27,8 +27,8 @@ directly) a protected constructor can be provided prevent direct instantiation. @@ -380,7 +380,7 @@ better placed in classes or enums. See Effective Java, item 19. @@ -426,8 +426,8 @@ By convention, the default label should be the last label in a switch statement. @@ -1075,7 +1075,7 @@ can be avoided, they will just return false. //PrimaryExpression[ PrimaryPrefix[Name[(ends-with(@Image, '.equals'))]] [ - (../PrimarySuffix/Arguments/ArgumentList/Expression/PrimaryExpression/PrimaryPrefix/Literal[@StringLiteral='true']) + (../PrimarySuffix/Arguments/ArgumentList/Expression/PrimaryExpression/PrimaryPrefix/Literal[@StringLiteral= true()]) and ( count(../PrimarySuffix/Arguments/ArgumentList/Expression) = 1 ) ] diff --git a/pmd-java/src/main/resources/category/java/codestyle.xml b/pmd-java/src/main/resources/category/java/codestyle.xml index 6f8e3e2ae4..1cb7beabeb 100644 --- a/pmd-java/src/main/resources/category/java/codestyle.xml +++ b/pmd-java/src/main/resources/category/java/codestyle.xml @@ -28,12 +28,12 @@ by {% rule java/codestyle/ClassNamingConventions %}. @@ -116,7 +116,7 @@ If the goal is to avoid defining constants in a scope smaller than the class, th @@ -244,12 +244,13 @@ visibility cannot be reduced). Clarify your intent by using private or package a 3 + @@ -318,7 +319,7 @@ prefix for these methods. @@ -623,16 +624,16 @@ usage by developers who should be implementing their own versions in the concret 1 or - string:upper-case(@Image) != @Image + upper-case(@Image) != @Image ] ]]> @@ -947,7 +948,7 @@ by the rule {% rule java/codestyle/ControlStatementBraces %}. @@ -1728,9 +1729,9 @@ by the more general rule {% rule java/codestyle/FieldNamingConventions %}. @@ -1767,7 +1768,7 @@ which class a static member comes from (Sun 1.5 Language Guide). $maximumStaticImports] +.[count(ImportDeclaration[@Static = true()]) > $maximumStaticImports] ]]> @@ -1970,15 +1971,15 @@ which makes the code also more readable. @@ -2031,7 +2032,7 @@ List stringsWithDiamond = new ArrayList<>(); // using the diamond operat not(./CastExpression) and not(./EqualityExpression)] | -//Expression/AdditiveExpression[not(./PrimaryExpression/PrimaryPrefix/Literal[@StringLiteral='true'])] +//Expression/AdditiveExpression[not(./PrimaryExpression/PrimaryPrefix/Literal[@StringLiteral= true()])] /PrimaryExpression[1]/PrimaryPrefix/Expression[ count(*)=1 and not(./CastExpression) and @@ -2093,9 +2094,9 @@ public class Foo { diff --git a/pmd-java/src/main/resources/category/java/design.xml b/pmd-java/src/main/resources/category/java/design.xml index 73008d3e30..4a3dfa92dc 100644 --- a/pmd-java/src/main/resources/category/java/design.xml +++ b/pmd-java/src/main/resources/category/java/design.xml @@ -27,7 +27,7 @@ protected constructor in order to prevent instantiation than make the class misl @@ -339,9 +339,9 @@ is invoked by a inner class. = 1 ] -[count(./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration[(@Public = 'true') or (@Protected = 'true') or (@PackagePrivate = 'true')]) = 0 ] +[@Final = false()] +[count(./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration[@Private = true()]) >= 1 ] +[count(./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration[(@Public = true()) or (@Protected = true()) or (@PackagePrivate = true())]) = 0 ] [not(.//ClassOrInterfaceDeclaration)] ]]> @@ -370,12 +370,12 @@ Sometimes two consecutive 'if' statements can be consolidated by separating thei @@ -727,7 +727,7 @@ in each object at runtime. @@ -1373,7 +1373,7 @@ ConditionalAndExpression [EqualityExpression[@Image='!=']//NullLiteral and InstanceOfExpression - [PrimaryExpression[count(PrimarySuffix[@ArrayDereference='true'])=0] + [PrimaryExpression[count(PrimarySuffix[@ArrayDereference= true()])=0] //Name[not(contains(@Image,'.'))]/@Image = ancestor::ConditionalAndExpression /EqualityExpression/PrimaryExpression/PrimaryPrefix/Name/@Image] and diff --git a/pmd-java/src/main/resources/category/java/documentation.xml b/pmd-java/src/main/resources/category/java/documentation.xml index 7b6d5cd81f..daee1d547d 100644 --- a/pmd-java/src/main/resources/category/java/documentation.xml +++ b/pmd-java/src/main/resources/category/java/documentation.xml @@ -94,8 +94,8 @@ and unintentional empty constructors. @@ -128,7 +128,7 @@ empty methods. diff --git a/pmd-java/src/main/resources/category/java/errorprone.xml b/pmd-java/src/main/resources/category/java/errorprone.xml index 938ca51d55..3415522b1e 100644 --- a/pmd-java/src/main/resources/category/java/errorprone.xml +++ b/pmd-java/src/main/resources/category/java/errorprone.xml @@ -734,7 +734,7 @@ public String bar(String string) { @Name='onStart' ] /Block[not( - (BlockStatement[1]/Statement/StatementExpression/PrimaryExpression[./PrimaryPrefix[@SuperModifier='true']]/PrimarySuffix[@Image= ancestor::MethodDeclaration/@Name]))] + (BlockStatement[1]/Statement/StatementExpression/PrimaryExpression[./PrimaryPrefix[@SuperModifier= true()]]/PrimarySuffix[@Image= ancestor::MethodDeclaration/@Name]))] [ancestor::ClassOrInterfaceDeclaration[ExtendsList/ClassOrInterfaceType[ pmd-java:typeIs('android.app.Activity') or pmd-java:typeIs('android.app.Application') or @@ -779,7 +779,7 @@ Super should be called at the end of the method @Name='onTerminate' ] /Block/BlockStatement[last()] - [not(Statement/StatementExpression/PrimaryExpression[./PrimaryPrefix[@SuperModifier='true']]/PrimarySuffix[@Image= ancestor::MethodDeclaration/@Name])] + [not(Statement/StatementExpression/PrimaryExpression[./PrimaryPrefix[@SuperModifier= true()]]/PrimarySuffix[@Image= ancestor::MethodDeclaration/@Name])] [ancestor::ClassOrInterfaceDeclaration[ExtendsList/ClassOrInterfaceType[ pmd-java:typeIs('android.app.Activity') or pmd-java:typeIs('android.app.Application') or @@ -892,7 +892,7 @@ Object.clone (which is protected) with a public method." @@ -1013,7 +1013,7 @@ and count(NameList/Name[contains (@Image,'CloneNotSupportedException')]) = 0 ] [ -../../../../ClassOrInterfaceDeclaration[@Final = 'false'] +../../../../ClassOrInterfaceDeclaration[@Final = false()] ] ]]> @@ -1474,7 +1474,7 @@ or reported. /Block /BlockStatement[last()] [not(Statement/StatementExpression/PrimaryExpression - [./PrimaryPrefix[@SuperModifier='true']] + [./PrimaryPrefix[@SuperModifier= true()]] [./PrimarySuffix[@Image='finalize']] ) ] [not(Statement/TryStatement/FinallyStatement /Block/BlockStatement/Statement/StatementExpression/PrimaryExpression - [./PrimaryPrefix[@SuperModifier='true']] + [./PrimaryPrefix[@SuperModifier= true()]] [./PrimarySuffix[@Image='finalize']] ) ] @@ -1924,7 +1924,7 @@ If the finalize() is implemented, it should do something besides just calling su /Block[count(BlockStatement)=1] /BlockStatement[ Statement/StatementExpression/PrimaryExpression - [./PrimaryPrefix[@SuperModifier='true']] + [./PrimaryPrefix[@SuperModifier= true()]] [./PrimarySuffix[@Image='finalize']] ] ]]> @@ -1990,7 +1990,7 @@ Note that Oracle has declared Object.finalize() as deprecated since JDK 9. @@ -2199,7 +2199,7 @@ The suite() method in a JUnit test needs to be both public and static. @@ -2333,7 +2333,7 @@ Either the check is useless (the variable will never be "null") or it is incorre public class Foo { void bar() { if (a.equals(baz) && a != null) {} // a could be null, misplaced null check - + if (a != null && a.equals(baz)) {} // correct null check } } @@ -2344,7 +2344,7 @@ public class Foo { public class Foo { void bar() { if (a.equals(baz) || a == null) {} // a could be null, misplaced null check - + if (a == null || a.equals(baz)) {} // correct null check } } @@ -2372,7 +2372,7 @@ may indicate problematic behaviour. Empty cases are ignored as these indicate an + count(BlockStatement//Statement/ReturnStatement) + count(BlockStatement//Statement/ContinueStatement) + count(BlockStatement//Statement/ThrowStatement) - + count(BlockStatement//Statement/IfStatement[@Else='true' and Statement[2][ReturnStatement|ContinueStatement|ThrowStatement]]/Statement[1][ReturnStatement|ContinueStatement|ThrowStatement]) + + count(BlockStatement//Statement/IfStatement[@Else= true() and Statement[2][ReturnStatement|ContinueStatement|ThrowStatement]]/Statement[1][ReturnStatement|ContinueStatement|ThrowStatement]) + count(SwitchLabel[name(following-sibling::node()) = 'SwitchLabel']) + count(SwitchLabel[count(following-sibling::node()) = 0]) < count (SwitchLabel))] @@ -2421,7 +2421,7 @@ chain needs an own serialVersionUID field. See also [Should an abstract class ha @@ -3377,7 +3372,7 @@ To make sure the full stacktrace is printed out, use the logging statement with concat(ancestor::ClassOrInterfaceDeclaration/ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/FieldDeclaration [Type//ClassOrInterfaceType[@Image='Log']] /VariableDeclarator/VariableDeclaratorId/@Image, '.'))]] -[PrimarySuffix/Arguments[@Size='1']] +[PrimarySuffix/Arguments[@Size= 1]] [PrimarySuffix/Arguments//Name/@Image = ancestor::CatchStatement/FormalParameter/VariableDeclaratorId/@Image] ]]> diff --git a/pmd-java/src/main/resources/category/java/multithreading.xml b/pmd-java/src/main/resources/category/java/multithreading.xml index c1ca1d5c2f..35a0d2ec5c 100644 --- a/pmd-java/src/main/resources/category/java/multithreading.xml +++ b/pmd-java/src/main/resources/category/java/multithreading.xml @@ -23,7 +23,7 @@ gets it. 3 - //MethodDeclaration[@Synchronized='true'] + //MethodDeclaration[@Synchronized= true()] @@ -102,7 +102,7 @@ the volatile keyword should not be used for maintenance purpose and portability. 2 - //FieldDeclaration[contains(@Volatile,'true')] + //FieldDeclaration[@Volatile = true()] @@ -379,7 +379,7 @@ one is chosen. The thread chosen is arbitrary; thus its usually safer to call n - \ No newline at end of file + diff --git a/pmd-java/src/main/resources/category/java/performance.xml b/pmd-java/src/main/resources/category/java/performance.xml index 7d0c75b226..7f1b0e4308 100644 --- a/pmd-java/src/main/resources/category/java/performance.xml +++ b/pmd-java/src/main/resources/category/java/performance.xml @@ -850,7 +850,7 @@ You must use new ArrayList<>(Arrays.asList(...)) if that is inconvenient for you (Arrays.asList(...)) if that is inconvenient for you and PrimaryExpression/PrimarySuffix/Arguments/ArgumentList/Expression/PrimaryExpression/PrimaryPrefix/Name [ - @Image = ancestor::MethodDeclaration[1]//LocalVariableDeclaration/VariableDeclarator/VariableDeclaratorId[@ArrayType="true"]/@Image + @Image = ancestor::MethodDeclaration[1]//LocalVariableDeclaration/VariableDeclarator/VariableDeclaratorId[@ArrayType=true()]/@Image or @Image = ancestor::MethodDeclaration[1]//FormalParameter/VariableDeclaratorId/@Image ] diff --git a/pmd-javascript/src/main/resources/category/ecmascript/bestpractices.xml b/pmd-javascript/src/main/resources/category/ecmascript/bestpractices.xml index 52cdac1822..a6ce322d0e 100644 --- a/pmd-javascript/src/main/resources/category/ecmascript/bestpractices.xml +++ b/pmd-javascript/src/main/resources/category/ecmascript/bestpractices.xml @@ -82,7 +82,7 @@ Global variables can lead to side-effects that are hard to debug. diff --git a/pmd-javascript/src/main/resources/category/ecmascript/codestyle.xml b/pmd-javascript/src/main/resources/category/ecmascript/codestyle.xml index 285262c59d..04d4e1abb2 100644 --- a/pmd-javascript/src/main/resources/category/ecmascript/codestyle.xml +++ b/pmd-javascript/src/main/resources/category/ecmascript/codestyle.xml @@ -25,17 +25,17 @@ indicative of the bug where the assignment operator '=' was used instead of the @@ -110,7 +110,7 @@ Avoid using if..else statements without using curly braces. @@ -149,7 +149,7 @@ Avoid using if statements without using curly braces. diff --git a/pmd-javascript/src/main/resources/category/ecmascript/errorprone.xml b/pmd-javascript/src/main/resources/category/ecmascript/errorprone.xml index 4b48e85dfe..a39190db2e 100644 --- a/pmd-javascript/src/main/resources/category/ecmascript/errorprone.xml +++ b/pmd-javascript/src/main/resources/category/ecmascript/errorprone.xml @@ -23,9 +23,9 @@ This rule helps improve code portability due to differences in browser treatment @@ -62,7 +62,7 @@ same type. The === operator avoids the casting. Date: Thu, 19 Mar 2020 01:49:55 +0100 Subject: [PATCH 02/42] Add relevant property tag --- .../resources/category/apex/codestyle.xml | 5 ++ .../resources/category/apex/errorprone.xml | 6 ++ .../sourceforge/pmd/lang/rule/XPathRule.java | 4 +- .../net/sourceforge/pmd/xml/j2ee.xml | 9 +++ .../test/resources/rulesets/dummy/basic.xml | 4 +- .../resources/rulesets/ruledoctest/sample.xml | 3 + .../rulesets/ruledoctest/sample2.xml | 1 + .../resources/category/java/bestpractices.xml | 22 +++++++ .../resources/category/java/codestyle.xml | 32 ++++++++++ .../main/resources/category/java/design.xml | 18 ++++++ .../resources/category/java/documentation.xml | 4 +- .../resources/category/java/errorprone.xml | 58 +++++++++++++++++++ .../category/java/multithreading.xml | 7 +++ .../resources/category/java/performance.xml | 13 +++++ .../pmd/ant/classpathtest/ruleset.xml | 1 + .../category/ecmascript/bestpractices.xml | 4 ++ .../category/ecmascript/codestyle.xml | 9 +++ .../category/ecmascript/errorprone.xml | 3 + .../resources/category/jsp/bestpractices.xml | 4 ++ .../main/resources/category/jsp/design.xml | 3 + .../resources/category/jsp/errorprone.xml | 1 + .../main/resources/category/jsp/security.xml | 1 + .../category/plsql/bestpractices.xml | 1 + .../resources/category/plsql/codestyle.xml | 2 + .../main/resources/category/plsql/design.xml | 1 + .../resources/category/plsql/errorprone.xml | 3 + .../main/resources/rulesets/dummy/basic.xml | 3 +- .../src/main/resources/category/vm/design.xml | 1 + .../resources/category/pom/errorprone.xml | 2 + .../resources/category/xml/errorprone.xml | 1 + .../main/resources/category/xsl/codestyle.xml | 1 + .../resources/category/xsl/performance.xml | 1 + 32 files changed, 223 insertions(+), 5 deletions(-) diff --git a/pmd-apex/src/main/resources/category/apex/codestyle.xml b/pmd-apex/src/main/resources/category/apex/codestyle.xml index 72c22c83ce..e76d6ebb8a 100644 --- a/pmd-apex/src/main/resources/category/apex/codestyle.xml +++ b/pmd-apex/src/main/resources/category/apex/codestyle.xml @@ -45,6 +45,7 @@ from the rest. 3 + 3 + 3 + 1 + 3 + 3 + 3 + 3 + 3 + 3 + 3 + tmp = new HashMap<>(); - tmp.put(XPATH_1_0, XPATH_2_0); - tmp.put(XPATH_1_0_COMPATIBILITY, XPATH_2_0); + tmp.put(XPATH_1_0, XPATH_1_0); + tmp.put(XPATH_1_0_COMPATIBILITY, XPATH_1_0_COMPATIBILITY); tmp.put(XPATH_2_0, XPATH_2_0); XPATH_VERSIONS = Collections.unmodifiableMap(tmp); } diff --git a/pmd-core/src/test/resources/net/sourceforge/pmd/xml/j2ee.xml b/pmd-core/src/test/resources/net/sourceforge/pmd/xml/j2ee.xml index 607967f6fd..17443c62ab 100644 --- a/pmd-core/src/test/resources/net/sourceforge/pmd/xml/j2ee.xml +++ b/pmd-core/src/test/resources/net/sourceforge/pmd/xml/j2ee.xml @@ -20,6 +20,7 @@ 3 + 4 + 4 + 4 + 4 + 4 + 3 + 3 + 3 + 3 + 3 + - \ No newline at end of file + diff --git a/pmd-doc/src/test/resources/rulesets/ruledoctest/sample.xml b/pmd-doc/src/test/resources/rulesets/ruledoctest/sample.xml index 44e2bb76fb..78591da860 100644 --- a/pmd-doc/src/test/resources/rulesets/ruledoctest/sample.xml +++ b/pmd-doc/src/test/resources/rulesets/ruledoctest/sample.xml @@ -57,6 +57,7 @@ Here might be <script>alert('XSS');</script> as well. And "quotes". 3 + 3 + 3 + 3 + 3 + 3 + 3 + 3 + 3 + + //ForInit/LocalVariableDeclaration[count(VariableDeclarator) > $maximumVariables] @@ -587,6 +593,7 @@ through the @RunWith(Suite.class) annotation. 3 + 3 + 3 + 3 + 3 + 4 + 3 + 3 + 3 + 3 + //Type/ReferenceType/ClassOrInterfaceType[@Image='Hashtable'] @@ -1206,6 +1222,7 @@ Consider replacing Vector usages with the newer java.util.ArrayList if expensive 3 + //Type/ReferenceType/ClassOrInterfaceType[@Image='Vector'] @@ -1267,6 +1284,7 @@ will (and by priority) and avoid clogging the Standard out log. 2 + 3 + 3 + 3 + 3 + 3 + 3 + 4 + 3 + 2 + //Name[starts-with(@Image,'System.loadLibrary')] @@ -314,6 +319,7 @@ prefix for these methods. 4 + 3 + 3 + 1 + No need to explicitly extend Object. 4 + 3 + 3 + //ForStatement[not(Statement/Block)] @@ -861,6 +873,7 @@ Names for references to generic values should be limited to a single uppercase l 4 + 3 + 3 + 4 + 4 + 3 + 4 + 3 + 3 + //PackageDeclaration/Name[lower-case(@Image)!=@Image] @@ -1532,6 +1553,7 @@ Remote Interface of a Session EJB should not have a suffix. 4 + 4 + 4 + 3 + 3 + + 3 + stringsWithDiamond = new ArrayList<>(); // using the diamond operat Useless parentheses should be removed. 4 + 3 + 3 + 3 + //WhileStatement[not(Statement/Block)] diff --git a/pmd-java/src/main/resources/category/java/design.xml b/pmd-java/src/main/resources/category/java/design.xml index 4a3dfa92dc..04ac51af08 100644 --- a/pmd-java/src/main/resources/category/java/design.xml +++ b/pmd-java/src/main/resources/category/java/design.xml @@ -23,6 +23,7 @@ protected constructor in order to prevent instantiation than make the class misl 1 + 3 + 3 + 3 + 1 + 1 + 3 + 1 + 3 + 3 + 3 + 3 + 3 + 3 + 3 + 3 + 3 + +## Describe the PR -Before submitting a PR, please check that: - - [ ] The PR is submitted against `master`. The PMD team will merge back to support branches as needed. - - [ ] `./mvnw clean verify` passes. This will [build](https://github.com/pmd/pmd/blob/master/BUILDING.md) and test PMD, execute PMD and checkstyle rules. [Check this for more info](https://github.com/pmd/pmd/blob/master/CONTRIBUTING.md#code-style) + -**PR Description:** +## Related issues + + + +- Fixes # + +## Ready? + + + +- [ ] Added unit tests for fixed bug/feature +- [ ] Passing all unit tests +- [ ] Complete build `./mvnw clean verify` passes (checked automatically by travis) +- [ ] Added (in-code) documentation (if needed) From 054b3acf6735c44f18a608af78d7c49177779b94 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 18 Apr 2020 11:49:37 +0200 Subject: [PATCH 37/42] [doc] Update release notes, fixes #2398 --- docs/pages/release_notes.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 794293b75c..4e45593dd7 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -50,6 +50,8 @@ Note that XPath 1.0 support, the default XPath version, is deprecated since PMD * [#2356](https://github.com/pmd/pmd/issues/2356): \[doc] Add missing doc about pmd.github.io * java * [#2378](https://github.com/pmd/pmd/issues/2378): \[java] AbstractJUnitRule has bad performance on large code bases +* java-bestpractices + * [#2398](https://github.com/pmd/pmd/issues/2398): \[java] AbstractClassWithoutAbstractMethod false negative with inner abstract classes * java-codestyle * [#1164](https://github.com/pmd/pmd/issues/1164): \[java] ClassNamingConventions suggests to add Util for class containing only static constants * [#1723](https://github.com/pmd/pmd/issues/1723): \[java] UseDiamondOperator false-positive inside lambda From 400ca5dca5a52bdc93de17b0e06290f640914bd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Sat, 18 Apr 2020 17:42:56 +0200 Subject: [PATCH 38/42] Delete test for findChildNodesWithXPath --- .../pmd/lang/ast/AbstractNodeTest.java | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/AbstractNodeTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/AbstractNodeTest.java index b5ece64c46..64136ef446 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/AbstractNodeTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/AbstractNodeTest.java @@ -6,10 +6,8 @@ package net.sourceforge.pmd.lang.ast; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import org.jaxen.JaxenException; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -228,24 +226,4 @@ public class AbstractNodeTest { assertEquals(0, grandChild.getNumChildren()); } - - @Test - public void testDeprecatedAttributeXPathQuery() throws JaxenException { - class MyRootNode extends DummyNode implements RootNode { - - private MyRootNode(int id) { - super(id); - } - } - - addChild(new MyRootNode(nextId()), new DummyNodeWithDeprecatedAttribute(2)).findChildNodesWithXPath("//dummyNode[@Size=1]"); - - String log = loggingRule.getLog(); - - assertTrue(log.contains("deprecated")); - assertTrue(log.contains("attribute")); - assertTrue(log.contains("dummyNode/@Size")); - } - - } From 4a9aa1a926ae07cc6b9dd4e4424eb02c17551278 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 18 Apr 2020 17:43:18 +0200 Subject: [PATCH 39/42] [core] Log deprecated attribute usage in findChildNodesWithXPath queries --- .../pmd/lang/ast/AbstractNode.java | 5 +++- .../internal/ContextualizedNavigator.java | 1 + .../xpath/internal/DeprecatedAttrLogger.java | 25 +++++++++++++++++++ .../pmd/lang/ast/AbstractNodeTest.java | 3 ++- 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/AbstractNode.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/AbstractNode.java index c14ede1d12..2f8d3eb78c 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/AbstractNode.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/AbstractNode.java @@ -25,6 +25,8 @@ import net.sourceforge.pmd.annotation.InternalApi; import net.sourceforge.pmd.lang.ast.xpath.Attribute; import net.sourceforge.pmd.lang.ast.xpath.AttributeAxisIterator; import net.sourceforge.pmd.lang.ast.xpath.DocumentNavigator; +import net.sourceforge.pmd.lang.ast.xpath.internal.ContextualizedNavigator; +import net.sourceforge.pmd.lang.ast.xpath.internal.DeprecatedAttrLogger; import net.sourceforge.pmd.lang.dfa.DataFlowNode; import net.sourceforge.pmd.util.DataMap; import net.sourceforge.pmd.util.DataMap.DataKey; @@ -513,7 +515,8 @@ public abstract class AbstractNode implements Node { @Override @SuppressWarnings("unchecked") public List findChildNodesWithXPath(final String xpathString) throws JaxenException { - return new BaseXPath(xpathString, new DocumentNavigator()).selectNodes(this); + return new BaseXPath(xpathString, new ContextualizedNavigator(DeprecatedAttrLogger.createAdHocLogger())) + .selectNodes(this); } @Override diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/internal/ContextualizedNavigator.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/internal/ContextualizedNavigator.java index f48dc03031..358d7d7ce7 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/internal/ContextualizedNavigator.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/internal/ContextualizedNavigator.java @@ -1,6 +1,7 @@ /* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ + package net.sourceforge.pmd.lang.ast.xpath.internal; import net.sourceforge.pmd.lang.ast.xpath.Attribute; diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/internal/DeprecatedAttrLogger.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/internal/DeprecatedAttrLogger.java index b0501e7a78..e6c2358a06 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/internal/DeprecatedAttrLogger.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/internal/DeprecatedAttrLogger.java @@ -35,6 +35,14 @@ public abstract class DeprecatedAttrLogger { } } + public static DeprecatedAttrLogger createAdHocLogger() { + if (LOG.isLoggable(Level.WARNING)) { + return new AdhocLoggerImpl(); + } else { + return noop(); + } + } + public static Noop noop() { return Noop.INSTANCE; } @@ -89,4 +97,21 @@ public abstract class DeprecatedAttrLogger { return name; } } + + private static class AdhocLoggerImpl extends DeprecatedAttrLogger { + @Override + public void recordUsageOf(Attribute attribute) { + String replacement = attribute.replacementIfDeprecated(); + if (replacement != null) { + String name = getLoggableAttributeName(attribute); + // this message needs to be kept in sync with PMDCoverageTest / BinaryDistributionIT + String msg = "Use of deprecated attribute '" + name + "' in a findChildNodesWithXPath navigation"; + if (!replacement.isEmpty()) { + msg += ", please use " + replacement + " instead"; + } + // log with execption stack trace to help figure out where exactly the xpath is used. + LOG.log(Level.WARNING, msg, new RuntimeException(msg)); + } + } + } } diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/AbstractNodeTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/AbstractNodeTest.java index b5ece64c46..5cc793c267 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/AbstractNodeTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/AbstractNodeTest.java @@ -238,7 +238,8 @@ public class AbstractNodeTest { } } - addChild(new MyRootNode(nextId()), new DummyNodeWithDeprecatedAttribute(2)).findChildNodesWithXPath("//dummyNode[@Size=1]"); + Node root = addChild(new MyRootNode(nextId()), new DummyNodeWithDeprecatedAttribute(2)); + root.findChildNodesWithXPath("//dummyNode[@Size=1]"); String log = loggingRule.getLog(); From bcae7e52437da422ab85e97afd9230808f913b04 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 18 Apr 2020 18:10:05 +0200 Subject: [PATCH 40/42] Small corrections --- .../pmd/lang/ast/xpath/AttributeAxisIteratorTest.java | 2 +- .../pmd/lang/java/ast/ASTAdditiveExpression.java | 4 ++-- .../pmd/lang/java/ast/ASTVariableDeclaratorId.java | 4 ++-- .../pmd/lang/java/ast/AbstractAnyTypeDeclaration.java | 11 ++++++++--- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/xpath/AttributeAxisIteratorTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/xpath/AttributeAxisIteratorTest.java index d5697ed3bd..a1dda2cb58 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/xpath/AttributeAxisIteratorTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/xpath/AttributeAxisIteratorTest.java @@ -71,7 +71,7 @@ public class AttributeAxisIteratorTest { assertFalse(atts.containsKey("NodeList")); } - public Map toMap(AttributeAxisIterator it) { + private Map toMap(AttributeAxisIterator it) { Map atts = new HashMap<>(); while (it.hasNext()) { Attribute attribute = it.next(); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAdditiveExpression.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAdditiveExpression.java index 9d6c995cb9..f60a30e32d 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAdditiveExpression.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTAdditiveExpression.java @@ -48,13 +48,13 @@ public class ASTAdditiveExpression extends AbstractJavaTypeNode { @Deprecated @DeprecatedAttribute(replaceWith = "@Operator") public String getImage() { - return super.getImage(); + return getOperator(); } /** * Returns the image of the operator, i.e. "+" or "-". */ public String getOperator() { - return getImage(); + return super.getImage(); } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTVariableDeclaratorId.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTVariableDeclaratorId.java index cde67aa449..f20ecb7034 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTVariableDeclaratorId.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTVariableDeclaratorId.java @@ -107,12 +107,12 @@ public class ASTVariableDeclaratorId extends AbstractJavaTypeNode implements Dim @DeprecatedAttribute(replaceWith = "@Name") @Deprecated public String getImage() { - return super.getImage(); + return getName(); } /** Returns the name of the variable. */ public String getName() { - return getImage(); + return super.getImage(); } /** diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractAnyTypeDeclaration.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractAnyTypeDeclaration.java index 4bd5fc0607..ed9076c669 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractAnyTypeDeclaration.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractAnyTypeDeclaration.java @@ -6,6 +6,7 @@ package net.sourceforge.pmd.lang.java.ast; import net.sourceforge.pmd.annotation.InternalApi; import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.lang.ast.xpath.internal.DeprecatedAttribute; import net.sourceforge.pmd.lang.java.qname.JavaTypeQualifiedName; import net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition; @@ -37,10 +38,14 @@ public abstract class AbstractAnyTypeDeclaration extends AbstractJavaAccessTypeN || getParent() instanceof ASTRecordBody; } - @Override + /** + * @deprecated Use {@link #getSimpleName()} + */ @Deprecated + @DeprecatedAttribute(replaceWith = "@SimpleName") + @Override public String getImage() { - return super.getImage(); + return getSimpleName(); } @Override @@ -50,7 +55,7 @@ public abstract class AbstractAnyTypeDeclaration extends AbstractJavaAccessTypeN @Override public String getSimpleName() { - return getImage(); + return super.getImage(); } From 5cad2ff741b4dacc312d1769b9b5894f513e9035 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 18 Apr 2020 18:22:07 +0200 Subject: [PATCH 41/42] [doc] Update release notes, refs #2425, fixes #2019 --- docs/pages/release_notes.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 794293b75c..5b7f998098 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -46,6 +46,7 @@ Note that XPath 1.0 support, the default XPath version, is deprecated since PMD * [#2210](https://github.com/pmd/pmd/issues/2210): \[apex] ApexCRUDViolation: Support WITH SECURITY_ENFORCED * [#2399](https://github.com/pmd/pmd/issues/2399): \[apex] ApexCRUDViolation: false positive with security enforced with line break * core + * [#2019](https://github.com/pmd/pmd/issues/2019): \[core] Insufficient deprecation warnings for XPath attributes * [#2355](https://github.com/pmd/pmd/issues/2355): \[doc] Improve documentation about incremental analysis * [#2356](https://github.com/pmd/pmd/issues/2356): \[doc] Add missing doc about pmd.github.io * java @@ -123,6 +124,12 @@ implementations, and their corresponding Parser if it exists (in the same packag * {% jdoc matlab::lang.matlab.MatlabTokenManager %} * {% jdoc objectivec::lang.objectivec.ObjectiveCTokenManager %} +In the **Java AST** the following attributes are deprecated and will issue a warning when used in XPath rules: + +* {% jdoc !!java::lang.java.ast.ASTAdditiveExpression#getImage() %} - use `getOperator()` instead +* {% jdoc !!java::lang.java.ast.ASTVariableDeclaratorId#getImage() %} - use `getName()` instead +* {% jdoc !!java::lang.java.ast.ASTVariableDeclaratorId#getVariableName() %} - use `getName()` instead + ##### For removal * {% jdoc !!core::lang.Parser#getTokenManager(java.lang.String,java.io.Reader) %} From aee04b4c6890b3bf64236c8d7665ac31db9e4536 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 18 Apr 2020 19:06:29 +0200 Subject: [PATCH 42/42] [java] Fix more deprecated attribute usages --- .../ForLoopCanBeForeachRule.java | 2 +- .../resources/category/java/codestyle.xml | 8 ++--- .../main/resources/category/java/design.xml | 2 +- .../resources/category/java/errorprone.xml | 8 ++--- .../java/net/sourceforge/pmd/ReportTest.java | 2 +- .../pmd/lang/java/rule/XPathRuleTest.java | 12 ++++---- .../typeresolution/ClassTypeResolverTest.java | 29 ++++++++----------- .../xml/UnusedFormalParameter.xml | 2 +- 8 files changed, 30 insertions(+), 35 deletions(-) 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 fccfd94132..fb7873371d 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 @@ -229,7 +229,7 @@ public class ForLoopCanBeForeachRule extends AbstractJavaRule { + "/Name[matches(@Image,'\\w+\\.(size|length)')]" + "|" + "./RelationalExpression[@Image='<=']/AdditiveExpression[count(*)=2 and " - + "@Image='-' and PrimaryExpression/PrimaryPrefix/Literal[@Image='1']]" + + "@Operator='-' and PrimaryExpression/PrimaryPrefix/Literal[@Image='1']]" + "/PrimaryExpression/PrimaryPrefix/Name[matches(@Image,'\\w+\\.(size|length)')]"); if (left.isEmpty()) { diff --git a/pmd-java/src/main/resources/category/java/codestyle.xml b/pmd-java/src/main/resources/category/java/codestyle.xml index 4de7330e0d..e8995ca3a9 100644 --- a/pmd-java/src/main/resources/category/java/codestyle.xml +++ b/pmd-java/src/main/resources/category/java/codestyle.xml @@ -164,7 +164,7 @@ by the more general rule {% rule java/codestyle/FormalParameterNamingConventions @@ -1364,7 +1364,7 @@ by the more general rule @@ -1758,7 +1758,7 @@ by the more general rule {% rule java/codestyle/FieldNamingConventions %}. //ClassOrInterfaceDeclaration[@Interface= false()] /ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/FieldDeclaration [@Final= false()] - [VariableDeclarator/VariableDeclaratorId[upper-case(@Image)=@Image]] + [VariableDeclarator/VariableDeclaratorId[upper-case(@Name)=@Name]] ]]> @@ -2067,7 +2067,7 @@ List stringsWithDiamond = new ArrayList<>(); // using the diamond operat /PrimaryExpression[1]/PrimaryPrefix/Expression[ count(*)=1 and not(./CastExpression) and - not(./AdditiveExpression[@Image = '-']) and + not(./AdditiveExpression[@Operator = '-']) and not(./ShiftExpression) and not(./RelationalExpression) and not(./InstanceOfExpression) and diff --git a/pmd-java/src/main/resources/category/java/design.xml b/pmd-java/src/main/resources/category/java/design.xml index fd13c4cf42..dba3f14cec 100644 --- a/pmd-java/src/main/resources/category/java/design.xml +++ b/pmd-java/src/main/resources/category/java/design.xml @@ -133,7 +133,7 @@ Catch blocks that merely rethrow a caught exception only add to code size and ru diff --git a/pmd-java/src/main/resources/category/java/errorprone.xml b/pmd-java/src/main/resources/category/java/errorprone.xml index 5b2b1f5929..07d265c0ba 100644 --- a/pmd-java/src/main/resources/category/java/errorprone.xml +++ b/pmd-java/src/main/resources/category/java/errorprone.xml @@ -422,7 +422,7 @@ Each caught exception type should be handled in its own catch clause. /following-sibling::Block//InstanceOfExpression/PrimaryExpression/PrimaryPrefix /Name[ @Image = ./ancestor::Block/preceding-sibling::FormalParameter - /VariableDeclaratorId/@Image + /VariableDeclaratorId/@Name ] ]]> @@ -2414,7 +2414,7 @@ chain needs an own serialVersionUID field. See also [Should an abstract class ha //ClassOrInterfaceDeclaration [@Interface = false()] [count(ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration - /FieldDeclaration/VariableDeclarator/VariableDeclaratorId[@Image='serialVersionUID']) = 0] + /FieldDeclaration/VariableDeclarator/VariableDeclaratorId[@Name='serialVersionUID']) = 0] [(ImplementsList | ExtendsList)/ClassOrInterfaceType[pmd-java:typeIs('java.io.Serializable')]] ]]> @@ -3362,11 +3362,11 @@ To make sure the full stacktrace is printed out, use the logging statement with [starts-with(@Image, concat((ancestor::ClassOrInterfaceDeclaration/ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/FieldDeclaration [Type//ClassOrInterfaceType[@Image='Log']] - /VariableDeclarator/VariableDeclaratorId/@Image)[1], '.')) + /VariableDeclarator/VariableDeclaratorId/@Name)[1], '.')) ] ] [PrimarySuffix/Arguments[@Size= 1]] - [PrimarySuffix/Arguments//Name/@Image = ancestor::CatchStatement/FormalParameter/VariableDeclaratorId/@Image] + [PrimarySuffix/Arguments//Name/@Image = ancestor::CatchStatement/FormalParameter/VariableDeclaratorId/@Name] ]]> diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/ReportTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/ReportTest.java index cac861109b..383d64cbde 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/ReportTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/ReportTest.java @@ -40,7 +40,7 @@ public class ReportTest extends RuleTst { public void testExclusionsInReportWithRuleViolationSuppressXPath() { Report rpt = new Report(); Rule rule = new FooRule(); - rule.setProperty(Rule.VIOLATION_SUPPRESS_XPATH_DESCRIPTOR, ".[@Image = 'Foo']"); + rule.setProperty(Rule.VIOLATION_SUPPRESS_XPATH_DESCRIPTOR, ".[@SimpleName = 'Foo']"); runTestFromString(TEST1, rule, rpt, defaultLanguage); assertTrue(rpt.isEmpty()); assertEquals(1, rpt.getSuppressedRuleViolations().size()); diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/XPathRuleTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/XPathRuleTest.java index a82c36426b..90e01fabe5 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/XPathRuleTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/rule/XPathRuleTest.java @@ -1,4 +1,4 @@ -/** +/* * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ @@ -53,7 +53,7 @@ public class XPathRuleTest extends RuleTst { @Test public void testPluginname() throws Exception { - XPathRule rule = makeXPath("//VariableDeclaratorId[string-length(@Image) < 3]"); + XPathRule rule = makeXPath("//VariableDeclaratorId[string-length(@Name) < 3]"); rule.setMessage("{0}"); Report report = getReportForTestString(rule, TEST1); RuleViolation rv = report.iterator().next(); @@ -63,7 +63,7 @@ public class XPathRuleTest extends RuleTst { @Test public void testXPathMultiProperty() throws Exception { - XPathRule rule = makeXPath("//VariableDeclaratorId[@Image=$forbiddenNames]"); + XPathRule rule = makeXPath("//VariableDeclaratorId[@Name=$forbiddenNames]"); rule.setMessage("Avoid vars"); PropertyDescriptor> varDescriptor = PropertyFactory.stringListProperty("forbiddenNames") @@ -86,7 +86,7 @@ public class XPathRuleTest extends RuleTst { @Test public void testVariables() throws Exception { - XPathRule rule = makeXPath("//VariableDeclaratorId[@Image=$var]"); + XPathRule rule = makeXPath("//VariableDeclaratorId[@Name=$var]"); rule.setMessage("Avoid vars"); PropertyDescriptor varDescriptor = PropertyFactory.stringProperty("var").desc("Test var").defaultValue("").build(); @@ -99,7 +99,7 @@ public class XPathRuleTest extends RuleTst { @Test public void testFnPrefixOnSaxon() throws Exception { - XPathRule rule = makeXPath("//VariableDeclaratorId[fn:matches(@Image, 'fiddle')]"); + XPathRule rule = makeXPath("//VariableDeclaratorId[fn:matches(@Name, 'fiddle')]"); Report report = getReportForTestString(rule, TEST2); RuleViolation rv = report.iterator().next(); assertEquals(3, rv.getBeginLine()); @@ -107,7 +107,7 @@ public class XPathRuleTest extends RuleTst { @Test public void testNoFnPrefixOnSaxon() throws Exception { - XPathRule rule = makeXPath("//VariableDeclaratorId[matches(@Image, 'fiddle')]"); + XPathRule rule = makeXPath("//VariableDeclaratorId[matches(@Name, 'fiddle')]"); Report report = getReportForTestString(rule, TEST2); RuleViolation rv = report.iterator().next(); assertEquals(3, rv.getBeginLine()); diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/typeresolution/ClassTypeResolverTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/typeresolution/ClassTypeResolverTest.java index 57ae0deec3..94756287bb 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/typeresolution/ClassTypeResolverTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/typeresolution/ClassTypeResolverTest.java @@ -452,8 +452,7 @@ public class ClassTypeResolverTest { ASTCompilationUnit acu = java5.parseClass(Promotion.class); List expressions = convertList( acu.findChildNodesWithXPath( - "//Block[preceding-sibling::MethodDeclarator[@Image = " - + "'unaryNumericPromotion']]//Expression[UnaryExpression]"), + "//MethodDeclaration[@Name = 'unaryNumericPromotion']/Block//Expression[UnaryExpression]"), ASTExpression.class); int index = 0; @@ -474,8 +473,7 @@ public class ClassTypeResolverTest { ASTCompilationUnit acu = java5.parseClass(Promotion.class); List expressions = convertList( acu.findChildNodesWithXPath( - "//Block[preceding-sibling::MethodDeclarator[@Image = " - + "'binaryNumericPromotion']]//Expression[AdditiveExpression]"), + "//MethodDeclaration[@Name = 'binaryNumericPromotion']/Block//Expression[AdditiveExpression]"), ASTExpression.class); int index = 0; @@ -545,7 +543,7 @@ public class ClassTypeResolverTest { ASTCompilationUnit acu = java5.parseClass(Promotion.class); List expressions = convertList( acu.findChildNodesWithXPath( - "//Block[preceding-sibling::MethodDeclarator[@Image = 'binaryStringPromotion']]//Expression"), + "//MethodDeclaration[@Name = 'binaryStringPromotion']/Block//Expression"), ASTExpression.class); int index = 0; @@ -564,7 +562,7 @@ public class ClassTypeResolverTest { ASTCompilationUnit acu = java5.parseClass(Operators.class); List expressions = convertList( acu.findChildNodesWithXPath( - "//Block[preceding-sibling::MethodDeclarator[@Image = 'unaryLogicalOperators']]//Expression"), + "//MethodDeclaration[@Name = 'unaryLogicalOperators']/Block//Expression"), ASTExpression.class); int index = 0; @@ -580,7 +578,7 @@ public class ClassTypeResolverTest { ASTCompilationUnit acu = java5.parseClass(Operators.class); List expressions = convertList( acu.findChildNodesWithXPath( - "//Block[preceding-sibling::MethodDeclarator[@Image = 'binaryLogicalOperators']]//Expression"), + "//MethodDeclaration[@Name = 'binaryLogicalOperators']/Block//Expression"), ASTExpression.class); int index = 0; @@ -606,24 +604,22 @@ public class ClassTypeResolverTest { public void testUnaryNumericOperators() throws JaxenException { ASTCompilationUnit acu = java5.parseClass(Operators.class); List expressions = new ArrayList<>(); + final String baseXPath = "//MethodDeclaration[@Name = 'unaryNumericOperators']/Block"; expressions.addAll(convertList( acu.findChildNodesWithXPath( - "//Block[preceding-sibling::MethodDeclarator[@Image = 'unaryNumericOperators']]//Expression"), + baseXPath + "//Expression"), TypeNode.class)); expressions.addAll(convertList( acu.findChildNodesWithXPath( - "//Block[preceding-sibling::MethodDeclarator[@Image = " - + "'unaryNumericOperators']]//PostfixExpression"), + baseXPath + "//PostfixExpression"), TypeNode.class)); expressions.addAll(convertList( acu.findChildNodesWithXPath( - "//Block[preceding-sibling::MethodDeclarator[@Image = " - + "'unaryNumericOperators']]//PreIncrementExpression"), + baseXPath + "//PreIncrementExpression"), TypeNode.class)); expressions.addAll(convertList( acu.findChildNodesWithXPath( - "//Block[preceding-sibling::MethodDeclarator[@Image = " - + "'unaryNumericOperators']]//PreDecrementExpression"), + baseXPath + "//PreDecrementExpression"), TypeNode.class)); int index = 0; @@ -643,7 +639,7 @@ public class ClassTypeResolverTest { ASTCompilationUnit acu = java5.parseClass(Operators.class); List expressions = convertList( acu.findChildNodesWithXPath( - "//Block[preceding-sibling::MethodDeclarator[@Image = 'binaryNumericOperators']]//Expression"), + "//MethodDeclaration[@Name = 'binaryNumericOperators']/Block//Expression"), ASTExpression.class); int index = 0; @@ -665,8 +661,7 @@ public class ClassTypeResolverTest { ASTCompilationUnit acu = java5.parseClass(Operators.class); List expressions = convertList( acu.findChildNodesWithXPath( - "//Block[preceding-sibling::MethodDeclarator[@Image = " - + "'assignmentOperators']]//StatementExpression"), + "//MethodDeclaration[@Name = 'assignmentOperators']/Block//StatementExpression"), ASTStatementExpression.class); int index = 0; diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/UnusedFormalParameter.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/UnusedFormalParameter.xml index ba281bbf8a..879d7d748a 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/UnusedFormalParameter.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/UnusedFormalParameter.xml @@ -217,7 +217,7 @@ class Foo { violation suppression xpath works, by name ]]> - .[@Image = 'paramB'] + .[@Name = 'paramB'] 0