DocTreeScanner.java revision 2571:10fc81ac75b4
1/* 2 * Copyright (c) 2011, 2014, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package com.sun.source.util; 27 28import com.sun.source.doctree.*; 29 30 31/** 32 * A TreeVisitor that visits all the child tree nodes. 33 * To visit nodes of a particular type, just override the 34 * corresponding visitXYZ method. 35 * Inside your method, call super.visitXYZ to visit descendant 36 * nodes. 37 * 38 * <p>The default implementation of the visitXYZ methods will determine 39 * a result as follows: 40 * <ul> 41 * <li>If the node being visited has no children, the result will be {@code null}. 42 * <li>If the node being visited has one child, the result will be the 43 * result of calling {@code scan} on that child. The child may be a simple node 44 * or itself a list of nodes. 45 * <li> If the node being visited has more than one child, the result will 46 * be determined by calling {@code scan} each child in turn, and then combining the 47 * result of each scan after the first with the cumulative result 48 * so far, as determined by the {@link #reduce} method. Each child may be either 49 * a simple node of a list of nodes. The default behavior of the {@code reduce} 50 * method is such that the result of the visitXYZ method will be the result of 51 * the last child scanned. 52 * </ul> 53 * 54 * <p>Here is an example to count the number of erroneous nodes in a tree: 55 * <pre> 56 * class CountErrors extends DocTreeScanner<Integer,Void> { 57 * {@literal @}Override 58 * public Integer visitErroneous(ErroneousTree node, Void p) { 59 * return 1; 60 * } 61 * {@literal @}Override 62 * public Integer reduce(Integer r1, Integer r2) { 63 * return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2); 64 * } 65 * } 66 * </pre> 67 * 68 * @since 1.8 69 */ 70@jdk.Exported 71public class DocTreeScanner<R,P> implements DocTreeVisitor<R,P> { 72 73 /** 74 * Scans a single node. 75 * @param node the node to be scanned 76 * @param p a parameter value passed to the visit method 77 * @return the result value from the visit method 78 */ 79 public R scan(DocTree node, P p) { 80 return (node == null) ? null : node.accept(this, p); 81 } 82 83 private R scanAndReduce(DocTree node, P p, R r) { 84 return reduce(scan(node, p), r); 85 } 86 87 /** 88 * Scans a sequence of nodes. 89 * @param nodes the nodes to be scanned 90 * @param p a parameter value to be passed to the visit method for each node 91 * @return the combined return value from the visit methods. 92 * The values are combined using the {@link #reduce reduce} method. 93 */ 94 public R scan(Iterable<? extends DocTree> nodes, P p) { 95 R r = null; 96 if (nodes != null) { 97 boolean first = true; 98 for (DocTree node : nodes) { 99 r = (first ? scan(node, p) : scanAndReduce(node, p, r)); 100 first = false; 101 } 102 } 103 return r; 104 } 105 106 private R scanAndReduce(Iterable<? extends DocTree> nodes, P p, R r) { 107 return reduce(scan(nodes, p), r); 108 } 109 110 /** 111 * Reduces two results into a combined result. 112 * The default implementation is to return the first parameter. 113 * The general contract of the method is that it may take any action whatsoever. 114 * @param r1 the first of the values to be combined 115 * @param r2 the second of the values to be combined 116 * @return the result of combining the two parameters 117 */ 118 public R reduce(R r1, R r2) { 119 return r1; 120 } 121 122 123/* *************************************************************************** 124 * Visitor methods 125 ****************************************************************************/ 126 127 /** 128 * {@inheritDoc} This implementation returns {@code null}. 129 * 130 * @param node {@inheritDoc} 131 * @param p {@inheritDoc} 132 * @return the result of scanning 133 */ 134 @Override 135 public R visitAttribute(AttributeTree node, P p) { 136 return null; 137 } 138 139 /** 140 * {@inheritDoc} This implementation scans the children in left to right order. 141 * 142 * @param node {@inheritDoc} 143 * @param p {@inheritDoc} 144 * @return the result of scanning 145 */ 146 @Override 147 public R visitAuthor(AuthorTree node, P p) { 148 return scan(node.getName(), p); 149 } 150 151 /** 152 * {@inheritDoc} This implementation returns {@code null}. 153 * 154 * @param node {@inheritDoc} 155 * @param p {@inheritDoc} 156 * @return the result of scanning 157 */ 158 @Override 159 public R visitComment(CommentTree node, P p) { 160 return null; 161 } 162 163 /** 164 * {@inheritDoc} This implementation scans the children in left to right order. 165 * 166 * @param node {@inheritDoc} 167 * @param p {@inheritDoc} 168 * @return the result of scanning 169 */ 170 @Override 171 public R visitDeprecated(DeprecatedTree node, P p) { 172 return scan(node.getBody(), p); 173 } 174 175 /** 176 * {@inheritDoc} This implementation scans the children in left to right order. 177 * 178 * @param node {@inheritDoc} 179 * @param p {@inheritDoc} 180 * @return the result of scanning 181 */ 182 @Override 183 public R visitDocComment(DocCommentTree node, P p) { 184 R r = scan(node.getFirstSentence(), p); 185 r = scanAndReduce(node.getBody(), p, r); 186 r = scanAndReduce(node.getBlockTags(), p, r); 187 return r; 188 } 189 190 /** 191 * {@inheritDoc} This implementation returns {@code null}. 192 * 193 * @param node {@inheritDoc} 194 * @param p {@inheritDoc} 195 * @return the result of scanning 196 */ 197 @Override 198 public R visitDocRoot(DocRootTree node, P p) { 199 return null; 200 } 201 202 /** 203 * {@inheritDoc} This implementation returns {@code null}. 204 * 205 * @param node {@inheritDoc} 206 * @param p {@inheritDoc} 207 * @return the result of scanning 208 */ 209 @Override 210 public R visitEndElement(EndElementTree node, P p) { 211 return null; 212 } 213 214 /** 215 * {@inheritDoc} This implementation returns {@code null}. 216 * 217 * @param node {@inheritDoc} 218 * @param p {@inheritDoc} 219 * @return the result of scanning 220 */ 221 @Override 222 public R visitEntity(EntityTree node, P p) { 223 return null; 224 } 225 226 /** 227 * {@inheritDoc} This implementation returns {@code null}. 228 * 229 * @param node {@inheritDoc} 230 * @param p {@inheritDoc} 231 * @return the result of scanning 232 */ 233 @Override 234 public R visitErroneous(ErroneousTree node, P p) { 235 return null; 236 } 237 238 /** 239 * {@inheritDoc} This implementation returns {@code null}. 240 * 241 * @param node {@inheritDoc} 242 * @param p {@inheritDoc} 243 * @return the result of scanning 244 */ 245 @Override 246 public R visitIdentifier(IdentifierTree node, P p) { 247 return null; 248 } 249 250 /** 251 * {@inheritDoc} This implementation returns {@code null}. 252 * 253 * @param node {@inheritDoc} 254 * @param p {@inheritDoc} 255 * @return the result of scanning 256 */ 257 @Override 258 public R visitInheritDoc(InheritDocTree node, P p) { 259 return null; 260 } 261 262 /** 263 * {@inheritDoc} This implementation scans the children in left to right order. 264 * 265 * @param node {@inheritDoc} 266 * @param p {@inheritDoc} 267 * @return the result of scanning 268 */ 269 @Override 270 public R visitLink(LinkTree node, P p) { 271 R r = scan(node.getReference(), p); 272 r = scanAndReduce(node.getLabel(), p, r); 273 return r; 274 } 275 276 /** 277 * {@inheritDoc} This implementation returns {@code null}. 278 * 279 * @param node {@inheritDoc} 280 * @param p {@inheritDoc} 281 * @return the result of scanning 282 */ 283 @Override 284 public R visitLiteral(LiteralTree node, P p) { 285 return null; 286 } 287 288 /** 289 * {@inheritDoc} This implementation scans the children in left to right order. 290 * 291 * @param node {@inheritDoc} 292 * @param p {@inheritDoc} 293 * @return the result of scanning 294 */ 295 @Override 296 public R visitParam(ParamTree node, P p) { 297 R r = scan(node.getName(), p); 298 r = scanAndReduce(node.getDescription(), p, r); 299 return r; 300 } 301 302 /** 303 * {@inheritDoc} This implementation returns {@code null}. 304 * 305 * @param node {@inheritDoc} 306 * @param p {@inheritDoc} 307 * @return the result of scanning 308 */ 309 @Override 310 public R visitReference(ReferenceTree node, P p) { 311 return null; 312 } 313 314 /** 315 * {@inheritDoc} This implementation scans the children in left to right order. 316 * 317 * @param node {@inheritDoc} 318 * @param p {@inheritDoc} 319 * @return the result of scanning 320 */ 321 @Override 322 public R visitReturn(ReturnTree node, P p) { 323 return scan(node.getDescription(), p); 324 } 325 326 /** 327 * {@inheritDoc} This implementation scans the children in left to right order. 328 * 329 * @param node {@inheritDoc} 330 * @param p {@inheritDoc} 331 * @return the result of scanning 332 */ 333 @Override 334 public R visitSee(SeeTree node, P p) { 335 return scan(node.getReference(), p); 336 } 337 338 /** 339 * {@inheritDoc} This implementation scans the children in left to right order. 340 * 341 * @param node {@inheritDoc} 342 * @param p {@inheritDoc} 343 * @return the result of scanning 344 */ 345 @Override 346 public R visitSerial(SerialTree node, P p) { 347 return scan(node.getDescription(), p); 348 } 349 350 /** 351 * {@inheritDoc} This implementation scans the children in left to right order. 352 * 353 * @param node {@inheritDoc} 354 * @param p {@inheritDoc} 355 * @return the result of scanning 356 */ 357 @Override 358 public R visitSerialData(SerialDataTree node, P p) { 359 return scan(node.getDescription(), p); 360 } 361 362 /** 363 * {@inheritDoc} This implementation scans the children in left to right order. 364 * 365 * @param node {@inheritDoc} 366 * @param p {@inheritDoc} 367 * @return the result of scanning 368 */ 369 @Override 370 public R visitSerialField(SerialFieldTree node, P p) { 371 R r = scan(node.getName(), p); 372 r = scanAndReduce(node.getType(), p, r); 373 r = scanAndReduce(node.getDescription(), p, r); 374 return r; 375 } 376 377 /** 378 * {@inheritDoc} This implementation scans the children in left to right order. 379 * 380 * @param node {@inheritDoc} 381 * @param p {@inheritDoc} 382 * @return the result of scanning 383 */ 384 @Override 385 public R visitSince(SinceTree node, P p) { 386 return scan(node.getBody(), p); 387 } 388 389 /** 390 * {@inheritDoc} This implementation scans the children in left to right order. 391 * 392 * @param node {@inheritDoc} 393 * @param p {@inheritDoc} 394 * @return the result of scanning 395 */ 396 @Override 397 public R visitStartElement(StartElementTree node, P p) { 398 return scan(node.getAttributes(), p); 399 } 400 401 /** 402 * {@inheritDoc} This implementation returns {@code null}. 403 * 404 * @param node {@inheritDoc} 405 * @param p {@inheritDoc} 406 * @return the result of scanning 407 */ 408 @Override 409 public R visitText(TextTree node, P p) { 410 return null; 411 } 412 413 /** 414 * {@inheritDoc} This implementation scans the children in left to right order. 415 * 416 * @param node {@inheritDoc} 417 * @param p {@inheritDoc} 418 * @return the result of scanning 419 */ 420 @Override 421 public R visitThrows(ThrowsTree node, P p) { 422 R r = scan(node.getExceptionName(), p); 423 r = scanAndReduce(node.getDescription(), p, r); 424 return r; 425 } 426 427 /** 428 * {@inheritDoc} This implementation scans the children in left to right order. 429 * 430 * @param node {@inheritDoc} 431 * @param p {@inheritDoc} 432 * @return the result of scanning 433 */ 434 @Override 435 public R visitUnknownBlockTag(UnknownBlockTagTree node, P p) { 436 return scan(node.getContent(), p); 437 } 438 439 /** 440 * {@inheritDoc} This implementation scans the children in left to right order. 441 * 442 * @param node {@inheritDoc} 443 * @param p {@inheritDoc} 444 * @return the result of scanning 445 */ 446 @Override 447 public R visitUnknownInlineTag(UnknownInlineTagTree node, P p) { 448 return scan(node.getContent(), p); 449 } 450 451 /** 452 * {@inheritDoc} This implementation scans the children in left to right order. 453 * 454 * @param node {@inheritDoc} 455 * @param p {@inheritDoc} 456 * @return the result of scanning 457 */ 458 @Override 459 public R visitValue(ValueTree node, P p) { 460 return scan(node.getReference(), p); 461 } 462 463 /** 464 * {@inheritDoc} This implementation scans the children in left to right order. 465 * 466 * @param node {@inheritDoc} 467 * @param p {@inheritDoc} 468 * @return the result of scanning 469 */ 470 @Override 471 public R visitVersion(VersionTree node, P p) { 472 return scan(node.getBody(), p); 473 } 474 475 /** 476 * {@inheritDoc} This implementation returns {@code null}. 477 * 478 * @param node {@inheritDoc} 479 * @param p {@inheritDoc} 480 * @return the result of scanning 481 */ 482 @Override 483 public R visitOther(DocTree node, P p) { 484 return null; 485 } 486 487} 488