Search in sources :

Example 1 with PositionInformation

use of org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup.PositionInformation in project flux by eclipse.

the class ModifierChangeCorrectionProposal method getRewrite.

@Override
protected ASTRewrite getRewrite() throws CoreException {
    CompilationUnit astRoot = ASTResolving.findParentCompilationUnit(fNode);
    ASTNode boundNode = astRoot.findDeclaringNode(fBinding);
    ASTNode declNode = null;
    if (boundNode != null) {
        // is same CU
        declNode = boundNode;
    } else {
        //setSelectionDescription(selectionDescription);
        CompilationUnit newRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
        declNode = newRoot.findDeclaringNode(fBinding.getKey());
    }
    if (declNode != null) {
        AST ast = declNode.getAST();
        ASTRewrite rewrite = ASTRewrite.create(ast);
        if (declNode.getNodeType() == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
            VariableDeclarationFragment fragment = (VariableDeclarationFragment) declNode;
            ASTNode parent = declNode.getParent();
            if (parent instanceof FieldDeclaration) {
                FieldDeclaration fieldDecl = (FieldDeclaration) parent;
                if (fieldDecl.fragments().size() > 1 && (fieldDecl.getParent() instanceof AbstractTypeDeclaration)) {
                    // split
                    VariableDeclarationRewrite.rewriteModifiers(fieldDecl, new VariableDeclarationFragment[] { fragment }, fIncludedModifiers, fExcludedModifiers, rewrite, null);
                    return rewrite;
                }
            } else if (parent instanceof VariableDeclarationStatement) {
                VariableDeclarationStatement varDecl = (VariableDeclarationStatement) parent;
                if (varDecl.fragments().size() > 1 && (varDecl.getParent() instanceof Block)) {
                    // split
                    VariableDeclarationRewrite.rewriteModifiers(varDecl, new VariableDeclarationFragment[] { fragment }, fIncludedModifiers, fExcludedModifiers, rewrite, null);
                    return rewrite;
                }
            } else if (parent instanceof VariableDeclarationExpression) {
            // can't separate
            }
            declNode = parent;
        } else if (declNode.getNodeType() == ASTNode.METHOD_DECLARATION) {
            MethodDeclaration methodDecl = (MethodDeclaration) declNode;
            if (!methodDecl.isConstructor()) {
                IMethodBinding methodBinding = methodDecl.resolveBinding();
                if (methodDecl.getBody() == null && methodBinding != null && Modifier.isAbstract(methodBinding.getModifiers()) && Modifier.isStatic(fIncludedModifiers)) {
                    // add body
                    ICompilationUnit unit = getCompilationUnit();
                    String delimiter = unit.findRecommendedLineSeparator();
                    //$NON-NLS-1$
                    String bodyStatement = "";
                    Block body = ast.newBlock();
                    rewrite.set(methodDecl, MethodDeclaration.BODY_PROPERTY, body, null);
                    Type returnType = methodDecl.getReturnType2();
                    if (returnType != null) {
                        Expression expression = ASTNodeFactory.newDefaultExpression(ast, returnType, methodDecl.getExtraDimensions());
                        if (expression != null) {
                            ReturnStatement returnStatement = ast.newReturnStatement();
                            returnStatement.setExpression(expression);
                            bodyStatement = ASTNodes.asFormattedString(returnStatement, 0, delimiter, unit.getJavaProject().getOptions(true));
                        }
                    }
                    String placeHolder = CodeGeneration.getMethodBodyContent(unit, methodBinding.getDeclaringClass().getName(), methodBinding.getName(), false, bodyStatement, delimiter);
                    if (placeHolder != null) {
                        ReturnStatement todoNode = (ReturnStatement) rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
                        body.statements().add(todoNode);
                    }
                }
            }
        }
        ModifierRewrite listRewrite = ModifierRewrite.create(rewrite, declNode);
        PositionInformation trackedDeclNode = listRewrite.setModifiers(fIncludedModifiers, fExcludedModifiers, null);
        //$NON-NLS-1$
        LinkedProposalPositionGroup positionGroup = new LinkedProposalPositionGroup("group");
        positionGroup.addPosition(trackedDeclNode);
        getLinkedProposalModel().addPositionGroup(positionGroup);
        if (boundNode != null) {
            // only set end position if in same CU
            setEndPosition(rewrite.track(fNode));
        }
        return rewrite;
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ModifierRewrite(org.eclipse.jdt.internal.corext.dom.ModifierRewrite) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PositionInformation(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup.PositionInformation) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) Type(org.eclipse.jdt.core.dom.Type) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Block(org.eclipse.jdt.core.dom.Block) LinkedProposalPositionGroup(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 2 with PositionInformation

use of org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup.PositionInformation in project che by eclipse.

the class ModifierChangeCorrectionProposal method getRewrite.

@Override
protected ASTRewrite getRewrite() throws CoreException {
    CompilationUnit astRoot = ASTResolving.findParentCompilationUnit(fNode);
    ASTNode boundNode = astRoot.findDeclaringNode(fBinding);
    ASTNode declNode = null;
    if (boundNode != null) {
        // is same CU
        declNode = boundNode;
    } else {
        //setSelectionDescription(selectionDescription);
        CompilationUnit newRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
        declNode = newRoot.findDeclaringNode(fBinding.getKey());
    }
    if (declNode != null) {
        AST ast = declNode.getAST();
        ASTRewrite rewrite = ASTRewrite.create(ast);
        if (declNode.getNodeType() == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
            VariableDeclarationFragment fragment = (VariableDeclarationFragment) declNode;
            ASTNode parent = declNode.getParent();
            if (parent instanceof FieldDeclaration) {
                FieldDeclaration fieldDecl = (FieldDeclaration) parent;
                if (fieldDecl.fragments().size() > 1 && (fieldDecl.getParent() instanceof AbstractTypeDeclaration)) {
                    // split
                    VariableDeclarationRewrite.rewriteModifiers(fieldDecl, new VariableDeclarationFragment[] { fragment }, fIncludedModifiers, fExcludedModifiers, rewrite, null);
                    return rewrite;
                }
            } else if (parent instanceof VariableDeclarationStatement) {
                VariableDeclarationStatement varDecl = (VariableDeclarationStatement) parent;
                if (varDecl.fragments().size() > 1 && (varDecl.getParent() instanceof Block)) {
                    // split
                    VariableDeclarationRewrite.rewriteModifiers(varDecl, new VariableDeclarationFragment[] { fragment }, fIncludedModifiers, fExcludedModifiers, rewrite, null);
                    return rewrite;
                }
            } else if (parent instanceof VariableDeclarationExpression) {
            // can't separate
            }
            declNode = parent;
        } else if (declNode.getNodeType() == ASTNode.METHOD_DECLARATION) {
            MethodDeclaration methodDecl = (MethodDeclaration) declNode;
            if (!methodDecl.isConstructor()) {
                IMethodBinding methodBinding = methodDecl.resolveBinding();
                if (methodDecl.getBody() == null && methodBinding != null && Modifier.isAbstract(methodBinding.getModifiers()) && Modifier.isStatic(fIncludedModifiers)) {
                    // add body
                    ICompilationUnit unit = getCompilationUnit();
                    String delimiter = unit.findRecommendedLineSeparator();
                    //$NON-NLS-1$
                    String bodyStatement = "";
                    Block body = ast.newBlock();
                    rewrite.set(methodDecl, MethodDeclaration.BODY_PROPERTY, body, null);
                    Type returnType = methodDecl.getReturnType2();
                    if (returnType != null) {
                        Expression expression = ASTNodeFactory.newDefaultExpression(ast, returnType, methodDecl.getExtraDimensions());
                        if (expression != null) {
                            ReturnStatement returnStatement = ast.newReturnStatement();
                            returnStatement.setExpression(expression);
                            bodyStatement = ASTNodes.asFormattedString(returnStatement, 0, delimiter, unit.getJavaProject().getOptions(true));
                        }
                    }
                    String placeHolder = CodeGeneration.getMethodBodyContent(unit, methodBinding.getDeclaringClass().getName(), methodBinding.getName(), false, bodyStatement, delimiter);
                    if (placeHolder != null) {
                        ReturnStatement todoNode = (ReturnStatement) rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
                        body.statements().add(todoNode);
                    }
                }
            }
        }
        ModifierRewrite listRewrite = ModifierRewrite.create(rewrite, declNode);
        PositionInformation trackedDeclNode = listRewrite.setModifiers(fIncludedModifiers, fExcludedModifiers, null);
        //$NON-NLS-1$
        LinkedProposalPositionGroup positionGroup = new LinkedProposalPositionGroup("group");
        positionGroup.addPosition(trackedDeclNode);
        getLinkedProposalModel().addPositionGroup(positionGroup);
        if (boundNode != null) {
            // only set end position if in same CU
            setEndPosition(rewrite.track(fNode));
        }
        return rewrite;
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ModifierRewrite(org.eclipse.jdt.internal.corext.dom.ModifierRewrite) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PositionInformation(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup.PositionInformation) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) Type(org.eclipse.jdt.core.dom.Type) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Block(org.eclipse.jdt.core.dom.Block) LinkedProposalPositionGroup(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Aggregations

ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 AST (org.eclipse.jdt.core.dom.AST)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)2 Block (org.eclipse.jdt.core.dom.Block)2 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)2 Expression (org.eclipse.jdt.core.dom.Expression)2 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)2 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)2 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)2 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)2 Type (org.eclipse.jdt.core.dom.Type)2 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)2 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)2 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)2 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)2 ModifierRewrite (org.eclipse.jdt.internal.corext.dom.ModifierRewrite)2 LinkedProposalPositionGroup (org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup)2 PositionInformation (org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup.PositionInformation)2