Search in sources :

Example 1 with ExpressionStatement

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

the class MissingReturnTypeCorrectionProposal method getRewrite.

/*(non-Javadoc)
	 * @see org.eclipse.jdt.internal.ui.text.correction.ASTRewriteCorrectionProposal#getRewrite()
	 */
@Override
protected ASTRewrite getRewrite() {
    AST ast = getAST();
    ITypeBinding returnBinding = getReturnTypeBinding();
    if (fExistingReturn != null) {
        ASTRewrite rewrite = ASTRewrite.create(ast);
        Expression expression = evaluateReturnExpressions(ast, returnBinding, fExistingReturn.getStartPosition());
        if (expression != null) {
            rewrite.set(fExistingReturn, ReturnStatement.EXPRESSION_PROPERTY, expression, null);
            addLinkedPosition(rewrite.track(expression), true, RETURN_EXPRESSION_KEY);
        }
        return rewrite;
    } else {
        ASTRewrite rewrite = ASTRewrite.create(ast);
        ASTNode body = getBody();
        // For lambda the body can be a block or an expression.
        if (body instanceof Block) {
            Block block = (Block) body;
            List<Statement> statements = block.statements();
            int nStatements = statements.size();
            ASTNode lastStatement = null;
            if (nStatements > 0) {
                lastStatement = statements.get(nStatements - 1);
            }
            if (returnBinding != null && lastStatement instanceof ExpressionStatement && lastStatement.getNodeType() != ASTNode.ASSIGNMENT) {
                Expression expression = ((ExpressionStatement) lastStatement).getExpression();
                ITypeBinding binding = expression.resolveTypeBinding();
                if (binding != null && binding.isAssignmentCompatible(returnBinding)) {
                    Expression placeHolder = (Expression) rewrite.createMoveTarget(expression);
                    ReturnStatement returnStatement = ast.newReturnStatement();
                    returnStatement.setExpression(placeHolder);
                    rewrite.replace(lastStatement, returnStatement, null);
                    return rewrite;
                }
            }
            int offset;
            if (lastStatement == null) {
                offset = block.getStartPosition() + 1;
            } else {
                offset = lastStatement.getStartPosition() + lastStatement.getLength();
            }
            ReturnStatement returnStatement = ast.newReturnStatement();
            Expression expression = evaluateReturnExpressions(ast, returnBinding, offset);
            returnStatement.setExpression(expression);
            rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY).insertLast(returnStatement, null);
            addLinkedPosition(rewrite.track(returnStatement.getExpression()), true, RETURN_EXPRESSION_KEY);
        }
        return rewrite;
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Expression(org.eclipse.jdt.core.dom.Expression) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) Statement(org.eclipse.jdt.core.dom.Statement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block)

Example 2 with ExpressionStatement

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

the class NewMethodCorrectionProposal method getNewMethodType.

/* (non-Javadoc)
	 * @see org.eclipse.jdt.internal.ui.text.correction.proposals.AbstractMethodCorrectionProposal#getNewMethodType(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)
	 */
@Override
protected Type getNewMethodType(ASTRewrite rewrite) throws CoreException {
    ASTNode node = getInvocationNode();
    AST ast = rewrite.getAST();
    Type newTypeNode = null;
    ITypeBinding[] otherProposals = null;
    ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(node, getImportRewrite());
    if (node.getParent() instanceof MethodInvocation) {
        MethodInvocation parent = (MethodInvocation) node.getParent();
        if (parent.getExpression() == node) {
            ITypeBinding[] bindings = ASTResolving.getQualifierGuess(node.getRoot(), parent.getName().getIdentifier(), parent.arguments(), getSenderBinding());
            if (bindings.length > 0) {
                newTypeNode = getImportRewrite().addImport(bindings[0], ast, importRewriteContext);
                otherProposals = bindings;
            }
        }
    }
    if (newTypeNode == null) {
        ITypeBinding binding = ASTResolving.guessBindingForReference(node);
        if (binding != null && binding.isWildcardType()) {
            binding = ASTResolving.normalizeWildcardType(binding, false, ast);
        }
        if (binding != null) {
            newTypeNode = getImportRewrite().addImport(binding, ast, importRewriteContext);
        } else {
            ASTNode parent = node.getParent();
            if (parent instanceof ExpressionStatement) {
                newTypeNode = ast.newPrimitiveType(PrimitiveType.VOID);
            } else {
                newTypeNode = ASTResolving.guessTypeForReference(ast, node);
                if (newTypeNode == null) {
                    //$NON-NLS-1$
                    newTypeNode = ast.newSimpleType(ast.newSimpleName("Object"));
                }
            }
        }
    }
    addLinkedPosition(rewrite.track(newTypeNode), false, KEY_TYPE);
    if (otherProposals != null) {
        for (int i = 0; i < otherProposals.length; i++) {
            addLinkedPositionProposal(KEY_TYPE, otherProposals[i]);
        }
    }
    return newTypeNode;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) 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) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation)

Example 3 with ExpressionStatement

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

the class AssignToVariableAssistProposal method doAddLocal.

private ASTRewrite doAddLocal() {
    Expression expression = ((ExpressionStatement) fNodeToAssign).getExpression();
    AST ast = fNodeToAssign.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    createImportRewrite((CompilationUnit) fNodeToAssign.getRoot());
    String[] varNames = suggestLocalVariableNames(fTypeBinding, expression);
    for (int i = 0; i < varNames.length; i++) {
        addLinkedPositionProposal(KEY_NAME, varNames[i], null);
    }
    VariableDeclarationFragment newDeclFrag = ast.newVariableDeclarationFragment();
    newDeclFrag.setName(ast.newSimpleName(varNames[0]));
    newDeclFrag.setInitializer((Expression) rewrite.createCopyTarget(expression));
    Type type = evaluateType(ast);
    if (ASTNodes.isControlStatementBody(fNodeToAssign.getLocationInParent())) {
        Block block = ast.newBlock();
        block.statements().add(rewrite.createMoveTarget(fNodeToAssign));
        rewrite.replace(fNodeToAssign, block, null);
    }
    if (needsSemicolon(expression)) {
        VariableDeclarationStatement varStatement = ast.newVariableDeclarationStatement(newDeclFrag);
        varStatement.setType(type);
        rewrite.replace(expression, varStatement, null);
    } else {
        // trick for bug 43248: use an VariableDeclarationExpression and keep the ExpressionStatement
        VariableDeclarationExpression varExpression = ast.newVariableDeclarationExpression(newDeclFrag);
        varExpression.setType(type);
        rewrite.replace(expression, varExpression, null);
    }
    addLinkedPosition(rewrite.track(newDeclFrag.getName()), true, KEY_NAME);
    addLinkedPosition(rewrite.track(type), false, KEY_TYPE);
    // set cursor after expression statement
    setEndPosition(rewrite.track(fNodeToAssign));
    return rewrite;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) 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) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement)

Example 4 with ExpressionStatement

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

the class IntroduceFactoryRefactoring method getCtorCallAt.

/**
	 * Look "in the vicinity" of the given range to find the <code>ClassInstanceCreation</code>
	 * node that this search hit identified. Necessary because the <code>SearchEngine</code>
	 * doesn't always cough up text extents that <code>NodeFinder.perform()</code> agrees with.
	 * @param start
	 * @param length
	 * @param unitAST
	 * @return return a {@link ClassInstanceCreation} or a {@link MethodRef} or <code>null</code> if this is really a constructor->constructor call (e.g. "this(...)")
	 * @throws CoreException
	 */
private ASTNode getCtorCallAt(int start, int length, CompilationUnit unitAST) throws CoreException {
    ICompilationUnit unitHandle = ASTCreator.getCu(unitAST);
    ASTNode node = NodeFinder.perform(unitAST, start, length);
    if (node == null)
        throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_noASTNodeForConstructorSearchHit, new Object[] { Integer.toString(start), Integer.toString(start + length), BasicElementLabels.getJavaCodeString(unitHandle.getSource().substring(start, start + length)), BasicElementLabels.getFileName(unitHandle) }), null));
    if (node instanceof ClassInstanceCreation) {
        if (((ClassInstanceCreation) node).getAnonymousClassDeclaration() != null) {
            // Cannot replace anonymous inner class, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=250660
            fConstructorVisibility = Modifier.PROTECTED;
            return null;
        }
        return node;
    } else if (node instanceof VariableDeclaration) {
        Expression init = ((VariableDeclaration) node).getInitializer();
        if (init instanceof ClassInstanceCreation) {
            return init;
        } else if (init != null)
            throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_unexpectedInitializerNodeType, new Object[] { BasicElementLabels.getJavaCodeString(init.toString()), BasicElementLabels.getFileName(unitHandle) }), null));
        else
            throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_noConstructorCallNodeInsideFoundVarbleDecl, BasicElementLabels.getJavaCodeString(node.toString())), null));
    } else if (node instanceof ConstructorInvocation) {
        // to another flavor on the same class.
        return null;
    } else if (node instanceof SuperConstructorInvocation) {
        // This is a call we can bypass; it's from one constructor flavor
        // to another flavor on the same class.
        fConstructorVisibility = Modifier.PROTECTED;
        return null;
    } else if (node instanceof ExpressionStatement) {
        Expression expr = ((ExpressionStatement) node).getExpression();
        if (expr instanceof ClassInstanceCreation)
            return expr;
        else
            throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_unexpectedASTNodeTypeForConstructorSearchHit, new Object[] { BasicElementLabels.getJavaCodeString(expr.toString()), BasicElementLabels.getFileName(unitHandle) }), null));
    } else if (node instanceof SimpleName && (node.getParent() instanceof MethodDeclaration || node.getParent() instanceof AbstractTypeDeclaration)) {
        // We seem to have been given a hit for an implicit call to the base-class constructor.
        // Do nothing with this (implicit) call, but have to make sure we make the derived class
        // doesn't lose access to the base-class constructor (so make it 'protected', not 'private').
        fConstructorVisibility = Modifier.PROTECTED;
        return null;
    } else if (node instanceof MethodRef) {
        return node;
    } else
        throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_unexpectedASTNodeTypeForConstructorSearchHit, new Object[] { BasicElementLabels.getJavaElementName(node.getClass().getName() + "('" + node.toString() + "')"), BasicElementLabels.getFileName(unitHandle) }), //$NON-NLS-1$ //$NON-NLS-2$
        null));
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) MethodRef(org.eclipse.jdt.core.dom.MethodRef) CoreException(org.eclipse.core.runtime.CoreException) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) Expression(org.eclipse.jdt.core.dom.Expression) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 5 with ExpressionStatement

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

the class ExtractTempRefactoring method getSelectedExpression.

private IExpressionFragment getSelectedExpression() throws JavaModelException {
    if (fSelectedExpression != null)
        return fSelectedExpression;
    IASTFragment selectedFragment = ASTFragmentFactory.createFragmentForSourceRange(new SourceRange(fSelectionStart, fSelectionLength), fCompilationUnitNode, fCu);
    if (selectedFragment instanceof IExpressionFragment && !Checks.isInsideJavadoc(selectedFragment.getAssociatedNode())) {
        fSelectedExpression = (IExpressionFragment) selectedFragment;
    } else if (selectedFragment != null) {
        if (selectedFragment.getAssociatedNode() instanceof ExpressionStatement) {
            ExpressionStatement exprStatement = (ExpressionStatement) selectedFragment.getAssociatedNode();
            Expression expression = exprStatement.getExpression();
            fSelectedExpression = (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(expression);
        } else if (selectedFragment.getAssociatedNode() instanceof Assignment) {
            Assignment assignment = (Assignment) selectedFragment.getAssociatedNode();
            fSelectedExpression = (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(assignment);
        }
    }
    if (fSelectedExpression != null && Checks.isEnumCase(fSelectedExpression.getAssociatedExpression().getParent())) {
        fSelectedExpression = null;
    }
    return fSelectedExpression;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) IASTFragment(org.eclipse.jdt.internal.corext.dom.fragments.IASTFragment) IExpressionFragment(org.eclipse.jdt.internal.corext.dom.fragments.IExpressionFragment) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) SourceRange(org.eclipse.jdt.core.SourceRange)

Aggregations

ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)22 Expression (org.eclipse.jdt.core.dom.Expression)17 ASTNode (org.eclipse.jdt.core.dom.ASTNode)14 AST (org.eclipse.jdt.core.dom.AST)11 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)10 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)10 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)10 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)10 Block (org.eclipse.jdt.core.dom.Block)9 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)9 Statement (org.eclipse.jdt.core.dom.Statement)9 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)9 Assignment (org.eclipse.jdt.core.dom.Assignment)8 CastExpression (org.eclipse.jdt.core.dom.CastExpression)8 SimpleName (org.eclipse.jdt.core.dom.SimpleName)8 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)7 PostfixExpression (org.eclipse.jdt.core.dom.PostfixExpression)7 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)7 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)6 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)6