test/tools/javac/api/TestDocComments.java

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

mercurial