Source:
public boolean visit(VariableDeclarationFragment node) { Expression initializer = node.getInitializer(); if (initializer != null) { if (initializer.getNodeType() == ASTNode.CAST_EXPRESSION) { initializer = ((CastExpression) initializer).getExpression(); } if (initializer.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) { ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) initializer; InitializerVisitor initializerVisitor = new InitializerVisitor(); initializer.accept(initializerVisitor); if (initializerVisitor.initializerCanBeExtracted) { Statement declarationStatement = (Statement) getFirstAncestorOrNull(node, VariableDeclarationStatement.class); if (declarationStatement != null) { final ASTBuilder b = this.ctx.getASTBuilder(); final Refactorings r = this.ctx.getRefactorings(); if (isMethodBindingSubclassOf(node.getName().resolveTypeBinding(), Arrays.asList("AbstractCollection", "Collection", "List", "AbstractMap", "Map"))) { if (classInstanceCreation.arguments().size() == 0) { r.insertBefore(b.move(declarationStatement), onDrawDeclaration); ASTNode clearNode = b.getAST() .newExpressionStatement(b.invoke(node.getName().getIdentifier(), "clear")); List bodyStatements = onDrawDeclaration.getBody().statements(); Statement lastStatement = (Statement) bodyStatements.get(bodyStatements.size() - 1); if (ASTNode.RETURN_STATEMENT == lastStatement.getNodeType()) { r.insertBefore(clearNode, lastStatement); } else { r.insertAfter(clearNode, lastStatement); } } } else { r.insertBefore(b.move(declarationStatement), onDrawDeclaration); } } } } } return VISIT_SUBTREE; }

Target:
public boolean visit(VariableDeclarationFragment node) { Expression initializer = node.getInitializer(); if (initializer != null) { if (initializer.getNodeType() == ASTNode.CAST_EXPRESSION) { initializer = ((CastExpression) initializer).getExpression(); } if (initializer.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) { ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) initializer; InitializerVisitor initializerVisitor = new InitializerVisitor(); classInstanceCreation.accept(initializerVisitor); if (initializerVisitor.initializerCanBeExtracted) { Statement declarationStatement = getAncestor(node, VariableDeclarationStatement.class); if (declarationStatement != null) { final ASTBuilder b = this.ctx.getASTBuilder(); final Refactorings r = this.ctx.getRefactorings(); if (isTypeBindingSubclassOf(node.getName().resolveTypeBinding(), Arrays.asList("AbstractCollection", "Collection", "List", "AbstractMap", "Map"))) { if (classInstanceCreation.arguments().size() == 0) { r.insertBefore(b.move(declarationStatement), onDrawDeclaration); ASTNode clearNode = b.toStmt(b.invoke(node.getName().getIdentifier(), "clear")); List<Statement> bodyStatements = statements(onDrawDeclaration.getBody()); Statement lastStatement = bodyStatements.get(bodyStatements.size() - 1); if (ASTNode.RETURN_STATEMENT == lastStatement.getNodeType()) { r.insertBefore(clearNode, lastStatement); } else { r.insertAfter(clearNode, lastStatement); } return DO_NOT_VISIT_SUBTREE; } } else { r.insertBefore(b.move(declarationStatement), onDrawDeclaration); return DO_NOT_VISIT_SUBTREE; } } } } } return VISIT_SUBTREE; }