0

I am playing with JavaParser and I am looking for a way to filter all System.out.println();

I am able to do it with the following methods

public class SystemOutFilter extends ModifierVisitorAdapter{



    @Override
    public Node visit(ExpressionStmt n, Object arg) {
        if(isSystemOut(n)){
            return null;
        }
        return super.visit(n, arg);
    }



private boolean isSystemOut(ExpressionStmt n){
    if(n.getExpression() instanceof  MethodCallExpr){
        MethodCallExpr methodExpr = (MethodCallExpr) n.getExpression();
        if(methodExpr.getScope() instanceof FieldAccessExpr){
            FieldAccessExpr fieldExpr = (FieldAccessExpr) methodExpr.getScope();
            NameExpr nameExpr = (NameExpr) fieldExpr.getScope();

            if(nameExpr.getName().equals("System") && fieldExpr.getField().equals("out")){
                return true;
            };
        }
    }
    return false;
}

private boolean isSystemOut_1(ExpressionStmt n){
    NameExpr clazz = new NameExpr("System");
    FieldAccessExpr field = new FieldAccessExpr(clazz, "out");
    MethodCallExpr method = new MethodCallExpr(field, "println");
    if(n.getExpression() instanceof MethodCallExpr){
        MethodCallExpr inputExpr = (MethodCallExpr) n.getExpression();
        if(inputExpr.getName().equals(method.getName()) && inputExpr.getScope().equals(method.getScope()))
            return true;
    }
    return false;
}

private boolean isSystemOut_2(ExpressionStmt n) {
    NameExpr clazz = new NameExpr("System");
    FieldAccessExpr field = new FieldAccessExpr(clazz, "out");
    MethodCallExpr method = new MethodCallExpr(field, "println");
    if(n.getExpression() instanceof MethodCallExpr){
        MethodCallExpr inputExpr = (MethodCallExpr) n.getExpression();
        method.setArgs(inputExpr.getArgs());
        return inputExpr.equals(method);
    }
    return false;
}

private boolean isSystemOut_3(ExpressionStmt n){
    try{
        Statement expr = JavaParser.parseStatement("System.out.println();");

        if(expr instanceof  ExpressionStmt) {
            ExpressionStmt methodStmt = (ExpressionStmt) expr;
            if (n.getExpression() instanceof MethodCallExpr) {
                MethodCallExpr inputExpr = (MethodCallExpr) n.getExpression();
                MethodCallExpr methodExpr = (MethodCallExpr) methodStmt.getExpression();
                methodExpr.setArgs(inputExpr.getArgs());
                return inputExpr.equals(methodExpr);
            }
        }
    }catch(ParseException pe){
        pe.printStackTrace();
    }

    return false;
}

}

I don't like that solution since I need to create a new MethodExpression everytime to match the arguments. I would like to create one static final MethodExpression and compare input parameters with the constant.

Something like

private static final ExpressionStmt  systemOutExpr =      (ExpressionStmt)JavaParser.parseStatement("System.out.println();");

private boolean isSystemOut(ExpressionStmt n){
    return n.match(systemOutExpr).withArgs(Matchers.any());
}

Is there a way to compare statements in JParser outside of the equals methods?

Federico Tomassetti
  • 2,100
  • 1
  • 19
  • 26

1 Answers1

0

I am not sure if I exactly understand what you are trying to achieve, but I think this would be a way to filter out a System.out.println("anythingHere") , you could use MethodCallExpr. Something like this:

    public void visit(MethodCallExpr n, Object a) {

        boolean isIt = isItSOP(n);

        super.visit(n, a);

    }

    private boolean isItSOP(MethodCallExpr n) {
        Node child = n.getChildrenNodes().get(0);

        String sChild=child.toString();

        if(sChild.equals("System.out")&&n.getName().equals("println")){
            return true;
        }
        return false;
    }`
AmeyaKetkar
  • 117
  • 1
  • 9