@Override public void visit(InvocationExpr expr) { Expr[] args = new Expr[expr.getArguments().size()]; for (int i = expr.getArguments().size() - 1; i >= 0; --i) { expr.getArguments().get(i).acceptVisitor(this); args[i] = resultExpr; } for (int i = 0; i < args.length; ++i) { expr.getArguments().set(i, args[i]); } resultExpr = expr; }
private boolean tryApplyConstructor(InvocationExpr expr) { if (!expr.getMethod().getName().equals("<init>")) { return false; } if (resultSequence == null || resultSequence.isEmpty()) { return false; } Statement last = resultSequence.get(resultSequence.size() - 1); if (!(last instanceof AssignmentStatement)) { return false; } AssignmentStatement assignment = (AssignmentStatement) last; if (!(assignment.getLeftValue() instanceof VariableExpr)) { return false; } VariableExpr var = (VariableExpr) assignment.getLeftValue(); if (!(expr.getArguments().get(0) instanceof VariableExpr)) { return false; } VariableExpr target = (VariableExpr) expr.getArguments().get(0); if (target.getIndex() != var.getIndex()) { return false; } if (!(assignment.getRightValue() instanceof NewExpr)) { return false; } NewExpr constructed = (NewExpr) assignment.getRightValue(); if (!constructed.getConstructedClass().equals(expr.getMethod().getClassName())) { return false; } Expr[] args = expr.getArguments().toArray(new Expr[0]); args = Arrays.copyOfRange(args, 1, args.length); InvocationExpr constructrExpr = Expr.constructObject(expr.getMethod(), args); constructrExpr.setLocation(expr.getLocation()); assignment.setRightValue(constructrExpr); readFrequencies[var.getIndex()]--; return true; }