You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[jsp] NoClassAttribute: False positive for jsp:useBean #2033
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.
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.
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"...
changed the title [-][JSP] NoClassAttribute rule is generating a false positive.[/-][+][jsp] NoClassAttribute rule is generating a false positive[/+]on Oct 3, 2019
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.
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.
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.
changed the title [-][jsp] NoClassAttribute rule is generating a false positive[/-][+][jsp] NoClassAttribute: False positive for jsp:useBean[/+]on Aug 20, 2026
Activity
adangel commentedon Oct 3, 2019
Quoting the rule description:
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.
(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:
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"...
[-][JSP] NoClassAttribute rule is generating a false positive.[/-][+][jsp] NoClassAttribute rule is generating a false positive[/+]linusjf commentedon Oct 3, 2019
linusjf commentedon Oct 4, 2019
adangel commentedon Nov 15, 2019
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.
pmd/pmd-jsp/src/main/resources/category/jsp/bestpractices.xml
Line 60 in 9923dd2
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.
linusjf commentedon Nov 15, 2019
jsotuyod commentedon May 15, 2020
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
styleClassattribute 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
classattribute, raising the importance of usingstyleClassinstead.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
classattribute (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.linusjf commentedon May 15, 2020
http://ibmwcsworld.blogspot.com/2012/09/wcbaseusebean-and-jsp-best-practises.html?m=1
Classname would have been a better choice, but, as you rightly pointed out, that ship has sailed.
linusjf commentedon May 16, 2020
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:
https://stackoverflow.com/questions/18620840/what-should-i-use-instead-of-jspusebean
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?
[jsp] Fix false positive in NoClassAttribute for jsp:useBean (pmd#2033)
[-][jsp] NoClassAttribute rule is generating a false positive[/-][+][jsp] NoClassAttribute: False positive for jsp:useBean[/+]