Search in sources :

Example 36 with ReturnStatement

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

the class JUnitCodeGenerator method createGetFieldMethod.

@SuppressWarnings({ "rawtypes", "unchecked" })
private void createGetFieldMethod(final TypeDeclaration td, final CompilationUnit cu, final AST ast) {
    // public static void setField(final String clazzName, final String fieldName, final Object receiver, final Object value) throws Exception
    // {
    // final Class<?> clazz = Class.forName(clazzName);
    // final Field    f     = clazz.getDeclaredField(fieldName);
    // f.setAccessible(true);
    // f.set(receiver, value);
    // }
    // -- add necessary import statements
    List imports = cu.imports();
    ImportDeclaration id = ast.newImportDeclaration();
    id.setName(ast.newName(new String[] { "java", "lang", "reflect", "Field" }));
    imports.add(id);
    // -- create method frame: "public static Object setProtectedField(final String clazzName, final String fieldName, final Object receiver) throws Exception"
    final MethodDeclaration md = ast.newMethodDeclaration();
    td.bodyDeclarations().add(md);
    md.setName(ast.newSimpleName("getField"));
    List modifiers = md.modifiers();
    modifiers.add(ast.newModifier(Modifier.ModifierKeyword.PRIVATE_KEYWORD));
    modifiers.add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));
    md.thrownExceptions().add(ast.newSimpleName("Exception"));
    List parameters = md.parameters();
    SingleVariableDeclaration svd = ast.newSingleVariableDeclaration();
    svd.setType(ast.newSimpleType(ast.newSimpleName("String")));
    svd.setName(ast.newSimpleName("clazzName"));
    parameters.add(svd);
    svd = ast.newSingleVariableDeclaration();
    svd.setType(ast.newSimpleType(ast.newSimpleName("String")));
    svd.setName(ast.newSimpleName("fieldName"));
    parameters.add(svd);
    svd = ast.newSingleVariableDeclaration();
    svd.setType(ast.newSimpleType(ast.newSimpleName("Object")));
    svd.setName(ast.newSimpleName("receiver"));
    parameters.add(svd);
    md.setReturnType2(ast.newSimpleType(ast.newSimpleName("Object")));
    // -- create method body
    // final Class<?> clazz = Class.forName(clazzName);
    // final Field    f     = clazz.getDeclaredField(fieldName);
    // f.setAccessible(true);
    // return f.get(receiver);
    final Block methodBlock = ast.newBlock();
    md.setBody(methodBlock);
    final List methodStmts = methodBlock.statements();
    // final Class clazz = Class.forName(clazzName);
    MethodInvocation init = ast.newMethodInvocation();
    init.setName(ast.newSimpleName("forName"));
    init.setExpression(ast.newSimpleName("Class"));
    init.arguments().add(ast.newSimpleName("clazzName"));
    VariableDeclarationFragment varDeclFrag = ast.newVariableDeclarationFragment();
    varDeclFrag.setName(ast.newSimpleName("clazz"));
    varDeclFrag.setInitializer(init);
    VariableDeclarationStatement varDeclStmt = ast.newVariableDeclarationStatement(varDeclFrag);
    varDeclStmt.setType(ast.newSimpleType(ast.newSimpleName("Class")));
    methodStmts.add(varDeclStmt);
    // final Field f = clazz.getDeclaredField(fieldName);
    init = ast.newMethodInvocation();
    init.setName(ast.newSimpleName("getDeclaredField"));
    init.setExpression(ast.newSimpleName("clazz"));
    init.arguments().add(ast.newSimpleName("fieldName"));
    varDeclFrag = ast.newVariableDeclarationFragment();
    varDeclFrag.setName(ast.newSimpleName("f"));
    varDeclFrag.setInitializer(init);
    varDeclStmt = ast.newVariableDeclarationStatement(varDeclFrag);
    varDeclStmt.setType(ast.newSimpleType(ast.newSimpleName("Field")));
    methodStmts.add(varDeclStmt);
    // f.setAccessible(true);
    MethodInvocation minv = ast.newMethodInvocation();
    minv.setName(ast.newSimpleName("setAccessible"));
    minv.setExpression(ast.newSimpleName("f"));
    minv.arguments().add(ast.newBooleanLiteral(true));
    methodStmts.add(ast.newExpressionStatement(minv));
    // return f.get(receiver);
    minv = ast.newMethodInvocation();
    minv.setName(ast.newSimpleName("get"));
    minv.setExpression(ast.newSimpleName("f"));
    minv.arguments().add(ast.newSimpleName("receiver"));
    final ReturnStatement returnStmt = ast.newReturnStatement();
    returnStmt.setExpression(minv);
    methodStmts.add(returnStmt);
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) TIntArrayList(gnu.trove.list.array.TIntArrayList) List(java.util.List) ArrayList(java.util.ArrayList) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 37 with ReturnStatement

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

the class QuickAssistProcessor method getChangeLambdaBodyToExpressionProposal.

private static boolean getChangeLambdaBodyToExpressionProposal(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
    LambdaExpression lambda;
    if (covering instanceof LambdaExpression) {
        lambda = (LambdaExpression) covering;
    } else if (covering.getLocationInParent() == LambdaExpression.BODY_PROPERTY) {
        lambda = (LambdaExpression) covering.getParent();
    } else {
        return false;
    }
    if (!(lambda.getBody() instanceof Block))
        return false;
    Block lambdaBody = (Block) lambda.getBody();
    if (lambdaBody.statements().size() != 1)
        return false;
    Expression exprBody;
    Statement singleStatement = (Statement) lambdaBody.statements().get(0);
    if (singleStatement instanceof ReturnStatement) {
        Expression returnExpr = ((ReturnStatement) singleStatement).getExpression();
        if (returnExpr == null)
            return false;
        exprBody = returnExpr;
    } else if (singleStatement instanceof ExpressionStatement) {
        Expression expression = ((ExpressionStatement) singleStatement).getExpression();
        if (isValidExpressionBody(expression)) {
            exprBody = expression;
        } else {
            return false;
        }
    } else {
        return false;
    }
    if (resultingCollections == null)
        return true;
    AST ast = lambda.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    Expression movedBody = (Expression) rewrite.createMoveTarget(exprBody);
    rewrite.set(lambda, LambdaExpression.BODY_PROPERTY, movedBody, null);
    // add proposal
    String label = CorrectionMessages.QuickAssistProcessor_change_lambda_body_to_expression;
    // Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CHANGE_LAMBDA_BODY_TO_EXPRESSION);
    resultingCollections.add(proposal);
    return true;
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) AST(org.eclipse.jdt.core.dom.AST) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 38 with ReturnStatement

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

the class AdvancedQuickAssistProcessor method getConvertToIfReturnProposals.

private static boolean getConvertToIfReturnProposals(IInvocationContext context, ASTNode coveringNode, ArrayList<ICommandAccess> resultingCollections) {
    if (!(coveringNode instanceof IfStatement)) {
        return false;
    }
    IfStatement ifStatement = (IfStatement) coveringNode;
    if (ifStatement.getElseStatement() != null) {
        return false;
    }
    // enclosing lambda or method should return 'void'
    LambdaExpression enclosingLambda = ASTResolving.findEnclosingLambdaExpression(ifStatement);
    if (enclosingLambda != null) {
        IMethodBinding lambdaMethodBinding = enclosingLambda.resolveMethodBinding();
        if (lambdaMethodBinding == null) {
            return false;
        }
        if (!(ifStatement.getAST().resolveWellKnownType("void").equals(lambdaMethodBinding.getReturnType()))) {
            // $NON-NLS-1$
            return false;
        }
    } else {
        MethodDeclaration coveringMethod = ASTResolving.findParentMethodDeclaration(ifStatement);
        if (coveringMethod == null) {
            return false;
        }
        Type returnType = coveringMethod.getReturnType2();
        if (!isVoid(returnType)) {
            return false;
        }
    }
    // should be present in a block
    if (!(ifStatement.getParent() instanceof Block)) {
        return false;
    }
    // should have at least one statement in 'then' part other than 'return'
    Statement thenStatement = ifStatement.getThenStatement();
    if (thenStatement instanceof ReturnStatement) {
        return false;
    }
    if (thenStatement instanceof Block) {
        List<Statement> thenStatements = ((Block) thenStatement).statements();
        if (thenStatements.isEmpty() || (thenStatements.size() == 1 && (thenStatements.get(0) instanceof ReturnStatement))) {
            return false;
        }
    }
    // should have no further executable statement
    if (!isLastStatementInEnclosingMethodOrLambda(ifStatement)) {
        return false;
    }
    // we could produce quick assist
    if (resultingCollections == null) {
        return true;
    }
    AST ast = coveringNode.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    // create inverted 'if' statement
    Expression inversedExpression = getInversedExpression(rewrite, ifStatement.getExpression());
    IfStatement newIf = ast.newIfStatement();
    newIf.setExpression(inversedExpression);
    newIf.setThenStatement(ast.newReturnStatement());
    ListRewrite listRewriter = rewrite.getListRewrite(ifStatement.getParent(), (ChildListPropertyDescriptor) ifStatement.getLocationInParent());
    listRewriter.replace(ifStatement, newIf, null);
    // remove last 'return' in 'then' block
    ArrayList<Statement> statements = getUnwrappedStatements(ifStatement.getThenStatement());
    Statement lastStatement = statements.get(statements.size() - 1);
    if (lastStatement instanceof ReturnStatement) {
        statements.remove(lastStatement);
    }
    // add statements from 'then' to the end of block
    for (Statement statement : statements) {
        listRewriter.insertLast(rewrite.createMoveTarget(statement), null);
    }
    // add correction proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_convertToIfReturn;
    // Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CONVERT_TO_IF_RETURN);
    resultingCollections.add(proposal);
    return true;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) AssertStatement(org.eclipse.jdt.core.dom.AssertStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) IfStatement(org.eclipse.jdt.core.dom.IfStatement) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 39 with ReturnStatement

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

the class AdvancedQuickAssistProcessor method createReturnExpression.

private static ReturnStatement createReturnExpression(ASTRewrite rewrite, Expression expression) {
    AST ast = rewrite.getAST();
    ReturnStatement thenReturn = ast.newReturnStatement();
    thenReturn.setExpression((Expression) rewrite.createCopyTarget(expression));
    return thenReturn;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement)

Example 40 with ReturnStatement

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

the class AdvancedQuickAssistProcessor method hasStopAsLastExecutableStatement.

private static boolean hasStopAsLastExecutableStatement(Statement lastStatement) {
    if (lastStatement instanceof ReturnStatement || lastStatement instanceof BreakStatement) {
        return true;
    }
    if (lastStatement instanceof Block) {
        Block block = (Block) lastStatement;
        lastStatement = (Statement) block.statements().get(block.statements().size() - 1);
        return hasStopAsLastExecutableStatement(lastStatement);
    }
    return false;
}
Also used : BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block)

Aggregations

ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)85 Block (org.eclipse.jdt.core.dom.Block)53 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)46 Expression (org.eclipse.jdt.core.dom.Expression)42 ASTNode (org.eclipse.jdt.core.dom.ASTNode)39 AST (org.eclipse.jdt.core.dom.AST)36 Type (org.eclipse.jdt.core.dom.Type)33 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)28 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)27 SimpleName (org.eclipse.jdt.core.dom.SimpleName)23 Statement (org.eclipse.jdt.core.dom.Statement)23 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)22 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)20 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)19 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)18 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)17 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)16 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)15 Javadoc (org.eclipse.jdt.core.dom.Javadoc)15 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)15