langtools/test/tools/javac/api/TestDocComments.java
author avstepan
Tue, 19 May 2015 16:04:14 +0400
changeset 30655 d83f50188ca9
parent 27319 030080f03e4f
child 30730 d3ce7619db2c
permissions -rw-r--r--
8080422: some docs cleanup for core libs Summary: some docs cleanup Reviewed-by: rriggs, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7631
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
     1
/*
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7631
diff changeset
     2
 * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
7631
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
     4
 *
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
     8
 *
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    13
 * accompanied this code).
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    14
 *
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    18
 *
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    21
 * questions.
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    22
 */
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    23
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    24
/* @test
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    25
 * @bug 6985202
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    26
 * @summary no access to doc comments from Tree API
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    27
 */
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    28
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    29
import java.io.*;
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    30
import java.util.*;
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    31
import javax.tools.*;
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    32
import com.sun.source.tree.*;
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    33
import com.sun.source.util.*;
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    34
import com.sun.tools.javac.api.JavacTool;
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    35
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    36
/**
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    37
 * class-TestDocComments.
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    38
 */
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    39
public class TestDocComments {
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    40
    /**
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    41
     * method-main.
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    42
     */
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    43
    public static void main(String... args) throws Exception {
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    44
        new TestDocComments().run();
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    45
    }
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    46
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    47
    /**
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    48
     * method-run.
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    49
     */
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    50
    void run() throws Exception {
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    51
        File testSrc = new File(System.getProperty("test.src"));
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    52
        File file = new File(testSrc, "TestDocComments.java");
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    53
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    54
        JavacTool tool = JavacTool.create();
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7631
diff changeset
    55
        try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) {
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7631
diff changeset
    56
            StringWriter sw = new StringWriter();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7631
diff changeset
    57
            PrintWriter pw = new PrintWriter(sw);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7631
diff changeset
    58
            Iterable<? extends JavaFileObject> fileObjects = fm.getJavaFileObjects(file);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7631
diff changeset
    59
            JavacTask task = tool.getTask(pw, fm, null, null, null, fileObjects);
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7631
diff changeset
    60
            Iterable<? extends CompilationUnitTree> units = task.parse();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7631
diff changeset
    61
            Trees trees = Trees.instance(task);
7631
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    62
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7631
diff changeset
    63
            CommentScanner s = new CommentScanner();
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7631
diff changeset
    64
            int n = s.scan(units, trees);
7631
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    65
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7631
diff changeset
    66
            if (n != 12)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7631
diff changeset
    67
                error("Unexpected number of doc comments found: " + n);
7631
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    68
27319
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7631
diff changeset
    69
            if (errors > 0)
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7631
diff changeset
    70
                throw new Exception(errors + " errors occurred");
030080f03e4f 8062348: langtools tests should close file manager (group 1)
jjg
parents: 7631
diff changeset
    71
        }
7631
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    72
    }
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    73
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    74
    /**
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    75
     * class-CommentScanner.
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    76
     */
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    77
    class CommentScanner extends TreePathScanner<Integer,Trees> {
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    78
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    79
        /**
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    80
         * method-visitClass.
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    81
         */
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    82
        @Override
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    83
        public Integer visitClass(ClassTree t, Trees trees) {
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    84
            return reduce(super.visitClass(t, trees),
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    85
                    check(trees, "class-" + t.getSimpleName() + "."));
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    86
        }
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    87
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    88
        /**
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    89
         * method-visitMethod.
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    90
         */
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    91
        @Override
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    92
        public Integer visitMethod(MethodTree t, Trees trees) {
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    93
            return reduce(super.visitMethod(t, trees),
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    94
                    check(trees, "method-" + t.getName() + "."));
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    95
        }
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    96
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    97
        /**
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    98
         * method-visitVariable.
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
    99
         */
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   100
        @Override
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   101
        public Integer visitVariable(VariableTree t, Trees trees) {
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   102
            // for simplicity, only check fields, not parameters or local decls
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   103
            int n = (getCurrentPath().getParentPath().getLeaf().getKind() == Tree.Kind.CLASS)
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   104
                    ? check(trees, "field-" + t.getName() + ".")
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   105
                    : 0;
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   106
            return reduce(super.visitVariable(t, trees), n);
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   107
        }
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   108
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   109
        /**
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   110
         * method-reduce.
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   111
         */
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   112
        @Override
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   113
        public Integer reduce(Integer i1, Integer i2) {
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   114
            return (i1 == null) ? i2 : (i2 == null) ? i1 : Integer.valueOf(i1 + i2);
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   115
        }
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   116
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   117
        /**
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   118
         * method-check.
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   119
         */
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   120
        int check(Trees trees, String expect) {
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   121
            TreePath p = getCurrentPath();
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   122
            String dc = trees.getDocComment(p);
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   123
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   124
            if (dc != null && dc.trim().equals(expect))
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   125
                return 1;
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   126
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   127
            Tree.Kind k = p.getLeaf().getKind();
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   128
            if (dc == null)
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   129
                error("no doc comment for " + k);
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   130
            else
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   131
                error("unexpected doc comment for " + k + "\nexpect: " + expect + "\nfound:  " + dc);
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   132
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   133
            return 0;
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   134
        }
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   135
    }
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   136
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   137
    /**
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   138
     * method-nullCheck.
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   139
     */
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   140
    int nullCheck(Integer i) {
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   141
        return (i == null) ? 0 : i;
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   142
    }
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   143
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   144
    /**
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   145
     * method-error.
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   146
     */
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   147
    void error(String msg) {
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   148
        System.err.println("Error: " + msg);
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   149
        errors++;
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   150
    }
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   151
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   152
    /**
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   153
     * field-errors.
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   154
     */
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   155
    int errors;
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   156
}
57bce845dcee 6985202: no access to doc comments from Tree API
jjg
parents:
diff changeset
   157