Example usage for org.eclipse.jdt.core.dom BodyDeclaration getJavadoc

List of usage examples for org.eclipse.jdt.core.dom BodyDeclaration getJavadoc

Introduction

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

Prototype

public Javadoc getJavadoc() 

Source Link

Document

Returns the doc comment node.

Usage

From source file:br.uff.ic.gems.resources.ast.Visitor.java

public int beginLine(BodyDeclaration node) {
    int beginLine = INVALID_LINE;

    List modifiers = node.modifiers();
    for (Object modifier : modifiers) {
        if (modifier instanceof Modifier) {
            Modifier m = (Modifier) modifier;
            beginLine = cu.getLineNumber(m.getStartPosition());
        }//from   w  w w. java2  s. com
    }

    if (beginLine == INVALID_LINE) {

        beginLine = cu.getLineNumber(node.getStartPosition());

        Javadoc javadoc = node.getJavadoc();
        int javadocBegin = INVALID_JAVADOC;

        if (javadoc != null) {
            javadocBegin = cu.getLineNumber(javadoc.getStartPosition());
        }

        if (beginLine == javadocBegin) {
            beginLine = cu.getLineNumber(javadoc.getStartPosition() + javadoc.getLength() + 1);
        }
    }
    return beginLine;
}

From source file:br.uff.ic.gems.resources.ast.Visitor.java

public int beginColunm(BodyDeclaration node) {

    int begincolumn = INVALID_COLUMN;

    List modifiers = node.modifiers();
    for (Object modifier : modifiers) {
        if (modifier instanceof Modifier) {
            Modifier m = (Modifier) modifier;
            begincolumn = cu.getColumnNumber(m.getStartPosition());
        }//from w ww .j a v  a  2s  .  c om
    }

    if (begincolumn == INVALID_COLUMN) {

        begincolumn = cu.getColumnNumber(node.getStartPosition());

        Javadoc javadoc = node.getJavadoc();
        int javadocBegin = INVALID_JAVADOC;

        if (javadoc != null) {
            javadocBegin = cu.getColumnNumber(javadoc.getStartPosition());
        }

        if (begincolumn == javadocBegin) {
            begincolumn = cu.getColumnNumber(javadoc.getStartPosition() + javadoc.getLength() + 1);
        }
    }

    return begincolumn;
}

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 w w. ja  va  2  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.crispico.flower.mp.codesync.code.java.adapter.JavaAbstractAstNodeModelAdapter.java

License:Open Source License

protected Object getJavaDoc(Object element) {
    if (element instanceof BodyDeclaration) {
        BodyDeclaration node = (BodyDeclaration) element;
        if (node.getJavadoc() != null) {
            String docComment = null;
            for (Object o : node.getJavadoc().tags()) {
                TagElement tag = (TagElement) o;
                String tagName = tag.getTagName();
                if (getModelAdapterFactorySet().useUIDs() && FLOWER_UID.equals(tagName)) {
                    continue;
                }// ww  w  .j a  va  2s.  com
                if (docComment == null) {
                    docComment = new String();
                }
                if (tagName != null) {
                    docComment += tag.getTagName() + " ";
                }
                for (Object o2 : tag.fragments()) {
                    docComment += getTextFromDocElement(o2);
                }
                docComment += "\n";
            }
            return docComment;
        }
    }
    return null;
}

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

License:Open Source License

protected void setJavaDoc(Object element, Object docComment) {
    if (element instanceof BodyDeclaration) {
        BodyDeclaration node = (BodyDeclaration) element;
        ASTParser parser = ASTParser.newParser(AST.JLS4);
        parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);
        parser.setSource(("/** " + docComment + "*/ int x;").toCharArray());
        TypeDeclaration type = (TypeDeclaration) parser.createAST(null);
        BodyDeclaration x = (BodyDeclaration) type.bodyDeclarations().get(0);
        Javadoc javadoc = x.getJavadoc();
        node.setJavadoc((Javadoc) ASTNode.copySubtree(node.getAST(), javadoc));
    }//w  ww .ja  va 2  s  .c  om
}

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

License:Apache License

public static void addJavadocTag(ASTRewrite rewrite, BodyDeclaration node, String tagText) {
    Javadoc javadoc = node.getJavadoc();
    if (javadoc == null) {
        AST ast = node.getAST();/*from www  .j av  a 2 s.  c  o  m*/
        javadoc = (Javadoc) ast.createInstance(Javadoc.class);
        rewrite.set(node, node.getJavadocProperty(), javadoc, null /* editGroup */);
    }
    addJavadocTag(rewrite, javadoc, tagText);
}

From source file:com.google.dart.java2dart.SyntaxTranslator.java

License:Open Source License

private Comment translateJavadoc(org.eclipse.jdt.core.dom.BodyDeclaration node) {
    return (Comment) translate(node.getJavadoc());
}

From source file:de.akra.idocit.java.ui.JavaEditorSelectionListener.java

License:Apache License

/**
 * Collect assigned ThematicRoles from the method and it's parents (classes,
 * interfaces, enumerations) and find the reference ThematicGrid.
 * //from  w ww. j  a  v a  2  s.  co  m
 * @param method
 *            [SOURCE]
 * @throws Exception
 */
private RecommendedGridsViewSelection prepareViewSelection(final IMethod method) throws Exception {
    final AbsJavadocParser javadocParser = JavaParser.getJavadocParser();
    final List<Addressee> addressees = ServiceManager.getInstance().getPersistenceService()
            .loadConfiguredAddressees();
    final List<ThematicRole> roles = ServiceManager.getInstance().getPersistenceService().loadThematicRoles();

    final RecommendedGridsViewSelection selection = new RecommendedGridsViewSelection();
    selection.setOperationIdentifier(method.getElementName());

    final Set<ThematicRole> assignedThematicRoles = new TreeSet<ThematicRole>();
    selection.setAssignedThematicRoles(assignedThematicRoles);

    /*
     * Collect documentations from method
     */
    final String methodSource = extractCodeSnippetOf(method);
    final ASTNode node = parseCodeSnippet(methodSource.toCharArray(), ASTParser.K_CLASS_BODY_DECLARATIONS);

    if ((node.getNodeType() == ASTNode.TYPE_DECLARATION || node.getNodeType() == ASTNode.ENUM_DECLARATION)) {
        final TypeDeclaration typeDeclaration = (TypeDeclaration) node;
        @SuppressWarnings("unchecked")
        final List<BodyDeclaration> bodyDeclarations = (List<BodyDeclaration>) typeDeclaration
                .bodyDeclarations();

        // only one method is parsed
        if (bodyDeclarations.size() == 1) {
            final BodyDeclaration bodyDeclaration = bodyDeclarations.get(0);
            if (bodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION
                    && bodyDeclaration.getJavadoc() != null) {
                selection.setReferenceThematicGridName(
                        javadocParser.parseIDocItReferenceGrid(bodyDeclaration.getJavadoc()));

                final List<Documentation> parsedDocs = javadocParser
                        .parseIDocItJavadoc(bodyDeclaration.getJavadoc(), addressees, roles, null);
                for (final Documentation doc : parsedDocs) {
                    if (doc.getThematicRole() != null) {
                        assignedThematicRoles.add(doc.getThematicRole());
                    }
                }
            }

            /*
             * Collect documentations from parent interfaces, classes and enumerations
             */
            IJavaElement parent = method.getParent();
            while (parent != null && parent.getElementType() == IJavaElement.TYPE) {
                final IType type = (IType) parent;
                final String typeSource = extractCodeSnippetOf(type);

                final ASTNode parentNode = parseCodeSnippet(typeSource.toCharArray(),
                        ASTParser.K_CLASS_BODY_DECLARATIONS);

                if ((parentNode.getNodeType() == ASTNode.TYPE_DECLARATION
                        || parentNode.getNodeType() == ASTNode.ENUM_DECLARATION)) {
                    final TypeDeclaration typeDecl = (TypeDeclaration) parentNode;

                    @SuppressWarnings("unchecked")
                    final List<BodyDeclaration> bodyDecls = (List<BodyDeclaration>) typeDecl.bodyDeclarations();

                    // only one class, interface or enum is parsed
                    if (bodyDecls.size() == 1) {
                        final BodyDeclaration bodyDecl = bodyDecls.get(0);

                        // process if it is a class, interface or enum and has Javadoc
                        if ((bodyDecl.getNodeType() == ASTNode.TYPE_DECLARATION
                                || bodyDecl.getNodeType() == ASTNode.ENUM_DECLARATION)
                                && bodyDecl.getJavadoc() != null) {
                            final List<Documentation> parsedDocs = javadocParser
                                    .parseIDocItJavadoc(bodyDecl.getJavadoc(), addressees, roles, null);
                            for (final Documentation doc : parsedDocs) {
                                if (doc.getThematicRole() != null) {
                                    assignedThematicRoles.add(doc.getThematicRole());
                                }
                            }
                        }
                    }

                    // parse next parent
                    parent = parent.getParent();
                } else {
                    LOG.fine("Stop parsing because of incorrect source code syntax.");
                    parent = null;
                }
            }
        }
    } else {
        LOG.info("Parsing of code snippet failed. No assigned ThematicRoles can be collected.");
    }
    return selection;
}

From source file:de.ovgu.featureide.munge.signatures.MungeSignatureBuilder.java

License:Open Source License

private static Collection<AbstractSignature> parse(ProjectSignatures projectSignatures, ASTNode root) {
    final HashMap<AbstractSignature, AbstractSignature> map = new HashMap<>();

    final CompilationUnit cu = (CompilationUnit) root;
    final PackageDeclaration pckgDecl = cu.getPackage();
    final String packageName = (pckgDecl == null) ? null : pckgDecl.getName().getFullyQualifiedName();
    List<?> l = cu.getCommentList();
    List<Javadoc> cl = new LinkedList<>();
    for (Object object : l) {
        if (object instanceof Javadoc) {
            Javadoc comment = (Javadoc) object;
            cl.add(comment);//from   w ww.j a va  2s  .c  om
        }
    }

    final ListIterator<Javadoc> it = cl.listIterator();
    final FeatureDataConstructor featureDataConstructor = new FeatureDataConstructor(projectSignatures,
            FeatureDataConstructor.TYPE_PP);

    root.accept(new ASTVisitor() {
        private BodyDeclaration curDeclaration = null;
        private PreprocessorFeatureData curfeatureData = null;
        private String lastComment = null;
        private MethodDeclaration lastCommentedMethod = null;

        @Override
        public boolean visit(Javadoc node) {
            if (curDeclaration != null) {
                final StringBuilder sb = new StringBuilder();
                while (it.hasNext()) {
                    final Javadoc comment = it.next();
                    if (comment.getStartPosition() <= curDeclaration.getStartPosition()) {
                        sb.append(comment);
                        sb.append("\n");
                    } else {
                        it.previous();
                        break;
                    }
                }
                lastComment = sb.toString();

                curfeatureData.setComment(lastComment);
                lastCommentedMethod = (curDeclaration instanceof MethodDeclaration)
                        ? (MethodDeclaration) curDeclaration
                        : null;
            }
            return false;
        }

        private void attachFeatureData(AbstractSignature curSignature, BodyDeclaration curDeclaration) {
            this.curDeclaration = curDeclaration;
            final Javadoc javadoc = curDeclaration.getJavadoc();
            final int startPosition = (javadoc == null) ? curDeclaration.getStartPosition()
                    : curDeclaration.getStartPosition() + javadoc.getLength();
            curfeatureData = (PreprocessorFeatureData) featureDataConstructor.create(null,
                    unit.getLineNumber(startPosition),
                    unit.getLineNumber(curDeclaration.getStartPosition() + curDeclaration.getLength()));
            curSignature.setFeatureData(curfeatureData);
            map.put(curSignature, curSignature);
        }

        @Override
        public boolean visit(CompilationUnit unit) {
            this.unit = unit;
            return true;
        }

        CompilationUnit unit = null;

        @Override
        public boolean visit(MethodDeclaration node) {
            int pos = unit.getLineNumber(node.getBody().getStartPosition());
            int end = unit.getLineNumber(node.getBody().getStartPosition() + node.getBody().getLength());
            final MungeMethodSignature methodSignature = new MungeMethodSignature(getParent(node.getParent()),
                    node.getName().getIdentifier(), node.getModifiers(), node.getReturnType2(),
                    node.parameters(), node.isConstructor(), pos, end);

            attachFeatureData(methodSignature, node);

            if (node.getJavadoc() == null && lastCommentedMethod != null
                    && lastCommentedMethod.getName().equals(node.getName())) {
                curfeatureData.setComment(lastComment);
            } else {
                lastCommentedMethod = null;
            }
            return true;
        }

        private AbstractClassSignature getParent(ASTNode astnode) {
            final AbstractClassSignature sig;
            if (astnode instanceof TypeDeclaration) {
                final TypeDeclaration node = (TypeDeclaration) astnode;
                sig = new MungeClassSignature(null, node.getName().getIdentifier(), node.getModifiers(),
                        node.isInterface() ? "interface" : "class", packageName);
            } else {
                return null;
            }
            AbstractClassSignature uniqueSig = (AbstractClassSignature) map.get(sig);
            if (uniqueSig == null) {
                visit((TypeDeclaration) astnode);
            }
            return uniqueSig;
        }

        @Override
        public boolean visit(FieldDeclaration node) {
            for (Iterator<?> it = node.fragments().iterator(); it.hasNext();) {
                VariableDeclarationFragment fragment = (VariableDeclarationFragment) it.next();

                final MungeFieldSignature fieldSignature = new MungeFieldSignature(getParent(node.getParent()),
                        fragment.getName().getIdentifier(), node.getModifiers(), node.getType());

                attachFeatureData(fieldSignature, node);
            }

            return true;
        }

        @Override
        public boolean visit(TypeDeclaration node) {
            final MungeClassSignature classSignature = new MungeClassSignature(getParent(node.getParent()),
                    node.getName().getIdentifier(), node.getModifiers(),
                    node.isInterface() ? "interface" : "class", packageName);

            attachFeatureData(classSignature, node);

            return super.visit(node);
        }

    });
    return map.keySet();
}

From source file:javadoctest.internal.DocTestExtractor.java

License:Open Source License

private List<ExtractedDocTest> extractExamples(Collection<String> imports, String pkg, String rootClass,
        String source, BodyDeclaration node) {
    Javadoc javadoc = node.getJavadoc();
    if (javadoc == null) {
        return Collections.emptyList();
    }/* w w  w .j av a  2s. c om*/

    return extractCodeBlocks(source, imports, pkg, rootClass, extractJavadoc(javadoc.getComment()));
}