Re: [pmd-devel] Custom Apex rule failures
A source code analyzer
Brought to you by:
adangel,
juansotuyo
|
From: Juan M. S. D. <jua...@gm...> - 2017-07-17 18:20:46
|
Don't duplicate code. Just run PMD using a custom ruleset, and on that you can reference the NcssMethodCountRule with a custom value for properties. You can check the docs: https://pmd.github.io/pmd-5.8.1/customizing/howtomakearuleset.html On Mon, Jul 17, 2017 at 3:19 PM, Caleb Knox <cal...@en...> wrote: > Sorry, I was unclear about the NcssMethodCountRule. I copy/pasted the code > into my custom rule. > > Again, what do I change? > > On Mon, Jul 17, 2017 at 1:16 PM, Caleb Knox <cal...@en...> > wrote: > >> As for your casts... why are you assuming ALL BooleanExpressions are >>> formed with a method call and a literal? I can think of plenty of boolean >>> expressions that are not built that way (ie: myVar == 1) >> >> >> I am not, as far as I know. I am just looking for such expressions that >> contain a method call and a literal. Specifically, and .size() comparison >> to 0. >> >> Finally, do not edit the property value on the source. It's a property so >>> people can customize the value on their rulesets. Just configure it as you >>> wish on your custom ruleset. >>> >> >> I am not, but in my custom rule, what property do I change? That is what >> I had asked. >> >> On Mon, Jul 17, 2017 at 1:12 PM, Juan Martín Sotuyo Dodero < >> jua...@gm...> wrote: >> >>> As for NPEs, you can either debug when analyzing your real-code, or make >>> new code samples based on the contents of the file on which the NPE is >>> found. You are most probably just missing cases on your test scenarios. >>> >>> As for your casts... why are you assuming ALL BooleanExpressions are >>> formed with a method call and a literal? I can think of plenty of boolean >>> expressions that are not built that way (ie: myVar == 1) >>> >>> Finally, do not edit the property value on the source. It's a property >>> so people can customize the value on their rulesets. Just configure it as >>> you wish on your custom ruleset. >>> >>> Regards >>> >>> On Mon, Jul 17, 2017 at 3:07 PM, Caleb Knox <cal...@en...> >>> wrote: >>> >>>> 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 >>>> >>>> ------------------------------------------------------------ >>>> ------------------ >>>> Check out the vibrant tech community on one of the world's most >>>> engaging tech sites, Slashdot.org! http://sdm.link/slashdot >>>> _______________________________________________ >>>> Pmd-devel mailing list >>>> Pmd...@li... >>>> https://lists.sourceforge.net/lists/listinfo/pmd-devel >>>> >>>> >>> >> >> >> -- >> Caleb Knox >> >> Endeveran >> > > > > -- > Caleb Knox > > Endeveran > |