Search in sources :

Example 66 with AST

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

the class ConvertForLoopOperation method convertBody.

private void convertBody(Statement body, final IBinding indexBinding, final IBinding arrayBinding, final String parameterName, final ASTRewrite rewrite, final TextEditGroup editGroup, final LinkedProposalPositionGroup pg) {
    final AST ast = body.getAST();
    body.accept(new GenericVisitor() {

        @Override
        public boolean visit(ArrayAccess node) {
            IBinding binding = getBinding(node.getArray());
            if (arrayBinding.equals(binding)) {
                IBinding index = getBinding(node.getIndex());
                if (indexBinding.equals(index)) {
                    replaceAccess(node);
                }
            }
            return super.visit(node);
        }

        private void replaceAccess(ASTNode node) {
            if (fElementDeclaration != null && node.getLocationInParent() == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
                VariableDeclarationFragment fragment = (VariableDeclarationFragment) node.getParent();
                IBinding targetBinding = fragment.getName().resolveBinding();
                if (targetBinding != null) {
                    VariableDeclarationStatement statement = (VariableDeclarationStatement) fragment.getParent();
                    if (statement.fragments().size() == 1) {
                        rewrite.remove(statement, editGroup);
                    } else {
                        ListRewrite listRewrite = rewrite.getListRewrite(statement, VariableDeclarationStatement.FRAGMENTS_PROPERTY);
                        listRewrite.remove(fragment, editGroup);
                    }
                } else {
                    SimpleName name = ast.newSimpleName(parameterName);
                    rewrite.replace(node, name, editGroup);
                    pg.addPosition(rewrite.track(name), true);
                }
            } else {
                SimpleName name = ast.newSimpleName(parameterName);
                rewrite.replace(node, name, editGroup);
                pg.addPosition(rewrite.track(name), true);
            }
        }
    });
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) IBinding(org.eclipse.jdt.core.dom.IBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) GenericVisitor(org.eclipse.jdt.internal.corext.dom.GenericVisitor)

Example 67 with AST

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

the class SelfEncapsulateFieldRefactoring method addGetterSetterChanges.

private List<TextEditGroup> addGetterSetterChanges(CompilationUnit root, ASTRewrite rewriter, String lineDelimiter, boolean usingLocalSetter, boolean usingLocalGetter) throws CoreException {
    List<TextEditGroup> result = new ArrayList<TextEditGroup>(2);
    AST ast = root.getAST();
    FieldDeclaration decl = (FieldDeclaration) ASTNodes.getParent(fFieldDeclaration, ASTNode.FIELD_DECLARATION);
    int position = 0;
    int numberOfMethods = 0;
    List<BodyDeclaration> members = ASTNodes.getBodyDeclarations(decl.getParent());
    for (Iterator<BodyDeclaration> iter = members.iterator(); iter.hasNext(); ) {
        BodyDeclaration element = iter.next();
        if (element.getNodeType() == ASTNode.METHOD_DECLARATION) {
            if (fInsertionIndex == -1) {
                break;
            } else if (fInsertionIndex == numberOfMethods) {
                position++;
                break;
            }
            numberOfMethods++;
        }
        position++;
    }
    TextEditGroup description;
    ListRewrite rewrite = fRewriter.getListRewrite(decl.getParent(), getBodyDeclarationsProperty(decl.getParent()));
    if (!usingLocalGetter) {
        description = new TextEditGroup(RefactoringCoreMessages.SelfEncapsulateField_add_getter);
        result.add(description);
        rewrite.insertAt(createGetterMethod(ast, rewriter, lineDelimiter), position++, description);
    }
    if (!JdtFlags.isFinal(fField) && !usingLocalSetter) {
        description = new TextEditGroup(RefactoringCoreMessages.SelfEncapsulateField_add_setter);
        result.add(description);
        rewrite.insertAt(createSetterMethod(ast, rewriter, lineDelimiter), position, description);
    }
    if (!JdtFlags.isPrivate(fField))
        result.add(makeDeclarationPrivate(rewriter, decl));
    return result;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ArrayList(java.util.ArrayList) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 68 with AST

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

the class CallInliner method replaceCall.

private void replaceCall(RefactoringStatus status, String[] blocks, TextEditGroup textEditGroup) {
    // Inline empty body
    if (blocks.length == 0 && fTargetNode != null) {
        if (fNeedsStatement) {
            fRewrite.replace(fTargetNode, fTargetNode.getAST().newEmptyStatement(), textEditGroup);
        } else {
            fRewrite.remove(fTargetNode, textEditGroup);
        }
    } else {
        ASTNode node = null;
        for (int i = 0; i < blocks.length - 1; i++) {
            node = fRewrite.createStringPlaceholder(blocks[i], ASTNode.RETURN_STATEMENT);
            fListRewrite.insertAt(node, fInsertionIndex++, textEditGroup);
        }
        String block = blocks[blocks.length - 1];
        // returned expression must be evaluated.
        if (fContext.callMode == ASTNode.EXPRESSION_STATEMENT && fSourceProvider.hasReturnValue()) {
            if (fSourceProvider.mustEvaluateReturnedExpression()) {
                if (fSourceProvider.returnValueNeedsLocalVariable()) {
                    IMethodBinding invocation = Invocations.resolveBinding(fInvocation);
                    node = createLocalDeclaration(invocation.getReturnType(), fInvocationScope.createName(fSourceProvider.getMethodName(), true), (Expression) fRewrite.createStringPlaceholder(block, ASTNode.METHOD_INVOCATION));
                } else {
                    node = fRewrite.getAST().newExpressionStatement((Expression) fRewrite.createStringPlaceholder(block, ASTNode.METHOD_INVOCATION));
                }
            } else {
                node = null;
            }
        } else if (fTargetNode instanceof Expression) {
            node = fRewrite.createStringPlaceholder(block, ASTNode.METHOD_INVOCATION);
            // fixes bug #24941
            if (needsExplicitCast(status)) {
                AST ast = node.getAST();
                CastExpression castExpression = ast.newCastExpression();
                Type returnType = fImportRewrite.addImport(fSourceProvider.getReturnType(), ast);
                castExpression.setType(returnType);
                if (NecessaryParenthesesChecker.needsParentheses(fSourceProvider.getReturnExpressions().get(0), castExpression, CastExpression.EXPRESSION_PROPERTY)) {
                    ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
                    parenthesized.setExpression((Expression) node);
                    node = parenthesized;
                }
                castExpression.setExpression((Expression) node);
                node = castExpression;
                if (NecessaryParenthesesChecker.needsParentheses(castExpression, fTargetNode.getParent(), fTargetNode.getLocationInParent())) {
                    ParenthesizedExpression parenthesized = ast.newParenthesizedExpression();
                    parenthesized.setExpression((Expression) node);
                    node = parenthesized;
                }
            } else if (fSourceProvider.needsReturnedExpressionParenthesis(fTargetNode.getParent(), fTargetNode.getLocationInParent())) {
                ParenthesizedExpression pExp = fTargetNode.getAST().newParenthesizedExpression();
                pExp.setExpression((Expression) node);
                node = pExp;
            }
        } else {
            node = fRewrite.createStringPlaceholder(block, ASTNode.RETURN_STATEMENT);
        }
        // Now replace the target node with the source node
        if (node != null) {
            if (fTargetNode == null) {
                fListRewrite.insertAt(node, fInsertionIndex++, textEditGroup);
            } else {
                fRewrite.replace(fTargetNode, node, textEditGroup);
            }
        } else {
            if (fTargetNode != null) {
                fRewrite.remove(fTargetNode, textEditGroup);
            }
        }
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) AST(org.eclipse.jdt.core.dom.AST) Type(org.eclipse.jdt.core.dom.Type) TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) CastExpression(org.eclipse.jdt.core.dom.CastExpression)

Example 69 with AST

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

the class ConvertAnonymousToNestedRefactoring method addArgumentsForLocalsUsedInInnerClass.

private void addArgumentsForLocalsUsedInInnerClass(ClassInstanceCreation newClassCreation) {
    IVariableBinding[] usedLocals = getUsedLocalVariables();
    for (int i = 0; i < usedLocals.length; i++) {
        final AST ast = fAnonymousInnerClassNode.getAST();
        final IVariableBinding binding = usedLocals[i];
        Name name = null;
        if (binding.isEnumConstant())
            name = ast.newQualifiedName(ast.newSimpleName(binding.getDeclaringClass().getName()), ast.newSimpleName(binding.getName()));
        else
            name = ast.newSimpleName(binding.getName());
        newClassCreation.arguments().add(name);
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name)

Example 70 with AST

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

the class ConvertAnonymousToNestedRefactoring method copyArguments.

private void copyArguments(CompilationUnitRewrite rewrite, ClassInstanceCreation newClassCreation) {
    Iterator<Expression> iter = ((ClassInstanceCreation) fAnonymousInnerClassNode.getParent()).arguments().iterator();
    if (!iter.hasNext())
        return;
    IMethodBinding superConstructorBinding = getSuperConstructorBinding();
    ITypeBinding[] parameterTypes = superConstructorBinding.getParameterTypes();
    List<Expression> arguments = newClassCreation.arguments();
    ASTRewrite astRewrite = rewrite.getASTRewrite();
    int last = parameterTypes.length - 1;
    for (int i = 0; i < last; i++) {
        arguments.add((Expression) astRewrite.createCopyTarget(iter.next()));
    }
    if (superConstructorBinding.isVarargs()) {
        AST ast = astRewrite.getAST();
        ArrayCreation arrayCreation = ast.newArrayCreation();
        arrayCreation.setType((ArrayType) rewrite.getImportRewrite().addImport(parameterTypes[last], ast));
        ArrayInitializer initializer = ast.newArrayInitializer();
        arrayCreation.setInitializer(initializer);
        arguments.add(arrayCreation);
        arguments = initializer.expressions();
    }
    while (iter.hasNext()) {
        arguments.add((Expression) astRewrite.createCopyTarget(iter.next()));
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) AST(org.eclipse.jdt.core.dom.AST) Expression(org.eclipse.jdt.core.dom.Expression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Aggregations

AST (org.eclipse.jdt.core.dom.AST)169 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)78 Expression (org.eclipse.jdt.core.dom.Expression)77 ASTNode (org.eclipse.jdt.core.dom.ASTNode)70 Type (org.eclipse.jdt.core.dom.Type)52 SimpleName (org.eclipse.jdt.core.dom.SimpleName)51 Block (org.eclipse.jdt.core.dom.Block)44 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)43 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)42 CastExpression (org.eclipse.jdt.core.dom.CastExpression)40 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)40 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)39 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)38 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)38 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)34 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)33 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)33 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)33 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)30 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)30