Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] CommentSize rule violation is not suppressed at method level #2996

Open
linusjf opened this issue Jan 5, 2021 · 2 comments · May be fixed by #4939
Open

[java] CommentSize rule violation is not suppressed at method level #2996

linusjf opened this issue Jan 5, 2021 · 2 comments · May be fixed by #4939
Labels
a:bug PMD crashes or fails to analyse a file.
Milestone

Comments

@linusjf
Copy link

linusjf commented Jan 5, 2021

Affects PMD Version:
6.30.0

Description:
Violations of rule CommentSize are not suppressed by the annotations @SuppressWarnings("PMD.CommentSize") or @SuppressWarnings("PMD") when placed on the method definition.

Code Sample demonstrating the issue:

package pmdtests;

public final class TestComment {
  @SuppressWarnings("PMD.CommentSize")
  private TestComment() {
    /*
     * Writing and maintaining complicated software is a difficult task,
     * and every programmer inadvertently makes mistakes.
     * Usually these are minor typographical errors that will be caught at
     * compile time, but others may remain undetected until the system is in production.
     * In the most extreme cases, the bug will cause system failure, as was the case in
     * the massive blackout in the Northeast last summer.
     * In addition to the consequences of system malfunction,
     * software defects have a substantial financial cost.
     * A 2003 report from the National Institute of Standards and Technology (NIST)
     * calculated the total annual cost of bugs at nearly $60 billion.
     * The report also confirmed what most developers already know:
     * the difficulty and cost of fixing a problem grows dramatically throughout the development cycle.
     * Although the report concluded that it is not practical to locate and remove all defects
     * from an application, it stated that nearly 40% could be eliminated by an improved inspection and testing
     * process.
     *
     * Two common methods for improving software quality are code reviews and automated
     * testing. The goal of both is to detect mistakes, but the distinction between them
     * is how this is achieved. Automated testing attempts to expose problems by executing
     * the code, while reviews rely on "another set of eyes" to verify that it's correct.
     * Problems can arise when we get too involved with the code to be objective or to consider
     * unusual paths of execution. Certainly every programmer has at one time
     * spent hours trying to track down a problem only to have someone else spot it
     * immediately.
     */
    throw new UnsupportedOperationException(
        "This is a utility class and cannot be instantiated");
  }

  public static void main(String... args) {
    // empty main
  }
}

Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]

Referred from: #2995

@linusjf linusjf added the a:bug PMD crashes or fails to analyse a file. label Jan 5, 2021
@oowekyala oowekyala added this to the 7.0.0 milestone Jan 17, 2021
@oowekyala oowekyala added this to To do in PMD 7 Apr 3, 2021
@cshezhang
Copy link

@oowekyala Hi, oowekyala, I think this issue is very interesting and find you have added this to 7.0 milestone, could you please tell me the root cause or spot the potental buggy code file? THX.

@oowekyala
Copy link
Member

Rules normally report violations on an AST node that corresponds to the location of the violation, and that node is used to find an enclosing annotation, by inspecting its ancestors until the root of the tree. The problem is that comments are not nodes in the AST, and we also have no API to find a node close to a comment (although that exists in the designer currently). So this rule always uses the root node as a placeholder, eg here https://github.com/pmd/pmd/blob/master/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/CommentSizeRule.java#L105

This could be fixed in PMD 6 if the functionality to find a node from coordinates was backported from the designer into pmd-core. The relevant code is this function and its implementation: https://github.com/pmd/pmd-designer/blob/ee66c3678693deefc2582bebf0b0e8eb3259fa74/src/main/java/net/sourceforge/pmd/util/fxdesigner/util/codearea/PmdCoordinatesSystem.java#L92

I marked this for PMD 7 because maybe we can have a nicer solution then, eg by representing comments as actual nodes in the tree. There is a todo item in #3898 about that. Also moving the linked designer code into pmd-core would be easier in PMD 7 as there are already data types there to represent text positions, which in PMD 6 only exist in the designer codebase (this TextPos2d class).

@adangel adangel modified the milestones: 7.0.0, 7.x Jan 23, 2023
@adangel adangel removed this from To do in PMD 7 Jan 23, 2023
@jsotuyod jsotuyod added the needs:pmd7-revalidation The issue hasn't yet been retested vs PMD 7 and may be stale label Apr 2, 2024
@oowekyala oowekyala linked a pull request Apr 6, 2024 that will close this issue
4 tasks
@oowekyala oowekyala removed the needs:pmd7-revalidation The issue hasn't yet been retested vs PMD 7 and may be stale label Apr 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:bug PMD crashes or fails to analyse a file.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants