Example usage for org.eclipse.jdt.core.dom Javadoc getAST

List of usage examples for org.eclipse.jdt.core.dom Javadoc getAST

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom Javadoc getAST.

Prototype

public final AST getAST() 

Source Link

Document

Returns this node's AST.

Usage

From source file:com.crispico.flower.mp.codesync.code.java.adapter.JavaAbstractAstNodeModelAdapter.java

License:Open Source License

@Override
protected void updateUID(Object element, Object correspondingElement) {
    if (element instanceof BodyDeclaration) {
        BodyDeclaration node = (BodyDeclaration) element;
        Javadoc javadoc = node.getJavadoc();
        // if it doesn't have any doc, create it
        if (javadoc == null) {
            javadoc = node.getAST().newJavadoc();
            node.setJavadoc(javadoc);//from w ww.  ja va2 s. c  o m
        }
        // first remove the existing flower tag, this way we also make sure that it's the last tag
        // note: if we only change the id, the rewriter won't format it correctly
        for (Object obj : javadoc.tags()) {
            if (FLOWER_UID.equals(((TagElement) obj).getTagName())) {
                javadoc.tags().remove(obj);
                break;
            }
        }
        // create new tag element for UID
        TagElement tag = javadoc.getAST().newTagElement();
        tag.setTagName(FLOWER_UID);
        javadoc.tags().add(tag);
        TextElement text = javadoc.getAST().newTextElement();
        tag.fragments().add(text);
        EObject eObject = (EObject) correspondingElement;
        text.setText(eObject.eResource().getURIFragment(eObject));
        System.out.println(javadoc);
    }
}

From source file:com.google.currysrc.api.process.JavadocUtils.java

License:Apache License

public static void addJavadocTag(ASTRewrite rewrite, Javadoc javadoc, String tagText) {
    AST ast = javadoc.getAST();
    TagElement tagElement = AstNodes.createTextTagElement(ast, tagText);
    ListRewrite listRewrite = rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY);
    listRewrite.insertLast(tagElement, null /* editGroup */);
}

From source file:de.akra.idocit.java.services.JavadocGenerator.java

License:Apache License

/**
 * Append the information out of <code>documentations</code> to the {@link Javadoc}
 * block comment. If <code>tagName != null</code> the documentations are added to a
 * new {@link TagElement} with that name. Add first the general description text with
 * <code>tagName == null</code>. After that all other wished tags.
 * /*from  w  ww.  j a  va 2s  .co  m*/
 * @param documentations
 *            The list of {@link Documentation}s which should be converted to
 *            {@link Javadoc}.
 * @param tagName
 *            The name of the tag element, or <code>null</code> (@see
 *            {@link TagElement#setTagName(String)} ).
 * @param paramName
 *            The name of the described parameter, or <code>null</code> if no name is
 *            needed.
 * @param javadoc
 *            The {@link Javadoc} reference to which the {@link TagElement}s should be
 *            added.
 */
@Override
public void appendDocsToJavadoc(List<Documentation> documentations, String tagName, String paramName,
        String thematicGridName, Javadoc javadoc, List<TagElement> additionalTagElements, JavaMethod method) {
    @SuppressWarnings("unchecked")
    List<TagElement> tags = javadoc.tags();
    AST jdocAST = javadoc.getAST();

    boolean tagChanged = false;

    TagElement newTag = jdocAST.newTagElement();

    if (tagName != null) {
        tagChanged = true;
        newTag.setTagName(tagName + de.akra.idocit.common.utils.StringUtils.EMPTY);
    }

    @SuppressWarnings("unchecked")
    List<ASTNode> fragments = newTag.fragments();

    if (paramName != null) {
        tagChanged = true;
        TextElement paramNameElement = jdocAST.newTextElement();
        paramNameElement.setText(paramName + de.akra.idocit.common.utils.StringUtils.EMPTY);
        fragments.add(paramNameElement);
    }

    final String tableStartTag = "<table name=\"" + IDOCIT_HTML_TABLE_NAME
            + "\" border=\"1\" cellspacing=\"0\">\n";

    for (Documentation doc : documentations) {
        // write only if there is something to write
        if (doc.getThematicRole() != null || !doc.getDocumentation().isEmpty()) {
            tagChanged = true;
            StringBuffer textElem = new StringBuffer();
            if (fragments.size() >= 1) {
                textElem.append("\n<br />");
            }
            textElem.append(tableStartTag);

            if (doc.getSignatureElementIdentifier() != null) {
                textElem.append("<tr><td>Element:</td><td>");
                textElem.append(quoteGenericsInIdentifier(doc.getSignatureElementIdentifier()));
                textElem.append("</td></tr>\n");
            }

            if (doc.getThematicRole() != null) {
                StringBuffer thematicRoleBuffer = new StringBuffer();
                thematicRoleBuffer.append(doc.getThematicRole().getName());

                if (doc.isErrorCase()) {
                    thematicRoleBuffer.append(JavadocUtils.getComplexErrorFlagPostfix());
                }

                textElem.append("<tr><td>Role:</td><td>");
                textElem.append(thematicRoleBuffer.toString());
                textElem.append("</td></tr>\n");
            }

            Map<Addressee, String> docMap = doc.getDocumentation();
            for (Addressee addressee : doc.getAddresseeSequence()) {
                String text = docMap.get(addressee);
                if (!text.isEmpty()) {
                    textElem.append("<tr><td><b>");
                    textElem.append(addressee.getName());
                    textElem.append("</b>:</td><td>");
                    textElem.append(StringUtils.escapeHtml(docMap.get(addressee)));
                    textElem.append("</td></tr>\n");
                }
            }
            textElem.append("</table>");

            TextElement textElement = jdocAST.newTextElement();
            textElement.setText(textElem.toString());
            fragments.add(textElement);
        }
    }
    if (tagChanged) {
        tags.add(newTag);
    }
}

From source file:de.akra.idocit.java.services.SimpleJavadocGenerator.java

License:Apache License

/**
 * Creates a Javadoc {@link TagElement} for each given documentation and adds it to
 * the given <code>javadoc</code>.
 * /*from  ww  w  .jav  a2 s.c o m*/
 * @param documentations
 *            [SOURCE]
 * @param referenceGridName
 *            [ATTRIBUTE] Used to determine the format of the {@link TagElement}
 * @param javadoc
 *            [DESTINATION]
 * @param method
 *            [ATTRIBUTE] Used to determine whether the new Javadoc-tags are attached
 *            to a method or a class.
 * 
 * @thematicgrid Putting Operations
 */
private void addTaglessJavadocs(final List<Documentation> documentations, final String referenceGridName,
        final Javadoc javadoc, final JavaMethod method) {
    boolean hasIntroductionSentence = containsAction(documentations)
            || containsClassDescription(documentations, method);

    @SuppressWarnings("unchecked")
    final List<TagElement> tags = javadoc.tags();

    for (int i = 0; i < documentations.size(); i++) {
        final Documentation documentation = documentations.get(i);

        final AST jdocAST = javadoc.getAST();
        final TagElement newTag = createTagElement(documentation, referenceGridName, jdocAST, method);

        @SuppressWarnings("unchecked")
        final List<ASTNode> fragments = newTag.fragments();
        final TextElement textElement = jdocAST.newTextElement();

        if (!hasIntroductionSentence) {
            String docText = getDocText(documentation).trim();

            if (newTag.getTagName() == null) {
                String formattedRoleName = getThematicRoleName(documentation);

                if (!StringUtils.EMPTY.equals(formattedRoleName.trim())) {
                    formattedRoleName = StringUtils.capitalizeFirstChar(formattedRoleName) + ':';
                }
                docText = formattedRoleName + StringUtils.SPACE + docText.trim();
            }

            textElement.setText(docText.trim());
            hasIntroductionSentence = true;
        } else {
            textElement.setText(getDocText(documentation).trim());
        }

        fragments.add(textElement);
        tags.add(newTag);
    }
}

From source file:de.akra.idocit.java.services.SimpleJavadocGenerator.java

License:Apache License

/**
 * @param javadoc//from   ww w .j a va 2s. com
 *            [FACTORY]
 * @return[OBJECT]
 * 
 * @thematicgrid Creating Operations
 */
private TagElement createaEmptyJavadocRow(final Javadoc javadoc) {
    final AST jdocAST = javadoc.getAST();
    final TagElement newTag = jdocAST.newTagElement();

    @SuppressWarnings("unchecked")
    final List<ASTNode> fragments = newTag.fragments();

    final TextElement textElement = jdocAST.newTextElement();
    textElement.setText(StringUtils.EMPTY);
    fragments.add(textElement);

    return newTag;
}

From source file:de.akra.idocit.java.services.SimpleJavadocGenerator.java

License:Apache License

/**
 * Creates Javadoc for the given parameter and its children.
 * // w w  w .java  2s .c  om
 * @param parameter
 *            [SOURCE]
 * @param javadoc
 *            [DESTINATION] This object is modified!
 * @param level
 *            Recursion level (starts with 0)
 * @param tagElementName
 *            [FORMAT] Name of the taglet ({@link TagElement#TAG_RETURN},
 *            {@link TagElement#TAG_PARAM} or {@link TagElement#TAG_THROWS})
 * @param subTagElementName
 *            [FORMAT] Name of the sub-taglet (subreturn, subparam or subexception)
 * 
 * @thematicgrid Creating Operations
 */
private void createParamJavadoc(final List<Documentation> documentations, final Javadoc javadoc,
        final String tagElementName, final String identifier) {
    @SuppressWarnings("unchecked")
    final List<TagElement> tags = javadoc.tags();
    final AST jdocAST = javadoc.getAST();

    if ((documentations != null) && !documentations.isEmpty()) {
        for (int i = 0; i < documentations.size(); i++) {
            final Documentation documentation = documentations.get(i);

            final TagElement newTag = jdocAST.newTagElement();
            newTag.setTagName(deriveTagElementName(tagElementName, i));

            @SuppressWarnings("unchecked")
            final List<ASTNode> fragments = newTag.fragments();
            final StringBuffer docText = new StringBuffer();

            if (!TagElement.TAG_RETURN.equals(tagElementName)) {
                final String paramIdentifier = extractIdentifier(documentation);
                docText.append(StringUtils.toString(paramIdentifier).trim());
                docText.append(StringUtils.SPACE);
            }

            final ThematicRole role = documentation.getThematicRole();
            if (role != null) {
                docText.append(StringUtils.inBrackets(role.getName()));
                docText.append(StringUtils.SPACE);
            }

            docText.append(getDocText(documentation));

            final TextElement identifierElement = jdocAST.newTextElement();
            identifierElement.setText(docText.toString().trim());

            fragments.add(identifierElement);
            tags.add(newTag);
        }
    } else if (JavadocUtils.isStandardJavadocTaglet(tagElementName)) {
        // @param, @return and @throws should mentioned even if no documentation has
        // been created for the parameter, return-type or exception!
        final TagElement newTag = jdocAST.newTagElement();
        newTag.setTagName(tagElementName);

        @SuppressWarnings("unchecked")
        final List<ASTNode> fragments = newTag.fragments();

        final TextElement identifierElement = jdocAST.newTextElement();
        identifierElement.setText(StringUtils.toString(identifier));

        fragments.add(identifierElement);
        tags.add(newTag);
    }
}

From source file:de.akra.idocit.java.services.SimpleJavadocGenerator.java

License:Apache License

/**
 * Creates Javadoc-representation for all {@link Documentation}s in
 * <code>signatureElement</code> and it's children.
 * //from  ww  w.  j  ava 2 s .c  o m
 * @format This implementation generates a simplified Javadoc which has a
 *         100%compliance with the Javadoc conventions.
 * @instrument To parse the Java and Javadoc code, the parser provided by the <a
 *             href="http://www.eclipse.org/jdt/">Eclipse Java Development Tools</a>
 *             is used.
 * 
 * @param documentations
 *            [OBJECT]
 * 
 * @param tagName
 *            [ATTRIBUTE] The of the Javadoc-tag of the given documentations (e.g.
 *            '@param'). This parameter is nullable.
 * 
 * @param paramName
 *            [ATTRIBUTE] Name of the corresponding Java-element. This parameter is
 *            nullable.
 * 
 * @param thematicGridName
 *            [ATTRIBUTE] Reference-grid of the given Java method. This parameter is
 *            nullable.
 * 
 * @param javadoc
 *            [DESTINATION]
 * 
 * @param additionalTagElements
 *            [ATTRIBUTE] Existing Javadoc tags which are kept and not changed by
 *            iDocIt!.
 * 
 * @param method
 *            [DESTINATION] The given Javadoc belongs to this method.
 * 
 * @throws ParsingException
 *             [NONE]
 * @thematicgrid Putting Operations
 */
@Override
public void appendDocsToJavadoc(final List<Documentation> documentations, final String tagName,
        final String paramName, final String thematicGridName, final Javadoc javadoc,
        final List<TagElement> additionalTagElements, final JavaMethod method) throws ParsingException {
    checkInvariant(documentations);

    if (tagName == null) { // ACTION or new tags for specific thematic roles.
        addTaglessJavadocs(documentations, thematicGridName, javadoc, method);
    } else {
        appendJavadoc(documentations, tagName, javadoc, paramName, additionalTagElements);
    }

    @SuppressWarnings("unchecked")
    final List<TagElement> tags = javadoc.tags();
    final List<TagElement> copiedTags = new ArrayList<TagElement>();
    copiedTags.addAll(tags);
    tags.clear();

    List<TagElement> mergedTags = mergeTagElements(copiedTags);
    try {
        tags.addAll(splitTextInToFragments(mergedTags, javadoc.getAST()));
        insertEmptyRows(javadoc);
    } catch (ParserConfigurationException e) {
        LOGGER.log(Level.WARNING,
                "ParserConfigurationException occured with "
                        + de.akra.idocit.java.utils.StringUtils.asStringSequence(documentations, tagName,
                                paramName, thematicGridName, javadoc, additionalTagElements, method),
                e);

        throw new ParsingException(
                "The internal XML-parser could not be inialized. It said: " + e.getLocalizedMessage());
    } catch (SAXException e) {
        LOGGER.log(Level.WARNING,
                "SAXException occured with "
                        + de.akra.idocit.java.utils.StringUtils.asStringSequence(documentations, tagName,
                                paramName, thematicGridName, javadoc, additionalTagElements, method),
                e);

        throw new ParsingException(
                "The internal XML-parser caused an error. Maybe it is not able to parse your documentation texts. It said: "
                        + e.getLocalizedMessage());
    } catch (IOException e) {
        LOGGER.log(Level.WARNING,
                "IOException occured with "
                        + de.akra.idocit.java.utils.StringUtils.asStringSequence(documentations, tagName,
                                paramName, thematicGridName, javadoc, additionalTagElements, method),
                e);

        throw new ParsingException(
                "The internal XML-parser could not read your Java-file. It said: " + e.getLocalizedMessage());
    }
}

From source file:org.eclim.plugin.jdt.command.doc.CommentCommand.java

License:Open Source License

/**
 * Comment a type declaration.//from   w  w  w  . j  a v  a  2  s  . com
 *
 * @param src The source file.
 * @param javadoc The Javadoc.
 * @param element The IJavaElement.
 * @param isNew true if there was no previous javadoc for this element.
 */
private void commentType(ICompilationUnit src, Javadoc javadoc, IJavaElement element, boolean isNew)
        throws Exception {
    if (element.getParent().getElementType() == IJavaElement.COMPILATION_UNIT) {
        @SuppressWarnings("unchecked")
        List<TagElement> tags = javadoc.tags();
        IProject project = element.getJavaProject().getProject();
        if (isNew) {
            addTag(javadoc, tags.size(), null, null);
            addTag(javadoc, tags.size(), null, null);
            addTag(javadoc, tags.size(), TagElement.TAG_AUTHOR, getAuthor(project));
            String version = getPreferences().getValue(project, "org.eclim.java.doc.version");
            if (version != null && !StringUtils.EMPTY.equals(version.trim())) {
                version = StringUtils.replace(version, "\\$", "$");
                addTag(javadoc, tags.size(), TagElement.TAG_VERSION, version);
            }
        } else {
            // check if author tag exists.
            int index = -1;
            String author = getAuthor(project);
            for (int ii = 0; ii < tags.size(); ii++) {
                TagElement tag = (TagElement) tags.get(ii);
                if (TagElement.TAG_AUTHOR.equals(tag.getTagName())) {
                    String authorText = tag.fragments().size() > 0
                            ? ((TextElement) tag.fragments().get(0)).getText()
                            : null;
                    // check if author tag is the same.
                    if (authorText != null && author.trim().equals(authorText.trim())) {
                        index = -1;
                        break;
                    }
                    index = ii + 1;
                } else if (tag.getTagName() != null) {
                    if (index == -1) {
                        index = ii;
                    }
                }
            }

            // insert author tag if it doesn't exist.
            if (index > -1) {
                TagElement authorTag = javadoc.getAST().newTagElement();
                TextElement authorText = javadoc.getAST().newTextElement();
                authorText.setText(author);
                authorTag.setTagName(TagElement.TAG_AUTHOR);

                @SuppressWarnings("unchecked")
                List<ASTNode> fragments = authorTag.fragments();
                fragments.add(authorText);
                tags.add(index, authorTag);
            }

            // add the version tag if it doesn't exist.
            boolean versionExists = false;
            for (int ii = 0; ii < tags.size(); ii++) {
                TagElement tag = (TagElement) tags.get(ii);
                if (TagElement.TAG_VERSION.equals(tag.getTagName())) {
                    versionExists = true;
                    break;
                }
            }
            if (!versionExists) {
                String version = getPreferences().getValue(project, "org.eclim.java.doc.version");
                version = StringUtils.replace(version, "\\$", "$");
                addTag(javadoc, tags.size(), TagElement.TAG_VERSION, version);
            }
        }
    } else {
        commentOther(src, javadoc, element, isNew);
    }
}

From source file:org.eclim.plugin.jdt.command.doc.CommentCommand.java

License:Open Source License

/**
 * Adds a tag to the supplied list of tags.
 *
 * @param javadoc The Javadoc instance.//  w w  w . j  a  va2  s . co m
 * @param index The index to insert the new tag at.
 * @param name The tag name.
 * @param text The tag text.
 */
private void addTag(Javadoc javadoc, int index, String name, String text) throws Exception {
    TagElement tag = javadoc.getAST().newTagElement();
    tag.setTagName(name);

    if (text != null) {
        TextElement textElement = javadoc.getAST().newTextElement();
        textElement.setText(text);

        @SuppressWarnings("unchecked")
        List<ASTNode> fragments = tag.fragments();
        fragments.add(textElement);
    }

    @SuppressWarnings("unchecked")
    List<TagElement> tags = javadoc.tags();
    tags.add(index, tag);
}

From source file:org.eclipse.wb.internal.core.utils.ast.AstEditor.java

License:Open Source License

/**
 * Sets new text for JavaDoc {@link TagElement}, i.e. {@link TextElement}.
 * /*from  w ww . j a v  a 2s. co m*/
 * @param declaration
 *          the {@link BodyDeclaration} to update {@link Javadoc}.
 * @param tagName
 *          the name of tag, such as <code>"myTag"</code>, with leading <code>"@"</code>.
 * @param tagText
 *          the text to set for tag, with leading space, or <code>null</code> if tag should be
 *          removed.
 * 
 * @return the {@link TagElement} that has single {@link TextElement} fragment with
 *         <code>tagText</code> as text; or <code>null</code> if tag was removed.
 */
public TagElement setJavadocTagText(BodyDeclaration declaration, String tagName, String tagText)
        throws Exception {
    Assert.isNotNull(tagName);
    Assert.isLegal(tagName.length() != 0, "Empty name of tag.");
    Assert.isLegal(tagName.startsWith("@"), "Tag name should start with '@'.");
    Javadoc javadoc = declaration.getJavadoc();
    // update/add tag
    if (tagText != null) {
        // update existing JavaDoc
        if (javadoc != null) {
            // try to find existing tag
            for (TagElement tagElement : DomGenerics.tags(javadoc)) {
                if (tagName.equals(tagElement.getTagName())) {
                    setJavadocTagText_replaceFragments(tagElement, tagText);
                    return tagElement;
                }
            }
            // add new tag
            {
                List<TagElement> tags = DomGenerics.tags(javadoc);
                // prepare position for new TagElement
                int position;
                {
                    int javadocPosition = javadoc.getStartPosition();
                    String prefix = getWhitespaceToLeft(javadocPosition, false);
                    String endOfLine = getGeneration().getEndOfLine();
                    if (tags.isEmpty()) {
                        position = javadocPosition + "/**".length();
                    } else {
                        position = AstNodeUtils.getSourceEnd(tags.get(tags.size() - 1));
                    }
                    String newCommentLine = endOfLine + prefix + " * ";
                    replaceSubstring(position, 0, newCommentLine);
                    position += newCommentLine.length();
                }
                // replace source
                String tagSource = tagName + tagText;
                replaceSubstring(position, 0, tagSource);
                // add TagElement
                TagElement tagElement = javadoc.getAST().newTagElement();
                tagElement.setSourceRange(position, tagSource.length());
                tagElement.setTagName(tagName);
                tags.add(tagElement);
                // add TextElement
                TextElement textElement = javadoc.getAST().newTextElement();
                textElement.setSourceRange(position + tagName.length(), tagText.length());
                textElement.setText(tagText);
                DomGenerics.fragments(tagElement).add(textElement);
                // new TagElement
                return tagElement;
            }
        } else {
            javadoc = setJavadoc(declaration, new String[] { tagName + tagText });
            return DomGenerics.tags(javadoc).get(0);
        }
    }
    // remove tag
    if (javadoc != null) {
        for (Iterator<TagElement> I = DomGenerics.tags(javadoc).iterator(); I.hasNext();) {
            TagElement tagElement = I.next();
            if (tagName.equals(tagElement.getTagName())) {
                int begin = AstNodeUtils.getSourceBegin(tagElement);
                int end = AstNodeUtils.getSourceEnd(tagElement);
                end = indexOfAnyBut(" \t\r\n*", end + 1);
                // check for removing last line
                if (getChar(end) == '/') {
                    begin = indexOfAnyButBackward("*", begin);
                }
                // replace source
                replaceSubstring(begin, end - begin, "");
                // remove tag
                I.remove();
                // remove JavaDoc is empty
                if (DomGenerics.tags(javadoc).isEmpty()) {
                    setJavadoc(declaration, null);
                }
                // done
                break;
            }
        }
    }
    return null;
}