Search in sources :

Example 66 with ASTNode

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;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) AST(org.eclipse.jdt.core.dom.AST) TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Type(org.eclipse.jdt.core.dom.Type) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) TextElement(org.eclipse.jdt.core.dom.TextElement) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TagElement(org.eclipse.jdt.core.dom.TagElement) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 67 with ASTNode

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;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTParser(org.eclipse.jdt.core.dom.ASTParser) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 68 with ASTNode

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;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTParser(org.eclipse.jdt.core.dom.ASTParser)

Example 69 with ASTNode

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()]);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList) IProblem(org.eclipse.jdt.core.compiler.IProblem)

Example 70 with ASTNode

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()]);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Message(org.eclipse.jdt.core.dom.Message) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList)

Aggregations

ASTNode (org.eclipse.jdt.core.dom.ASTNode)432 SimpleName (org.eclipse.jdt.core.dom.SimpleName)99 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)98 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)90 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)85 Expression (org.eclipse.jdt.core.dom.Expression)81 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)73 AST (org.eclipse.jdt.core.dom.AST)70 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)69 ArrayList (java.util.ArrayList)62 Type (org.eclipse.jdt.core.dom.Type)62 Block (org.eclipse.jdt.core.dom.Block)61 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)49 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)47 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)45 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)44 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)44 CastExpression (org.eclipse.jdt.core.dom.CastExpression)42 IBinding (org.eclipse.jdt.core.dom.IBinding)41 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)37