Affects PMD Version:
7.7.0
Description:
Hello, I encountered an error while running the pmd.bat command-line tool and checking the following example code with InsufficientStringBufferDeclarationRule. The warning message and exception stack information were output in the console.
The following two lines of code caused an exception.
StringBuffer sb1 = new StringBuffer('c'); StringBuilder sb2 = new StringBuilder('c');
My expectation is that no abnormalities will occur. After reviewing the source code of InsufficientStringBufferDeclarationRule, I feel that there is a lack of handling for char type parameters when calculating the capacity parameters of the StringBuffer and the StringBuilder. Please confirm.
Exception Stacktrace:
Exception Context:
[1:Rule applied on node=!debug only! [VariableId:7:23]sb1]
---------------------------------
at net.sourceforge.pmd.util.AssertionUtil.contexted(AssertionUtil.java:236)
at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.applyOnIndex(RuleApplicator.java:79)
at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.apply(RuleApplicator.java:57)
at net.sourceforge.pmd.lang.rule.internal.RuleSets.apply(RuleSets.java:155)
at net.sourceforge.pmd.lang.impl.PmdRunnable.processSource(PmdRunnable.java:140)
at net.sourceforge.pmd.lang.impl.PmdRunnable.run(PmdRunnable.java:80)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:842)
Caused by: java.lang.ClassCastException: class java.lang.Character cannot be cast to class java.lang.Integer (java.lang.Character and java.lang.Integer are in module java.base of loader 'bootstrap')
at net.sourceforge.pmd.lang.java.rule.performance.InsufficientStringBufferDeclarationRule.calculateExpression(InsufficientStringBufferDeclarationRule.java:243)
at net.sourceforge.pmd.lang.java.rule.performance.InsufficientStringBufferDeclarationRule.getConstructorCapacity(InsufficientStringBufferDeclarationRule.java:235)
at net.sourceforge.pmd.lang.java.rule.performance.InsufficientStringBufferDeclarationRule.visit(InsufficientStringBufferDeclarationRule.java:126)
at net.sourceforge.pmd.lang.java.ast.ASTVariableId.acceptVisitor(ASTVariableId.java:57)
at net.sourceforge.pmd.lang.java.ast.AbstractJavaNode.acceptVisitor(AbstractJavaNode.java:38)
at net.sourceforge.pmd.lang.java.rule.AbstractJavaRule.apply(AbstractJavaRule.java:30)
at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.applyOnIndex(RuleApplicator.java:77)
... 9 more
[WARN] Exception occurred on node !debug only! [VariableId:7:23]sb1
Code Sample demonstrating the issue:
public class StringBufferInstantiationWithCharTest { public void example01() { // misleading instantiation, these buffers // are actually sized to 99 characters long StringBuffer sb1 = new StringBuffer('c'); StringBuilder sb2 = new StringBuilder('c'); // in these forms, just single characters are allocated StringBuffer sb3 = new StringBuffer("c"); StringBuilder sb4 = new StringBuilder("c"); } }
Running PMD through: CLI
java version "17.0.13" 2024-10-15 LTS
Java(TM) SE Runtime Environment (build 17.0.13+10-LTS-268)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.13+10-LTS-268, mixed mode, sharing)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4