com.sun.source.util.DocTrees Java Examples

The following examples show how to use com.sun.source.util.DocTrees. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DocCommentTreeApiTester.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Tests DocTrees.getDocCommentTree(FileObject fo).
 *
 * @param htmlfileName the file to be parsed
 * @throws Exception when an error occurs.
 */
public void runFileObjectTest(String htmlfileName) throws Exception {
    List<File> javaFiles =  Collections.emptyList();

    List<File> otherFiles = new ArrayList<>();
    otherFiles.add(new File(testSrc, htmlfileName));

    try (StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null)) {
        Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(javaFiles);
        Iterable<? extends JavaFileObject> others = fm.getJavaFileObjectsFromFiles(otherFiles);

        final JavacTask t = javac.getTask(null, fm, null, null, null, fos);
        final DocTrees trees = DocTrees.instance(t);

        StringWriter sw = new StringWriter();

        printer.print(trees.getDocCommentTree(others.iterator().next()), sw);
        String found = sw.toString();
        String expected = getExpected(otherFiles.iterator().next().toPath());
        astcheck(otherFiles.toString(), expected, found);
    }
}
 
Example #2
Source File: DocTreePathScannerTest.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
void run() throws Exception {
    List<File> files = new ArrayList<File>();
    File testSrc = new File(System.getProperty("test.src"));
    for (File f: testSrc.listFiles()) {
        if (f.isFile() && f.getName().endsWith(".java"))
            files.add(f);
    }

    JavacTool javac = JavacTool.create();
    StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null);

    Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);

    JavacTask t = javac.getTask(null, fm, null, null, null, fos);
    DocTrees trees = DocTrees.instance(t);

    Iterable<? extends CompilationUnitTree> units = t.parse();

    DeclScanner ds = new DeclScanner(trees);
    for (CompilationUnitTree unit: units) {
        ds.scan(unit, null);
    }

    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
 
Example #3
Source File: RenameTransformer.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public DocTree visitReference(ReferenceTree node, Element elementToFind) {
    DocTreePath currentDocPath = getCurrentDocPath();
    DocTrees trees = workingCopy.getDocTrees();
    Element el = trees.getElement(currentDocPath);
    ExpressionTree classReference = workingCopy.getTreeUtilities().getReferenceClass(currentDocPath);
    if((el == null || !(el.equals(elementToFind) || isMethodMatch(el))) && classReference != null) {
        el = trees.getElement(new TreePath(getCurrentPath(), classReference));
    }
    if (el != null && (el.equals(elementToFind) || isMethodMatch(el))) {
        ReferenceTree newRef;
        Name memberName = workingCopy.getTreeUtilities().getReferenceName(currentDocPath);
        List<? extends Tree> methodParameters = workingCopy.getTreeUtilities().getReferenceParameters(currentDocPath);
        if(el.getKind().isClass() || el.getKind().isInterface()) {
            newRef = make.Reference(make.setLabel(classReference, newName), memberName, methodParameters);
        } else {
            newRef = make.Reference(classReference, newName, methodParameters);
        }
        rewrite(currentDocPath.getTreePath().getLeaf(), node, newRef);
    }
    return super.visitReference(node, elementToFind);
}
 
Example #4
Source File: Test.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public boolean run(DocletEnvironment root) {
    DocTrees docTrees = root.getDocTrees();
    System.out.println("classes:" + ElementFilter.typesIn(root.getIncludedElements()));

    Element klass = ElementFilter.typesIn(root.getIncludedElements()).iterator().next();
    String text = "";
    try {
        DocCommentTree dcTree = docTrees.getDocCommentTree(klass, overviewpath);
        text = dcTree.getFullBody().toString();
    } catch (IOException ioe) {
        throw new Error(ioe);
    }

    if (text.length() < 64)
        System.err.println("text: '" + text + "'");
    else
        System.err.println("text: '"
                + text.substring(0, 20)
                + "..."
                + text.substring(text.length() - 20)
                + "'");
    return text.startsWith("ABC") && text.endsWith("XYZ");
}
 
Example #5
Source File: InlineTagsWithBraces.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public boolean run(DocletEnvironment root) {
        DocTrees trees = root.getDocTrees();
        TypeElement cd = ElementFilter.typesIn(root.getIncludedElements()).iterator().next();
        DocCommentTree docCommentTree = trees.getDocCommentTree(cd);
        List<? extends DocTree> tags = docCommentTree.getBody();

        for (int i = 0; i < tags.size(); i++) {
            System.out.println(tags.get(0).getKind());
//            if (!tags[i].name().equals(expectedTags[i]) ||
//                        !tags[i].text().equals(expectedText[i])) {
//                throw new Error("Tag \"" + tags[i] + "\" not as expected");
//            }
        }

        return true;
    }
 
Example #6
Source File: DocTreePathScannerTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
void run() throws Exception {
    List<File> files = new ArrayList<File>();
    File testSrc = new File(System.getProperty("test.src"));
    for (File f: testSrc.listFiles()) {
        if (f.isFile() && f.getName().endsWith(".java"))
            files.add(f);
    }

    JavacTool javac = JavacTool.create();
    StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null);

    Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);

    JavacTask t = javac.getTask(null, fm, null, null, null, fos);
    DocTrees trees = DocTrees.instance(t);

    Iterable<? extends CompilationUnitTree> units = t.parse();

    DeclScanner ds = new DeclScanner(trees);
    for (CompilationUnitTree unit: units) {
        ds.scan(unit, null);
    }

    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
 
Example #7
Source File: FindLocalUsagesQuery.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public DocTree visitIdentifier(com.sun.source.doctree.IdentifierTree node, Element p) {
    DocTrees trees = info.getDocTrees();
    Element el = trees.getElement(getCurrentPath());
    if (el != null && el.equals(toFind)) {
        DocSourcePositions sp = trees.getSourcePositions();
        CompilationUnitTree cut = info.getCompilationUnit();
        DocCommentTree docComment = getCurrentPath().getDocComment();
        long start = sp.getStartPosition(cut, docComment, node);
        long end = sp.getEndPosition(cut, docComment, node);
        if(start != Diagnostic.NOPOS && end != Diagnostic.NOPOS) {
            try {
                MutablePositionRegion region = createRegion(doc, (int)start, (int)end);
                usages.add(region);
            } catch (BadLocationException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    }
    return super.visitIdentifier(node, p);
}
 
Example #8
Source File: DocTreePathScannerTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
void run() throws Exception {
    List<File> files = new ArrayList<File>();
    File testSrc = new File(System.getProperty("test.src"));
    for (File f: testSrc.listFiles()) {
        if (f.isFile() && f.getName().endsWith(".java"))
            files.add(f);
    }

    JavacTool javac = JavacTool.create();
    StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null);

    Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);

    JavacTask t = javac.getTask(null, fm, null, null, null, fos);
    DocTrees trees = DocTrees.instance(t);

    Iterable<? extends CompilationUnitTree> units = t.parse();

    DeclScanner ds = new DeclScanner(trees);
    for (CompilationUnitTree unit: units) {
        ds.scan(unit, null);
    }

    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
 
Example #9
Source File: BeakerxDoclet.java    From beakerx with Apache License 2.0 6 votes vote down vote up
private static MethodInspect getInspect(Element element, DocTrees docTrees) {
  DocCommentTree eeDocComment = docTrees.getDocCommentTree(element);
  List<String> signature = new ArrayList<>();
  if (element instanceof ExecutableElement) {
    ExecutableElement eElement = (ExecutableElement) element;
    for (VariableElement v : eElement.getParameters()) {
      String n = v.asType().toString();
      if (v.asType().toString().contains("<")) {
        n = v.asType().toString().substring(0, v.asType().toString().indexOf("<"));
      }
      signature.add(n + " " + v.getSimpleName());
    }
  }
  String comment = (eeDocComment != null) ? eeDocComment.getFullBody().toString() : "";
  String name = element.toString().subSequence(0, element.toString().indexOf("(")).toString();
  MethodInspect methodInspect = new MethodInspect(name, comment, String.join(", ", signature));
  return methodInspect;
}
 
Example #10
Source File: FindLocalUsagesQuery.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public DocTree visitReference(ReferenceTree node, Element p) {
    DocTrees trees = info.getDocTrees();
    Element el = trees.getElement(getCurrentPath());
    if (el != null && el.equals(toFind)) {
        int[] span = treeUtils.findNameSpan(getCurrentPath().getDocComment(), node);
        if(span != null) {
            try {
                MutablePositionRegion region = createRegion(doc, span[0], span[1]);
                usages.add(region);
            } catch (BadLocationException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    }
    return super.visitReference(node, p);
}
 
Example #11
Source File: DocTreePathScannerTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
void run() throws Exception {
    List<File> files = new ArrayList<File>();
    File testSrc = new File(System.getProperty("test.src"));
    for (File f: testSrc.listFiles()) {
        if (f.isFile() && f.getName().endsWith(".java"))
            files.add(f);
    }

    JavacTool javac = JavacTool.create();
    StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null);

    Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);

    JavacTask t = javac.getTask(null, fm, null, null, null, fos);
    DocTrees trees = DocTrees.instance(t);

    Iterable<? extends CompilationUnitTree> units = t.parse();

    DeclScanner ds = new DeclScanner(trees);
    for (CompilationUnitTree unit: units) {
        ds.scan(unit, null);
    }

    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
 
Example #12
Source File: TreeUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**Find the type (the part before {@code #}) that is being referenced by the given {@link ReferenceTree}.
     * 
     * @param path the leaf must be {@link ReferenceTree}
     * @return the referred type, or {@code null} if none.
     * @since 0.124
     */
    public @CheckForNull ExpressionTree getReferenceClass(@NonNull DocTreePath path) {
        TreePath tp = path.getTreePath();
        DCReference ref = (DCReference) path.getLeaf();
        
        ((DocTrees) this.info.getTrees()).getElement(path);
//        was:
//        ((JavacTrees) this.info.getTrees()).ensureDocReferenceAttributed(tp, ref);
        
        return (ExpressionTree) ref.qualifierExpression;
    }
 
Example #13
Source File: DocTreePathScannerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
void run() throws Exception {
    List<File> files = new ArrayList<File>();
    File testSrc = new File(System.getProperty("test.src"));
    for (File f: testSrc.listFiles()) {
        if (f.isFile() && f.getName().endsWith(".java"))
            files.add(f);
    }

    JavacTool javac = JavacTool.create();
    try (StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null)) {

        Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);

        JavacTask t = javac.getTask(null, fm, null, null, null, fos);
        DocTrees trees = DocTrees.instance(t);

        Iterable<? extends CompilationUnitTree> units = t.parse();

        DeclScanner ds = new DeclScanner(trees);
        for (CompilationUnitTree unit: units) {
            ds.scan(unit, null);
        }

        if (errors > 0)
            throw new Exception(errors + " errors occurred");
    }
}
 
Example #14
Source File: Test.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
void testTaskListener(JavacTool javac, StandardJavaFileManager fm,
        Iterable<? extends JavaFileObject> files, PrintWriter out,
        int expectedDocComments) {
    out.println("Test task listener");
    JavacTask task = javac.getTask(out, fm, null, null, null, files);
    TaskListnr tl = new TaskListnr(DocTrees.instance(task));
    task.addTaskListener(tl);
    task.call();
    tl.checker.checkDocComments(expectedDocComments);
}
 
Example #15
Source File: Test.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
void testAnnoProcessor(JavacTool javac, StandardJavaFileManager fm,
        Iterable<? extends JavaFileObject> files, PrintWriter out,
        int expectedDocComments) {
    out.println("Test annotation processor");
    JavacTask task = javac.getTask(out, fm, null, null, null, files);
    AnnoProc ap = new AnnoProc(DocTrees.instance(task));
    task.setProcessors(Arrays.asList(ap));
    task.call();
    ap.checker.checkDocComments(expectedDocComments);
}
 
Example #16
Source File: SimpleDocTreeVisitorTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
void run() throws Exception {
    List<File> files = new ArrayList<File>();
    File testSrc = new File(System.getProperty("test.src"));
    for (File f: testSrc.listFiles()) {
        if (f.isFile() && f.getName().endsWith(".java"))
            files.add(f);
    }

    JavacTool javac = JavacTool.create();
    StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null);

    Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);

    JavacTask t = javac.getTask(null, fm, null, null, null, fos);
    DocTrees trees = DocTrees.instance(t);

    Iterable<? extends CompilationUnitTree> units = t.parse();

    Set<DocTree.Kind> found = EnumSet.noneOf(DocTree.Kind.class);
    DeclScanner ds = new DeclScanner(trees, found);
    for (CompilationUnitTree unit: units) {
        ds.scan(unit, null);
    }

    for (DocTree.Kind k: DocTree.Kind.values()) {
        if (!found.contains(k) && k != DocTree.Kind.OTHER)
            error("not found: " + k);
    }

    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
 
Example #17
Source File: NoStar.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public boolean run(DocletEnvironment root) {
    Set<TypeElement> classes = ElementFilter.typesIn(root.getIncludedElements());
    if (classes.size() != 1)
        throw new Error("1 " + Arrays.asList(classes));
    TypeElement self = classes.iterator().next();
    DocTrees trees = root.getDocTrees();
    DocCommentTree docCommentTree = trees.getDocCommentTree(self);
    String c = docCommentTree.getFullBody().toString();
    System.out.println("\"" + c + "\"");
    return c.equals("First sentence.\n0\n 1\n  2\n   3\n    4\n     5");
}
 
Example #18
Source File: Test.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
void testAnnoProcessor(JavacTool javac, StandardJavaFileManager fm,
        Iterable<? extends JavaFileObject> files, PrintWriter out,
        int expectedDocComments) {
    out.println("Test annotation processor");
    JavacTask task = javac.getTask(out, fm, null, null, null, files);
    AnnoProc ap = new AnnoProc(DocTrees.instance(task));
    task.setProcessors(Arrays.asList(ap));
    task.call();
    ap.checker.checkDocComments(expectedDocComments);
}
 
Example #19
Source File: SimpleDocTreeVisitorTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
void run() throws Exception {
    List<File> files = new ArrayList<File>();
    File testSrc = new File(System.getProperty("test.src"));
    for (File f: testSrc.listFiles()) {
        if (f.isFile() && f.getName().endsWith(".java"))
            files.add(f);
    }

    JavacTool javac = JavacTool.create();
    StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null);

    Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);

    JavacTask t = javac.getTask(null, fm, null, null, null, fos);
    DocTrees trees = DocTrees.instance(t);

    Iterable<? extends CompilationUnitTree> units = t.parse();

    Set<DocTree.Kind> found = EnumSet.noneOf(DocTree.Kind.class);
    DeclScanner ds = new DeclScanner(trees, found);
    for (CompilationUnitTree unit: units) {
        ds.scan(unit, null);
    }

    for (DocTree.Kind k: DocTree.Kind.values()) {
        if (!found.contains(k) && k != DocTree.Kind.OTHER)
            error("not found: " + k);
    }

    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
 
Example #20
Source File: Test.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
void testTaskListener(JavacTool javac, StandardJavaFileManager fm,
        Iterable<? extends JavaFileObject> files, PrintWriter out,
        int expectedDocComments) {
    out.println("Test task listener");
    JavacTask task = javac.getTask(out, fm, null, null, null, files);
    TaskListnr tl = new TaskListnr(DocTrees.instance(task));
    task.addTaskListener(tl);
    task.call();
    tl.checker.checkDocComments(expectedDocComments);
}
 
Example #21
Source File: JavadocHelper.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private DocCommentTree parseDocComment(JavacTask task, String javadoc) {
    DocTrees trees = DocTrees.instance(task);
    try {
        return trees.getDocCommentTree(new SimpleJavaFileObject(new URI("mem://doc.html"), javax.tools.JavaFileObject.Kind.HTML) {
            @Override @DefinedBy(Api.COMPILER)
            public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
                return "<body>" + javadoc + "</body>";
            }
        });
    } catch (URISyntaxException ex) {
        return null;
    }
}
 
Example #22
Source File: SimpleDocTreeVisitorTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
void run() throws Exception {
    List<File> files = new ArrayList<File>();
    File testSrc = new File(System.getProperty("test.src"));
    for (File f: testSrc.listFiles()) {
        if (f.isFile() && f.getName().endsWith(".java"))
            files.add(f);
    }

    JavacTool javac = JavacTool.create();
    StandardJavaFileManager fm = javac.getStandardFileManager(null, null, null);

    Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);

    JavacTask t = javac.getTask(null, fm, null, null, null, fos);
    DocTrees trees = DocTrees.instance(t);

    Iterable<? extends CompilationUnitTree> units = t.parse();

    Set<DocTree.Kind> found = EnumSet.noneOf(DocTree.Kind.class);
    DeclScanner ds = new DeclScanner(trees, found);
    for (CompilationUnitTree unit: units) {
        ds.scan(unit, null);
    }

    for (DocTree.Kind k: DocTree.Kind.values()) {
        if (!found.contains(k) && k != DocTree.Kind.OTHER)
            error("not found: " + k);
    }

    if (errors > 0)
        throw new Exception(errors + " errors occurred");
}
 
Example #23
Source File: JavaParser.java    From manifold with Apache License 2.0 5 votes vote down vote up
public boolean parseText( String src, List<CompilationUnitTree> trees, Consumer<SourcePositions> sourcePositions,
                          Consumer<DocTrees> docTrees, DiagnosticCollector<JavaFileObject> errorHandler )
{
  init();

  ArrayList<JavaFileObject> javaStringObjects = new ArrayList<>();
  javaStringObjects.add( new StringJavaFileObject( "sample", src ) );
  StringWriter errors = new StringWriter();
  BasicJavacTask javacTask = (BasicJavacTask)_javac.getTask(
    errors, _mfm, errorHandler, Collections.singletonList( "-proc:none" ), null, javaStringObjects );
  try
  {
    initTypeProcessing( javacTask, Collections.singleton( "sample" ) );
    Iterable<? extends CompilationUnitTree> iterable = javacTask.parse();
    if( errors.getBuffer().length() > 0 )
    {
      System.err.println( errors.getBuffer() );
    }
    for( CompilationUnitTree x : iterable )
    {
      trees.add( x );
    }
    if( sourcePositions != null )
    {
      sourcePositions.accept( Trees.instance( javacTask ).getSourcePositions() );
    }
    if( docTrees != null )
    {
      docTrees.accept( DocTrees.instance( javacTask ) );
    }
    return true;
  }
  catch( Exception e )
  {
    return false;
  }
}
 
Example #24
Source File: RenameTransformer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Tree scan(Tree tree, Element p) {
    if(p == null && handle == null) {
        p = docHandle != null? ((DocTrees)workingCopy.getTrees()).getElement(docHandle.resolve(workingCopy))
                                                   : handle.resolveElement(workingCopy);
    }
    return super.scan(tree, p);
}
 
Example #25
Source File: TreeUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**Find the parameters that are specified in the given {@link ReferenceTree}.
     * 
     * @param path the leaf must be {@link ReferenceTree}
     * @return the parameters for the referred method, or {@code null} if none.
     * @since 0.124
     */
    public @CheckForNull List<? extends Tree> getReferenceParameters(@NonNull DocTreePath path) {
        TreePath tp = path.getTreePath();
        DCReference ref = (DCReference) path.getLeaf();
        
        ((DocTrees) this.info.getTrees()).getElement(path);
//        was:
//        ((JavacTrees) this.info.getTrees()).ensureDocReferenceAttributed(tp, ref);
        
        return ref.paramTypes;
    }
 
Example #26
Source File: Test.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
void testAnnoProcessor(JavacTool javac, StandardJavaFileManager fm,
        Iterable<? extends JavaFileObject> files, PrintWriter out,
        int expectedDocComments) {
    out.println("Test annotation processor");
    JavacTask task = javac.getTask(out, fm, null, null, null, files);
    AnnoProc ap = new AnnoProc(DocTrees.instance(task));
    task.setProcessors(Arrays.asList(ap));
    task.call();
    ap.checker.checkDocComments(expectedDocComments);
}
 
Example #27
Source File: DocCommentTester.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
PrettyChecker(DocCommentTester test, DocTrees t) {
    test.super(t);
}
 
Example #28
Source File: DocCommentTester.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
ASTChecker(DocCommentTester test, DocTrees t) {
    test.super(t);
}
 
Example #29
Source File: Test.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
AnnoProc(DocTrees trees) {
    checker = new Checker(trees);
}
 
Example #30
Source File: DocCommentTester.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
ASTChecker(DocCommentTester test, DocTrees t) {
    test.super(t);
}