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);
}
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;
}
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;
}
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;
}
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;
}
Aggregations