Menu

Suggestion to handle String concatenation violation.

2020-12-22
2021-01-08
  • amith kumar

    amith kumar - 2020-12-22

    We have written a custom pmd rule where we want to throw violations if a string concatenation occurs with + operator.

    But the violations are being thrown even for arithmetic additions containing + in the statement.

    Please find the attachment for the snippet of violations flagged for an arithmetic operation.

    Below is our code:

    public class InefficientStringOperationRule extends AbstractApexRule {

    @Override
    public Object visit(ASTExpressionStatement node, Object data) {
    //to check string concatination
    try {
    String str = node.getNode().toString();
    if( str.contains("+") && !str.contains("System.debug") ) {
    if(!str.contains("system.debug") )
    addViolation(data, node);
    }
    }catch(java.util.NoSuchElementException e) {
    //Re throwing it does not make sense, nothing to do. Even puttting in logs adds no value.
    }

        return data;
    

    }
    }

     
  • Andreas Dangel

    Andreas Dangel - 2021-01-08

    Thanks for the question. Would you mind to create this question in our new discussions place at github? https://github.com/pmd/pmd/discussions

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.