Search in sources :

Example 6 with ReturnStatement

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

the class IntroduceIndirectionRefactoring method encapsulateInvocation.

private Statement encapsulateInvocation(MethodDeclaration declaration, MethodInvocation invocation) {
    final Type type = declaration.getReturnType2();
    if (type == null || (type instanceof PrimitiveType && PrimitiveType.VOID.equals(((PrimitiveType) type).getPrimitiveTypeCode())))
        return invocation.getAST().newExpressionStatement(invocation);
    ReturnStatement statement = invocation.getAST().newReturnStatement();
    statement.setExpression(invocation);
    return statement;
}
Also used : IType(org.eclipse.jdt.core.IType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) Type(org.eclipse.jdt.core.dom.Type) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType)

Example 7 with ReturnStatement

use of org.eclipse.jdt.core.dom.ReturnStatement 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)

Example 8 with ReturnStatement

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

the class ModifierCorrectionSubProcessor method addNativeMethodProposals.

public static void addNativeMethodProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    CompilationUnit astRoot = context.getASTRoot();
    ASTNode selectedNode = problem.getCoveringNode(astRoot);
    if (selectedNode == null) {
        return;
    }
    MethodDeclaration decl;
    if (selectedNode instanceof SimpleName) {
        decl = (MethodDeclaration) selectedNode.getParent();
    } else if (selectedNode instanceof MethodDeclaration) {
        decl = (MethodDeclaration) selectedNode;
    } else {
        return;
    }
    {
        AST ast = astRoot.getAST();
        ASTRewrite rewrite = ASTRewrite.create(ast);
        removeModifier(decl, rewrite, Modifier.NATIVE);
        Block newBody = ast.newBlock();
        rewrite.set(decl, MethodDeclaration.BODY_PROPERTY, newBody, null);
        Type returnType = decl.getReturnType2();
        if (returnType != null) {
            Expression expr = ASTNodeFactory.newDefaultExpression(ast, returnType, decl.getExtraDimensions());
            if (expr != null) {
                ReturnStatement returnStatement = ast.newReturnStatement();
                returnStatement.setExpression(expr);
                newBody.statements().add(returnStatement);
            }
        }
        String label = CorrectionMessages.ModifierCorrectionSubProcessor_removenative_description;
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.REMOVE_NATIVE, image);
        proposals.add(proposal);
    }
    if (decl.getBody() != null) {
        ASTRewrite rewrite = ASTRewrite.create(decl.getAST());
        rewrite.remove(decl.getBody(), null);
        String label = CorrectionMessages.ModifierCorrectionSubProcessor_removebody_description;
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        ASTRewriteCorrectionProposal proposal2 = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.REMOVE_METHOD_BODY, image);
        proposals.add(proposal2);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Image(org.eclipse.swt.graphics.Image) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) Expression(org.eclipse.jdt.core.dom.Expression) 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 9 with ReturnStatement

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

the class ModifierCorrectionSubProcessor method addMethodRequiresBodyProposals.

public static void addMethodRequiresBodyProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    AST ast = context.getASTRoot().getAST();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (!(selectedNode instanceof MethodDeclaration)) {
        return;
    }
    MethodDeclaration decl = (MethodDeclaration) selectedNode;
    Modifier modifierNode;
    {
        ASTRewrite rewrite = ASTRewrite.create(ast);
        modifierNode = removeModifier(decl, rewrite, Modifier.ABSTRACT);
        Block body = ast.newBlock();
        rewrite.set(decl, MethodDeclaration.BODY_PROPERTY, body, null);
        if (!decl.isConstructor()) {
            Type returnType = decl.getReturnType2();
            if (returnType != null) {
                Expression expression = ASTNodeFactory.newDefaultExpression(ast, returnType, decl.getExtraDimensions());
                if (expression != null) {
                    ReturnStatement returnStatement = ast.newReturnStatement();
                    returnStatement.setExpression(expression);
                    body.statements().add(returnStatement);
                }
            }
        }
        String label = CorrectionMessages.ModifierCorrectionSubProcessor_addmissingbody_description;
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.ADD_MISSING_BODY, image);
        proposals.add(proposal);
    }
    IMethodBinding binding = decl.resolveBinding();
    if (modifierNode == null && binding != null) {
        String label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_changemodifiertoabstract_description, getMethodLabel(binding));
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        int included = binding.getDeclaringClass().isInterface() ? Modifier.NONE : Modifier.ABSTRACT;
        int excluded = Modifier.STATIC | Modifier.DEFAULT;
        ModifierChangeCorrectionProposal proposal = new ModifierChangeCorrectionProposal(label, cu, binding, decl, included, excluded, IProposalRelevance.ADD_ABSTRACT_MODIFIER, image);
        proposals.add(proposal);
    }
}
Also used : 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) Image(org.eclipse.swt.graphics.Image) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) Expression(org.eclipse.jdt.core.dom.Expression) ModifierChangeCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ModifierChangeCorrectionProposal) 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) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier)

Example 10 with ReturnStatement

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

the class SelfEncapsulateFieldRefactoring method createGetterMethod.

private MethodDeclaration createGetterMethod(AST ast, ASTRewrite rewriter, String lineDelimiter) throws CoreException {
    FieldDeclaration field = (FieldDeclaration) ASTNodes.getParent(fFieldDeclaration, FieldDeclaration.class);
    Type type = field.getType();
    MethodDeclaration result = ast.newMethodDeclaration();
    result.setName(ast.newSimpleName(fGetterName));
    result.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiers()));
    Type returnType = DimensionRewrite.copyTypeAndAddDimensions(type, fFieldDeclaration.extraDimensions(), rewriter);
    result.setReturnType2(returnType);
    Block block = ast.newBlock();
    result.setBody(block);
    String body = CodeGeneration.getGetterMethodBodyContent(fField.getCompilationUnit(), getTypeName(field.getParent()), fGetterName, fField.getElementName(), lineDelimiter);
    if (body != null) {
        ASTNode getterNode = rewriter.createStringPlaceholder(body, ASTNode.BLOCK);
        block.statements().add(getterNode);
    } else {
        ReturnStatement rs = ast.newReturnStatement();
        rs.setExpression(ast.newSimpleName(fField.getElementName()));
        block.statements().add(rs);
    }
    if (fGenerateJavadoc) {
        String string = CodeGeneration.getGetterComment(fField.getCompilationUnit(), getTypeName(field.getParent()), fGetterName, fField.getElementName(), ASTNodes.asString(type), StubUtility.getBaseName(fField), lineDelimiter);
        if (string != null) {
            Javadoc javadoc = (Javadoc) fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC);
            result.setJavadoc(javadoc);
        }
    }
    return result;
}
Also used : IType(org.eclipse.jdt.core.IType) Type(org.eclipse.jdt.core.dom.Type) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) Javadoc(org.eclipse.jdt.core.dom.Javadoc) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Aggregations

ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)34 Block (org.eclipse.jdt.core.dom.Block)24 AST (org.eclipse.jdt.core.dom.AST)23 Expression (org.eclipse.jdt.core.dom.Expression)22 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)21 ASTNode (org.eclipse.jdt.core.dom.ASTNode)20 Type (org.eclipse.jdt.core.dom.Type)19 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)18 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)14 Statement (org.eclipse.jdt.core.dom.Statement)12 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)11 SimpleName (org.eclipse.jdt.core.dom.SimpleName)11 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)11 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)10 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)9 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)9 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)9 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)8 DoStatement (org.eclipse.jdt.core.dom.DoStatement)8 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)8