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

AvoidRecompilingXPathExpression for Kotlin #295

Merged
merged 12 commits into from Mar 14, 2024

Conversation

jborgers
Copy link
Owner

@jborgers jborgers commented Mar 8, 2024

AvoidRecompilingXPathExpression for Kotlin.
Note a code example of the good solution is TODO, the Java example is with ThreadLocals, unclear how this works for Kotlin.

Copy link
Collaborator

@stokpop stokpop left a comment

Choose a reason for hiding this comment

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

some remarks, which we partially discussed already... maybe you want to clarify/fix, but otherwise merge is fine

@jborgers jborgers requested a review from stokpop March 11, 2024 15:02
Copy link
Collaborator

@stokpop stokpop left a comment

Choose a reason for hiding this comment

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

Some questions, otherwise good to merge!

Hope use of ThreadLocals will not impact future code bases that use reactive style or virtual threads.

<priority>2</priority>
<properties>
<property name="tag" value="jpinpoint-rule" type="String" description="for-sonar"/>
<property name="version" value="2.0"/>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use 3.1?

Copy link
Owner Author

@jborgers jborgers Mar 12, 2024

Choose a reason for hiding this comment

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

Right, I think it is the default. PMD7 doc even says:
PMD uses XPath 3.1 for its XPath rules since PMD 7. Before then, the default version was XPath 1.0, with opt-in support for XPath 2.0.

The property version of XPathRule is deprecated and has been removed with PMD 7.


class AvoidRecompilingXPathExpressionKotlin {
fun bad1(doc: Document?): NodeList {
val xpath: XPath = XPathFactory.newInstance().newXPath()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we also have a rule to flag use of XPath.newInstance() each time? Also preferred to move to ThreadLocal?

Copy link
Owner Author

Choose a reason for hiding this comment

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

No we don't have it at the moment. Might be useful.

<priority>2</priority>
<properties>
<property name="tag" value="jpinpoint-rule" type="String" description="for-sonar"/>
<property name="version" value="2.0"/>
Copy link
Collaborator

Choose a reason for hiding this comment

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

also move to 3.1?

Copy link
Owner Author

Choose a reason for hiding this comment

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

also remove version attribute

} catch (e: XPathExpressionException) {
throw RuntimeException(e)
}
tlExpr = ThreadLocal.withInitial { expr } // good
Copy link
Collaborator

Choose a reason for hiding this comment

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

The rule does not check for use in ThreadLocal... probably compile call is only checked in FunctionBody, thus allowed in init block. Is good enough?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Yes, FunctionBody is used in the rule xpath expression. And I think good enough.

…lingXPathExpression

# Conflicts:
#	docs/EvaluateRulesForKotlin.md
#	src/main/resources/category/kotlin/common.xml
@jborgers jborgers merged commit 22eec3b into master Mar 14, 2024
Copy link

sonarcloud bot commented Mar 14, 2024

Quality Gate Passed Quality Gate passed

Issues
1 New issue
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@jborgers jborgers deleted the kotlin/AvoidRecompilingXPathExpression branch March 14, 2024 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants