23
23
24
24
/*
25
25
* @test
26
- * @bug 7021614
26
+ * @bug 7021614 8278373
27
27
* @summary extend com.sun.source API to support parsing javadoc comments
28
28
* @summary check references in at-see and {at-link} tags
29
29
* @modules jdk.compiler
39
39
import com .sun .source .doctree .TextTree ;
40
40
import com .sun .source .util .DocTreePath ;
41
41
import com .sun .source .util .DocTreePathScanner ;
42
- import com .sun .source .util .DocTreeScanner ;
43
42
import com .sun .source .util .DocTrees ;
44
43
import com .sun .source .util .TreePath ;
45
44
46
45
import java .util .List ;
47
46
import java .util .Set ;
47
+ import java .util .stream .Collectors ;
48
48
import javax .annotation .processing .AbstractProcessor ;
49
49
import javax .annotation .processing .ProcessingEnvironment ;
50
50
import javax .annotation .processing .RoundEnvironment ;
51
51
import javax .annotation .processing .SupportedAnnotationTypes ;
52
52
import javax .lang .model .SourceVersion ;
53
53
import javax .lang .model .element .Element ;
54
+ import javax .lang .model .element .ExecutableElement ;
55
+ import javax .lang .model .element .QualifiedNameable ;
54
56
import javax .lang .model .element .TypeElement ;
57
+ import javax .lang .model .type .DeclaredType ;
58
+ import javax .lang .model .type .TypeMirror ;
55
59
import javax .tools .Diagnostic .Kind ;
56
60
57
61
/**
@@ -174,15 +178,48 @@ void checkReference(ReferenceTree tree, List<? extends DocTree> label) {
174
178
if (label .size () > 0 && label .get (0 ) instanceof TextTree )
175
179
expect = ((TextTree ) label .get (0 )).getBody ();
176
180
177
- if (!expect .equalsIgnoreCase (found == null ? "bad" : found .getKind ().name ())) {
178
- error (tree , "Unexpected value found: " + found +", expected: " + expect );
181
+ if (expect .startsWith ("signature:" )) {
182
+ expect = expect .substring ("signature:" .length ());
183
+
184
+ String signature = found .getKind ().name () + ":" + elementSignature (found );
185
+
186
+ if (!expect .equalsIgnoreCase (signature )) {
187
+ error (tree , "Unexpected value found: " + signature +", expected: " + expect );
188
+ }
189
+ } else {
190
+ if (!expect .equalsIgnoreCase (found == null ? "bad" : found .getKind ().name ())) {
191
+ error (tree , "Unexpected value found: " + found +", expected: " + expect );
192
+ }
179
193
}
180
194
}
181
195
182
196
void error (DocTree tree , String msg ) {
183
197
trees .printMessage (Kind .ERROR , msg , tree , dc , path .getCompilationUnit ());
184
198
}
185
199
}
200
+
201
+ String elementSignature (Element el ) {
202
+ return switch (el .getKind ()) {
203
+ case METHOD -> elementSignature (el .getEnclosingElement ()) + "." + el .getSimpleName () + "(" + executableParamNames ((ExecutableElement ) el ) + ")" ;
204
+ case CLASS , INTERFACE -> ((QualifiedNameable ) el ).getQualifiedName ().toString ();
205
+ default -> throw new AssertionError ("Unhandled Element kind: " + el .getKind ());
206
+ };
207
+ }
208
+
209
+ String executableParamNames (ExecutableElement ee ) {
210
+ return ee .getParameters ()
211
+ .stream ()
212
+ .map (p -> type2Name (p .asType ()))
213
+ .collect (Collectors .joining (", " ));
214
+ }
215
+
216
+ String type2Name (TypeMirror type ) {
217
+ return switch (type .getKind ()) {
218
+ case DECLARED -> elementSignature (((DeclaredType ) type ).asElement ());
219
+ case INT , LONG -> type .toString ();
220
+ default -> throw new AssertionError ("Unhandled type kind: " + type .getKind ());
221
+ };
222
+ }
186
223
}
187
224
188
225
/**
@@ -199,6 +236,17 @@ void error(DocTree tree, String msg) {
199
236
* @see #varargs(int... args) Method
200
237
* @see #varargs(int[]) Method
201
238
* @see #varargs(int[] args) Method
239
+ *
240
+ * @see #methodSearch(String) signature:METHOD:ReferenceTestExtras.methodSearch(java.lang.String)
241
+ * @see #methodSearch(StringBuilder) signature:METHOD:ReferenceTestExtras.methodSearch(java.lang.CharSequence)
242
+ * @see #methodSearchPrimitive1(int, int) signature:METHOD:ReferenceTestExtras.methodSearchPrimitive1(int, int)
243
+ * @see #methodSearchPrimitive1(long, int) signature:METHOD:ReferenceTestExtras.methodSearchPrimitive1(long, int)
244
+ * @see #methodSearchPrimitive1(int, long) signature:METHOD:ReferenceTestExtras.methodSearchPrimitive1(int, long)
245
+ * @see #methodSearchPrimitive1(long, long) signature:METHOD:ReferenceTestExtras.methodSearchPrimitive1(long, long)
246
+ * @see #methodSearchPrimitive2(int, int) signature:METHOD:ReferenceTestExtras.methodSearchPrimitive2(int, int)
247
+ * @see #methodSearchPrimitive2(long, int) signature:METHOD:ReferenceTestExtras.methodSearchPrimitive2(long, int)
248
+ * @see #methodSearchPrimitive2(int, long) signature:METHOD:ReferenceTestExtras.methodSearchPrimitive2(int, long)
249
+ * @see #methodSearchPrimitive2(long, long) signature:METHOD:ReferenceTestExtras.methodSearchPrimitive2(long, long)
202
250
*/
203
251
class ReferenceTestExtras {
204
252
int ReferenceTestExtras ; // field
@@ -214,6 +262,20 @@ void m(int i) { }
214
262
void m (int i , int j ) { }
215
263
216
264
void varargs (int ... args ) { }
265
+
266
+ void methodSearch (Object o ) {}
267
+ void methodSearch (String s ) {}
268
+ void methodSearch (CharSequence cs ) {}
269
+
270
+ void methodSearchPrimitive1 (int i , int j ) {}
271
+ void methodSearchPrimitive1 (long i , int j ) {}
272
+ void methodSearchPrimitive1 (int i , long j ) {}
273
+ void methodSearchPrimitive1 (long i , long j ) {}
274
+
275
+ void methodSearchPrimitive2 (long i , long j ) {}
276
+ void methodSearchPrimitive2 (int i , long j ) {}
277
+ void methodSearchPrimitive2 (long i , int j ) {}
278
+ void methodSearchPrimitive2 (int i , int j ) {}
217
279
}
218
280
219
281
0 commit comments