author | avstepan |
Tue, 19 May 2015 16:04:14 +0400 | |
changeset 30655 | d83f50188ca9 |
parent 27319 | 030080f03e4f |
child 30730 | d3ce7619db2c |
permissions | -rw-r--r-- |
7631 | 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 | 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(); |
|
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 | 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 | 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 | 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 | 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 |