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 getIfReturnIntoIfElseAtEndOfVoidMethodProposals.
private static boolean getIfReturnIntoIfElseAtEndOfVoidMethodProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
if (!(covering instanceof IfStatement)) {
return false;
}
IfStatement ifStatement = (IfStatement) covering;
if (ifStatement.getElseStatement() != null) {
return false;
}
// 'then' block should have 'return' as last statement
Statement thenStatement = ifStatement.getThenStatement();
if (!(thenStatement instanceof Block)) {
return false;
}
Block thenBlock = (Block) thenStatement;
List<Statement> thenStatements = thenBlock.statements();
if (thenStatements.isEmpty() || !(thenStatements.get(thenStatements.size() - 1) instanceof ReturnStatement)) {
return false;
}
// method should return 'void'
MethodDeclaration coveringMetod = ASTResolving.findParentMethodDeclaration(covering);
if (coveringMetod == null) {
return false;
}
Type returnType = coveringMetod.getReturnType2();
if (!isVoid(returnType)) {
return false;
}
//
List<Statement> statements = coveringMetod.getBody().statements();
int ifIndex = statements.indexOf(ifStatement);
if (ifIndex == -1) {
return false;
}
// we could produce quick assist
if (resultingCollections == null) {
return true;
}
//
AST ast = covering.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
// remove last 'return' in 'then' block
ListRewrite listRewriter = rewrite.getListRewrite(thenBlock, (ChildListPropertyDescriptor) ifStatement.getLocationInParent());
listRewriter.remove(thenStatements.get(thenStatements.size() - 1), null);
// prepare original nodes
Expression conditionPlaceholder = (Expression) rewrite.createMoveTarget(ifStatement.getExpression());
Statement thenPlaceholder = (Statement) rewrite.createMoveTarget(ifStatement.getThenStatement());
// prepare 'else' block
Block elseBlock = ast.newBlock();
for (int i = ifIndex + 1; i < statements.size(); i++) {
Statement statement = statements.get(i);
elseBlock.statements().add(rewrite.createMoveTarget(statement));
}
// prepare new 'if' statement
IfStatement newIf = ast.newIfStatement();
newIf.setExpression(conditionPlaceholder);
newIf.setThenStatement(thenPlaceholder);
newIf.setElseStatement(elseBlock);
rewrite.replace(ifStatement, newIf, null);
// add correction proposal
String label = CorrectionMessages.AdvancedQuickAssistProcessor_convertToIfElse_description;
// Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.CONVERT_TO_IF_ELSE);
resultingCollections.add(proposal);
return true;
}
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;
}
use of org.eclipse.jdt.core.dom.ReturnStatement in project flux by eclipse.
the class ModifierChangeCorrectionProposal method getRewrite.
@Override
protected ASTRewrite getRewrite() throws CoreException {
CompilationUnit astRoot = ASTResolving.findParentCompilationUnit(fNode);
ASTNode boundNode = astRoot.findDeclaringNode(fBinding);
ASTNode declNode = null;
if (boundNode != null) {
// is same CU
declNode = boundNode;
} else {
//setSelectionDescription(selectionDescription);
CompilationUnit newRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
declNode = newRoot.findDeclaringNode(fBinding.getKey());
}
if (declNode != null) {
AST ast = declNode.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
if (declNode.getNodeType() == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
VariableDeclarationFragment fragment = (VariableDeclarationFragment) declNode;
ASTNode parent = declNode.getParent();
if (parent instanceof FieldDeclaration) {
FieldDeclaration fieldDecl = (FieldDeclaration) parent;
if (fieldDecl.fragments().size() > 1 && (fieldDecl.getParent() instanceof AbstractTypeDeclaration)) {
// split
VariableDeclarationRewrite.rewriteModifiers(fieldDecl, new VariableDeclarationFragment[] { fragment }, fIncludedModifiers, fExcludedModifiers, rewrite, null);
return rewrite;
}
} else if (parent instanceof VariableDeclarationStatement) {
VariableDeclarationStatement varDecl = (VariableDeclarationStatement) parent;
if (varDecl.fragments().size() > 1 && (varDecl.getParent() instanceof Block)) {
// split
VariableDeclarationRewrite.rewriteModifiers(varDecl, new VariableDeclarationFragment[] { fragment }, fIncludedModifiers, fExcludedModifiers, rewrite, null);
return rewrite;
}
} else if (parent instanceof VariableDeclarationExpression) {
// can't separate
}
declNode = parent;
} else if (declNode.getNodeType() == ASTNode.METHOD_DECLARATION) {
MethodDeclaration methodDecl = (MethodDeclaration) declNode;
if (!methodDecl.isConstructor()) {
IMethodBinding methodBinding = methodDecl.resolveBinding();
if (methodDecl.getBody() == null && methodBinding != null && Modifier.isAbstract(methodBinding.getModifiers()) && Modifier.isStatic(fIncludedModifiers)) {
// add body
ICompilationUnit unit = getCompilationUnit();
String delimiter = unit.findRecommendedLineSeparator();
//$NON-NLS-1$
String bodyStatement = "";
Block body = ast.newBlock();
rewrite.set(methodDecl, MethodDeclaration.BODY_PROPERTY, body, null);
Type returnType = methodDecl.getReturnType2();
if (returnType != null) {
Expression expression = ASTNodeFactory.newDefaultExpression(ast, returnType, methodDecl.getExtraDimensions());
if (expression != null) {
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(expression);
bodyStatement = ASTNodes.asFormattedString(returnStatement, 0, delimiter, unit.getJavaProject().getOptions(true));
}
}
String placeHolder = CodeGeneration.getMethodBodyContent(unit, methodBinding.getDeclaringClass().getName(), methodBinding.getName(), false, bodyStatement, delimiter);
if (placeHolder != null) {
ReturnStatement todoNode = (ReturnStatement) rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
body.statements().add(todoNode);
}
}
}
}
ModifierRewrite listRewrite = ModifierRewrite.create(rewrite, declNode);
PositionInformation trackedDeclNode = listRewrite.setModifiers(fIncludedModifiers, fExcludedModifiers, null);
//$NON-NLS-1$
LinkedProposalPositionGroup positionGroup = new LinkedProposalPositionGroup("group");
positionGroup.addPosition(trackedDeclNode);
getLinkedProposalModel().addPositionGroup(positionGroup);
if (boundNode != null) {
// only set end position if in same CU
setEndPosition(rewrite.track(fNode));
}
return rewrite;
}
return null;
}
use of org.eclipse.jdt.core.dom.ReturnStatement in project flux 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);
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);
proposals.add(proposal2);
}
}
Aggregations