Questions tagged [pmd]

PMD scans source code and looks for potential problems like possible bugs, dead code, suboptimal code, overcomplicated expressions, and duplicate code. PMD supports Java, JavaScript, XML, and many more.

PMD scans source code and looks for potential problems like possible bugs, dead code, suboptimal code, overcomplicated expressions, and .

857 questions
25
votes
3 answers

How to suppress violations in PMD?

When I run a PMD analysis I receive violation: Each class should declare at least one constructor This violation is on a Spring controller. This controller is instantiated by Spring, so I shouldn't need to invoke this class. What is recommended…
blue-sky
  • 51,962
  • 152
  • 427
  • 752
24
votes
2 answers

PMD: Avoid instantiating new objects inside loops

I've got an issue with the PMD rule Avoid instantiating new objects inside loops. Here is some example code: import java.awt.Dimension; public class PMDDemo { public static void main(final String[] args) { final Dimension[] arr = new…
brimborium
  • 9,362
  • 9
  • 48
  • 76
23
votes
4 answers

PMD/CPD: Ignore bits of code using comments

Is there a way to tell PMD to ignore checking parts of code for duplication? For example, can I do something like this: // CPD-Ignore-On ... // CPD-Ignore-Off Currently I have PMD set up like this using Maven, but don't see any arguments that would…
digiarnie
  • 22,305
  • 31
  • 78
  • 126
23
votes
2 answers

Dataflow anomaly analysis warnings from PMD

I am using Eclipse with the PMD Plug-in (4.0.0.v20130510-1000) and get a lot of those violations: Found 'DD'-anomaly for variable 'freq' (lines '187'-'189'). Found 'DU'-anomaly for variable 'freq' (lines '189'-'333'). In this SO answer, it says that…
brimborium
  • 9,362
  • 9
  • 48
  • 76
23
votes
9 answers

if(x!=y) vs if(x==y)

I have run the PMD plugin in Eclipse against my code and I'm getting a high priority warning for code similar to the one shown below: if(singleRequest !=null){ // do my work }else{ // do my other work } PMD says `Avoid if (x != y) ..;…
someone
  • 6,577
  • 7
  • 37
  • 60
21
votes
3 answers

Java for each loop being flagged as UR anomaly by PMD

I would like to confirm if this is a bug on PMD? How do I file a ticket if it is. public static void main(final String[] args) { for (final String string : args) { string.getBytes(); //UR Anomaly } for (int i…
John Doe
  • 1,364
  • 1
  • 12
  • 19
19
votes
5 answers

How to fix the following PMD violations

I am using PMD to analyze code and it produces a few high priority warnings which I do not know how to fix. 1) Avoid if(x!=y)..; else...; But what should I do if I need this logic? That is, I do need to check if x!=y? How can I refactor it? 2) Use…
sarahTheButterFly
  • 1,894
  • 3
  • 22
  • 36
19
votes
2 answers

What is NPath Complexity and how to avoid it?

In this line: public Map getAll(BusinessTargetPK pkBusinessTargetId) throws Exception I am getting this error: NPath Complexity is 32,768 (max allowed is 200) And in this line: public Map getAll( Long RLE_ROLE_ID ) throws Exception { I get…
Wolverine789
  • 407
  • 2
  • 6
  • 10
19
votes
5 answers

Is there a Findbugs and / or PMD equivalent for C/C++?

I was recently asked about alternatives to Coverity Prevent for a code base that includes both C/C++ and Java. Obviously, on the Java side, the free tools available include Findbugs (compiled code analysis) and PMD (static code analysis). They are…
Bob Cross
  • 22,116
  • 12
  • 58
  • 95
16
votes
1 answer

Any current workarounds to use Sonar for Java 7 code?

I try to use Sonar on a Java 7 project (which relies on new syntactic features) and the PMD part and the Checkstyle part fail to parse those files. The Findbugs part fails to read Java 7 class files. This causes Sonar to consider only 10% of my…
java.is.for.desktop
  • 10,748
  • 12
  • 69
  • 103
15
votes
1 answer

Running all PMD rulesets from command line

I want to know if there is a way to run all PMD rulesets from command line. I've used PMD integrated with Eclipse IDE and Maven. But now I need to run it from CLI. I've checked this page http://pmd.sourceforge.net/pmd-5.1.0/running.html and it says…
Angelo
  • 905
  • 1
  • 17
  • 35
14
votes
1 answer

PMD - NPath complexity very high with ternary operator (?

I'm using PMD to generate some code quality report on a project. I don't understand a result for the NPath complexity inspection. I have created a dull class that is show-casing the result (this is not the real class, but it is using the same…
Guillaume
  • 5,488
  • 11
  • 47
  • 83
14
votes
9 answers

Default constructor is good or evil? Checkstyle and PMD are opposite here

Checkstyle says: Class should define a constructor. PMD says: Avoid unnecessary constructors - the compiler will generate these for you. Who is right? Or let's put it this way - what are the pros and cons of having an empty default ctor in a…
yegor256
  • 102,010
  • 123
  • 446
  • 597
14
votes
4 answers

Using character instead of String for single-character values in StringBuffer append

I was going through the PMD rule AppendCharacterWithChar. It says Avoid concatenating characters as strings in StringBuffer.append. StringBuffer sb = new StringBuffer(); // Avoid this sb.append("a"); // use instead something like this …
Zeeshan
  • 11,851
  • 21
  • 73
  • 98
14
votes
3 answers

String literal expressions should be on the left side of an equals comparison

!mapData.get("PARTY_ID").equals("") // <-- gives SonarQube error In the above piece of code, I am getting "String literal expressions should be on the left side of an equals comparison" this error in Sonar. So how we can avoid it. I tried…
Wolverine789
  • 407
  • 2
  • 6
  • 10
1
2
3
57 58