pmd

Iterating over the content of a text file line by line - is there a best practice? (vs. PMD's AssignmentInOperand)

可紊 提交于 2019-11-27 18:59:08
We have a Java Application that has a few modules that know to read text files. They do it quite simply with a code like this: BufferedReader br = new BufferedReader(new FileReader(file)); String line = null; while ((line = br.readLine()) != null) { ... // do stuff to file here } I ran PMD on my project and got the ' AssignmentInOperand ' violation on the while (...) line. Is there a simpler way of doing this loop other than the obvious: String line = br.readLine(); while (line != null) { ... // do stuff to file here line = br.readLine(); } Is this considered a better practice? (although we

Avoid printStackTrace(); use a logger call instead

自古美人都是妖i 提交于 2019-11-27 17:17:09
In my application, I am running my code through PMD.It shows me this message: Avoid printStackTrace(); use a logger call instead. What does that mean? It means you should use logging framework like logback or log4j and instead of printing exceptions directly: e.printStackTrace(); you should log them using this frameworks' API: log.error("Ops!", e); Logging frameworks give you a lot of flexibility, e.g. you can choose whether you want to log to console or file - or maybe skip some messages if you find them no longer relevant in some environment. If you call printStackTrace() on an exception the

RegExp matching string not starting with my

只谈情不闲聊 提交于 2019-11-27 10:36:07
For PMD I'd like to have a rule which warns me of those ugly variables which start with my. This means I have to accept all variables which do NOT start with my. So, I need a RegEx (re) which behaves as follows: re.match('myVar') == false re.match('manager') == true re.match('thisIsMyVar') == true re.match('myOtherVar') == false re.match('stuff') == true I've tried different ones (will list them here later, sorry, no access to them right now) but haven't got it working yet. You could either use a lookahead assertion like others have suggested. Or, if you just want to use basic regular

What is the reason for these PMD rules?

ε祈祈猫儿з 提交于 2019-11-27 07:05:14
DataflowAnomalyAnalysis: Found 'DD'-anomaly for variable 'variable' (lines 'n1'-'n2'). DataflowAnomalyAnalysis: Found 'DU'-anomaly for variable 'variable' (lines 'n1'-'n2'). DD and DU sound familiar...I want to say in things like testing and analysis relating to weakest pre and post conditions, but I don't remember the specifics. NullAssignment: Assigning an Object to null is a code smell. Consider refactoring. Wouldn't setting an object to null assist in garbage collection, if the object is a local object (not used outside of the method)? Or is that a myth? MethodArgumentCouldBeFinal:

PMD violationSuppressXPath for all REST @GET methods?

只谈情不闲聊 提交于 2019-11-27 06:21:27
问题 I am trying to narrow down the PMD rules, how can I exclude all REST methods which are annotated with @GET from PMD checks? 回答1: We are using for example this rules to suppress checks on REST methods for final declaration. Maybe you need similar? <rule ref="rulesets/java/optimizations.xml/MethodArgumentCouldBeFinal"> <properties> <!-- Ignore Rest resources --> <property name="violationSuppressXPath" value=" //ClassOrInterfaceBodyDeclaration/Annotation/MarkerAnnotation//Name[@Image='GET'] | /

Logger vs. System.out.println

社会主义新天地 提交于 2019-11-27 00:41:50
问题 I'm using the PMD plugin for eclipse and it gives me an error when using System.out.println() with the explanation: System.(out|err).print is used, consider using a logger. My question is - What is a Logger? How is it used to print to the screen? Why is it better? 回答1: See this short introduction to log4j. The issue is in using System.out to print debugging or diagnostic information. It is a bad practice because you cannot easily change log levels, turn it off, customize it, etc. However if

Iterating over the content of a text file line by line - is there a best practice? (vs. PMD's AssignmentInOperand)

大兔子大兔子 提交于 2019-11-26 22:44:51
问题 We have a Java Application that has a few modules that know to read text files. They do it quite simply with a code like this: BufferedReader br = new BufferedReader(new FileReader(file)); String line = null; while ((line = br.readLine()) != null) { ... // do stuff to file here } I ran PMD on my project and got the ' AssignmentInOperand ' violation on the while (...) line. Is there a simpler way of doing this loop other than the obvious: String line = br.readLine(); while (line != null) { ...

RegExp matching string not starting with my

半世苍凉 提交于 2019-11-26 17:56:55
问题 For PMD I'd like to have a rule which warns me of those ugly variables which start with my. This means I have to accept all variables which do NOT start with my. So, I need a RegEx (re) which behaves as follows: re.match('myVar') == false re.match('manager') == true re.match('thisIsMyVar') == true re.match('myOtherVar') == false re.match('stuff') == true I've tried different ones (will list them here later, sorry, no access to them right now) but haven't got it working yet. 回答1: You could

What is the reason for these PMD rules?

邮差的信 提交于 2019-11-26 13:02:23
问题 DataflowAnomalyAnalysis: Found \'DD\'-anomaly for variable \'variable\' (lines \'n1\'-\'n2\'). DataflowAnomalyAnalysis: Found \'DU\'-anomaly for variable \'variable\' (lines \'n1\'-\'n2\'). DD and DU sound familiar...I want to say in things like testing and analysis relating to weakest pre and post conditions, but I don\'t remember the specifics. NullAssignment: Assigning an Object to null is a code smell. Consider refactoring. Wouldn\'t setting an object to null assist in garbage collection,