java type proposals_Java MissingReturnTypeCorrectionProposal類代碼示例

本文介绍了一段使用 Eclipse JDT API 的代码片段,该片段展示了如何为缺少返回类型的 Java 方法提供修正建议。包括添加缺失的返回语句、修改方法返回类型为 void 并更新对应的 Javadoc。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值