[java] Shrink reported locations for some rules#6084
[java] Shrink reported locations for some rules#6084
Conversation
|
Compared to main: (comment created at 2026-01-22 18:35:15+00:00 for d15e3b0) |
adangel
left a comment
There was a problem hiding this comment.
Thanks for the PR!
Before merging this, I'd like to clarify, that we really report on useful lines (e.g. report on the class identifier instead of on the class keyword).
After this PR, I'd suggest the following approach:
- create a list of all rules
- Cross out the rules, for which you created a test in this PR already
- select one single rule
- create a test case "Verify location of violation" with the expected report location. Keep in mind, that the end-line seems to where github actions would show the annotation (see #3769)
- fix this rule
- repeat with 2 until no rules are left
- extend documentation "how to write/test a rule" to describe, that not only a positive and negative test case is required, but also a verification of the report location test.
Because now, we fixed "some rules" but we don't know which exactly, and when we have actually done #3769. This makes it hard to monitor progress on this front.
Additionally, we might need to think about extending the regression tester. I think, we currently only compare the beginLine, but maybe we should compare the complete report location for each violation. But maybe this is not needed, if we have for every rule such a test case. Maybe that's sufficient.
pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTCompilationUnit.java
Outdated
Show resolved
Hide resolved
pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTSwitchStatement.java
Show resolved
Hide resolved
pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/ExcessiveImportsRule.java
Show resolved
Hide resolved
.../net/sourceforge/pmd/lang/java/rule/bestpractices/xml/AbstractClassWithoutAbstractMethod.xml
Outdated
Show resolved
Hide resolved
.../test/resources/net/sourceforge/pmd/lang/java/rule/bestpractices/xml/NonExhaustiveSwitch.xml
Show resolved
Hide resolved
pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/NoPackage.xml
Outdated
Show resolved
Hide resolved
...src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/TooManyStaticImports.xml
Show resolved
Hide resolved
pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/TooManyMethods.xml
Outdated
Show resolved
Hide resolved
pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/ExcessiveImportsRule.java
Outdated
Show resolved
Hide resolved
| int threshold = getProperty(reportLevel); | ||
| if (metric >= threshold) { | ||
| asCtx(data).addViolation(node, metric, threshold); | ||
| asCtx(data).addViolationWithPosition(t, t.getAstInfo(), getReportLocation(t), getMessage(), metric, threshold); |
There was a problem hiding this comment.
This is a slight conflict with #6260 because this overload of addViolationWithPosition is removed in that PR. If that PR is merged before this one, the correct incantation would be
| asCtx(data).addViolationWithPosition(t, t.getAstInfo(), getReportLocation(t), getMessage(), metric, threshold); | |
| asCtx(data).at(t.atLocation(getReportLocation(t))).warnWithArgs(metric, threshold); |
|
Note: I'm going to rebase this a couple of times... |
bf4235c to
0e585ff
Compare
|
Status update (see also #730): The two rules AbstractClassWithoutAbstractMethod and NonExhaustiveSwitch of category "Best Practices" are done now. |
pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractTypeDeclaration.java
Outdated
Show resolved
Hide resolved
pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTCompilationUnit.java
Outdated
Show resolved
Hide resolved
56b87d5 to
78f0bb7
Compare
|
When looking at the regression report, the following additional rules have been changed:
Once the new report is available, maybe less rules are affected by this change. But if there are more rules affected (because e.g. we changed all rules that report on ClassDeclaration), we should already handle them here in this PR. I'm also inclined to actually implement pmd/pmd-regression-tester#148 before merging this change, just to be sure, the new report location makes sense and we don't inadvertently change other rules as well. So, this is maybe not ready even for 7.21.0. |
…e location of the violation
… keyword. Define a report location for ASTCompilationUnit
…ynchronized keyword.
78f0bb7 to
b26811f
Compare
|
I've update it now and added test cases for other affected rules (AbstractClassWithoutAnyMethod, AvoidDollarSigns, ClassNamingConventions, CyclomaticComplexity, DataClass, ExhaustiveSwitchHasDefault, GodClass, ImplicitFunctionalInterface, JUnit5TestShouldBePackagePrivate, MissingSerialVersionUID, MissingStaticMethodInNonInstantiatableClass, SwitchDensity, TooFewBranchesForSwitch, NcssCount, SingleMethodSingleton, LocalHomeNamingConvention, LocalInterfaceSessionNamingConvention). They all report on the identifier of class/interfaces now or on the switch keyword. I fixed CouplingBetweenObjects to correctly recognize nested classes. Hopefully the results make more sense now (lets wait for regression tester result). Two rules are affected, but I didn't include them, as they would not be finished completely:
In total, we have now updated 13% of our Java rules (37 of 289 rules). Next steps:
|
Describe the PR
This PR shrinks the reported locations for some rules. There is more to do on this front, but I'd like to get this part merged.
Related issues
Ready?
./mvnw clean verifypasses (checked automatically by github actions)