Hello!
I am trying to write a rule that will make the programmers avoid using System.out.print
This is what I have so far:
<rule name="DontUseSyste.out.print" message="Do not use System.out.println, nobody likes those, specially Josh. Use the logger instead" class="net.sourceforge.pmd.rules.XPathRule" include="true">
<description> Do not use System.out.println </description>
<example><![CDATA[
Sorry about the delay in replying - I was on vacation for a few days and we had an off-site yesterday, so I'm going through my email catching up with things now.
Glad you sorted it out, FWIW, I've been using a Java rule to catch these:
=============
public class SystemOutRule extends AbstractRule implements Rule {
Hello!
I am trying to write a rule that will make the programmers avoid using System.out.print
This is what I have so far:
<rule name="DontUseSyste.out.print" message="Do not use System.out.println, nobody likes those, specially Josh. Use the logger instead" class="net.sourceforge.pmd.rules.XPathRule" include="true">
<description> Do not use System.out.println </description>
<example><![CDATA[
System.out.println // don't do this!
]]></example>
<priority>1</priority>
<properties>
<property name="xpath">
<value><![CDATA[
//AllocationExpression[Name/@Image='System.out.println'][not(ArrayDimsAndInits)]>
]]></value>
</property>
</properties>
</rule>
I am using eclipse 3.0. And theres is something wrong
with that code.
Anybody?
Thanks.
Nevermind, I got it.
Hi Jorge -
Sorry about the delay in replying - I was on vacation for a few days and we had an off-site yesterday, so I'm going through my email catching up with things now.
Glad you sorted it out, FWIW, I've been using a Java rule to catch these:
=============
public class SystemOutRule extends AbstractRule implements Rule {
public Object visit(ASTName node, Object data){
RuleContext ctx = (RuleContext)data;
if (node.getImage() != null && (node.getImage().startsWith("System.out") || node.getImage().startsWith("System.err") || node.getImage().startsWith("System.in"))) {
ctx.getReport().addRuleViolation(createRuleViolation(ctx, node.getBeginLine()));
}
return super.visit(node,data);
}
}
=============
but if you got an XPath rule to work that's definitely an easier way to go...
Yours,
Tom
Hi Jorge
I am also trying to write the same rule with XPath.
Do you see anything wrong with the following rule?
//Statement/StatementExpression/PrimaryExpression/PrimaryPrefix/Name[@Image='System.out.println']
Could it be possible for you to post your rule.
Thanks !!!
Could someone post the correct version of the XPath rule that throws a warning when it comes across a System.out.println statement?
Thanks in advance!!!!
Pravin. D
Hi Pravin -
Yup, here it is:
//Statement
child:://Name[
starts-with(@Image, 'System.out.print')
or
starts-with(@Image, 'System.err.print')
Also, you can download a new pmd-2.3.jar file here:
http://infoether.com/~tom/pmd-2.3.jar
that contains this fix.
Yours,
Tom