@Override public void visit(AssignmentStatement statement) { if (statement.getLeftValue() == null) { statement.getRightValue().acceptVisitor(this); if (resultExpr instanceof InvocationExpr && tryApplyConstructor((InvocationExpr) resultExpr)) { resultStmt = new SequentialStatement(); } else { statement.setRightValue(resultExpr); resultStmt = statement; } } else { statement.getRightValue().acceptVisitor(this); Expr right = resultExpr; Expr left = statement.getLeftValue(); if (!(statement.getLeftValue() instanceof VariableExpr)) { statement.getLeftValue().acceptVisitor(this); left = resultExpr; } statement.setLeftValue(left); statement.setRightValue(right); resultStmt = statement; } }
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; }