import org.eclipse.jdt.internal.ui.text.correction.proposals.MissingReturnTypeCorrectionProposal; //導入依賴的package包/類
public static void addMissingReturnStatementProposals(IInvocationContext context, IProblemLocation problem, Collection proposals) {
ICompilationUnit cu= context.getCompilationUnit();
ASTNode selectedNode= problem.getCoveringNode(context.getASTRoot());
if (selectedNode == null) {
return;
}
BodyDeclaration decl= ASTResolving.findParentBodyDeclaration(selectedNode);
if (decl instanceof MethodDeclaration) {
MethodDeclaration methodDecl= (MethodDeclaration) decl;
Block block= methodDecl.getBody();
if (block == null) {
return;
}
ReturnStatement existingStatement= (selectedNode instanceof ReturnStatement) ? (ReturnStatement) selectedNode : null;
proposals.add( new MissingReturnTypeCorrectionProposal(cu, methodDecl, existingStatement, 6));
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, 5, image);
proposals.add(proposal);
}
}
}