Custom Apex rule failures

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Custom Apex rule failures

Caleb Knox
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

------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/pmd-devel
Reply | Threaded
Open this post in threaded view
|

Re: Custom Apex rule failures

Caleb Knox
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 <[hidden email]> 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

------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/pmd-devel
Reply | Threaded
Open this post in threaded view
|

Re: Custom Apex rule failures

Caleb Knox
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 <[hidden email]> 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 <[hidden email]> 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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/pmd-devel
Reply | Threaded
Open this post in threaded view
|

Re: Custom Apex rule failures

Juan Martín Sotuyo Dodero
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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/pmd-devel



------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/pmd-devel
Reply | Threaded
Open this post in threaded view
|

Re: Custom Apex rule failures

Caleb Knox
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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/pmd-devel





--
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/pmd-devel
Reply | Threaded
Open this post in threaded view
|

Re: Custom Apex rule failures

Caleb Knox
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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/pmd-devel





--
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/pmd-devel
Reply | Threaded
Open this post in threaded view
|

Re: Custom Apex rule failures

Juan Martín Sotuyo Dodero
In reply to this post by Caleb Knox
Yes you are..

    public Object visit(ASTBooleanExpression node, Object data){

        ASTMethodCallExpression method = (ASTMethodCallExpression) node.jjtGetChild(0);
        ASTLiteralExpression exp = (ASTLiteralExpression) node.jjtGetChild(1);

The visit() method is called for all nodes of the BooleanExpression... and you are assuming the types of it's children right away....



On Mon, Jul 17, 2017 at 3:16 PM, Caleb Knox <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/pmd-devel





--
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/pmd-devel
Reply | Threaded
Open this post in threaded view
|

Re: Custom Apex rule failures

Juan Martín Sotuyo Dodero
In reply to this post by Caleb Knox
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.


On Mon, Jul 17, 2017 at 3:19 PM, Caleb Knox <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/pmd-devel





--
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/pmd-devel
Reply | Threaded
Open this post in threaded view
|

Re: Custom Apex rule failures

Caleb Knox
Okay, guess I have some rewriting to do...

Anyway, back to the other rule.

I think I'll add some logic to test whether or not those nodes are indeed what I am looking for, instead of casting. Does that make sense?

On Mon, Jul 17, 2017 at 1:20 PM, Juan Martín Sotuyo Dodero <[hidden email]> wrote:
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.


On Mon, Jul 17, 2017 at 3:19 PM, Caleb Knox <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/pmd-devel





--
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/pmd-devel