java - sonar custom rule: disable 'system.out.println' -
my goal is: raise issue when java file contain system.out.println()
rule code snipped:
public void visitnode(tree tree) { //ohter code methodmatcher matcher = methodmatcher.create() .typedefinition(typecriteria.anytype()) .name("println") .withoutparameter(); methodinvocationtree methodinvocationtree = istreemethodinvocation(expressiontree.expression(), matcher); if(methodinvocationtree != null){ reportissue(tree, "msg"); } } private static methodinvocationtree istreemethodinvocation(expressiontree tree, methodmatcher matcher) { tree expression = expressionutils.skipparentheses(tree); if (expression.is(tree.kind.method_invocation)) { methodinvocationtree methodinvocation = (methodinvocationtree) expression; if (matcher.matches(methodinvocation)) { return methodinvocation; } } return null; }
test case code:
public class saasdisablesystemoutprintcheckcase { public void test() { test2222(); system.out.println("aaa"); } public void test2222() {} // noncompliant }
it not work? change code this:
methodmatcher matcher = methodmatcher.create() //.typedefinition(typecriteria.is("java.io.printstream")) .name("test2222") .withoutparameter();
that's correct!
how use methodmatcher
detect special expression?
thanks
Comments
Post a Comment