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