use of org.eclipse.jdt.core.dom.TagElement 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.TagElement in project che by eclipse.
the class ReturnTypeSubProcessor method addMissingReturnTypeProposals.
public static void addMissingReturnTypeProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> 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));
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.MISSING_RETURN_TYPE, image);
ImportRewrite imports = proposal.createImportRewrite(astRoot);
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(decl, imports);
Type type = imports.addImport(typeBinding, ast, importRewriteContext);
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.TagElement in project che by eclipse.
the class ChangeMethodSignatureProposal method modifyExceptions.
private void modifyExceptions(ASTRewrite rewrite, MethodDeclaration methodDecl) {
AST ast = methodDecl.getAST();
ImportRewrite imports = getImportRewrite();
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(methodDecl, imports);
ListRewrite listRewrite = rewrite.getListRewrite(methodDecl, MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY);
// old exceptions
List<Type> exceptions = methodDecl.thrownExceptionTypes();
// index over the old exceptions
int k = 0;
for (int i = 0; i < fExceptionChanges.length; i++) {
ChangeDescription curr = fExceptionChanges[i];
if (curr == null) {
k++;
} else if (curr instanceof InsertDescription) {
InsertDescription desc = (InsertDescription) curr;
String type = imports.addImport(desc.type, context);
ASTNode newNode = imports.addImport(desc.type, ast, context);
listRewrite.insertAt(newNode, i, null);
String key = getExceptionTypeGroupId(i);
addLinkedPosition(rewrite.track(newNode), false, key);
Javadoc javadoc = methodDecl.getJavadoc();
if (javadoc != null && JavadocTagsSubProcessor.findThrowsTag(javadoc, type) == null) {
TagElement newTagElement = ast.newTagElement();
newTagElement.setTagName(TagElement.TAG_THROWS);
ASTNode newRef = ASTNodeFactory.newName(ast, type);
newTagElement.fragments().add(newRef);
//$NON-NLS-1$
insertTabStop(rewrite, newTagElement.fragments(), "throws_tagcomment" + i);
insertThrowsTag(rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY), exceptions, k, newTagElement);
addLinkedPosition(rewrite.track(newRef), false, key);
}
} else if (curr instanceof RemoveDescription) {
Type node = exceptions.get(k);
listRewrite.remove(node, null);
k++;
TagElement tagNode = findThrowsTag(methodDecl, node);
if (tagNode != null) {
rewrite.remove(tagNode, null);
}
} else if (curr instanceof EditDescription) {
EditDescription desc = (EditDescription) curr;
Type oldNode = exceptions.get(k);
String type = imports.addImport(desc.type, context);
ASTNode newNode = imports.addImport(desc.type, ast, context);
listRewrite.replace(oldNode, newNode, null);
String key = getExceptionTypeGroupId(i);
addLinkedPosition(rewrite.track(newNode), false, key);
k++;
TagElement tagNode = findThrowsTag(methodDecl, oldNode);
if (tagNode != null) {
ASTNode newRef = ASTNodeFactory.newType(ast, type);
rewrite.replace((ASTNode) tagNode.fragments().get(0), newRef, null);
addLinkedPosition(rewrite.track(newRef), false, key);
}
} else if (curr instanceof SwapDescription) {
Type decl1 = exceptions.get(k);
Type decl2 = exceptions.get(((SwapDescription) curr).index);
rewrite.replace(decl1, rewrite.createCopyTarget(decl2), null);
rewrite.replace(decl2, rewrite.createCopyTarget(decl1), null);
k++;
TagElement tagNode1 = findThrowsTag(methodDecl, decl1);
TagElement tagNode2 = findThrowsTag(methodDecl, decl2);
if (tagNode1 != null && tagNode2 != null) {
rewrite.replace(tagNode1, rewrite.createCopyTarget(tagNode2), null);
rewrite.replace(tagNode2, rewrite.createCopyTarget(tagNode1), null);
}
}
}
}
use of org.eclipse.jdt.core.dom.TagElement in project che by eclipse.
the class AddTypeParameterProposal method getRewrite.
@Override
protected ASTRewrite getRewrite() throws CoreException {
ASTNode boundNode = fAstRoot.findDeclaringNode(fBinding);
ASTNode declNode = null;
if (boundNode != null) {
// is same CU
declNode = boundNode;
createImportRewrite(fAstRoot);
} else {
CompilationUnit newRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
declNode = newRoot.findDeclaringNode(fBinding.getKey());
createImportRewrite(newRoot);
}
AST ast = declNode.getAST();
TypeParameter newTypeParam = ast.newTypeParameter();
newTypeParam.setName(ast.newSimpleName(fTypeParamName));
if (fBounds != null && fBounds.length > 0) {
List<Type> typeBounds = newTypeParam.typeBounds();
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(declNode, getImportRewrite());
for (int i = 0; i < fBounds.length; i++) {
Type newBound = getImportRewrite().addImport(fBounds[i], ast, importRewriteContext);
typeBounds.add(newBound);
}
}
ASTRewrite rewrite = ASTRewrite.create(ast);
ListRewrite listRewrite;
Javadoc javadoc;
List<TypeParameter> otherTypeParams;
if (declNode instanceof TypeDeclaration) {
TypeDeclaration declaration = (TypeDeclaration) declNode;
listRewrite = rewrite.getListRewrite(declaration, TypeDeclaration.TYPE_PARAMETERS_PROPERTY);
otherTypeParams = declaration.typeParameters();
javadoc = declaration.getJavadoc();
} else {
MethodDeclaration declaration = (MethodDeclaration) declNode;
listRewrite = rewrite.getListRewrite(declNode, MethodDeclaration.TYPE_PARAMETERS_PROPERTY);
otherTypeParams = declaration.typeParameters();
javadoc = declaration.getJavadoc();
}
listRewrite.insertLast(newTypeParam, null);
if (javadoc != null && otherTypeParams != null) {
ListRewrite tagsRewriter = rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY);
Set<String> previousNames = JavadocTagsSubProcessor.getPreviousTypeParamNames(otherTypeParams, null);
String name = '<' + fTypeParamName + '>';
TagElement newTag = ast.newTagElement();
newTag.setTagName(TagElement.TAG_PARAM);
TextElement text = ast.newTextElement();
text.setText(name);
newTag.fragments().add(text);
JavadocTagsSubProcessor.insertTag(tagsRewriter, newTag, previousNames);
}
return rewrite;
}
use of org.eclipse.jdt.core.dom.TagElement in project che by eclipse.
the class JavadocContentAccess2 method handleInheritDoc.
/**
* Handle {@inheritDoc}.
*
* @param node
* the node
* @return <code>true</code> iff the node was an {@inheritDoc} node and has been handled
*/
private boolean handleInheritDoc(TagElement node) {
if (!TagElement.TAG_INHERITDOC.equals(node.getTagName()))
return false;
try {
if (fMethod == null)
return false;
TagElement blockTag = (TagElement) node.getParent();
String blockTagName = blockTag.getTagName();
if (blockTagName == null) {
CharSequence inherited = fJavadocLookup.getInheritedMainDescription(fMethod);
return handleInherited(inherited);
} else if (TagElement.TAG_PARAM.equals(blockTagName)) {
List<? extends ASTNode> fragments = blockTag.fragments();
if (fragments.size() > 0) {
Object first = fragments.get(0);
if (first instanceof SimpleName) {
String name = ((SimpleName) first).getIdentifier();
String[] parameterNames = fMethod.getParameterNames();
for (int i = 0; i < parameterNames.length; i++) {
if (name.equals(parameterNames[i])) {
CharSequence inherited = fJavadocLookup.getInheritedParamDescription(fMethod, i);
return handleInherited(inherited);
}
}
}
}
} else if (TagElement.TAG_RETURN.equals(blockTagName)) {
CharSequence inherited = fJavadocLookup.getInheritedReturnDescription(fMethod);
return handleInherited(inherited);
} else if (TagElement.TAG_THROWS.equals(blockTagName) || TagElement.TAG_EXCEPTION.equals(blockTagName)) {
List<? extends ASTNode> fragments = blockTag.fragments();
if (fragments.size() > 0) {
Object first = fragments.get(0);
if (first instanceof Name) {
String name = ASTNodes.getSimpleNameIdentifier((Name) first);
CharSequence inherited = fJavadocLookup.getInheritedExceptionDescription(fMethod, name);
return handleInherited(inherited);
}
}
}
} catch (JavaModelException e) {
LOG.error(e.getMessage(), e);
}
return false;
}
Aggregations