Skip to content

Issue #9889: Prevent usage of 'getFileContents()' method in code#9909

Merged
romani merged 1 commit intocheckstyle:masterfrom
haanhvu:issue#9889
Jan 9, 2022
Merged

Issue #9889: Prevent usage of 'getFileContents()' method in code#9909
romani merged 1 commit intocheckstyle:masterfrom
haanhvu:issue#9889

Conversation

@haanhvu
Copy link
Copy Markdown
Contributor

@haanhvu haanhvu commented Apr 21, 2021

closes #9889

@haanhvu haanhvu force-pushed the issue#9889 branch 2 times, most recently from 29dd515 to 75dee4e Compare April 22, 2021 07:08
@romani
Copy link
Copy Markdown
Member

romani commented Apr 22, 2021

Wercker Failure:

[INFO] There are 2 errors reported by Checkstyle 8.42-SNAPSHOT with ../../../config/checkstyle_checks.xml ruleset.
[ERROR] src/main/java/com/github/sevntu/checkstyle/checks/coding/TernaryPerExpressionCountCheck.java:[239] (extension) noUsageOfGetFileContents()Method: The usage of getFileContents() method is not allowed
[ERROR] src/main/java/com/github/sevntu/checkstyle/checks/coding/TernaryPerExpressionCountCheck.java:[256] (extension) noUsageOfGetFileContents()Method: The usage of getFileContents() method is not allowed
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE

We need to set similar suppression in sevntu project.
Please send PR to update https://github.com/sevntu-checkstyle/sevntu.checkstyle/blob/master/sevntu-checks/suppressions.xml

@haanhvu
Copy link
Copy Markdown
Contributor Author

haanhvu commented Apr 25, 2021

Wercker Failure:

[INFO] There are 2 errors reported by Checkstyle 8.42-SNAPSHOT with ../../../config/checkstyle_checks.xml ruleset.
[ERROR] src/main/java/com/github/sevntu/checkstyle/checks/coding/TernaryPerExpressionCountCheck.java:[239] (extension) noUsageOfGetFileContents()Method: The usage of getFileContents() method is not allowed
[ERROR] src/main/java/com/github/sevntu/checkstyle/checks/coding/TernaryPerExpressionCountCheck.java:[256] (extension) noUsageOfGetFileContents()Method: The usage of getFileContents() method is not allowed
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE

We need to set similar suppression in sevntu project.
Please send PR to update https://github.com/sevntu-checkstyle/sevntu.checkstyle/blob/master/sevntu-checks/suppressions.xml

PR sent

@haanhvu haanhvu force-pushed the issue#9889 branch 2 times, most recently from b5b74ed to 61f57dc Compare April 25, 2021 16:22
</module>
<module name="RegexpSinglelineJava">
<property name="id" value="noUsageOfGetFileContents()Method"/>
<property name="format" value="getFileContents()"/>
Copy link
Copy Markdown
Contributor

@rnveach rnveach Apr 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is regexp, don't we have to sanitize the () since we want to match text? IE: \(\)

<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"/>
Copy link
Copy Markdown
Contributor

@rnveach rnveach Apr 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

@romani romani Apr 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either java check or xpath is fine.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go ahead and amend it. Just keep it rebased on the latest master.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

@haanhvu haanhvu marked this pull request as draft April 28, 2021 07:36
@haanhvu haanhvu closed this Apr 28, 2021
@haanhvu haanhvu reopened this Apr 28, 2021
@haanhvu haanhvu force-pushed the issue#9889 branch 5 times, most recently from ff3fe56 to 682128f Compare April 30, 2021 05:31
@haanhvu haanhvu marked this pull request as ready for review April 30, 2021 06:56
@haanhvu haanhvu requested review from rnveach and romani April 30, 2021 06:57
@haanhvu
Copy link
Copy Markdown
Contributor Author

haanhvu commented Apr 30, 2021

@romani @rnveach I amended the PR the XPath way. Please review.

Before suppression:

[ERROR] [checkstyle] [ERROR] C:\Users\Admin\Documents\Github\checkstyle\src\main\java\com\puppycrawl\tools\checkstyle\TreeWalker.java:54:1: Avoid using getFileContents() method in                                             AbstractCheck subclasses. [noUsageOfGetFileContents()Method]
[ERROR] [checkstyle] [ERROR] C:\Users\Admin\Documents\Github\checkstyle\src\main\java\com\puppycrawl\tools\checkstyle\checks\AvoidEscapedUnicodeCharactersCheck.java:166:1: Avoid using getFileContents() method in                                             AbstractCheck subclasses. [noUsageOfGetFileContents()Method]
[ERROR] [checkstyle] [ERROR] C:\Users\Admin\Documents\Github\checkstyle\src\main\java\com\puppycrawl\tools\checkstyle\checks\OuterTypeFilenameCheck.java:85:1: Avoid using getFileContents() method in                                             AbstractCheck subclasses. [noUsageOfGetFileContents()Method]
[ERROR] [checkstyle] [ERROR] C:\Users\Admin\Documents\Github\checkstyle\src\main\java\com\puppycrawl\tools\checkstyle\checks\annotation\MissingOverrideCheck.java:169:1: Avoid using getFileContents() method in                                             AbstractCheck subclasses. [noUsageOfGetFileContents()Method]
[ERROR] [checkstyle] [ERROR] C:\Users\Admin\Documents\Github\checkstyle\src\main\java\com\puppycrawl\tools\checkstyle\checks\annotation\PackageAnnotationCheck.java:79:1: Avoid using getFileContents() method in                                             AbstractCheck subclasses. [noUsageOfGetFileContents()Method]
[ERROR] [checkstyle] [ERROR] C:\Users\Admin\Documents\Github\checkstyle\src\main\java\com\puppycrawl\tools\checkstyle\checks\coding\FallThroughCheck.java:189:1: Avoid using getFileContents() method in                                             AbstractCheck subclasses. [noUsageOfGetFileContents()Method]
[ERROR] [checkstyle] [ERROR] C:\Users\Admin\Documents\Github\checkstyle\src\main\java\com\puppycrawl\tools\checkstyle\checks\coding\PackageDeclarationCheck.java:103:1: Avoid using getFileContents() method in                                             AbstractCheck subclasses. [noUsageOfGetFileContents()Method]
[ERROR] [checkstyle] [ERROR] C:\Users\Admin\Documents\Github\checkstyle\src\main\java\com\puppycrawl\tools\checkstyle\checks\imports\ImportControlCheck.java:449:1: Avoid using getFileContents() method in                                             AbstractCheck subclasses. [noUsageOfGetFileContents()Method]
[ERROR] [checkstyle] [ERROR] C:\Users\Admin\Documents\Github\checkstyle\src\main\java\com\puppycrawl\tools\checkstyle\checks\imports\UnusedImportsCheck.java:116:1: Avoid using getFileContents() method in                                             AbstractCheck subclasses. [noUsageOfGetFileContents()Method]
[ERROR] [checkstyle] [ERROR] C:\Users\Admin\Documents\Github\checkstyle\src\main\java\com\puppycrawl\tools\checkstyle\checks\javadoc\JavadocMethodCheck.java:289:1: Avoid using getFileContents() method in                                             AbstractCheck subclasses. [noUsageOfGetFileContents()Method]
[ERROR] [checkstyle] [ERROR] C:\Users\Admin\Documents\Github\checkstyle\src\main\java\com\puppycrawl\tools\checkstyle\checks\javadoc\JavadocStyleCheck.java:307:1: Avoid using getFileContents() method in                                             AbstractCheck subclasses. [noUsageOfGetFileContents()Method]
[ERROR] [checkstyle] [ERROR] C:\Users\Admin\Documents\Github\checkstyle\src\main\java\com\puppycrawl\tools\checkstyle\checks\javadoc\JavadocTypeCheck.java:203:1: Avoid using getFileContents() method in                                             AbstractCheck subclasses. [noUsageOfGetFileContents()Method]
[ERROR] [checkstyle] [ERROR] C:\Users\Admin\Documents\Github\checkstyle\src\main\java\com\puppycrawl\tools\checkstyle\checks\javadoc\JavadocVariableCheck.java:176:1: Avoid using getFileContents() method in                                             AbstractCheck subclasses. [noUsageOfGetFileContents()Method]
[ERROR] [checkstyle] [ERROR] C:\Users\Admin\Documents\Github\checkstyle\src\main\java\com\puppycrawl\tools\checkstyle\checks\javadoc\MissingJavadocMethodCheck.java:265:1: Avoid using getFileContents() method in                                             AbstractCheck subclasses. [noUsageOfGetFileContents()Method]
[ERROR] [checkstyle] [ERROR] C:\Users\Admin\Documents\Github\checkstyle\src\main\java\com\puppycrawl\tools\checkstyle\checks\javadoc\MissingJavadocPackageCheck.java:73:1: Avoid using getFileContents() method in                                             AbstractCheck subclasses. [noUsageOfGetFileContents()Method]
[ERROR] [checkstyle] [ERROR] C:\Users\Admin\Documents\Github\checkstyle\src\main\java\com\puppycrawl\tools\checkstyle\checks\javadoc\MissingJavadocTypeCheck.java:163:1: Avoid using getFileContents() method in                                             AbstractCheck subclasses. [noUsageOfGetFileContents()Method]
[ERROR] [checkstyle] [ERROR] C:\Users\Admin\Documents\Github\checkstyle\src\main\java\com\puppycrawl\tools\checkstyle\checks\javadoc\WriteTagCheck.java:185:1: Avoid using getFileContents() method in                                             AbstractCheck subclasses. [noUsageOfGetFileContents()Method]
[ERROR] [checkstyle] [ERROR] C:\Users\Admin\Documents\Github\checkstyle\src\main\java\com\puppycrawl\tools\checkstyle\checks\regexp\RegexpCheck.java:473:1: Avoid using getFileContents() method in                                             AbstractCheck subclasses. [noUsageOfGetFileContents()Method]
[ERROR] [checkstyle] [ERROR] C:\Users\Admin\Documents\Github\checkstyle\src\main\java\com\puppycrawl\tools\checkstyle\checks\regexp\RegexpSinglelineJavaCheck.java:209:1: Avoid using getFileContents() method in                                             AbstractCheck subclasses. [noUsageOfGetFileContents()Method]
[ERROR] [checkstyle] [ERROR] C:\Users\Admin\Documents\Github\checkstyle\src\main\java\com\puppycrawl\tools\checkstyle\checks\sizes\MethodLengthCheck.java:103:1: Avoid using getFileContents() method in                                             AbstractCheck subclasses. [noUsageOfGetFileContents()Method]
[ERROR] [checkstyle] [ERROR] C:\Users\Admin\Documents\Github\checkstyle\src\main\java\com\puppycrawl\tools\checkstyle\checks\whitespace\EmptyLineSeparatorCheck.java:328:1: Avoid using getFileContents() method in                                             AbstractCheck subclasses. [noUsageOfGetFileContents()Method]
[ERROR] [checkstyle] [ERROR] C:\Users\Admin\Documents\Github\checkstyle\src\main\java\com\puppycrawl\tools\checkstyle\meta\JavadocMetadataScraper.java:51:1: Avoid using getFileContents() method in                                             AbstractCheck subclasses. [noUsageOfGetFileContents()Method]
[ERROR] [checkstyle] [ERROR] C:\Users\Admin\Documents\Github\checkstyle\src\test\java\com\puppycrawl\tools\checkstyle\CheckerTest.java:1703:5: Avoid using getFileContents() method in                                             AbstractCheck subclasses. [noUsageOfGetFileContents()Method]

After suppression:
BUILD SUCCESS

rnveach
rnveach previously requested changes May 5, 2021
@rnveach rnveach assigned romani and unassigned rnveach May 6, 2021
@haanhvu haanhvu force-pushed the issue#9889 branch 2 times, most recently from 4d32bd1 to 6fed1de Compare June 11, 2021 13:47
@haanhvu haanhvu marked this pull request as draft June 11, 2021 14:08
@haanhvu haanhvu force-pushed the issue#9889 branch 4 times, most recently from c02ef62 to a1e7309 Compare June 13, 2021 11:58
Copy link
Copy Markdown
Member

@romani romani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Items:

@haanhvu haanhvu force-pushed the issue#9889 branch 2 times, most recently from 99557f3 to a5dd751 Compare June 16, 2021 09:16
@rnveach
Copy link
Copy Markdown
Contributor

rnveach commented Jul 27, 2021

@haanhvu Are you going to continue this PR and fix the CI failure?

@haanhvu
Copy link
Copy Markdown
Contributor Author

haanhvu commented Jul 28, 2021

@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?

@rnveach
Copy link
Copy Markdown
Contributor

rnveach commented Aug 3, 2021

The error printing in the CI is a checkstyle error in another repo.

pipeline/source/.ci-temp/methods-distance/methods-distance/src/main/java/com/github/sevntu/checkstyle/module/MethodCallDependencyCheckstyleModule.java:39:1: Avoid using getFileContents method in AbstractCheck subclasses. [noUsageOfGetFileContentsMethod]

https://github.com/sevntu-checkstyle/methods-distance
You will need to send a PR and correct the error.

I also recommend rebasing on the latest master.

@rnveach
Copy link
Copy Markdown
Contributor

rnveach commented Aug 25, 2021

@haanhvu ping

@haanhvu
Copy link
Copy Markdown
Contributor Author

haanhvu commented Aug 31, 2021

@haanhvu ping

yeah I'm working on it

@nrmancuso
Copy link
Copy Markdown
Contributor

@haanhvu any progress on this PR?

@haanhvu
Copy link
Copy Markdown
Contributor Author

haanhvu commented Nov 29, 2021

@nmancus1 I'm finishing it this week. Sorry for the delay.

@romani
Copy link
Copy Markdown
Member

romani commented Dec 18, 2021

@haanhvu , please rebase on latest code, medthod-distance PR is merged

@rnveach
Copy link
Copy Markdown
Contributor

rnveach commented Dec 31, 2021

Rebased.

@rnveach
Copy link
Copy Markdown
Contributor

rnveach commented Jan 8, 2022

@romani
Sonar fails because of the marked deprecated method in this PR.

TC fails from the usage of the deprecated methods. How do we suppress these in TC?

@romani
Copy link
Copy Markdown
Member

romani commented Jan 8, 2022

there is not way to suppress this in inspection configuration
image

so it mean that we need to suppress it by javadoc tags with comment until ...
I placed suppression to all, I hope we will address them at #11166
Sonar issues should be marked as "Wontfix until #11166"

@romani romani merged commit 769f4f7 into checkstyle:master Jan 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prevent usage of 'getFileContents()' method in code

4 participants