Search in sources :

Example 66 with ListRewrite

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

the class LocalCorrectionsSubProcessor method addFallThroughProposals.

public static void addFallThroughProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (selectedNode instanceof SwitchCase && selectedNode.getLocationInParent() == SwitchStatement.STATEMENTS_PROPERTY) {
        AST ast = selectedNode.getAST();
        ASTNode parent = selectedNode.getParent();
        // insert break:
        ASTRewrite rewrite = ASTRewrite.create(ast);
        ListRewrite listRewrite = rewrite.getListRewrite(parent, SwitchStatement.STATEMENTS_PROPERTY);
        listRewrite.insertBefore(ast.newBreakStatement(), selectedNode, null);
        String label = CorrectionMessages.LocalCorrectionsSubProcessor_insert_break_statement;
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INSERT_BREAK_STATEMENT, image);
        proposals.add(proposal);
        // insert //$FALL-THROUGH$:
        rewrite = ASTRewrite.create(ast);
        rewrite.setTargetSourceRangeComputer(new NoCommentSourceRangeComputer());
        listRewrite = rewrite.getListRewrite(parent, SwitchStatement.STATEMENTS_PROPERTY);
        //$NON-NLS-1$
        ASTNode fallThroughComment = rewrite.createStringPlaceholder("//$FALL-THROUGH$", ASTNode.EMPTY_STATEMENT);
        listRewrite.insertBefore(fallThroughComment, selectedNode, null);
        label = CorrectionMessages.LocalCorrectionsSubProcessor_insert_fall_through;
        image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INSERT_FALL_THROUGH, image);
        proposals.add(proposal);
    }
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) NoCommentSourceRangeComputer(org.eclipse.jdt.internal.corext.refactoring.util.NoCommentSourceRangeComputer) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Image(org.eclipse.swt.graphics.Image)

Example 67 with ListRewrite

use of org.eclipse.jdt.core.dom.rewrite.ListRewrite in project flux 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 68 with ListRewrite

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

the class QuickAssistProcessor method getUnrollMultiCatchProposals.

private static boolean getUnrollMultiCatchProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
    if (!JavaModelUtil.is17OrHigher(context.getCompilationUnit().getJavaProject()))
        return false;
    CatchClause catchClause = (CatchClause) ASTResolving.findAncestor(covering, ASTNode.CATCH_CLAUSE);
    if (catchClause == null) {
        return false;
    }
    Statement statement = ASTResolving.findParentStatement(covering);
    if (statement != catchClause.getParent() && statement != catchClause.getBody()) {
        // selection is in a statement inside the body
        return false;
    }
    Type type1 = catchClause.getException().getType();
    Type selectedMultiCatchType = null;
    if (type1.isUnionType() && covering instanceof Name) {
        Name topMostName = ASTNodes.getTopMostName((Name) covering);
        ASTNode parent = topMostName.getParent();
        if (parent instanceof SimpleType || parent instanceof NameQualifiedType) {
            selectedMultiCatchType = (Type) parent;
        }
    }
    if (selectedMultiCatchType != null)
        return false;
    SingleVariableDeclaration singleVariableDeclaration = catchClause.getException();
    Type type = singleVariableDeclaration.getType();
    if (!(type instanceof UnionType))
        return false;
    if (resultingCollections == null)
        return true;
    AST ast = covering.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    TryStatement tryStatement = (TryStatement) catchClause.getParent();
    ListRewrite listRewrite = rewrite.getListRewrite(tryStatement, TryStatement.CATCH_CLAUSES_PROPERTY);
    UnionType unionType = (UnionType) type;
    List<Type> types = unionType.types();
    for (int i = types.size() - 1; i >= 0; i--) {
        Type type2 = types.get(i);
        CatchClause newCatchClause = ast.newCatchClause();
        SingleVariableDeclaration newSingleVariableDeclaration = ast.newSingleVariableDeclaration();
        newSingleVariableDeclaration.setType((Type) rewrite.createCopyTarget(type2));
        newSingleVariableDeclaration.setName((SimpleName) rewrite.createCopyTarget(singleVariableDeclaration.getName()));
        newCatchClause.setException(newSingleVariableDeclaration);
        setCatchClauseBody(newCatchClause, rewrite, catchClause);
        listRewrite.insertAfter(newCatchClause, catchClause, null);
    }
    rewrite.remove(catchClause, null);
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    String label = CorrectionMessages.QuickAssistProcessor_convert_to_multiple_singletype_catch_blocks;
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.USE_SEPARATE_CATCH_BLOCKS, image);
    resultingCollections.add(proposal);
    return true;
}
Also used : ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Image(org.eclipse.swt.graphics.Image) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) IType(org.eclipse.jdt.core.IType) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Example 69 with ListRewrite

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

the class QuickAssistProcessor method getPickoutTypeFromMulticatchProposals.

private static boolean getPickoutTypeFromMulticatchProposals(IInvocationContext context, ASTNode node, ArrayList<ASTNode> coveredNodes, Collection<ICommandAccess> resultingCollections) {
    CatchClause catchClause = (CatchClause) ASTResolving.findAncestor(node, ASTNode.CATCH_CLAUSE);
    if (catchClause == null) {
        return false;
    }
    Statement statement = ASTResolving.findParentStatement(node);
    if (statement != catchClause.getParent() && statement != catchClause.getBody()) {
        // selection is in a statement inside the body
        return false;
    }
    Type type = catchClause.getException().getType();
    if (!type.isUnionType()) {
        return false;
    }
    Type selectedMultiCatchType = null;
    if (type.isUnionType() && node instanceof Name) {
        Name topMostName = ASTNodes.getTopMostName((Name) node);
        ASTNode parent = topMostName.getParent();
        if (parent instanceof SimpleType || parent instanceof NameQualifiedType) {
            selectedMultiCatchType = (Type) parent;
        }
    }
    boolean multipleExceptions = coveredNodes.size() > 1;
    if ((selectedMultiCatchType == null) && (!(node instanceof UnionType) || !multipleExceptions)) {
        return false;
    }
    if (!multipleExceptions) {
        coveredNodes.add(selectedMultiCatchType);
    }
    BodyDeclaration bodyDeclaration = ASTResolving.findParentBodyDeclaration(catchClause);
    if (!(bodyDeclaration instanceof MethodDeclaration) && !(bodyDeclaration instanceof Initializer)) {
        return false;
    }
    if (resultingCollections == null) {
        return true;
    }
    AST ast = bodyDeclaration.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    CatchClause newCatchClause = ast.newCatchClause();
    SingleVariableDeclaration newSingleVariableDeclaration = ast.newSingleVariableDeclaration();
    UnionType newUnionType = ast.newUnionType();
    List<Type> types = newUnionType.types();
    for (int i = 0; i < coveredNodes.size(); i++) {
        ASTNode typeNode = coveredNodes.get(i);
        types.add((Type) rewrite.createCopyTarget(typeNode));
        rewrite.remove(typeNode, null);
    }
    newSingleVariableDeclaration.setType(newUnionType);
    newSingleVariableDeclaration.setName((SimpleName) rewrite.createCopyTarget(catchClause.getException().getName()));
    newCatchClause.setException(newSingleVariableDeclaration);
    setCatchClauseBody(newCatchClause, rewrite, catchClause);
    TryStatement tryStatement = (TryStatement) catchClause.getParent();
    ListRewrite listRewrite = rewrite.getListRewrite(tryStatement, TryStatement.CATCH_CLAUSES_PROPERTY);
    listRewrite.insertAfter(newCatchClause, catchClause, null);
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_OBJS_EXCEPTION);
    String label = !multipleExceptions ? CorrectionMessages.QuickAssistProcessor_move_exception_to_separate_catch_block : CorrectionMessages.QuickAssistProcessor_move_exceptions_to_separate_catch_block;
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.MOVE_EXCEPTION_TO_SEPERATE_CATCH_BLOCK, image);
    resultingCollections.add(proposal);
    return true;
}
Also used : ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Image(org.eclipse.swt.graphics.Image) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) IType(org.eclipse.jdt.core.IType) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Example 70 with ListRewrite

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

the class ChangeMethodSignatureProposal method modifyParameters.

private void modifyParameters(ASTRewrite rewrite, MethodDeclaration methodDecl) {
    AST ast = methodDecl.getAST();
    ArrayList<String> usedNames = new ArrayList<String>();
    boolean hasCreatedVariables = false;
    IVariableBinding[] declaredFields = fSenderBinding.getDeclaringClass().getDeclaredFields();
    for (int i = 0; i < declaredFields.length; i++) {
        // avoid to take parameter names that are equal to field names
        usedNames.add(declaredFields[i].getName());
    }
    ImportRewrite imports = getImportRewrite();
    ImportRewriteContext context = new ContextSensitiveImportRewriteContext(methodDecl, imports);
    ListRewrite listRewrite = rewrite.getListRewrite(methodDecl, MethodDeclaration.PARAMETERS_PROPERTY);
    // old parameters
    List<SingleVariableDeclaration> parameters = methodDecl.parameters();
    // index over the oldParameters
    int k = 0;
    for (int i = 0; i < fParameterChanges.length; i++) {
        ChangeDescription curr = fParameterChanges[i];
        if (curr == null) {
            SingleVariableDeclaration oldParam = parameters.get(k);
            usedNames.add(oldParam.getName().getIdentifier());
            k++;
        } else if (curr instanceof InsertDescription) {
            InsertDescription desc = (InsertDescription) curr;
            SingleVariableDeclaration newNode = ast.newSingleVariableDeclaration();
            newNode.setType(imports.addImport(desc.type, ast, context));
            //$NON-NLS-1$
            newNode.setName(ast.newSimpleName("x"));
            // remember to set name later
            desc.resultingParamName = new SimpleName[] { newNode.getName() };
            desc.resultingParamType = newNode.getType();
            hasCreatedVariables = true;
            listRewrite.insertAt(newNode, i, null);
            Javadoc javadoc = methodDecl.getJavadoc();
            if (javadoc != null) {
                TagElement newTagElement = ast.newTagElement();
                newTagElement.setTagName(TagElement.TAG_PARAM);
                //$NON-NLS-1$
                SimpleName arg = ast.newSimpleName("x");
                newTagElement.fragments().add(arg);
                //$NON-NLS-1$
                insertTabStop(rewrite, newTagElement.fragments(), "param_tagcomment" + i);
                insertParamTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), parameters, k, newTagElement);
                // set the name later
                desc.resultingTagArg = arg;
            } else {
                desc.resultingTagArg = null;
            }
        } else if (curr instanceof RemoveDescription) {
            SingleVariableDeclaration decl = parameters.get(k);
            listRewrite.remove(decl, null);
            k++;
            TagElement tagNode = findParamTag(methodDecl, decl);
            if (tagNode != null) {
                rewrite.remove(tagNode, null);
            }
        } else if (curr instanceof EditDescription) {
            EditDescription desc = (EditDescription) curr;
            ITypeBinding newTypeBinding = desc.type;
            SingleVariableDeclaration decl = parameters.get(k);
            if (k == parameters.size() - 1 && i == fParameterChanges.length - 1 && decl.isVarargs() && newTypeBinding.isArray()) {
                // stick with varargs if it was before
                newTypeBinding = newTypeBinding.getElementType();
            } else {
                rewrite.set(decl, SingleVariableDeclaration.VARARGS_PROPERTY, Boolean.FALSE, null);
            }
            Type newType = imports.addImport(newTypeBinding, ast, context);
            rewrite.replace(decl.getType(), newType, null);
            DimensionRewrite.removeAllChildren(decl, SingleVariableDeclaration.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
            IBinding binding = decl.getName().resolveBinding();
            if (binding != null) {
                SimpleName[] names = LinkedNodeFinder.findByBinding(decl.getRoot(), binding);
                SimpleName[] newNames = new SimpleName[names.length];
                for (int j = 0; j < names.length; j++) {
                    //$NON-NLS-1$  // name will be set later
                    SimpleName newName = ast.newSimpleName("x");
                    newNames[j] = newName;
                    rewrite.replace(names[j], newName, null);
                }
                desc.resultingParamName = newNames;
            } else {
                //$NON-NLS-1$  // name will be set later
                SimpleName newName = ast.newSimpleName("x");
                rewrite.replace(decl.getName(), newName, null);
                // remember to set name later
                desc.resultingParamName = new SimpleName[] { newName };
            }
            desc.resultingParamType = newType;
            desc.orginalName = decl.getName().getIdentifier();
            hasCreatedVariables = true;
            k++;
            TagElement tagNode = findParamTag(methodDecl, decl);
            if (tagNode != null) {
                List<? extends ASTNode> fragments = tagNode.fragments();
                if (!fragments.isEmpty()) {
                    //$NON-NLS-1$
                    SimpleName arg = ast.newSimpleName("x");
                    rewrite.replace(fragments.get(0), arg, null);
                    desc.resultingTagArg = arg;
                }
            }
        } else if (curr instanceof SwapDescription) {
            SingleVariableDeclaration decl1 = parameters.get(k);
            SingleVariableDeclaration decl2 = parameters.get(((SwapDescription) curr).index);
            rewrite.replace(decl1, rewrite.createCopyTarget(decl2), null);
            rewrite.replace(decl2, rewrite.createCopyTarget(decl1), null);
            usedNames.add(decl1.getName().getIdentifier());
            k++;
            TagElement tagNode1 = findParamTag(methodDecl, decl1);
            TagElement tagNode2 = findParamTag(methodDecl, decl2);
            if (tagNode1 != null && tagNode2 != null) {
                rewrite.replace(tagNode1, rewrite.createCopyTarget(tagNode2), null);
                rewrite.replace(tagNode2, rewrite.createCopyTarget(tagNode1), null);
            }
        }
    }
    if (!hasCreatedVariables) {
        return;
    }
    if (methodDecl.getBody() != null) {
        // avoid take a name of a local variable inside
        CompilationUnit root = (CompilationUnit) methodDecl.getRoot();
        IBinding[] bindings = (new ScopeAnalyzer(root)).getDeclarationsAfter(methodDecl.getBody().getStartPosition(), ScopeAnalyzer.VARIABLES);
        for (int i = 0; i < bindings.length; i++) {
            usedNames.add(bindings[i].getName());
        }
    }
    fixupNames(rewrite, usedNames);
}
Also used : ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) ArrayList(java.util.ArrayList) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) TagElement(org.eclipse.jdt.core.dom.TagElement) ArrayList(java.util.ArrayList) List(java.util.List) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) AST(org.eclipse.jdt.core.dom.AST) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) 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) ScopeAnalyzer(org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer)

Aggregations

ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)79 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)45 ASTNode (org.eclipse.jdt.core.dom.ASTNode)37 AST (org.eclipse.jdt.core.dom.AST)30 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)19 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)19 Block (org.eclipse.jdt.core.dom.Block)18 Type (org.eclipse.jdt.core.dom.Type)18 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 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)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