Search in sources :

Example 36 with ASTNode

use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.

the class UnresolvedElementsSubProcessor method addMissingCastParentsProposal.

private static void addMissingCastParentsProposal(ICompilationUnit cu, MethodInvocation invocationNode, Collection<ICommandAccess> proposals) {
    Expression sender = invocationNode.getExpression();
    if (sender instanceof ThisExpression) {
        return;
    }
    ITypeBinding senderBinding = sender.resolveTypeBinding();
    if (senderBinding == null || Modifier.isFinal(senderBinding.getModifiers())) {
        return;
    }
    if (sender instanceof Name && ((Name) sender).resolveBinding() instanceof ITypeBinding) {
        // static access
        return;
    }
    ASTNode parent = invocationNode.getParent();
    while (parent instanceof Expression && parent.getNodeType() != ASTNode.CAST_EXPRESSION) {
        parent = parent.getParent();
    }
    boolean hasCastProposal = false;
    if (parent instanceof CastExpression) {
        //	(TestCase) x.getName() -> ((TestCase) x).getName
        hasCastProposal = useExistingParentCastProposal(cu, (CastExpression) parent, sender, invocationNode.getName(), getArgumentTypes(invocationNode.arguments()), proposals);
    }
    if (!hasCastProposal) {
        // x.getName() -> ((TestCase) x).getName
        Expression target = sender;
        while (target instanceof ParenthesizedExpression) {
            target = ((ParenthesizedExpression) target).getExpression();
        }
        String label;
        if (target.getNodeType() != ASTNode.CAST_EXPRESSION) {
            String targetName = null;
            if (target.getLength() <= 18) {
                targetName = ASTNodes.asString(target);
            }
            if (targetName == null) {
                label = CorrectionMessages.UnresolvedElementsSubProcessor_methodtargetcast_description;
            } else {
                label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_methodtargetcast2_description, BasicElementLabels.getJavaCodeString(targetName));
            }
        } else {
            String targetName = null;
            if (target.getLength() <= 18) {
                targetName = ASTNodes.asString(((CastExpression) target).getExpression());
            }
            if (targetName == null) {
                label = CorrectionMessages.UnresolvedElementsSubProcessor_changemethodtargetcast_description;
            } else {
                label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changemethodtargetcast2_description, BasicElementLabels.getJavaCodeString(targetName));
            }
        }
        proposals.add(new CastCorrectionProposal(label, cu, target, (ITypeBinding) null, IProposalRelevance.CHANGE_CAST));
    }
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) CastExpression(org.eclipse.jdt.core.dom.CastExpression) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name) CastCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.CastCorrectionProposal)

Example 37 with ASTNode

use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.

the class VarargsWarningsSubProcessor method addRemoveSafeVarargsProposals.

public static void addRemoveSafeVarargsProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ASTNode coveringNode = problem.getCoveringNode(context.getASTRoot());
    if (!(coveringNode instanceof MethodDeclaration))
        return;
    MethodDeclaration methodDeclaration = (MethodDeclaration) coveringNode;
    MarkerAnnotation annotation = null;
    List<? extends ASTNode> modifiers = methodDeclaration.modifiers();
    for (Iterator<? extends ASTNode> iterator = modifiers.iterator(); iterator.hasNext(); ) {
        ASTNode node = iterator.next();
        if (node instanceof MarkerAnnotation) {
            annotation = (MarkerAnnotation) node;
            if ("SafeVarargs".equals(annotation.resolveAnnotationBinding().getName())) {
                //$NON-NLS-1$
                break;
            }
        }
    }
    if (annotation == null)
        return;
    ASTRewrite rewrite = ASTRewrite.create(coveringNode.getAST());
    rewrite.remove(annotation, null);
    String label = CorrectionMessages.VarargsWarningsSubProcessor_remove_safevarargs_label;
    //JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE);
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_TOOL_DELETE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_SAFEVARARGS, image);
    proposals.add(proposal);
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) MarkerAnnotation(org.eclipse.jdt.core.dom.MarkerAnnotation) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Image(org.eclipse.swt.graphics.Image)

Example 38 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 39 with ASTNode

use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.

the class JavadocContentAccess2 method handleInheritDoc.

/**
     * Handle {&#64;inheritDoc}.
     *
     * @param node
     *         the node
     * @return <code>true</code> iff the node was an {&#64;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;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) TagElement(org.eclipse.jdt.core.dom.TagElement) List(java.util.List) ArrayList(java.util.ArrayList) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name)

Example 40 with ASTNode

use of org.eclipse.jdt.core.dom.ASTNode in project che by eclipse.

the class JavadocContentAccess2 method toHTML.

private String toHTML() {
    fBuf = new StringBuffer();
    fLiteralContent = 0;
    // After first loop, non-null entries in the following two lists are missing and need to be inherited:
    List<String> parameterNames = initParameterNames();
    List<String> exceptionNames = initExceptionNames();
    TagElement deprecatedTag = null;
    TagElement start = null;
    List<TagElement> parameters = new ArrayList<TagElement>();
    TagElement returnTag = null;
    List<TagElement> exceptions = new ArrayList<TagElement>();
    List<TagElement> versions = new ArrayList<TagElement>();
    List<TagElement> authors = new ArrayList<TagElement>();
    List<TagElement> sees = new ArrayList<TagElement>();
    List<TagElement> since = new ArrayList<TagElement>();
    List<TagElement> rest = new ArrayList<TagElement>();
    List<TagElement> tags = fJavadoc.tags();
    for (Iterator<TagElement> iter = tags.iterator(); iter.hasNext(); ) {
        TagElement tag = iter.next();
        String tagName = tag.getTagName();
        if (tagName == null) {
            start = tag;
        } else if (TagElement.TAG_PARAM.equals(tagName)) {
            parameters.add(tag);
            List<? extends ASTNode> fragments = tag.fragments();
            if (fragments.size() > 0) {
                Object first = fragments.get(0);
                if (first instanceof SimpleName) {
                    String name = ((SimpleName) first).getIdentifier();
                    int paramIndex = parameterNames.indexOf(name);
                    if (paramIndex != -1) {
                        parameterNames.set(paramIndex, null);
                    }
                }
            }
        } else if (TagElement.TAG_RETURN.equals(tagName)) {
            if (returnTag == null)
                // the Javadoc tool only shows the first return tag
                returnTag = tag;
        } else if (TagElement.TAG_EXCEPTION.equals(tagName) || TagElement.TAG_THROWS.equals(tagName)) {
            exceptions.add(tag);
            List<? extends ASTNode> fragments = tag.fragments();
            if (fragments.size() > 0) {
                Object first = fragments.get(0);
                if (first instanceof Name) {
                    String name = ASTNodes.getSimpleNameIdentifier((Name) first);
                    int exceptionIndex = exceptionNames.indexOf(name);
                    if (exceptionIndex != -1) {
                        exceptionNames.set(exceptionIndex, null);
                    }
                }
            }
        } else if (TagElement.TAG_SINCE.equals(tagName)) {
            since.add(tag);
        } else if (TagElement.TAG_VERSION.equals(tagName)) {
            versions.add(tag);
        } else if (TagElement.TAG_AUTHOR.equals(tagName)) {
            authors.add(tag);
        } else if (TagElement.TAG_SEE.equals(tagName)) {
            sees.add(tag);
        } else if (TagElement.TAG_DEPRECATED.equals(tagName)) {
            if (deprecatedTag == null)
                // the Javadoc tool only shows the first deprecated tag
                deprecatedTag = tag;
        } else {
            rest.add(tag);
        }
    }
    //TODO: @Documented annotations before header
    if (deprecatedTag != null)
        handleDeprecatedTag(deprecatedTag);
    if (start != null)
        handleContentElements(start.fragments());
    else if (fMethod != null) {
        CharSequence inherited = fJavadocLookup.getInheritedMainDescription(fMethod);
        // The Javadoc tool adds "Description copied from class: ..." (only for the main description).
        // We don't bother doing that.
        handleInherited(inherited);
    }
    CharSequence[] parameterDescriptions = new CharSequence[parameterNames.size()];
    boolean hasInheritedParameters = inheritParameterDescriptions(parameterNames, parameterDescriptions);
    boolean hasParameters = parameters.size() > 0 || hasInheritedParameters;
    CharSequence returnDescription = null;
    if (returnTag == null && needsReturnTag())
        returnDescription = fJavadocLookup.getInheritedReturnDescription(fMethod);
    boolean hasReturnTag = returnTag != null || returnDescription != null;
    CharSequence[] exceptionDescriptions = new CharSequence[exceptionNames.size()];
    boolean hasInheritedExceptions = inheritExceptionDescriptions(exceptionNames, exceptionDescriptions);
    boolean hasExceptions = exceptions.size() > 0 || hasInheritedExceptions;
    if (hasParameters || hasReturnTag || hasExceptions || versions.size() > 0 || authors.size() > 0 || since.size() > 0 || sees.size() > 0 || rest.size() > 0 || (fBuf.length() > 0 && (parameterDescriptions.length > 0 || exceptionDescriptions.length > 0))) {
        handleSuperMethodReferences();
        fBuf.append(BLOCK_TAG_START);
        handleParameterTags(parameters, parameterNames, parameterDescriptions);
        handleReturnTag(returnTag, returnDescription);
        handleExceptionTags(exceptions, exceptionNames, exceptionDescriptions);
        handleBlockTags(JavaDocMessages.JavaDoc2HTMLTextReader_since_section, since);
        handleBlockTags(JavaDocMessages.JavaDoc2HTMLTextReader_version_section, versions);
        handleBlockTags(JavaDocMessages.JavaDoc2HTMLTextReader_author_section, authors);
        handleBlockTags(JavaDocMessages.JavaDoc2HTMLTextReader_see_section, sees);
        handleBlockTags(rest);
        fBuf.append(BLOCK_TAG_END);
    } else if (fBuf.length() > 0) {
        handleSuperMethodReferences();
    }
    String result = fBuf.toString();
    fBuf = null;
    return result;
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) ASTNode(org.eclipse.jdt.core.dom.ASTNode) TagElement(org.eclipse.jdt.core.dom.TagElement) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

ASTNode (org.eclipse.jdt.core.dom.ASTNode)710 SimpleName (org.eclipse.jdt.core.dom.SimpleName)151 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)148 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)147 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)134 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)126 Expression (org.eclipse.jdt.core.dom.Expression)125 ArrayList (java.util.ArrayList)111 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)108 AST (org.eclipse.jdt.core.dom.AST)106 Type (org.eclipse.jdt.core.dom.Type)92 Block (org.eclipse.jdt.core.dom.Block)89 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)77 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)73 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)67 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)66 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)65 CastExpression (org.eclipse.jdt.core.dom.CastExpression)64 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)62 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)60