use of org.eclipse.jdt.core.dom.TextElement 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.TextElement in project che by eclipse.
the class ChangeMethodSignatureProposal method insertTabStop.
private void insertTabStop(ASTRewrite rewriter, List<ASTNode> fragments, String linkedName) {
TextElement textElement = rewriter.getAST().newTextElement();
//$NON-NLS-1$
textElement.setText("");
fragments.add(textElement);
addLinkedPosition(rewriter.track(textElement), false, linkedName);
}
use of org.eclipse.jdt.core.dom.TextElement 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.TextElement in project che by eclipse.
the class JavadocContentAccess2 method handleContentElements.
private void handleContentElements(List<? extends ASTNode> nodes, boolean skipLeadingWhitespace) {
ASTNode previousNode = null;
for (Iterator<? extends ASTNode> iter = nodes.iterator(); iter.hasNext(); ) {
ASTNode child = iter.next();
if (previousNode != null) {
int previousEnd = previousNode.getStartPosition() + previousNode.getLength();
int childStart = child.getStartPosition();
if (previousEnd > childStart) {
// should never happen, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=304826
Exception exception = new Exception(//$NON-NLS-1$
"Illegal ASTNode positions: previousEnd=" + previousEnd + ", childStart=" + //$NON-NLS-1$
childStart + ", element=" + //$NON-NLS-1$
fElement.getHandleIdentifier() + ", Javadoc:\n" + //$NON-NLS-1$
fSource);
LOG.error(exception.getMessage(), exception);
} else if (previousEnd != childStart) {
// Need to preserve whitespace before a node that's not
// directly following the previous node (e.g. on a new line)
// due to https://bugs.eclipse.org/bugs/show_bug.cgi?id=206518 :
String textWithStars = fSource.substring(previousEnd, childStart);
String text = removeDocLineIntros(textWithStars);
fBuf.append(text);
}
}
previousNode = child;
if (child instanceof TextElement) {
String text = ((TextElement) child).getText();
if (skipLeadingWhitespace) {
//$NON-NLS-1$ //$NON-NLS-2$
text = text.replaceFirst("^\\s+", "");
}
// workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=233481 :
//$NON-NLS-1$ //$NON-NLS-2$
text = text.replaceAll("(\r\n?|\n)([ \t]*\\*)", "$1");
handleText(text);
} else if (child instanceof TagElement) {
handleInlineTagElement((TagElement) child);
} else {
// This is unexpected. Fail gracefully by just copying the source.
int start = child.getStartPosition();
String text = fSource.substring(start, start + child.getLength());
fBuf.append(removeDocLineIntros(text));
}
}
}
use of org.eclipse.jdt.core.dom.TextElement in project che by eclipse.
the class DelegateCreator method getDelegateJavadocTag.
// ******************* INTERNAL HELPERS ***************************
private TagElement getDelegateJavadocTag(BodyDeclaration declaration) throws JavaModelException {
Assert.isNotNull(declaration);
String msg = RefactoringCoreMessages.DelegateCreator_use_member_instead;
//$NON-NLS-1$
int firstParam = msg.indexOf("{0}");
Assert.isTrue(firstParam != -1);
List<ASTNode> fragments = new ArrayList<ASTNode>();
TextElement text = getAst().newTextElement();
text.setText(msg.substring(0, firstParam).trim());
fragments.add(text);
fragments.add(createJavadocMemberReferenceTag(declaration, getAst()));
text = getAst().newTextElement();
text.setText(msg.substring(firstParam + 3).trim());
fragments.add(text);
final TagElement tag = getAst().newTagElement();
tag.setTagName(TagElement.TAG_DEPRECATED);
tag.fragments().addAll(fragments);
return tag;
}
Aggregations