[java] Update rule AvoidAccessibilityAlteration

This commit is contained in:
Andreas Dangel
2021-09-30 12:40:31 +02:00
parent 7cee47d71e
commit c94726f8ae
4 changed files with 10 additions and 11 deletions

View File

@@ -172,6 +172,7 @@
<rule ref="category/java/errorprone.xml/AssignmentInOperand"/>
<!-- <rule ref="category/java/errorprone.xml/AssignmentToNonFinalStatic"/> -->
<rule ref="category/java/errorprone.xml/AvoidAccessibilityAlteration"/>
<rule ref="category/java/errorprone.xml/AvoidAssertAsIdentifier"/>
<!-- <rule ref="category/java/errorprone.xml/AvoidBranchingStatementAsLastInLoop"/> -->
<!-- <rule ref="category/java/errorprone.xml/AvoidCallingFinalize"/> -->

View File

@@ -78,12 +78,13 @@ suppression methods (e.g. by using `@SuppressWarnings` annotation).
<property name="xpath">
<value>
<![CDATA[
//PrimaryPrefix
[Name[ends-with(@Image, '.setAccessible')]]
[not(following-sibling::PrimarySuffix/Arguments//BooleanLiteral[@True = false()])]
[pmd-java:typeIs('java.lang.reflect.AccessibleObject')]
//MethodCall[
pmd-java:matchesSig("java.lang.reflect.AccessibleObject#setAccessible(boolean)")
or pmd-java:matchesSig("_#setAccessible(java.lang.reflect.AccessibleObject[],boolean)")
]
[not(ArgumentList/BooleanLiteral[@True = false()])]
(: exclude anonymous privileged action classes :)
[not(ancestor::AllocationExpression[pmd-java:typeIs('java.security.PrivilegedAction')])]
[not(ancestor::ConstructorCall[1][pmd-java:typeIs('java.security.PrivilegedAction')]/AnonymousClassDeclaration)]
(: exclude inner privileged action classes :)
[not(ancestor::ClassOrInterfaceDeclaration[1][pmd-java:typeIs('java.security.PrivilegedAction')])]
]]>

View File

@@ -6,7 +6,6 @@ package net.sourceforge.pmd.lang.java.rule.errorprone;
import net.sourceforge.pmd.testframework.PmdRuleTst;
@org.junit.Ignore("Rule has not been updated yet")
public class AvoidAccessibilityAlterationTest extends PmdRuleTst {
// no additional unit tests
}

View File

@@ -80,9 +80,7 @@ public class Violation {
]]></code>
</test-code>
<test-code regressionTest="false">
<!-- Note: type res doesn't work well on method call chains in PMD 6, so none
of these are detected. -->
<test-code>
<description>Make sure to detect method call chains</description>
<expected-problems>6</expected-problems>
<expected-linenumbers>3,4,5,6,7,8</expected-linenumbers>
@@ -92,7 +90,6 @@ public class Violation {
this.getClass().getDeclaredConstructor(String.class).setAccessible(true);
this.getClass().getDeclaredMethod("aPrivateMethod").setAccessible(true);
this.getClass().getDeclaredField("aPrivateField").setAccessible(true);
Violation.class.getDeclaredConstructor(String.class).setAccessible(true);
Violation.class.getDeclaredMethod("aPrivateMethod").setAccessible(true);
Violation.class.getDeclaredField("aPrivateField").setAccessible(true);
@@ -115,7 +112,7 @@ public class NoViolation {
Method[] methods = AccessController.doPrivileged(new PrivilegedAction<Method[]>() {
@Override
public Method[] run() {
Method[] declaredMethods = Violation.class.getDeclaredMethods();
Method[] declaredMethods = NoViolation.class.getDeclaredMethods();
AccessibleObject.setAccessible(declaredMethods, true);
return declaredMethods;
}
@@ -156,6 +153,7 @@ public class Violation {
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
public class NoViolation { {