Skip to content

[jsp] NoClassAttribute: False positive for jsp:useBean #2033

Closed

Description

@linusjf

Affects PMD Version:
6.18.0+
7.0.0

Rule: NoClassAttribute

Description:
The rule generates an error for a jsp:usebean tag whose "class" is specified.

Code Sample demonstrating the issue:
In JSP page:

<jsp:usebean id="data" class="jdbc.JdbcBean"/>

Running PMD through:
Any.

The rule needs the ability to exclude the above JSP tag.

https://stackoverflow.com/questions/39035297/what-is-the-difference-between-class-and-styleclass-attributes?rq=1

The other option is to suppress this false-positive violation. Suppressing via the message generated by PMD is not possible currently since the message does not list the offending tag.
PMD doesn't compile the JSP files into java source. (Does PMD intend to go down that route?) Suppressing via Xpath isn't possible either.

https://pmd.github.io/latest/pmd_userdocs_suppressing_warnings.html#xpath-and-regex-message-suppression

Is it possible that PMD can generate ASTs for JSPs as well in the future?

The rule could be modified to ignore the above tag with a default property value which is extensible to user-specified tags if necessary.

Suppressions won't be needed in that case.

Activity

  1. added
    a:false-positive
    on Oct 3, 2019
  2. adangel commented on Oct 3, 2019

    @adangel
    Member

    What is the motivation for this rule? Is it to ensure that JSF tags use the styleclass attribute as against the class attribute for an HTML element? Or is to discourage in-lining of style elements in an HTML element, which is a best practice recommended for HTML? I'd have to assume it's the former. Either way, the above is still a bug.

    Quoting the rule description:

    Do not use an attribute called ‘class’. Use "styleclass" for CSS styles.

    So, this rule indeed seems to ensure, that JSF tags only use the "styleClass" attribute instead of the directly the html attribute "class". (Note, there is no "styleclass" attribute with all lowercase letters...)
    There are unfortunately no good test cases (see https://github.com/pmd/pmd/blob/master/pmd-jsp/src/test/resources/net/sourceforge/pmd/lang/jsp/rule/bestpractices/xml/NoClassAttribute.xml), e.g. no test case, that makes sure, that the alleged valid case with styleclass doesn't raise a violation (so protection against a false-positive even for the case the rule was written for).

    The rule could be easily adjusted to exclude elements from the "jsf" namespace, e.g.

    //Element[@HasNamespacePrefix = false() or @NamespacePrefix != 'jsp']
        /Attribute[ upper-case(@Name)="CLASS" ]
    

    (note: xpath 2.0 syntax)

    The valid namespaces should then be supplied via a property, so that it can be adjusted by the rule users.

    However, I can't say, whether this rule makes sense at all. E.g. the standard tag libraries (h.outputText don't support a class attribute anyway, you have to use styleClass.

    Also quoting from your linked stackoverflow question:

    And class attribute is used with HTML elements, and can't be used directly with JSF components.

    Maybe the rule wanted to prevent using "style"? Unless it wanted to prevent that html is directly used and every rendering should be done through jsf components (or similar alternatives), but than the rule should have been named "NoHtml"...

  3. changed the title [-][JSP] NoClassAttribute rule is generating a false positive.[/-] [+][jsp] NoClassAttribute rule is generating a false positive[/+] on Oct 3, 2019
  4. linusjf commented on Oct 3, 2019

    @linusjf
    Author
  5. linusjf commented on Oct 4, 2019

    @linusjf
    Author
  6. adangel commented on Nov 15, 2019

    @adangel
    Member

    You kinda got me with that Xpath expression that amends the rule. Is there a central location
    where these Xpath expressions for each individual rule is located so that they can be studied
    individually without firing up the Rule Designer?

    The xpath expressions for the built in rules are documented in the rule documentation. In the sources, they are in the corresponding category rulesets, e.g.

    //Attribute[ upper-case(@Name)="CLASS" ]

    Another point that's unrelated, is PMD, at some point , going to introduce a feature that allows
    users to track rules and the metrics associated with each rule as to how many times it occurs in
    the code base? This would be internal with an option to enable sharing that data with PMD via
    some sort of web service. Just a thought.

    Maybe. I guess that requires some sort of "multi file analysis" then, since you want to get an overview of the whole project. Currently the rules operate only on single files and don't see the big picture. For certain metrics, this is enough, but for others (e.g. calculating the average class size and compare each class to this average) we need this multi file analysis. Reporting these metrics then another point: Currently PMD is a standalone application, so we would probably extend/add report formats for metrics.

  7. linusjf commented on Nov 15, 2019

    @linusjf
    Author
  8. jsotuyod commented on May 15, 2020

    @jsotuyod
    Member

    Maybe the rule wanted to prevent using "style"? Unless it wanted to prevent that html is directly used and every rendering should be done through jsf components (or similar alternatives), but than the rule should have been named "NoHtml"...

    I think this is actually trying to avoid abuse… When an ad-hoc attribute is set on a tag, if it's not interpreted by the tag implementation it's usually passed through as-is to the rendered HTML (but the tag can change this behavior). The styleClass attribute on the other hand is supported by the renderer itself, and is therefore more reliable.

    Moreover, tags, such as the one cited, may give a different meaning to the class attribute, raising the importance of using styleClass instead.

    In hindsight, this was probably a very poor choice when designing the JSF tags… but that ship has already sailed.

    We should whitelist tags that define a class attribute (and assume the user knows it's intent). Namespaces are maybe too broad + can be set arbitrarily… The definitive solution would be using the taglib uri to infer the used prefix, and then checking for that prefix + tag, but that's going to be hard on XPath… In the meantime, simply whitelisting tag names or "standard prefix" + tagname should suffice.

  9. linusjf commented on May 16, 2020

    @linusjf
    Author

    This rule has been around for quite a while, yet there does not appear to be any issue raised about this rule.

    Possible reasons could be:

    1. Using a framework such as Spring MVC which precludes the use of a bean directly in the View.

    https://stackoverflow.com/questions/18620840/what-should-i-use-instead-of-jspusebean

    1. Use of JSF which requires beans to be specified in the configuration.

    https://www.icesoft.org/JForum/posts/list/7370.page

    How about a rule or message that discourages use of jsp:usebean in the view, as it ought to be part of the business logic instead?

  10. added
    needs:pmd7-revalidation
    on Mar 17, 2024
  11. added
    good first issue
    and removed
    needs:pmd7-revalidation
    on Apr 2, 2024
  12. added this to the 7.27.0 milestone on Aug 8, 2026
  13. changed the title [-][jsp] NoClassAttribute rule is generating a false positive[/-] [+][jsp] NoClassAttribute: False positive for jsp:useBean[/+] on Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivegood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions