use of org.eclipse.jdt.core.dom.ASTNode 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.ASTNode in project flux by eclipse.
the class ASTNodeFactory method newTypeParameter.
public static TypeParameter newTypeParameter(AST ast, String content) {
StringBuffer buffer = new StringBuffer(TYPEPARAM_HEADER);
buffer.append(content);
buffer.append(TYPEPARAM_FOOTER);
ASTParser p = ASTParser.newParser(ast.apiLevel());
p.setSource(buffer.toString().toCharArray());
CompilationUnit root = (CompilationUnit) p.createAST(null);
List<AbstractTypeDeclaration> list = root.types();
TypeDeclaration typeDecl = (TypeDeclaration) list.get(0);
MethodDeclaration methodDecl = typeDecl.getMethods()[0];
TypeParameter tp = (TypeParameter) methodDecl.typeParameters().get(0);
ASTNode result = ASTNode.copySubtree(ast, tp);
result.accept(new PositionClearer());
return (TypeParameter) result;
}
use of org.eclipse.jdt.core.dom.ASTNode in project flux by eclipse.
the class ASTNodeFactory method newStatement.
public static ASTNode newStatement(AST ast, String content) {
StringBuffer buffer = new StringBuffer(STATEMENT_HEADER);
buffer.append(content);
buffer.append(STATEMENT_FOOTER);
ASTParser p = ASTParser.newParser(ast.apiLevel());
p.setSource(buffer.toString().toCharArray());
CompilationUnit root = (CompilationUnit) p.createAST(null);
ASTNode result = ASTNode.copySubtree(ast, NodeFinder.perform(root, STATEMENT_HEADER.length(), content.length()));
result.accept(new PositionClearer());
return result;
}
use of org.eclipse.jdt.core.dom.ASTNode in project flux by eclipse.
the class ASTNodes method getProblems.
public static IProblem[] getProblems(ASTNode node, int scope, int severity) {
ASTNode root = node.getRoot();
if (!(root instanceof CompilationUnit))
return EMPTY_PROBLEMS;
IProblem[] problems = ((CompilationUnit) root).getProblems();
if (root == node)
return problems;
final int iterations = computeIterations(scope);
List<IProblem> result = new ArrayList<IProblem>(5);
for (int i = 0; i < problems.length; i++) {
IProblem problem = problems[i];
boolean consider = false;
if ((severity & PROBLEMS) == PROBLEMS)
consider = true;
else if ((severity & WARNING) != 0)
consider = problem.isWarning();
else if ((severity & ERROR) != 0)
consider = problem.isError();
if (consider) {
ASTNode temp = node;
int count = iterations;
do {
int nodeOffset = temp.getStartPosition();
int problemOffset = problem.getSourceStart();
if (nodeOffset <= problemOffset && problemOffset < nodeOffset + temp.getLength()) {
result.add(problem);
count = 0;
} else {
count--;
}
} while ((temp = temp.getParent()) != null && count > 0);
}
}
return result.toArray(new IProblem[result.size()]);
}
use of org.eclipse.jdt.core.dom.ASTNode in project flux by eclipse.
the class ASTNodes method getMessages.
public static Message[] getMessages(ASTNode node, int flags) {
ASTNode root = node.getRoot();
if (!(root instanceof CompilationUnit))
return EMPTY_MESSAGES;
Message[] messages = ((CompilationUnit) root).getMessages();
if (root == node)
return messages;
final int iterations = computeIterations(flags);
List<Message> result = new ArrayList<Message>(5);
for (int i = 0; i < messages.length; i++) {
Message message = messages[i];
ASTNode temp = node;
int count = iterations;
do {
int nodeOffset = temp.getStartPosition();
int messageOffset = message.getStartPosition();
if (nodeOffset <= messageOffset && messageOffset < nodeOffset + temp.getLength()) {
result.add(message);
count = 0;
} else {
count--;
}
} while ((temp = temp.getParent()) != null && count > 0);
}
return result.toArray(new Message[result.size()]);
}
Aggregations