aoqi@0: /* aoqi@0: * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* @test aoqi@0: * @bug 6985202 aoqi@0: * @summary no access to doc comments from Tree API aoqi@0: */ aoqi@0: aoqi@0: import java.io.*; aoqi@0: import java.util.*; aoqi@0: import javax.tools.*; aoqi@0: import com.sun.source.tree.*; aoqi@0: import com.sun.source.util.*; aoqi@0: import com.sun.tools.javac.api.JavacTool; aoqi@0: aoqi@0: /** aoqi@0: * class-TestDocComments. aoqi@0: */ aoqi@0: public class TestDocComments { aoqi@0: /** aoqi@0: * method-main. aoqi@0: */ aoqi@0: public static void main(String... args) throws Exception { aoqi@0: new TestDocComments().run(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * method-run. aoqi@0: */ aoqi@0: void run() throws Exception { aoqi@0: File testSrc = new File(System.getProperty("test.src")); aoqi@0: File file = new File(testSrc, "TestDocComments.java"); aoqi@0: aoqi@0: JavacTool tool = JavacTool.create(); aoqi@0: StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); aoqi@0: aoqi@0: StringWriter sw = new StringWriter(); aoqi@0: PrintWriter pw = new PrintWriter(sw); aoqi@0: Iterable fileObjects = fm.getJavaFileObjects(file); aoqi@0: JavacTask task = tool.getTask(pw, fm, null, null, null, fileObjects); aoqi@0: Iterable units = task.parse(); aoqi@0: Trees trees = Trees.instance(task); aoqi@0: aoqi@0: CommentScanner s = new CommentScanner(); aoqi@0: int n = s.scan(units, trees); aoqi@0: aoqi@0: if (n != 12) aoqi@0: error("Unexpected number of doc comments found: " + n); aoqi@0: aoqi@0: if (errors > 0) aoqi@0: throw new Exception(errors + " errors occurred"); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * class-CommentScanner. aoqi@0: */ aoqi@0: class CommentScanner extends TreePathScanner { aoqi@0: aoqi@0: /** aoqi@0: * method-visitClass. aoqi@0: */ aoqi@0: @Override aoqi@0: public Integer visitClass(ClassTree t, Trees trees) { aoqi@0: return reduce(super.visitClass(t, trees), aoqi@0: check(trees, "class-" + t.getSimpleName() + ".")); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * method-visitMethod. aoqi@0: */ aoqi@0: @Override aoqi@0: public Integer visitMethod(MethodTree t, Trees trees) { aoqi@0: return reduce(super.visitMethod(t, trees), aoqi@0: check(trees, "method-" + t.getName() + ".")); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * method-visitVariable. aoqi@0: */ aoqi@0: @Override aoqi@0: public Integer visitVariable(VariableTree t, Trees trees) { aoqi@0: // for simplicity, only check fields, not parameters or local decls aoqi@0: int n = (getCurrentPath().getParentPath().getLeaf().getKind() == Tree.Kind.CLASS) aoqi@0: ? check(trees, "field-" + t.getName() + ".") aoqi@0: : 0; aoqi@0: return reduce(super.visitVariable(t, trees), n); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * method-reduce. aoqi@0: */ aoqi@0: @Override aoqi@0: public Integer reduce(Integer i1, Integer i2) { aoqi@0: return (i1 == null) ? i2 : (i2 == null) ? i1 : Integer.valueOf(i1 + i2); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * method-check. aoqi@0: */ aoqi@0: int check(Trees trees, String expect) { aoqi@0: TreePath p = getCurrentPath(); aoqi@0: String dc = trees.getDocComment(p); aoqi@0: aoqi@0: if (dc != null && dc.trim().equals(expect)) aoqi@0: return 1; aoqi@0: aoqi@0: Tree.Kind k = p.getLeaf().getKind(); aoqi@0: if (dc == null) aoqi@0: error("no doc comment for " + k); aoqi@0: else aoqi@0: error("unexpected doc comment for " + k + "\nexpect: " + expect + "\nfound: " + dc); aoqi@0: aoqi@0: return 0; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * method-nullCheck. aoqi@0: */ aoqi@0: int nullCheck(Integer i) { aoqi@0: return (i == null) ? 0 : i; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * method-error. aoqi@0: */ aoqi@0: void error(String msg) { aoqi@0: System.err.println("Error: " + msg); aoqi@0: errors++; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * field-errors. aoqi@0: */ aoqi@0: int errors; aoqi@0: } aoqi@0: