Search in sources :

Example 81 with ASTNode

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

the class InferTypeArgumentsRefactoring method rewriteCastVariable.

private static ASTNode rewriteCastVariable(CastVariable2 castCv, CompilationUnitRewrite rewrite, InferTypeArgumentsTCModel tCModel) {
    //, List positionGroups) {
    ASTNode node = castCv.getRange().getNode(rewrite.getRoot());
    ConstraintVariable2 expressionVariable = castCv.getExpressionVariable();
    ConstraintVariable2 methodReceiverCv = tCModel.getMethodReceiverCv(expressionVariable);
    if (methodReceiverCv != null) {
        TType chosenReceiverType = InferTypeArgumentsConstraintsSolver.getChosenType(methodReceiverCv);
        if (chosenReceiverType == null)
            return null;
        else if (!InferTypeArgumentsTCModel.isAGenericType(chosenReceiverType))
            return null;
        else if (hasUnboundElement(methodReceiverCv, tCModel))
            return null;
    }
    CastExpression castExpression = (CastExpression) node;
    Expression expression = castExpression.getExpression();
    ASTNode nodeToReplace;
    if (castExpression.getParent() instanceof ParenthesizedExpression)
        nodeToReplace = castExpression.getParent();
    else
        nodeToReplace = castExpression;
    Expression newExpression = (Expression) rewrite.getASTRewrite().createMoveTarget(expression);
    rewrite.getASTRewrite().replace(nodeToReplace, newExpression, rewrite.createGroupDescription(RefactoringCoreMessages.InferTypeArgumentsRefactoring_removeCast));
    rewrite.getImportRemover().registerRemovedNode(nodeToReplace);
    return newExpression;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) 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) TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2) CastExpression(org.eclipse.jdt.core.dom.CastExpression)

Example 82 with ASTNode

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

the class SourceProvider method replaceParameterWithExpression.

private void replaceParameterWithExpression(ASTRewrite rewriter, CallContext context, ImportRewrite importRewrite) throws CoreException {
    Expression[] arguments = context.arguments;
    try {
        ITextFileBuffer buffer = RefactoringFileBuffers.acquire(context.compilationUnit);
        for (int i = 0; i < arguments.length; i++) {
            Expression expression = arguments[i];
            String expressionString = null;
            if (expression instanceof SimpleName) {
                expressionString = ((SimpleName) expression).getIdentifier();
            } else {
                try {
                    expressionString = buffer.getDocument().get(expression.getStartPosition(), expression.getLength());
                } catch (BadLocationException exception) {
                    JavaPlugin.log(exception);
                    continue;
                }
            }
            ParameterData parameter = getParameterData(i);
            List<SimpleName> references = parameter.references();
            for (Iterator<SimpleName> iter = references.iterator(); iter.hasNext(); ) {
                ASTNode element = iter.next();
                Expression newExpression = (Expression) rewriter.createStringPlaceholder(expressionString, expression.getNodeType());
                AST ast = rewriter.getAST();
                ITypeBinding explicitCast = ASTNodes.getExplicitCast(expression, (Expression) element);
                if (explicitCast != null) {
                    CastExpression cast = ast.newCastExpression();
                    if (NecessaryParenthesesChecker.needsParentheses(expression, cast, CastExpression.EXPRESSION_PROPERTY)) {
                        newExpression = createParenthesizedExpression(newExpression, ast);
                    }
                    cast.setExpression(newExpression);
                    ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(expression, importRewrite);
                    cast.setType(importRewrite.addImport(explicitCast, ast, importRewriteContext));
                    expression = newExpression = cast;
                }
                if (NecessaryParenthesesChecker.needsParentheses(expression, element.getParent(), element.getLocationInParent())) {
                    newExpression = createParenthesizedExpression(newExpression, ast);
                }
                rewriter.replace(element, newExpression, null);
            }
        }
    } finally {
        RefactoringFileBuffers.release(context.compilationUnit);
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) 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) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) ASTNode(org.eclipse.jdt.core.dom.ASTNode) CastExpression(org.eclipse.jdt.core.dom.CastExpression) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 83 with ASTNode

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

the class SourceProvider method isDangligIf.

public boolean isDangligIf() {
    List<Statement> statements = fDeclaration.getBody().statements();
    if (statements.size() != 1)
        return false;
    ASTNode p = statements.get(0);
    while (true) {
        if (p instanceof IfStatement) {
            return ((IfStatement) p).getElseStatement() == null;
        } else {
            ChildPropertyDescriptor childD;
            if (p instanceof WhileStatement) {
                childD = WhileStatement.BODY_PROPERTY;
            } else if (p instanceof ForStatement) {
                childD = ForStatement.BODY_PROPERTY;
            } else if (p instanceof EnhancedForStatement) {
                childD = EnhancedForStatement.BODY_PROPERTY;
            } else if (p instanceof DoStatement) {
                childD = DoStatement.BODY_PROPERTY;
            } else if (p instanceof LabeledStatement) {
                childD = LabeledStatement.BODY_PROPERTY;
            } else {
                return false;
            }
            Statement body = (Statement) p.getStructuralProperty(childD);
            if (body instanceof Block) {
                return false;
            } else {
                p = body;
            }
        }
    }
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) ChildPropertyDescriptor(org.eclipse.jdt.core.dom.ChildPropertyDescriptor) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement)

Example 84 with ASTNode

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

the class SourceProvider method createRange.

private IRegion createRange(List<Statement> statements, int end) {
    ASTNode first = statements.get(0);
    ASTNode last = statements.get(end);
    return createRange(first, last);
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 85 with ASTNode

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

the class SourceProvider method getExpressionRanges.

private List<IRegion> getExpressionRanges() {
    fMarkerMode = EXPRESSION_MODE;
    List<IRegion> result = new ArrayList<IRegion>(2);
    List<Statement> statements = fDeclaration.getBody().statements();
    ReturnStatement rs = null;
    int size = statements.size();
    ASTNode node;
    switch(size) {
        case 0:
            return result;
        case 1:
            node = statements.get(0);
            if (node.getNodeType() == ASTNode.RETURN_STATEMENT) {
                rs = (ReturnStatement) node;
            } else {
                result.add(createRange(node, node));
            }
            break;
        default:
            {
                node = statements.get(size - 1);
                if (node.getNodeType() == ASTNode.RETURN_STATEMENT) {
                    result.add(createRange(statements, size - 2));
                    rs = (ReturnStatement) node;
                } else {
                    result.add(createRange(statements, size - 1));
                }
                break;
            }
    }
    if (rs != null) {
        Expression exp = rs.getExpression();
        result.add(createRange(exp, exp));
    }
    return result;
}
Also used : ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) 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) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) ArrayList(java.util.ArrayList) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IRegion(org.eclipse.jface.text.IRegion)

Aggregations

ASTNode (org.eclipse.jdt.core.dom.ASTNode)432 SimpleName (org.eclipse.jdt.core.dom.SimpleName)99 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)98 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)90 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)85 Expression (org.eclipse.jdt.core.dom.Expression)81 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)73 AST (org.eclipse.jdt.core.dom.AST)70 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)69 ArrayList (java.util.ArrayList)62 Type (org.eclipse.jdt.core.dom.Type)62 Block (org.eclipse.jdt.core.dom.Block)61 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)49 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)47 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)45 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)44 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)44 CastExpression (org.eclipse.jdt.core.dom.CastExpression)42 IBinding (org.eclipse.jdt.core.dom.IBinding)41 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)37