use of org.eclipse.jdt.core.dom.ReturnStatement in project che by eclipse.
the class ReturnTypeSubProcessor method addMissingReturnStatementProposals.
public static void addMissingReturnStatementProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
ICompilationUnit cu = context.getCompilationUnit();
ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
if (selectedNode == null) {
return;
}
ReturnStatement existingStatement = (selectedNode instanceof ReturnStatement) ? (ReturnStatement) selectedNode : null;
// Lambda Expression can be in a MethodDeclaration or a Field Declaration
if (selectedNode instanceof LambdaExpression) {
MissingReturnTypeInLambdaCorrectionProposal proposal = new MissingReturnTypeInLambdaCorrectionProposal(cu, (LambdaExpression) selectedNode, existingStatement, IProposalRelevance.MISSING_RETURN_TYPE);
proposals.add(proposal);
} else {
BodyDeclaration decl = ASTResolving.findParentBodyDeclaration(selectedNode);
if (decl instanceof MethodDeclaration) {
MethodDeclaration methodDecl = (MethodDeclaration) decl;
Block block = methodDecl.getBody();
if (block == null) {
return;
}
proposals.add(new MissingReturnTypeCorrectionProposal(cu, methodDecl, existingStatement, IProposalRelevance.MISSING_RETURN_TYPE));
Type returnType = methodDecl.getReturnType2();
if (returnType != null && !"void".equals(ASTNodes.asString(returnType))) {
//$NON-NLS-1$
AST ast = methodDecl.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
rewrite.replace(returnType, ast.newPrimitiveType(PrimitiveType.VOID), null);
Javadoc javadoc = methodDecl.getJavadoc();
if (javadoc != null) {
TagElement tagElement = JavadocTagsSubProcessor.findTag(javadoc, TagElement.TAG_RETURN, null);
if (tagElement != null) {
rewrite.remove(tagElement, null);
}
}
String label = CorrectionMessages.ReturnTypeSubProcessor_changetovoid_description;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.CHANGE_RETURN_TYPE_TO_VOID, image);
proposals.add(proposal);
}
}
}
}
use of org.eclipse.jdt.core.dom.ReturnStatement in project che by eclipse.
the class ExtractToNullCheckedLocalProposal method getRewrite.
@Override
protected ASTRewrite getRewrite() throws CoreException {
// infrastructure:
AST ast = this.compilationUnit.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
ImportRewrite imports = ImportRewrite.create(this.compilationUnit, true);
TextEditGroup group = new TextEditGroup(FixMessages.ExtractToNullCheckedLocalProposal_extractCheckedLocal_editName);
LinkedProposalPositionGroup localNameGroup = new LinkedProposalPositionGroup(LOCAL_NAME_POSITION_GROUP);
getLinkedProposalModel().addPositionGroup(localNameGroup);
// AST context:
Statement origStmt = (Statement) ASTNodes.getParent(this.fieldReference, Statement.class);
// determine suitable strategy for rearranging elements towards a new code structure:
RearrangeStrategy rearrangeStrategy = RearrangeStrategy.create(origStmt, rewrite, group);
Expression toReplace;
ASTNode directParent = this.fieldReference.getParent();
if (directParent instanceof FieldAccess) {
toReplace = (Expression) directParent;
} else if (directParent instanceof QualifiedName && this.fieldReference.getLocationInParent() == QualifiedName.NAME_PROPERTY) {
toReplace = (Expression) directParent;
} else {
toReplace = this.fieldReference;
}
// new local declaration initialized from the field reference
VariableDeclarationFragment localFrag = ast.newVariableDeclarationFragment();
VariableDeclarationStatement localDecl = ast.newVariableDeclarationStatement(localFrag);
// ... type
localDecl.setType(newType(toReplace.resolveTypeBinding(), ast, imports));
localDecl.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD));
// ... name
String localName = proposeLocalName(this.fieldReference, this.compilationUnit, getCompilationUnit().getJavaProject());
localFrag.setName(ast.newSimpleName(localName));
// ... initialization
localFrag.setInitializer((Expression) ASTNode.copySubtree(ast, toReplace));
rearrangeStrategy.insertLocalDecl(localDecl);
// if statement:
IfStatement ifStmt = ast.newIfStatement();
// condition:
InfixExpression nullCheck = ast.newInfixExpression();
nullCheck.setLeftOperand(ast.newSimpleName(localName));
nullCheck.setRightOperand(ast.newNullLiteral());
nullCheck.setOperator(InfixExpression.Operator.NOT_EQUALS);
ifStmt.setExpression(nullCheck);
// then block: the original statement
Block thenBlock = ast.newBlock();
thenBlock.statements().add(rearrangeStrategy.createMoveTargetForOrigStmt());
ifStmt.setThenStatement(thenBlock);
// ... but with the field reference replaced by the new local:
SimpleName dereferencedName = ast.newSimpleName(localName);
rewrite.replace(toReplace, dereferencedName, group);
// else block: a Todo comment
Block elseBlock = ast.newBlock();
//$NON-NLS-1$
String elseStatement = "// TODO " + FixMessages.ExtractToNullCheckedLocalProposal_todoHandleNullDescription;
if (origStmt instanceof ReturnStatement) {
Type returnType = newType(((ReturnStatement) origStmt).getExpression().resolveTypeBinding(), ast, imports);
ReturnStatement returnStatement = ast.newReturnStatement();
returnStatement.setExpression(ASTNodeFactory.newDefaultExpression(ast, returnType, 0));
elseStatement += '\n' + ASTNodes.asFormattedString(returnStatement, 0, String.valueOf('\n'), getCompilationUnit().getJavaProject().getOptions(true));
}
EmptyStatement todoNode = (EmptyStatement) rewrite.createStringPlaceholder(elseStatement, ASTNode.EMPTY_STATEMENT);
elseBlock.statements().add(todoNode);
ifStmt.setElseStatement(elseBlock);
// link all three occurrences of the new local variable:
addLinkedPosition(rewrite.track(localFrag.getName()), true, /*first*/
LOCAL_NAME_POSITION_GROUP);
addLinkedPosition(rewrite.track(nullCheck.getLeftOperand()), false, LOCAL_NAME_POSITION_GROUP);
addLinkedPosition(rewrite.track(dereferencedName), false, LOCAL_NAME_POSITION_GROUP);
rearrangeStrategy.insertIfStatement(ifStmt, thenBlock);
return rewrite;
}
use of org.eclipse.jdt.core.dom.ReturnStatement 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;
}
}
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;
}
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);
}
}
Aggregations