[core] Use annotations to resolve rule dependencies to frameworks#437
[core] Use annotations to resolve rule dependencies to frameworks#437oowekyala wants to merge 4 commits into
Conversation
|
@oowekyala this is really interesting. I have to confess I've been thinking about this myself (actually, I even spent some time pondering on this earlier today!). Pros of this approach:
Cons of this approach:
I think this is very interesting, but needs a deep discussion and a proper planning on how such feature should work / rollout plan shall we decide to proceed with it. |
|
@jsotuyod Thanks for your input! I don't think the startup penalty would be significant. Besides, we already use reflection through instantiating rules with I agree we must think more deeply about how to tackle inconsistencies between the ruleset and the implementation of the rules. Dependencies are part of the implementation of a rule, so it makes sense that Java rules declare theirs inside the class and not the ruleset. However XPath rules have their implementation contained in the rulesets files, so we could make it compulsory for xpath rules to declare their dependencies, something like declaring an xpath rule like the following: <rule name="xxx"
description="xxxx" >
<xpath>
<dependencies typeResolution="true" /> <!-- the others are implicitly false -->
<expression>
<![CDATA[
//ForStatement (...)
]]>
</expression>
</xpath>
</rule>This would make it implicit that the Then we could drop the We could enforce stricter validation assertions in the XML schema, for example making sure that
Custom java rules would still use the
Message, description and site URL are metadata, so I don't think they should stick with the implementation of a rule. We could however broaden the contract of the All in all, this approach would make implementation stick with implementation, and metadata with metadata, which I think is great. Plus, that would be a best effort to ensure that rule dependencies can't be misconfigured in the ruleset. (Besides, |
|
Nice idea, indeed! Just my 2 cents:
|
I've been researching this a bit and there may be a way to do this... For Saxon we can write a visitor, start from For Jaxen it's a little more complicated. We would need to take the Of course we would want all of this to be done just once upon creation. Also of note, during this research I found that xpath expressions are recreated (and therefore re-parsed) each time they are evaluated. I'm not certain this is actually needed. |
|
@jsotuyod Out of curiosity, I wonder why we still support Jaxen. I mean, from what I understand, XPath 2.0 is a superset of XPath 1.0 so maybe there's no point in maintaining support for both separately? This would make the change you describe easier to implement in case we decide to proceed with it |
|
@oowekyala I'm not sure why, but our current implementation of XPath 2.0 is not necessarily compatible with XPath 1.0. All XPath rules are currently defaulting to 1.0 (Jaxen), and most of the time, changing them to 2.0 (or sometimes even 1.0 compatibility mode) will result in failure to match nodes. This may be an issue on our implementation (specially due to the shortcuts taken to use the Rule Chain), but at this point I'm not certain. For future releases we may check on those difference, check performance differences between both implementations, and deprecate whichever one we want to drop (also, we use a very old Saxon version, but updating is non-trivial). For the time being we are stuck with supporting both. |
|
Interesting, thanks! |
|
Closing this because of clutter. The discussion may be continued here or on an RFC when the time comes |
Changes description:
Rules written in Java can now use annotations to specify their dependencies to a specific framework (
@UsesDfa,@UsesTypeResolution,@UsesMetrics). The concept can be extended to other languages.Rationale:
Atm these dependencies must be specified as an xml attribute in the ruleset. However, the dependencies really are part of the implementation of the rules, and the rules can't work properly without them. For example,
DataflowAnomalyAnalysisRulethrows hundreds of NPEs if thedfa="true"attribute is ommited in the xml. Correct me if I'm wrong, but that's not a feature, that's a bug.I think it's safer that those dependencies be specified with the implementation of the rule (and annotations are an elegant way to do so). That way, the user need not worry about them (the implementation is none of their concern), and their rulesets can be a little bit cleaner. And of course old rulesets still work.
Possible improvements and discussion
dfaattribute, and it would be precisely to set it tofalse: should a user not want to use DFA (for performance issues, or benchmarking), setting the attribute to false should deactivate the rule (to prevent its malfunction). I could implement that if you agree.classattribute of a<rule>tag is necessary? I understand it helps the user find the class if they want to modify it, but wouldn't rulesets be easier to make and read if we could identify the class of the rule from its name?