Skip to content

Commit

Permalink
[yegor256#1198] update checkstyle and pmd rules
Browse files Browse the repository at this point in the history
  • Loading branch information
pnatashap committed Mar 12, 2024
1 parent 6bd7a3f commit c2d5d0d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 63 deletions.
2 changes: 1 addition & 1 deletion qulice-checkstyle/pom.xml
Expand Up @@ -70,7 +70,7 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>10.9.3</version>
<version>10.14.1</version>
<exclusions>
<exclusion>
<groupId>net.sf.saxon</groupId>
Expand Down
Expand Up @@ -437,6 +437,9 @@ void allowsOnlyProperlyOrderedAtClauses() throws Exception {
),
new ViolationMatcher(
message, file, "48", name
),
new ViolationMatcher(
"Class Class should be declared as final.", file, "59", "FinalClassCheck"
)
)
);
Expand Down
Expand Up @@ -101,7 +101,6 @@ public Collection<PmdError> validate(
for (final DataSource source : sources) {
final String name = source.getNiceFileName(false, path);
Logger.debug(this, "Processing file: %s", name);
this.context.setSourceCodeFilename(name);
this.context.setSourceCodeFile(new File(name));
this.validateOne(source);
}
Expand Down
73 changes: 13 additions & 60 deletions qulice-pmd/src/main/resources/com/qulice/pmd/ruleset.xml
Expand Up @@ -35,6 +35,7 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
</description>
<rule ref="category/java/bestpractices.xml">
<exclude name="JUnitTestsShouldIncludeAssert"/>
<exclude name="GuardLogStatement"/>
<exclude name="PositionLiteralsFirstInComparisons"/>
<exclude name="PositionLiteralsFirstInCaseInsensitiveComparisons"/>
</rule>
Expand Down Expand Up @@ -158,6 +159,17 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
]]></value>
</property>
</properties>
<example>
<![CDATA[
public final class DirectAccessToStaticFields {
private static int num = 1;
public static int number() {
return num;
}
}
]]>
</example>
</rule>
<rule name="AvoidAccessToStaticMembersViaThis" message="Static members should be accessed in a static way [CLASS_NAME.FIELD_NAME], not via instance reference." language="java" class="net.sourceforge.pmd.lang.rule.XPathRule">
<description>
Expand Down Expand Up @@ -191,10 +203,9 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
and not (
MethodDeclarator[
count(FormalParameters/FormalParameter)=1
and @Image='main'
and FormalParameters/FormalParameter[1]/Type/ReferenceType/ClassOrInterfaceType[@Image='String']
and FormalParameters/FormalParameter[@Varargs='true']
] and not(ResultType/Type)
] and not(ResultType/Type) and @Name='main'
)
] and (
Annotation/MarkerAnnotation/Name[@Image!='BeforeClass' and @Image!='AfterClass'
Expand All @@ -219,64 +230,6 @@ OF THE POSSIBILITY OF SUCH DAMAGE.
</property>
</properties>
</rule>
<rule name="ReturnEmptyCollectionRatherThanNull" language="java" class="net.sourceforge.pmd.lang.rule.XPathRule" message="Returned null collection was found (NullCollection).">
<description>
Any method that returns an collection (i.e. an array or a List), it is better to return
an empty one rather than a null reference.
</description>
<priority>1</priority>
<properties>
<property name="version" value="2.0"/>
<!--Solve priority confict-->
<property name="xpath">
<value><![CDATA[
//MethodDeclaration[
(./ResultType/Type[pmd-java:typeIs('java.util.Collection')
or pmd-java:typeIs('java.util.Map') or @ArrayType=true()]) and
(./Block/BlockStatement/Statement/ReturnStatement/Expression/PrimaryExpression/PrimaryPrefix/Literal/NullLiteral)
]]]></value>
</property>
</properties>
</rule>
<rule name="JUnit5TestShouldBePackagePrivate" language="java" class="net.sourceforge.pmd.lang.rule.XPathRule" message="JUnit 5 tests should be package-private">
<description><![CDATA[
Reports JUnit 5 test classes and methods that are not package-private.
Contrary to JUnit 4 tests, which required public visibility to be run by the engine,
JUnit 5 tests can also be run if they're package-private. Marking them as such
is a good practice to limit their visibility.
Test methods are identified as those which use `@Test`, `@RepeatedTest`,
`@TestFactory`, `@TestTemplate` or `@ParameterizedTest`.
]]></description>
<priority>3</priority>
<properties>
<property name="version" value="2.0"/>
<!--Solve priority confict-->
<property name="xpath">
<value><![CDATA[
//ClassOrInterfaceDeclaration[
(: a Junit 5 test class, ie, it has methods with the annotation :)
@Interface=false() and
ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration
[Annotation//Name[
pmd-java:typeIs('org.junit.jupiter.api.Test') or pmd-java:typeIs('org.junit.jupiter.api.RepeatedTest')
or pmd-java:typeIs('org.junit.jupiter.api.TestFactory') or pmd-java:typeIs('org.junit.jupiter.api.TestTemplate')
or pmd-java:typeIs('org.junit.jupiter.params.ParameterizedTest')
]]
[MethodDeclaration]
]/(
self::*[@Abstract=false() and (@Public=true() or @Protected=true())]
| ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration
[Annotation//Name[
pmd-java:typeIs('org.junit.jupiter.api.Test') or pmd-java:typeIs('org.junit.jupiter.api.RepeatedTest')
or pmd-java:typeIs('org.junit.jupiter.api.TestFactory') or pmd-java:typeIs('org.junit.jupiter.api.TestTemplate')
or pmd-java:typeIs('org.junit.jupiter.params.ParameterizedTest')
]]
/MethodDeclaration[@Public=true() or @Protected=true()]
)
]]></value>
</property>
</properties>
</rule>
<rule name="JUnitTestClassShouldBeFinal" language="java" class="net.sourceforge.pmd.lang.rule.XPathRule" message="JUnit test class should be final">
<description><![CDATA[
Reports on JUnit test classes declared non-final.
Expand Down
Expand Up @@ -49,7 +49,7 @@ void failsForNullCollection() throws Exception {
"NullCollection.java",
Matchers.is(false),
Matchers.containsString(
"Returned null collection was found (NullCollection)."
"Return an empty collection rather than null. (ReturnEmptyCollectionRatherThanNull)"
)
).validate();
}
Expand Down

0 comments on commit c2d5d0d

Please sign in to comment.