Search in sources :

Example 76 with ASTNode

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

the class ModifierRewrite method internalSetModifiers.

/**
	 * Sets the given modifiers and removes all other modifiers that match the consideredFlags mask.
	 * Does not touch other flags and leaves annotations in place.
	 * 
	 * @param modifiers the modifiers to set
	 * @param consideredFlags mask of modifiers to consider
	 * @param editGroup the edit group in which to collect the corresponding text edits, or
	 *            <code>null</code> if ungrouped
	 * @return a tracked position that contains the changed modifiers
	 */
private PositionInformation internalSetModifiers(int modifiers, int consideredFlags, TextEditGroup editGroup) {
    int newModifiers = modifiers & consideredFlags;
    ITrackedNodePosition trackedFallback = null;
    List<ITrackedNodePosition> trackedNodes = new ArrayList<ITrackedNodePosition>();
    // remove modifiers
    List<IExtendedModifier> originalList = fModifierRewrite.getOriginalList();
    for (int i = 0; i < originalList.size(); i++) {
        ASTNode curr = (ASTNode) originalList.get(i);
        if (curr instanceof Modifier) {
            int flag = ((Modifier) curr).getKeyword().toFlagValue();
            if ((consideredFlags & flag) != 0) {
                if ((newModifiers & flag) == 0) {
                    fModifierRewrite.remove(curr, editGroup);
                    if (trackedFallback == null)
                        trackedFallback = fModifierRewrite.getASTRewrite().track(curr);
                }
                newModifiers &= ~flag;
            }
        }
    }
    // find last annotation
    IExtendedModifier lastAnnotation = null;
    List<IExtendedModifier> extendedList = fModifierRewrite.getRewrittenList();
    for (int i = 0; i < extendedList.size(); i++) {
        IExtendedModifier curr = extendedList.get(i);
        if (curr.isAnnotation())
            lastAnnotation = curr;
    }
    // add modifiers
    List<Modifier> newNodes = ASTNodeFactory.newModifiers(fAst, newModifiers);
    for (int i = 0; i < newNodes.size(); i++) {
        Modifier curr = newNodes.get(i);
        if ((curr.getKeyword().toFlagValue() & VISIBILITY_MODIFIERS) != 0) {
            if (lastAnnotation != null)
                fModifierRewrite.insertAfter(curr, (ASTNode) lastAnnotation, editGroup);
            else
                fModifierRewrite.insertFirst(curr, editGroup);
        } else {
            fModifierRewrite.insertLast(curr, editGroup);
        }
        trackedNodes.add(fModifierRewrite.getASTRewrite().track(curr));
    }
    if (trackedNodes.isEmpty()) {
        if (trackedFallback == null) {
            // out of tricks...
            trackedFallback = fModifierRewrite.getASTRewrite().track(fModifierRewrite.getParent());
        }
        return new LinkedProposalPositionGroup.StartPositionInformation(trackedFallback);
    } else {
        return new LinkedProposalPositionGroup.TrackedNodesPosition(trackedNodes);
    }
}
Also used : ArrayList(java.util.ArrayList) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ITrackedNodePosition(org.eclipse.jdt.core.dom.rewrite.ITrackedNodePosition) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier) Modifier(org.eclipse.jdt.core.dom.Modifier) IExtendedModifier(org.eclipse.jdt.core.dom.IExtendedModifier)

Example 77 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 78 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)

Example 79 with ASTNode

use of org.eclipse.jdt.core.dom.ASTNode 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 80 with ASTNode

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

the class SemanticHighlightings method getBinding.

/**
     * Extracts the binding from the token's simple name.
     * Works around bug 62605 to return the correct constructor binding in a ClassInstanceCreation.
     *
     * @param token
     *         the token to extract the binding from
     * @return the token's binding, or <code>null</code>
     */
private static IBinding getBinding(SemanticToken token) {
    ASTNode node = token.getNode();
    ASTNode normalized = ASTNodes.getNormalizedNode(node);
    if (normalized.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
        // work around: https://bugs.eclipse.org/bugs/show_bug.cgi?id=62605
        return ((ClassInstanceCreation) normalized.getParent()).resolveConstructorBinding();
    }
    return token.getBinding();
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

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