Search in sources :

Example 1 with ListRewrite

use of org.eclipse.jdt.core.dom.rewrite.ListRewrite in project che by eclipse.

the class ModifierRewrite method copyAllModifiers.

public void copyAllModifiers(ASTNode otherDecl, TextEditGroup editGroup, boolean copyIndividually) {
    ListRewrite modifierList = evaluateListRewrite(fModifierRewrite.getASTRewrite(), otherDecl);
    List<IExtendedModifier> originalList = modifierList.getOriginalList();
    if (originalList.isEmpty()) {
        return;
    }
    if (copyIndividually) {
        for (Iterator<IExtendedModifier> iterator = originalList.iterator(); iterator.hasNext(); ) {
            ASTNode modifier = (ASTNode) iterator.next();
            ASTNode copy = fModifierRewrite.getASTRewrite().createCopyTarget(modifier);
            if (copy != null) {
                //paranoia check (only left here because we're in RC1)
                fModifierRewrite.insertLast(copy, editGroup);
            }
        }
    } else {
        ASTNode copy = modifierList.createCopyTarget((ASTNode) originalList.get(0), (ASTNode) originalList.get(originalList.size() - 1));
        if (copy != null) {
            //paranoia check (only left here because we're in RC1)
            fModifierRewrite.insertLast(copy, editGroup);
        }
    }
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 2 with ListRewrite

use of org.eclipse.jdt.core.dom.rewrite.ListRewrite in project che by eclipse.

the class ModifierRewrite method copyAllAnnotations.

public void copyAllAnnotations(ASTNode otherDecl, TextEditGroup editGroup) {
    ListRewrite modifierList = evaluateListRewrite(fModifierRewrite.getASTRewrite(), otherDecl);
    List<IExtendedModifier> originalList = modifierList.getOriginalList();
    for (Iterator<IExtendedModifier> iterator = originalList.iterator(); iterator.hasNext(); ) {
        IExtendedModifier modifier = iterator.next();
        if (modifier.isAnnotation()) {
            fModifierRewrite.insertLast(fModifierRewrite.getASTRewrite().createCopyTarget((Annotation) modifier), editGroup);
        }
    }
}
Also used : ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Annotation(org.eclipse.jdt.core.dom.Annotation) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 3 with ListRewrite

use of org.eclipse.jdt.core.dom.rewrite.ListRewrite in project che by eclipse.

the class NewVariableCorrectionProposal method doAddEnumConst.

private ASTRewrite doAddEnumConst(CompilationUnit astRoot) {
    SimpleName node = fOriginalNode;
    ASTNode newTypeDecl = astRoot.findDeclaringNode(fSenderBinding);
    if (newTypeDecl == null) {
        astRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
        newTypeDecl = astRoot.findDeclaringNode(fSenderBinding.getKey());
    }
    if (newTypeDecl != null) {
        AST ast = newTypeDecl.getAST();
        ASTRewrite rewrite = ASTRewrite.create(ast);
        EnumConstantDeclaration constDecl = ast.newEnumConstantDeclaration();
        constDecl.setName(ast.newSimpleName(node.getIdentifier()));
        ListRewrite listRewriter = rewrite.getListRewrite(newTypeDecl, EnumDeclaration.ENUM_CONSTANTS_PROPERTY);
        listRewriter.insertLast(constDecl, null);
        addLinkedPosition(rewrite.track(constDecl.getName()), false, KEY_NAME);
        return rewrite;
    }
    return null;
}
Also used : EnumConstantDeclaration(org.eclipse.jdt.core.dom.EnumConstantDeclaration) AST(org.eclipse.jdt.core.dom.AST) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite)

Example 4 with ListRewrite

use of org.eclipse.jdt.core.dom.rewrite.ListRewrite in project che by eclipse.

the class ChangeMethodSignatureProposal method modifyExceptions.

private void modifyExceptions(ASTRewrite rewrite, MethodDeclaration methodDecl) {
    AST ast = methodDecl.getAST();
    ImportRewrite imports = getImportRewrite();
    ImportRewriteContext context = new ContextSensitiveImportRewriteContext(methodDecl, imports);
    ListRewrite listRewrite = rewrite.getListRewrite(methodDecl, MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY);
    // old exceptions
    List<Type> exceptions = methodDecl.thrownExceptionTypes();
    // index over the old exceptions
    int k = 0;
    for (int i = 0; i < fExceptionChanges.length; i++) {
        ChangeDescription curr = fExceptionChanges[i];
        if (curr == null) {
            k++;
        } else if (curr instanceof InsertDescription) {
            InsertDescription desc = (InsertDescription) curr;
            String type = imports.addImport(desc.type, context);
            ASTNode newNode = imports.addImport(desc.type, ast, context);
            listRewrite.insertAt(newNode, i, null);
            String key = getExceptionTypeGroupId(i);
            addLinkedPosition(rewrite.track(newNode), false, key);
            Javadoc javadoc = methodDecl.getJavadoc();
            if (javadoc != null && JavadocTagsSubProcessor.findThrowsTag(javadoc, type) == null) {
                TagElement newTagElement = ast.newTagElement();
                newTagElement.setTagName(TagElement.TAG_THROWS);
                ASTNode newRef = ASTNodeFactory.newName(ast, type);
                newTagElement.fragments().add(newRef);
                //$NON-NLS-1$
                insertTabStop(rewrite, newTagElement.fragments(), "throws_tagcomment" + i);
                insertThrowsTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), exceptions, k, newTagElement);
                addLinkedPosition(rewrite.track(newRef), false, key);
            }
        } else if (curr instanceof RemoveDescription) {
            Type node = exceptions.get(k);
            listRewrite.remove(node, null);
            k++;
            TagElement tagNode = findThrowsTag(methodDecl, node);
            if (tagNode != null) {
                rewrite.remove(tagNode, null);
            }
        } else if (curr instanceof EditDescription) {
            EditDescription desc = (EditDescription) curr;
            Type oldNode = exceptions.get(k);
            String type = imports.addImport(desc.type, context);
            ASTNode newNode = imports.addImport(desc.type, ast, context);
            listRewrite.replace(oldNode, newNode, null);
            String key = getExceptionTypeGroupId(i);
            addLinkedPosition(rewrite.track(newNode), false, key);
            k++;
            TagElement tagNode = findThrowsTag(methodDecl, oldNode);
            if (tagNode != null) {
                ASTNode newRef = ASTNodeFactory.newType(ast, type);
                rewrite.replace((ASTNode) tagNode.fragments().get(0), newRef, null);
                addLinkedPosition(rewrite.track(newRef), false, key);
            }
        } else if (curr instanceof SwapDescription) {
            Type decl1 = exceptions.get(k);
            Type decl2 = exceptions.get(((SwapDescription) curr).index);
            rewrite.replace(decl1, rewrite.createCopyTarget(decl2), null);
            rewrite.replace(decl2, rewrite.createCopyTarget(decl1), null);
            k++;
            TagElement tagNode1 = findThrowsTag(methodDecl, decl1);
            TagElement tagNode2 = findThrowsTag(methodDecl, decl2);
            if (tagNode1 != null && tagNode2 != null) {
                rewrite.replace(tagNode1, rewrite.createCopyTarget(tagNode2), null);
                rewrite.replace(tagNode2, rewrite.createCopyTarget(tagNode1), null);
            }
        }
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Type(org.eclipse.jdt.core.dom.Type) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ASTNode(org.eclipse.jdt.core.dom.ASTNode) TagElement(org.eclipse.jdt.core.dom.TagElement)

Example 5 with ListRewrite

use of org.eclipse.jdt.core.dom.rewrite.ListRewrite in project che by eclipse.

the class AddArgumentCorrectionProposal method getRewrite.

/*(non-Javadoc)
	 * @see org.eclipse.jdt.internal.ui.text.correction.ASTRewriteCorrectionProposal#getRewrite()
	 */
@Override
protected ASTRewrite getRewrite() {
    AST ast = fCallerNode.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    ChildListPropertyDescriptor property = getProperty();
    for (int i = 0; i < fInsertIndexes.length; i++) {
        int idx = fInsertIndexes[i];
        //$NON-NLS-1$
        String key = "newarg_" + i;
        Expression newArg = evaluateArgumentExpressions(ast, fParamTypes[idx], key);
        ListRewrite listRewriter = rewrite.getListRewrite(fCallerNode, property);
        listRewriter.insertAt(newArg, idx, null);
        addLinkedPosition(rewrite.track(newArg), i == 0, key);
    }
    return rewrite;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Expression(org.eclipse.jdt.core.dom.Expression) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) ChildListPropertyDescriptor(org.eclipse.jdt.core.dom.ChildListPropertyDescriptor)

Aggregations

ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)75 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)43 ASTNode (org.eclipse.jdt.core.dom.ASTNode)36 AST (org.eclipse.jdt.core.dom.AST)29 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)19 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)19 Type (org.eclipse.jdt.core.dom.Type)18 Block (org.eclipse.jdt.core.dom.Block)17 Image (org.eclipse.swt.graphics.Image)17 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)15 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)15 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)15 ArrayList (java.util.ArrayList)13 IType (org.eclipse.jdt.core.IType)13 Statement (org.eclipse.jdt.core.dom.Statement)13 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)13 Expression (org.eclipse.jdt.core.dom.Expression)12 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)12 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)12 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)12