Search in sources :

Example 1 with TextElement

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));
            }
        }
    }
}
Also used : ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) Javadoc(org.eclipse.jdt.core.dom.Javadoc) Image(org.eclipse.swt.graphics.Image) TextElement(org.eclipse.jdt.core.dom.TextElement) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TagElement(org.eclipse.jdt.core.dom.TagElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) AnnotationTypeDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) ReplaceCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.ReplaceCorrectionProposal)

Example 2 with TextElement

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

Example 3 with TextElement

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;
}
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 4 with TextElement

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));
        }
    }
}
Also used : TextElement(org.eclipse.jdt.core.dom.TextElement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) TagElement(org.eclipse.jdt.core.dom.TagElement) URISyntaxException(java.net.URISyntaxException) CoreException(org.eclipse.core.runtime.CoreException) JavaModelException(org.eclipse.jdt.core.JavaModelException) IOException(java.io.IOException)

Example 5 with TextElement

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

Aggregations

TextElement (org.eclipse.jdt.core.dom.TextElement)9 TagElement (org.eclipse.jdt.core.dom.TagElement)8 ASTNode (org.eclipse.jdt.core.dom.ASTNode)6 AST (org.eclipse.jdt.core.dom.AST)5 Javadoc (org.eclipse.jdt.core.dom.Javadoc)5 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)5 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)5 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)5 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)5 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)4 Type (org.eclipse.jdt.core.dom.Type)4 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)4 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)3 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)3 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)3 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 SimpleName (org.eclipse.jdt.core.dom.SimpleName)2 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)2