Skip to content

Rule for Request #707: AvoidRecreatingMessageDigests - MessageDigest.getInstance is expensive because of classloading - #710

Open
jborgers wants to merge 1 commit into
pmd7from
pmd7-issue707
Open

Rule for Request #707: AvoidRecreatingMessageDigests - MessageDigest.getInstance is expensive because of classloading#710
jborgers wants to merge 1 commit into
pmd7from
pmd7-issue707

Conversation

@jborgers

@jborgers jborgers commented Aug 7, 2026

Copy link
Copy Markdown
Owner

No description provided.

…getInstance is expensive because of classloading
@jborgers
jborgers requested a review from stokpop August 7, 2026 12:41
@sonarqubecloud

sonarqubecloud Bot commented Aug 7, 2026

Copy link
Copy Markdown

@stokpop stokpop left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Some questions and possible improvements.

Comment thread docs/JavaCodePerformance.md
Comment thread docs/JavaCodePerformance.md
LocalVariableDeclaration[ClassType[pmd-java:typeIs('java.security.MessageDigest')]]/VariableDeclarator/MethodCall[@MethodName='getInstance']
| MethodCall[starts-with(@MethodName,'digest')]/ConstructorCall[pmd-java:typeIs('org.apache.commons.codec.digest.DigestUtils')]
| LocalVariableDeclaration[ClassType[pmd-java:typeIs('java.security.MessageDigest')]]//MethodCall/TypeExpression[pmd-java:typeIs('org.apache.commons.codec.digest.DigestUtils')]
| MethodCall[starts-with(@MethodName,'sha') or starts-with(@MethodName,'md')]/TypeExpression[pmd-java:typeIs('org.apache.commons.codec.digest.DigestUtils')]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Document why these two method prefixes sha and md are ok. Not too brittle? There are also shake prefixes in DigestUtils... oh those also start with sha.

Comment thread docs/JavaCodePerformance.md

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds the AvoidRecreatingMessageDigests PMD rule requested in issue #707.

Changes:

  • Detects repeated MessageDigest and DigestUtils creation.
  • Adds rule tests and documentation.
  • Publishes the rule across distributed rulesets.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/main/resources/category/java/common.xml Defines the XPath rule.
rulesets/java/jpinpoint-rules.xml Adds the distributed rule.
rulesets/java/jpinpoint-java-rules.xml Adds the Java ruleset copy.
src/test/java/.../AvoidRecreatingMessageDigestsTest.java Adds the PMD test harness.
src/test/resources/.../AvoidRecreatingMessageDigests.xml Adds positive and negative cases.
docs/JavaCodePerformance.md Documents the performance guidance.
Suppressed comments (1)

src/test/resources/com/jpinpoint/perf/lang/java/ruleset/common/xml/AvoidRecreatingMessageDigests.xml:59

  • This negative case returns updateDigest directly, so it never exercises the XPath branch that matches a DigestUtils call inside a MessageDigest local declaration. Assign the returned (same) digest to a local first; the current rule will then expose its false positive and the corrected signature-based selector will keep the case clean.
    MessageDigest good3DigestUtils() {
        MessageDigest mdLocal = mdField.clone();
        return DigestUtils.updateDigest(mdLocal, dataToDigest);
    }

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +2604 to +2607
LocalVariableDeclaration[ClassType[pmd-java:typeIs('java.security.MessageDigest')]]/VariableDeclarator/MethodCall[@MethodName='getInstance']
| MethodCall[starts-with(@MethodName,'digest')]/ConstructorCall[pmd-java:typeIs('org.apache.commons.codec.digest.DigestUtils')]
| LocalVariableDeclaration[ClassType[pmd-java:typeIs('java.security.MessageDigest')]]//MethodCall/TypeExpression[pmd-java:typeIs('org.apache.commons.codec.digest.DigestUtils')]
| MethodCall[starts-with(@MethodName,'sha') or starts-with(@MethodName,'md')]/TypeExpression[pmd-java:typeIs('org.apache.commons.codec.digest.DigestUtils')]
Comment on lines +906 to +909
LocalVariableDeclaration[ClassType[pmd-java:typeIs('java.security.MessageDigest')]]/VariableDeclarator/MethodCall[@MethodName='getInstance']
| MethodCall[starts-with(@MethodName,'digest')]/ConstructorCall[pmd-java:typeIs('org.apache.commons.codec.digest.DigestUtils')]
| LocalVariableDeclaration[ClassType[pmd-java:typeIs('java.security.MessageDigest')]]//MethodCall/TypeExpression[pmd-java:typeIs('org.apache.commons.codec.digest.DigestUtils')]
| MethodCall[starts-with(@MethodName,'sha') or starts-with(@MethodName,'md')]/TypeExpression[pmd-java:typeIs('org.apache.commons.codec.digest.DigestUtils')]
Comment on lines +906 to +909
LocalVariableDeclaration[ClassType[pmd-java:typeIs('java.security.MessageDigest')]]/VariableDeclarator/MethodCall[@MethodName='getInstance']
| MethodCall[starts-with(@MethodName,'digest')]/ConstructorCall[pmd-java:typeIs('org.apache.commons.codec.digest.DigestUtils')]
| LocalVariableDeclaration[ClassType[pmd-java:typeIs('java.security.MessageDigest')]]//MethodCall/TypeExpression[pmd-java:typeIs('org.apache.commons.codec.digest.DigestUtils')]
| MethodCall[starts-with(@MethodName,'sha') or starts-with(@MethodName,'md')]/TypeExpression[pmd-java:typeIs('org.apache.commons.codec.digest.DigestUtils')]
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.

3 participants