Re: [pmd-devel] Custom Apex rule failures
A source code analyzer
Brought to you by:
adangel,
juansotuyo
|
From: Caleb K. <cal...@en...> - 2017-07-17 18:07:15
|
Okay, sorry about that. The hard-coded ID rule is working from command line
as well. I am unsure as to what is broken.
On Mon, Jul 17, 2017 at 1:01 PM, Caleb Knox <cal...@en...>
wrote:
> Did some more testing... If I run from the command line, the ApexUseSizeOverIsEmptyRule
> works just fine. Hard-coded ID rule is probably broken.
>
> On Mon, Jul 17, 2017 at 12:18 PM, Caleb Knox <cal...@en...>
> wrote:
>
>> Hello,
>>
>> For this rule, I sometimes get an NPE, depending on what Apex class I'm
>> analyzing.
>>
>> package net.sourceforge.pmd.lang.apex.custom;
>>
>> import java.util.regex.Matcher;
>> import java.util.regex.Pattern;
>>
>> import net.sourceforge.pmd.lang.apex.ast.ASTLiteralExpression;
>> import net.sourceforge.pmd.lang.apex.rule.AbstractApexRule;
>>
>> // this rule checks for hard coded ids
>>
>> public class ApexHardCodedIdRule extends AbstractApexRule {
>>
>> public Object visit(ASTLiteralExpression node, Object data){
>>
>> // look for string starting w/ 3 digits
>> Pattern pattern = Pattern.compile("^[0-9]{3}");
>> Matcher matcher = pattern.matcher(node.getNode().getLiteral().toString());
>> // NPE HAPPENS HERE
>>
>> // look for 15 or 18 digit string or
>> // look for 3 digit string at start
>> // of any string
>> if(node.getNode().getLiteral().toString().length() == 15 ||
>> node.getNode().getLiteral().toString().length() == 18 ||
>> matcher.find()) {
>>
>> addViolation(data, node);
>> }
>>
>> return data;
>>
>> }
>> }
>>
>> And for this rule, I get an error while trying to cast types of nodes.
>>
>> package net.sourceforge.pmd.lang.apex.custom;
>>
>> import net.sourceforge.pmd.lang.apex.ast.ASTBooleanExpression;
>> import net.sourceforge.pmd.lang.apex.ast.ASTLiteralExpression;
>> import net.sourceforge.pmd.lang.apex.ast.ASTMethodCallExpression;
>> import net.sourceforge.pmd.lang.apex.rule.AbstractApexRule;
>>
>> // this rule checks for the use of size() over isEmpty()
>>
>> public class ApexUseSizeOverIsEmptyRule extends AbstractApexRule {
>>
>> public Object visit(ASTBooleanExpression node, Object data){
>>
>> ASTMethodCallExpression method = (ASTMethodCallExpression)
>> node.jjtGetChild(0);
>> ASTLiteralExpression exp = (ASTLiteralExpression)
>> node.jjtGetChild(1);
>>
>> // if comparison to 0 used in conjunction with size() method
>> // add violation
>> if(node.getNode().getBooleanExpressionType().toString().equalsIgnoreCase("comparison")
>>
>> && method.getMethodName().toString().equalsIgnoreCase("size")
>>
>> && exp.getNode().getLiteral().toString().equalsIgnoreCase("0"))
>> {
>> addViolation(data, node);
>> }
>>
>> return data;
>>
>> }
>>
>> }
>>
>>
>> However, both of these classes run just fine when I use them in my custom
>> test class, with their respective custom test data. They both find the
>> correct errors in that case.
>>
>> I don't know why they work in a test environment, but get fatal errors
>> when I run them on other Apex classes.
>>
>> Also, when modifying the NcssMethodCountRule to 50 lines of code, do I
>> change the
>>
>> setProperty(MINIMUM_DESCRIPTOR, 40d);
>>
>> to 50d?
>>
>> Any help is welcome.
>>
>> --
>> Caleb Knox
>>
>> Endeveran
>>
>
>
>
> --
> Caleb Knox
>
> Endeveran
>
--
Caleb Knox
Endeveran
|