use of org.eclipse.jdt.core.dom.BodyDeclaration in project eclipse.jdt.ls by eclipse.
the class ReturnTypeSubProcessor method addMissingReturnStatementProposals.
public static void addMissingReturnStatementProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> 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;
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.CHANGE_RETURN_TYPE_TO_VOID);
proposals.add(proposal);
}
}
}
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project eclipse.jdt.ls by eclipse.
the class ReturnTypeSubProcessor method addVoidMethodReturnsProposals.
public static void addVoidMethodReturnsProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) {
ICompilationUnit cu = context.getCompilationUnit();
CompilationUnit astRoot = context.getASTRoot();
ASTNode selectedNode = problem.getCoveringNode(astRoot);
if (selectedNode == null) {
return;
}
BodyDeclaration decl = ASTResolving.findParentBodyDeclaration(selectedNode);
if (decl instanceof MethodDeclaration && selectedNode.getNodeType() == ASTNode.RETURN_STATEMENT) {
ReturnStatement returnStatement = (ReturnStatement) selectedNode;
Expression expr = returnStatement.getExpression();
if (expr != null) {
AST ast = astRoot.getAST();
ITypeBinding binding = Bindings.normalizeTypeBinding(expr.resolveTypeBinding());
if (binding == null) {
// $NON-NLS-1$
binding = ast.resolveWellKnownType("java.lang.Object");
}
if (binding.isWildcardType()) {
binding = ASTResolving.normalizeWildcardType(binding, true, ast);
}
MethodDeclaration methodDeclaration = (MethodDeclaration) decl;
ASTRewrite rewrite = ASTRewrite.create(ast);
String label = Messages.format(CorrectionMessages.ReturnTypeSubProcessor_voidmethodreturns_description, BindingLabelProvider.getBindingLabel(binding, BindingLabelProvider.DEFAULT_TEXTFLAGS));
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.VOID_METHOD_RETURNS);
ImportRewrite imports = proposal.createImportRewrite(astRoot);
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(methodDeclaration, imports);
Type newReturnType = imports.addImport(binding, ast, importRewriteContext, TypeLocation.RETURN_TYPE);
if (methodDeclaration.isConstructor()) {
rewrite.set(methodDeclaration, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null);
rewrite.set(methodDeclaration, MethodDeclaration.RETURN_TYPE2_PROPERTY, newReturnType, null);
} else {
rewrite.replace(methodDeclaration.getReturnType2(), newReturnType, null);
}
// $NON-NLS-1$
String key = "return_type";
proposal.addLinkedPosition(rewrite.track(newReturnType), true, key);
ITypeBinding[] bindings = ASTResolving.getRelaxingTypes(ast, binding);
for (int i = 0; i < bindings.length; i++) {
proposal.addLinkedPositionProposal(key, bindings[i]);
}
Javadoc javadoc = methodDeclaration.getJavadoc();
if (javadoc != null) {
TagElement newTag = ast.newTagElement();
newTag.setTagName(TagElement.TAG_RETURN);
TextElement commentStart = ast.newTextElement();
newTag.fragments().add(commentStart);
JavadocTagsSubProcessor.insertTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), newTag, null);
// $NON-NLS-1$
proposal.addLinkedPosition(rewrite.track(commentStart), false, "comment_start");
}
proposals.add(proposal);
}
ASTRewrite rewrite = ASTRewrite.create(decl.getAST());
rewrite.remove(returnStatement.getExpression(), null);
String label = CorrectionMessages.ReturnTypeSubProcessor_removereturn_description;
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.CHANGE_TO_RETURN);
proposals.add(proposal);
}
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project eclipse.jdt.ls by eclipse.
the class ReturnTypeSubProcessor method addMissingReturnTypeProposals.
public static void addMissingReturnTypeProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> proposals) {
ICompilationUnit cu = context.getCompilationUnit();
CompilationUnit astRoot = context.getASTRoot();
ASTNode selectedNode = problem.getCoveringNode(astRoot);
if (selectedNode == null) {
return;
}
BodyDeclaration decl = ASTResolving.findParentBodyDeclaration(selectedNode);
if (decl instanceof MethodDeclaration) {
MethodDeclaration methodDeclaration = (MethodDeclaration) decl;
ReturnStatementCollector eval = new ReturnStatementCollector();
decl.accept(eval);
AST ast = astRoot.getAST();
ITypeBinding typeBinding = eval.getTypeBinding(decl.getAST());
typeBinding = Bindings.normalizeTypeBinding(typeBinding);
if (typeBinding == null) {
// $NON-NLS-1$
typeBinding = ast.resolveWellKnownType("void");
}
if (typeBinding.isWildcardType()) {
typeBinding = ASTResolving.normalizeWildcardType(typeBinding, true, ast);
}
ASTRewrite rewrite = ASTRewrite.create(ast);
String label = Messages.format(CorrectionMessages.ReturnTypeSubProcessor_missingreturntype_description, BindingLabelProvider.getBindingLabel(typeBinding, BindingLabelProvider.DEFAULT_TEXTFLAGS));
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.MISSING_RETURN_TYPE);
ImportRewrite imports = proposal.createImportRewrite(astRoot);
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(decl, imports);
Type type = imports.addImport(typeBinding, ast, importRewriteContext, TypeLocation.RETURN_TYPE);
rewrite.set(methodDeclaration, MethodDeclaration.RETURN_TYPE2_PROPERTY, type, null);
rewrite.set(methodDeclaration, MethodDeclaration.CONSTRUCTOR_PROPERTY, Boolean.FALSE, null);
Javadoc javadoc = methodDeclaration.getJavadoc();
if (javadoc != null && typeBinding != null) {
TagElement newTag = ast.newTagElement();
newTag.setTagName(TagElement.TAG_RETURN);
TextElement commentStart = ast.newTextElement();
newTag.fragments().add(commentStart);
JavadocTagsSubProcessor.insertTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), newTag, null);
// $NON-NLS-1$
proposal.addLinkedPosition(rewrite.track(commentStart), false, "comment_start");
}
// $NON-NLS-1$
String key = "return_type";
proposal.addLinkedPosition(rewrite.track(type), true, key);
if (typeBinding != null) {
ITypeBinding[] bindings = ASTResolving.getRelaxingTypes(ast, typeBinding);
for (int i = 0; i < bindings.length; i++) {
proposal.addLinkedPositionProposal(key, bindings[i]);
}
}
proposals.add(proposal);
// change to constructor
ASTNode parentType = ASTResolving.findParentType(decl);
if (parentType instanceof AbstractTypeDeclaration) {
boolean isInterface = parentType instanceof TypeDeclaration && ((TypeDeclaration) parentType).isInterface();
if (!isInterface) {
String constructorName = ((AbstractTypeDeclaration) parentType).getName().getIdentifier();
ASTNode nameNode = methodDeclaration.getName();
label = Messages.format(CorrectionMessages.ReturnTypeSubProcessor_wrongconstructorname_description, BasicElementLabels.getJavaElementName(constructorName));
proposals.add(new ReplaceCorrectionProposal(label, cu, nameNode.getStartPosition(), nameNode.getLength(), constructorName, IProposalRelevance.CHANGE_TO_CONSTRUCTOR));
}
}
}
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project eclipse.jdt.ls by eclipse.
the class AbstractMethodCorrectionProposal method findConstructorInsertIndex.
private int findConstructorInsertIndex(List<BodyDeclaration> decls) {
int nDecls = decls.size();
int lastMethod = 0;
for (int i = nDecls - 1; i >= 0; i--) {
BodyDeclaration curr = decls.get(i);
if (curr instanceof MethodDeclaration) {
if (((MethodDeclaration) curr).isConstructor()) {
return i + 1;
}
lastMethod = i;
}
}
return lastMethod;
}
use of org.eclipse.jdt.core.dom.BodyDeclaration in project whole by wholeplatform.
the class JDTTransformerVisitor method preVisit.
@Override
public void preVisit(ASTNode node) {
ASTNode parent = node.getParent();
if (node instanceof BodyDeclaration && parent instanceof BodyDeclaration) {
List<Comment> comments = commentsMapper.extractNodeComments(node);
if (comments.isEmpty())
return;
for (Comment comment : comments) {
comment.accept(this);
appendBodyDeclaration(this.comment);
}
}
}
Aggregations