Wed, 23 Jan 2013 13:27:24 -0800
8006775: JSR 308: Compiler changes in JDK8
Reviewed-by: jjg
Contributed-by: mernst@cs.washington.edu, wmdietl@cs.washington.edu, mpapi@csail.mit.edu, mahmood@notnoop.com
jjg@1409 | 1 | /* |
jjg@1409 | 2 | * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. |
jjg@1409 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@1409 | 4 | * |
jjg@1409 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@1409 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@1409 | 7 | * published by the Free Software Foundation. Oracle designates this |
jjg@1409 | 8 | * particular file as subject to the "Classpath" exception as provided |
jjg@1409 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
jjg@1409 | 10 | * |
jjg@1409 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@1409 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@1409 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@1409 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@1409 | 15 | * accompanied this code). |
jjg@1409 | 16 | * |
jjg@1409 | 17 | * You should have received a copy of the GNU General Public License version |
jjg@1409 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@1409 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@1409 | 20 | * |
jjg@1409 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jjg@1409 | 22 | * or visit www.oracle.com if you need additional information or have any |
jjg@1409 | 23 | * questions. |
jjg@1409 | 24 | */ |
jjg@1409 | 25 | |
jjg@1409 | 26 | package com.sun.source.util; |
jjg@1409 | 27 | |
jjg@1409 | 28 | import com.sun.source.doctree.*; |
jjg@1409 | 29 | |
jjg@1409 | 30 | |
jjg@1409 | 31 | /** |
jjg@1409 | 32 | * A TreeVisitor that visits all the child tree nodes. |
jjg@1409 | 33 | * To visit nodes of a particular type, just override the |
jjg@1409 | 34 | * corresponding visitXYZ method. |
jjg@1409 | 35 | * Inside your method, call super.visitXYZ to visit descendant |
jjg@1409 | 36 | * nodes. |
jjg@1409 | 37 | * |
jjg@1409 | 38 | * <p>The default implementation of the visitXYZ methods will determine |
jjg@1409 | 39 | * a result as follows: |
jjg@1409 | 40 | * <ul> |
jjg@1409 | 41 | * <li>If the node being visited has no children, the result will be null. |
jjg@1409 | 42 | * <li>If the node being visited has one child, the result will be the |
jjg@1409 | 43 | * result of calling {@code scan} on that child. The child may be a simple node |
jjg@1409 | 44 | * or itself a list of nodes. |
jjg@1409 | 45 | * <li> If the node being visited has more than one child, the result will |
jjg@1409 | 46 | * be determined by calling {@code scan} each child in turn, and then combining the |
jjg@1409 | 47 | * result of each scan after the first with the cumulative result |
jjg@1409 | 48 | * so far, as determined by the {@link #reduce} method. Each child may be either |
jjg@1409 | 49 | * a simple node of a list of nodes. The default behavior of the {@code reduce} |
jjg@1409 | 50 | * method is such that the result of the visitXYZ method will be the result of |
jjg@1409 | 51 | * the last child scanned. |
jjg@1409 | 52 | * </ul> |
jjg@1409 | 53 | * |
jjg@1409 | 54 | * <p>Here is an example to count the number of erroneous nodes in a tree: |
jjg@1409 | 55 | * <pre> |
jjg@1409 | 56 | * class CountErrors extends DocTreeScanner<Integer,Void> { |
jjg@1409 | 57 | * {@literal @}Override |
jjg@1409 | 58 | * public Integer visitErroneous(ErroneousTree node, Void p) { |
jjg@1409 | 59 | * return 1; |
jjg@1409 | 60 | * } |
jjg@1409 | 61 | * {@literal @}Override |
jjg@1409 | 62 | * public Integer reduce(Integer r1, Integer r2) { |
jjg@1409 | 63 | * return (r1 == null ? 0 : r1) + (r2 == null ? 0 : r2); |
jjg@1409 | 64 | * } |
jjg@1409 | 65 | * } |
jjg@1409 | 66 | * </pre> |
jjg@1409 | 67 | * |
jjg@1409 | 68 | * @since 1.8 |
jjg@1409 | 69 | */ |
jjg@1409 | 70 | public class DocTreeScanner<R,P> implements DocTreeVisitor<R,P> { |
jjg@1409 | 71 | |
jjg@1409 | 72 | /** |
jjg@1409 | 73 | * Scan a single node. |
jjg@1409 | 74 | */ |
jjg@1409 | 75 | public R scan(DocTree node, P p) { |
jjg@1409 | 76 | return (node == null) ? null : node.accept(this, p); |
jjg@1409 | 77 | } |
jjg@1409 | 78 | |
jjg@1409 | 79 | private R scanAndReduce(DocTree node, P p, R r) { |
jjg@1409 | 80 | return reduce(scan(node, p), r); |
jjg@1409 | 81 | } |
jjg@1409 | 82 | |
jjg@1409 | 83 | /** |
jjg@1409 | 84 | * Scan a list of nodes. |
jjg@1409 | 85 | */ |
jjg@1409 | 86 | public R scan(Iterable<? extends DocTree> nodes, P p) { |
jjg@1409 | 87 | R r = null; |
jjg@1409 | 88 | if (nodes != null) { |
jjg@1409 | 89 | boolean first = true; |
jjg@1409 | 90 | for (DocTree node : nodes) { |
jjg@1409 | 91 | r = (first ? scan(node, p) : scanAndReduce(node, p, r)); |
jjg@1409 | 92 | first = false; |
jjg@1409 | 93 | } |
jjg@1409 | 94 | } |
jjg@1409 | 95 | return r; |
jjg@1409 | 96 | } |
jjg@1409 | 97 | |
jjg@1409 | 98 | private R scanAndReduce(Iterable<? extends DocTree> nodes, P p, R r) { |
jjg@1409 | 99 | return reduce(scan(nodes, p), r); |
jjg@1409 | 100 | } |
jjg@1409 | 101 | |
jjg@1409 | 102 | /** |
jjg@1409 | 103 | * Reduces two results into a combined result. |
jjg@1409 | 104 | * The default implementation is to return the first parameter. |
jjg@1409 | 105 | * The general contract of the method is that it may take any action whatsoever. |
jjg@1409 | 106 | */ |
jjg@1409 | 107 | public R reduce(R r1, R r2) { |
jjg@1409 | 108 | return r1; |
jjg@1409 | 109 | } |
jjg@1409 | 110 | |
jjg@1409 | 111 | |
jjg@1409 | 112 | /* *************************************************************************** |
jjg@1409 | 113 | * Visitor methods |
jjg@1409 | 114 | ****************************************************************************/ |
jjg@1409 | 115 | |
jjg@1409 | 116 | @Override |
jjg@1409 | 117 | public R visitAttribute(AttributeTree node, P p) { |
jjg@1409 | 118 | return null; |
jjg@1409 | 119 | } |
jjg@1409 | 120 | |
jjg@1409 | 121 | @Override |
jjg@1409 | 122 | public R visitAuthor(AuthorTree node, P p) { |
jjg@1409 | 123 | return scan(node.getName(), p); |
jjg@1409 | 124 | } |
jjg@1409 | 125 | |
jjg@1409 | 126 | @Override |
jjg@1409 | 127 | public R visitComment(CommentTree node, P p) { |
jjg@1409 | 128 | return null; |
jjg@1409 | 129 | } |
jjg@1409 | 130 | |
jjg@1409 | 131 | @Override |
jjg@1409 | 132 | public R visitDeprecated(DeprecatedTree node, P p) { |
jjg@1409 | 133 | return scan(node.getBody(), p); |
jjg@1409 | 134 | } |
jjg@1409 | 135 | |
jjg@1409 | 136 | @Override |
jjg@1409 | 137 | public R visitDocComment(DocCommentTree node, P p) { |
jjg@1409 | 138 | R r = scan(node.getFirstSentence(), p); |
jjg@1409 | 139 | r = scanAndReduce(node.getBody(), p, r); |
jjg@1409 | 140 | r = scanAndReduce(node.getBlockTags(), p, r); |
jjg@1409 | 141 | return r; |
jjg@1409 | 142 | } |
jjg@1409 | 143 | |
jjg@1409 | 144 | @Override |
jjg@1409 | 145 | public R visitDocRoot(DocRootTree node, P p) { |
jjg@1409 | 146 | return null; |
jjg@1409 | 147 | } |
jjg@1409 | 148 | |
jjg@1409 | 149 | @Override |
jjg@1409 | 150 | public R visitEndElement(EndElementTree node, P p) { |
jjg@1409 | 151 | return null; |
jjg@1409 | 152 | } |
jjg@1409 | 153 | |
jjg@1409 | 154 | @Override |
jjg@1409 | 155 | public R visitEntity(EntityTree node, P p) { |
jjg@1409 | 156 | return null; |
jjg@1409 | 157 | } |
jjg@1409 | 158 | |
jjg@1409 | 159 | @Override |
jjg@1409 | 160 | public R visitErroneous(ErroneousTree node, P p) { |
jjg@1409 | 161 | return null; |
jjg@1409 | 162 | } |
jjg@1409 | 163 | |
jjg@1409 | 164 | @Override |
jjg@1409 | 165 | public R visitIdentifier(IdentifierTree node, P p) { |
jjg@1409 | 166 | return null; |
jjg@1409 | 167 | } |
jjg@1409 | 168 | |
jjg@1409 | 169 | @Override |
jjg@1409 | 170 | public R visitInheritDoc(InheritDocTree node, P p) { |
jjg@1409 | 171 | return null; |
jjg@1409 | 172 | } |
jjg@1409 | 173 | |
jjg@1409 | 174 | @Override |
jjg@1409 | 175 | public R visitLink(LinkTree node, P p) { |
jjg@1409 | 176 | R r = scan(node.getReference(), p); |
jjg@1409 | 177 | r = scanAndReduce(node.getLabel(), p, r); |
jjg@1409 | 178 | return r; |
jjg@1409 | 179 | } |
jjg@1409 | 180 | |
jjg@1409 | 181 | @Override |
jjg@1409 | 182 | public R visitLiteral(LiteralTree node, P p) { |
jjg@1409 | 183 | return null; |
jjg@1409 | 184 | } |
jjg@1409 | 185 | |
jjg@1409 | 186 | @Override |
jjg@1409 | 187 | public R visitParam(ParamTree node, P p) { |
jjg@1409 | 188 | R r = scan(node.getName(), p); |
jjg@1409 | 189 | r = scanAndReduce(node.getDescription(), p, r); |
jjg@1409 | 190 | return r; |
jjg@1409 | 191 | } |
jjg@1409 | 192 | |
jjg@1409 | 193 | @Override |
jjg@1409 | 194 | public R visitReference(ReferenceTree node, P p) { |
jjg@1409 | 195 | return null; |
jjg@1409 | 196 | } |
jjg@1409 | 197 | |
jjg@1409 | 198 | @Override |
jjg@1409 | 199 | public R visitReturn(ReturnTree node, P p) { |
jjg@1409 | 200 | return scan(node.getDescription(), p); |
jjg@1409 | 201 | } |
jjg@1409 | 202 | |
jjg@1409 | 203 | @Override |
jjg@1409 | 204 | public R visitSee(SeeTree node, P p) { |
jjg@1409 | 205 | return scan(node.getReference(), p); |
jjg@1409 | 206 | } |
jjg@1409 | 207 | |
jjg@1409 | 208 | @Override |
jjg@1409 | 209 | public R visitSerial(SerialTree node, P p) { |
jjg@1409 | 210 | return scan(node.getDescription(), p); |
jjg@1409 | 211 | } |
jjg@1409 | 212 | |
jjg@1409 | 213 | @Override |
jjg@1409 | 214 | public R visitSerialData(SerialDataTree node, P p) { |
jjg@1409 | 215 | return scan(node.getDescription(), p); |
jjg@1409 | 216 | } |
jjg@1409 | 217 | |
jjg@1409 | 218 | @Override |
jjg@1409 | 219 | public R visitSerialField(SerialFieldTree node, P p) { |
jjg@1409 | 220 | R r = scan(node.getName(), p); |
jjg@1409 | 221 | r = scanAndReduce(node.getType(), p, r); |
jjg@1409 | 222 | r = scanAndReduce(node.getDescription(), p, r); |
jjg@1409 | 223 | return r; |
jjg@1409 | 224 | } |
jjg@1409 | 225 | |
jjg@1409 | 226 | @Override |
jjg@1409 | 227 | public R visitSince(SinceTree node, P p) { |
jjg@1409 | 228 | return scan(node.getBody(), p); |
jjg@1409 | 229 | } |
jjg@1409 | 230 | |
jjg@1409 | 231 | @Override |
jjg@1409 | 232 | public R visitStartElement(StartElementTree node, P p) { |
jjg@1409 | 233 | return scan(node.getAttributes(), p); |
jjg@1409 | 234 | } |
jjg@1409 | 235 | |
jjg@1409 | 236 | @Override |
jjg@1409 | 237 | public R visitText(TextTree node, P p) { |
jjg@1409 | 238 | return null; |
jjg@1409 | 239 | } |
jjg@1409 | 240 | |
jjg@1409 | 241 | @Override |
jjg@1409 | 242 | public R visitThrows(ThrowsTree node, P p) { |
jjg@1409 | 243 | R r = scan(node.getExceptionName(), p); |
jjg@1409 | 244 | r = scanAndReduce(node.getDescription(), p, r); |
jjg@1409 | 245 | return r; |
jjg@1409 | 246 | } |
jjg@1409 | 247 | |
jjg@1409 | 248 | @Override |
jjg@1409 | 249 | public R visitUnknownBlockTag(UnknownBlockTagTree node, P p) { |
jjg@1409 | 250 | return scan(node.getContent(), p); |
jjg@1409 | 251 | } |
jjg@1409 | 252 | |
jjg@1409 | 253 | @Override |
jjg@1409 | 254 | public R visitUnknownInlineTag(UnknownInlineTagTree node, P p) { |
jjg@1409 | 255 | return scan(node.getContent(), p); |
jjg@1409 | 256 | } |
jjg@1409 | 257 | |
jjg@1409 | 258 | @Override |
jjg@1409 | 259 | public R visitValue(ValueTree node, P p) { |
jjg@1409 | 260 | return scan(node.getReference(), p); |
jjg@1409 | 261 | } |
jjg@1409 | 262 | |
jjg@1409 | 263 | @Override |
jjg@1409 | 264 | public R visitVersion(VersionTree node, P p) { |
jjg@1409 | 265 | return scan(node.getBody(), p); |
jjg@1409 | 266 | } |
jjg@1409 | 267 | |
jjg@1409 | 268 | @Override |
jjg@1409 | 269 | public R visitOther(DocTree node, P p) { |
jjg@1409 | 270 | return null; |
jjg@1409 | 271 | } |
jjg@1409 | 272 | |
jjg@1409 | 273 | } |