@Override public void visit(VariableExpr expr) { int index = expr.getIndex(); resultExpr = expr; if (readFrequencies[index] != 1 || preservedVars[index]) { return; } if (resultSequence.isEmpty()) { return; } Statement last = resultSequence.get(resultSequence.size() - 1); if (!(last instanceof AssignmentStatement)) { return; } AssignmentStatement assignment = (AssignmentStatement) last; if (assignment.isAsync()) { return; } if (!(assignment.getLeftValue() instanceof VariableExpr)) { return; } VariableExpr var = (VariableExpr) assignment.getLeftValue(); if (var.getLocation() != null && assignment.getLocation() != null && !assignment.getLocation().equals(var.getLocation())) { return; } if (var.getIndex() == index) { resultSequence.remove(resultSequence.size() - 1); assignment.getRightValue().setLocation(assignment.getLocation()); assignment.getRightValue().acceptVisitor(this); } }
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; }
@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; } }