6985202: no access to doc comments from Tree API

Thu, 09 Dec 2010 08:48:08 -0800

author
jjg
date
Thu, 09 Dec 2010 08:48:08 -0800
changeset 783
90914ac50868
parent 782
bcf44475aeee
child 784
4dd1c0176d81

6985202: no access to doc comments from Tree API
Reviewed-by: mcimadamore

src/share/classes/com/sun/source/util/Trees.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/api/JavacTrees.java file | annotate | diff | comparison | revisions
test/tools/javac/api/TestDocComments.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/source/util/Trees.java	Thu Dec 09 08:24:42 2010 -0800
     1.2 +++ b/src/share/classes/com/sun/source/util/Trees.java	Thu Dec 09 08:48:08 2010 -0800
     1.3 @@ -163,6 +163,12 @@
     1.4      public abstract Scope getScope(TreePath path);
     1.5  
     1.6      /**
     1.7 +     * Gets the doc comment, if any, for the Tree node identified by a given TreePath.
     1.8 +     * Returns null if no doc comment was found.
     1.9 +     */
    1.10 +    public abstract String getDocComment(TreePath path);
    1.11 +
    1.12 +    /**
    1.13       * Checks whether a given type is accessible in a given scope.
    1.14       * @param scope the scope to be checked
    1.15       * @param type the type to be checked
     2.1 --- a/src/share/classes/com/sun/tools/javac/api/JavacTrees.java	Thu Dec 09 08:24:42 2010 -0800
     2.2 +++ b/src/share/classes/com/sun/tools/javac/api/JavacTrees.java	Thu Dec 09 08:48:08 2010 -0800
     2.3 @@ -228,6 +228,17 @@
     2.4          return new JavacScope(getAttrContext(path));
     2.5      }
     2.6  
     2.7 +    public String getDocComment(TreePath path) {
     2.8 +        CompilationUnitTree t = path.getCompilationUnit();
     2.9 +        if (t instanceof JCTree.JCCompilationUnit) {
    2.10 +            JCCompilationUnit cu = (JCCompilationUnit) t;
    2.11 +            if (cu.docComments != null) {
    2.12 +                return cu.docComments.get(path.getLeaf());
    2.13 +            }
    2.14 +        }
    2.15 +        return null;
    2.16 +    }
    2.17 +
    2.18      public boolean isAccessible(Scope scope, TypeElement type) {
    2.19          if (scope instanceof JavacScope && type instanceof ClassSymbol) {
    2.20              Env<AttrContext> env = ((JavacScope) scope).env;
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/tools/javac/api/TestDocComments.java	Thu Dec 09 08:48:08 2010 -0800
     3.3 @@ -0,0 +1,157 @@
     3.4 +/*
     3.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
     3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.7 + *
     3.8 + * This code is free software; you can redistribute it and/or modify it
     3.9 + * under the terms of the GNU General Public License version 2 only, as
    3.10 + * published by the Free Software Foundation.
    3.11 + *
    3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    3.15 + * version 2 for more details (a copy is included in the LICENSE file that
    3.16 + * accompanied this code).
    3.17 + *
    3.18 + * You should have received a copy of the GNU General Public License version
    3.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    3.21 + *
    3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    3.23 + * or visit www.oracle.com if you need additional information or have any
    3.24 + * questions.
    3.25 + */
    3.26 +
    3.27 +/* @test
    3.28 + * @bug 6985202
    3.29 + * @summary no access to doc comments from Tree API
    3.30 + */
    3.31 +
    3.32 +import java.io.*;
    3.33 +import java.util.*;
    3.34 +import javax.tools.*;
    3.35 +import com.sun.source.tree.*;
    3.36 +import com.sun.source.util.*;
    3.37 +import com.sun.tools.javac.api.JavacTool;
    3.38 +
    3.39 +/**
    3.40 + * class-TestDocComments.
    3.41 + */
    3.42 +public class TestDocComments {
    3.43 +    /**
    3.44 +     * method-main.
    3.45 +     */
    3.46 +    public static void main(String... args) throws Exception {
    3.47 +        new TestDocComments().run();
    3.48 +    }
    3.49 +
    3.50 +    /**
    3.51 +     * method-run.
    3.52 +     */
    3.53 +    void run() throws Exception {
    3.54 +        File testSrc = new File(System.getProperty("test.src"));
    3.55 +        File file = new File(testSrc, "TestDocComments.java");
    3.56 +
    3.57 +        JavacTool tool = JavacTool.create();
    3.58 +        StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    3.59 +
    3.60 +        StringWriter sw = new StringWriter();
    3.61 +        PrintWriter pw = new PrintWriter(sw);
    3.62 +        Iterable<? extends JavaFileObject> fileObjects = fm.getJavaFileObjects(file);
    3.63 +        JavacTask task = tool.getTask(pw, fm, null, null, null, fileObjects);
    3.64 +        Iterable<? extends CompilationUnitTree> units = task.parse();
    3.65 +        Trees trees = Trees.instance(task);
    3.66 +
    3.67 +        CommentScanner s = new CommentScanner();
    3.68 +        int n = s.scan(units, trees);
    3.69 +
    3.70 +        if (n != 12)
    3.71 +            error("Unexpected number of doc comments found: " + n);
    3.72 +
    3.73 +        if (errors > 0)
    3.74 +            throw new Exception(errors + " errors occurred");
    3.75 +    }
    3.76 +
    3.77 +    /**
    3.78 +     * class-CommentScanner.
    3.79 +     */
    3.80 +    class CommentScanner extends TreePathScanner<Integer,Trees> {
    3.81 +
    3.82 +        /**
    3.83 +         * method-visitClass.
    3.84 +         */
    3.85 +        @Override
    3.86 +        public Integer visitClass(ClassTree t, Trees trees) {
    3.87 +            return reduce(super.visitClass(t, trees),
    3.88 +                    check(trees, "class-" + t.getSimpleName() + "."));
    3.89 +        }
    3.90 +
    3.91 +        /**
    3.92 +         * method-visitMethod.
    3.93 +         */
    3.94 +        @Override
    3.95 +        public Integer visitMethod(MethodTree t, Trees trees) {
    3.96 +            return reduce(super.visitMethod(t, trees),
    3.97 +                    check(trees, "method-" + t.getName() + "."));
    3.98 +        }
    3.99 +
   3.100 +        /**
   3.101 +         * method-visitVariable.
   3.102 +         */
   3.103 +        @Override
   3.104 +        public Integer visitVariable(VariableTree t, Trees trees) {
   3.105 +            // for simplicity, only check fields, not parameters or local decls
   3.106 +            int n = (getCurrentPath().getParentPath().getLeaf().getKind() == Tree.Kind.CLASS)
   3.107 +                    ? check(trees, "field-" + t.getName() + ".")
   3.108 +                    : 0;
   3.109 +            return reduce(super.visitVariable(t, trees), n);
   3.110 +        }
   3.111 +
   3.112 +        /**
   3.113 +         * method-reduce.
   3.114 +         */
   3.115 +        @Override
   3.116 +        public Integer reduce(Integer i1, Integer i2) {
   3.117 +            return (i1 == null) ? i2 : (i2 == null) ? i1 : Integer.valueOf(i1 + i2);
   3.118 +        }
   3.119 +
   3.120 +        /**
   3.121 +         * method-check.
   3.122 +         */
   3.123 +        int check(Trees trees, String expect) {
   3.124 +            TreePath p = getCurrentPath();
   3.125 +            String dc = trees.getDocComment(p);
   3.126 +
   3.127 +            if (dc != null && dc.trim().equals(expect))
   3.128 +                return 1;
   3.129 +
   3.130 +            Tree.Kind k = p.getLeaf().getKind();
   3.131 +            if (dc == null)
   3.132 +                error("no doc comment for " + k);
   3.133 +            else
   3.134 +                error("unexpected doc comment for " + k + "\nexpect: " + expect + "\nfound:  " + dc);
   3.135 +
   3.136 +            return 0;
   3.137 +        }
   3.138 +    }
   3.139 +
   3.140 +    /**
   3.141 +     * method-nullCheck.
   3.142 +     */
   3.143 +    int nullCheck(Integer i) {
   3.144 +        return (i == null) ? 0 : i;
   3.145 +    }
   3.146 +
   3.147 +    /**
   3.148 +     * method-error.
   3.149 +     */
   3.150 +    void error(String msg) {
   3.151 +        System.err.println("Error: " + msg);
   3.152 +        errors++;
   3.153 +    }
   3.154 +
   3.155 +    /**
   3.156 +     * field-errors.
   3.157 +     */
   3.158 +    int errors;
   3.159 +}
   3.160 +

mercurial