[java] AvoidAccessibilityAlteration: allow setAccessible(false)

This restores the cheks for the Java language access control.
This commit is contained in:
Andreas Dangel
2021-09-03 09:31:03 +02:00
parent 1f520a9f6f
commit 83d56dab28
2 changed files with 17 additions and 0 deletions

View File

@ -76,6 +76,7 @@ is assumed to be deliberate and is not reported.
<![CDATA[
//PrimaryPrefix
[Name[ends-with(@Image, '.setAccessible')]]
[not(following-sibling::PrimarySuffix/Arguments//BooleanLiteral[@True = false()])]
[pmd-java:typeIs('java.lang.reflect.AccessibleObject')]
(: exclude anonymous privileged action classes :)
[not(ancestor::AllocationExpression[pmd-java:typeIs('java.security.PrivilegedAction')])]

View File

@ -165,4 +165,20 @@ public class NoViolation { {
} }
]]></code>
</test-code>
<test-code>
<description>setAccessible(false) is ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.lang.reflect.Constructor;
public class Violation {
private void invalidSetAccessCalls() throws NoSuchMethodException, SecurityException {
Constructor<?> constructor = this.getClass().getDeclaredConstructor(String.class);
// call to setAccessible with false - that's ok
constructor.setAccessible(false);
}
}
]]></code>
</test-code>
</test-data>