Issue #9889: Prevent usage of 'getFileContents()' method in code#9909
Issue #9889: Prevent usage of 'getFileContents()' method in code#9909romani merged 1 commit intocheckstyle:masterfrom
Conversation
29dd515 to
75dee4e
Compare
|
Wercker Failure: We need to set similar suppression in sevntu project. |
PR sent |
b5b74ed to
61f57dc
Compare
config/checkstyle_checks.xml
Outdated
| </module> | ||
| <module name="RegexpSinglelineJava"> | ||
| <property name="id" value="noUsageOfGetFileContents()Method"/> | ||
| <property name="format" value="getFileContents()"/> |
There was a problem hiding this comment.
Since this is regexp, don't we have to sanitize the () since we want to match text? IE: \(\)
config/suppressions.xml
Outdated
| <suppress id="noUsageOfGetFileContents()Method" files="SuppressionCommentFilter.java"/> | ||
| <suppress id="noUsageOfGetFileContents()Method" files="SuppressWithNearbyCommentFilter.java"/> | ||
| <suppress id="noUsageOfGetFileContents()Method" files="XpathQueryGenerator.java"/> | ||
| <suppress id="noUsageOfGetFileContents()Method" files="AbstractFileSetCheck.java"/> |
There was a problem hiding this comment.
This is just a comment.
This is an example of a benefit using java check over reg exp.
src/main/java/com/puppycrawl/tools/checkstyle/api/AbstractFileSetCheck.java
137: protected final FileContents getFileContents() {
This is a method declaration and not an usage of the method.
There was a problem hiding this comment.
Very good point.
Looks like we need xpath Check for this, as we need prevent usage of such method only classes that extend AbstractCheck that uses for all java Checks.
There was a problem hiding this comment.
This is just a comment.
This is an example of a benefit using java check over reg exp.
src/main/java/com/puppycrawl/tools/checkstyle/api/AbstractFileSetCheck.java
137: protected final FileContents getFileContents() {This is a method declaration and not an usage of the method.
Ok I dived deeper into the code and see what you mean here. From my understanding, XPath check is the strictest and the most secure one for this case. I vote for it. Do you all agree?
There was a problem hiding this comment.
Either java check or xpath is fine.
There was a problem hiding this comment.
Either java check or xpath is fine.
ok seems like @romani agrees on xPath too.
Should I close this PR or amend it the XPath way?
There was a problem hiding this comment.
Go ahead and amend it. Just keep it rebased on the latest master.
There was a problem hiding this comment.
here is example of method detection by Xpath - https://github.com/checkstyle/checkstyle/pull/9939/files#diff-3a0a81e04acf0df0c9c8adefc1cae87e39da88c3141a7c131bcaf7a8ab12c62cR421
but in your case you need to make sure that classes are AbstractCheck to show violation, all other usages should be allowed.
There was a problem hiding this comment.
@romani hey AbstractCheck has some abstract subclasses (AbstractSuperCheck, AbstractJavadocCheck, AbstractClassCouplingCheck,...)
We need to forbid the usage of getFileContents() in any classes that extend AbstractCheck and any classes that extend its abstract subclasses. Or otherwise stated, any classes that extend superclasses identified with texts "Abstract" and "Check".
Do I understand correctly?
There was a problem hiding this comment.
@haanhvu Please work on creating the xpath to match method calls we want to prevent.
Once that is working, we'll look at the areas it is flagging.
ff3fe56 to
682128f
Compare
|
@romani @rnveach I amended the PR the XPath way. Please review. Before suppression: After suppression: |
4d32bd1 to
6fed1de
Compare
c02ef62 to
a1e7309
Compare
99557f3 to
a5dd751
Compare
|
@haanhvu Are you going to continue this PR and fix the CI failure? |
Yes, I will and I want to. Sorry it's been crazy weeks here. Can you show me more about the context of the failure and what I can do to fix it? |
|
The error printing in the CI is a checkstyle error in another repo.
https://github.com/sevntu-checkstyle/methods-distance I also recommend rebasing on the latest master. |
|
@haanhvu ping |
yeah I'm working on it |
|
@haanhvu any progress on this PR? |
|
@nmancus1 I'm finishing it this week. Sorry for the delay. |
|
@haanhvu , please rebase on latest code, medthod-distance PR is merged |
|
Rebased. |
|
@romani TC fails from the usage of the deprecated methods. How do we suppress these in TC? |

closes #9889