aspect MergeVarDecl { // merge variable declaration with the assignment following it public VariableDeclaration VariableDeclaration.merge() { if(hasInit()) return this; if(!isInBlock()) throw new RefactoringException("cannot merge declarations outside blocks"); Block block = (Block)getParent().getParent(); int idx = block.getIndexOfStmt(this); assert(idx != -1); Stmt next = block.getStmt(idx+1); if(next instanceof ExprStmt) { Expr e = ((ExprStmt)next).getExpr(); if(e instanceof AssignSimpleExpr) { Expr dest = ((AssignSimpleExpr)e).getDest(); Expr src = ((AssignSimpleExpr)e).getSource(); if(dest instanceof VarAccess && ((VarAccess)dest).decl() == this) { this.setInit(src); block.removeStmt(idx+1); programRoot().flushCaches(); return this; } else { throw new RefactoringException("following assignment is not to this declaration"); } } else { throw new RefactoringException("following statement is not a simple assignment"); } } else { throw new RefactoringException("following statement is not an assignment"); } } }