Wed, 16 Oct 2013 16:33:04 -0400
8026286: Improper locking of annotation queues causes assertion failures.
8026063: Calls to annotate.flush() cause incorrect type annotations to be generated.
Summary: Fix locking in ClassReader.java
Reviewed-by: jfranck
mcimadamore@1113 | 1 | /* |
alundblad@2047 | 2 | * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. |
mcimadamore@1113 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
mcimadamore@1113 | 4 | * |
mcimadamore@1113 | 5 | * This code is free software; you can redistribute it and/or modify it |
mcimadamore@1113 | 6 | * under the terms of the GNU General Public License version 2 only, as |
mcimadamore@1113 | 7 | * published by the Free Software Foundation. Oracle designates this |
mcimadamore@1113 | 8 | * particular file as subject to the "Classpath" exception as provided |
mcimadamore@1113 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
mcimadamore@1113 | 10 | * |
mcimadamore@1113 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
mcimadamore@1113 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
mcimadamore@1113 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
mcimadamore@1113 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
mcimadamore@1113 | 15 | * accompanied this code). |
mcimadamore@1113 | 16 | * |
mcimadamore@1113 | 17 | * You should have received a copy of the GNU General Public License version |
mcimadamore@1113 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
mcimadamore@1113 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
mcimadamore@1113 | 20 | * |
mcimadamore@1113 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
mcimadamore@1113 | 22 | * or visit www.oracle.com if you need additional information or have any |
mcimadamore@1113 | 23 | * questions. |
mcimadamore@1113 | 24 | */ |
mcimadamore@1113 | 25 | |
mcimadamore@1113 | 26 | package com.sun.tools.javac.parser; |
mcimadamore@1113 | 27 | |
mcimadamore@1113 | 28 | import java.util.Locale; |
mcimadamore@1113 | 29 | |
mcimadamore@1113 | 30 | import com.sun.tools.javac.api.Formattable; |
mcimadamore@1113 | 31 | import com.sun.tools.javac.api.Messages; |
mcimadamore@1113 | 32 | import com.sun.tools.javac.parser.Tokens.Token.Tag; |
mcimadamore@1125 | 33 | import com.sun.tools.javac.util.List; |
mcimadamore@1113 | 34 | import com.sun.tools.javac.util.Name; |
mcimadamore@1113 | 35 | import com.sun.tools.javac.util.Context; |
mcimadamore@1503 | 36 | import com.sun.tools.javac.util.Filter; |
mcimadamore@1125 | 37 | import com.sun.tools.javac.util.ListBuffer; |
mcimadamore@1113 | 38 | import com.sun.tools.javac.util.Names; |
mcimadamore@1113 | 39 | |
mcimadamore@1113 | 40 | /** A class that defines codes/utilities for Java source tokens |
mcimadamore@1113 | 41 | * returned from lexical analysis. |
mcimadamore@1113 | 42 | * |
mcimadamore@1113 | 43 | * <p><b>This is NOT part of any supported API. |
mcimadamore@1113 | 44 | * If you write code that depends on this, you do so at your own risk. |
mcimadamore@1113 | 45 | * This code and its internal interfaces are subject to change or |
mcimadamore@1113 | 46 | * deletion without notice.</b> |
mcimadamore@1113 | 47 | */ |
mcimadamore@1113 | 48 | public class Tokens { |
mcimadamore@1113 | 49 | |
mcimadamore@1113 | 50 | private final Names names; |
mcimadamore@1113 | 51 | |
mcimadamore@1113 | 52 | /** |
mcimadamore@1113 | 53 | * Keyword array. Maps name indices to Token. |
mcimadamore@1113 | 54 | */ |
mcimadamore@1113 | 55 | private final TokenKind[] key; |
mcimadamore@1113 | 56 | |
mcimadamore@1113 | 57 | /** The number of the last entered keyword. |
mcimadamore@1113 | 58 | */ |
mcimadamore@1113 | 59 | private int maxKey = 0; |
mcimadamore@1113 | 60 | |
mcimadamore@1113 | 61 | /** The names of all tokens. |
mcimadamore@1113 | 62 | */ |
mcimadamore@1113 | 63 | private Name[] tokenName = new Name[TokenKind.values().length]; |
mcimadamore@1113 | 64 | |
mcimadamore@1113 | 65 | public static final Context.Key<Tokens> tokensKey = |
mcimadamore@1113 | 66 | new Context.Key<Tokens>(); |
mcimadamore@1113 | 67 | |
mcimadamore@1113 | 68 | public static Tokens instance(Context context) { |
mcimadamore@1113 | 69 | Tokens instance = context.get(tokensKey); |
mcimadamore@1113 | 70 | if (instance == null) |
mcimadamore@1113 | 71 | instance = new Tokens(context); |
mcimadamore@1113 | 72 | return instance; |
mcimadamore@1113 | 73 | } |
mcimadamore@1113 | 74 | |
mcimadamore@1113 | 75 | protected Tokens(Context context) { |
mcimadamore@1113 | 76 | context.put(tokensKey, this); |
mcimadamore@1113 | 77 | names = Names.instance(context); |
mcimadamore@1113 | 78 | for (TokenKind t : TokenKind.values()) { |
mcimadamore@1113 | 79 | if (t.name != null) |
mcimadamore@1113 | 80 | enterKeyword(t.name, t); |
mcimadamore@1113 | 81 | else |
mcimadamore@1113 | 82 | tokenName[t.ordinal()] = null; |
mcimadamore@1113 | 83 | } |
mcimadamore@1113 | 84 | |
mcimadamore@1113 | 85 | key = new TokenKind[maxKey+1]; |
mcimadamore@1113 | 86 | for (int i = 0; i <= maxKey; i++) key[i] = TokenKind.IDENTIFIER; |
mcimadamore@1113 | 87 | for (TokenKind t : TokenKind.values()) { |
mcimadamore@1113 | 88 | if (t.name != null) |
mcimadamore@1113 | 89 | key[tokenName[t.ordinal()].getIndex()] = t; |
mcimadamore@1113 | 90 | } |
mcimadamore@1113 | 91 | } |
mcimadamore@1113 | 92 | |
mcimadamore@1113 | 93 | private void enterKeyword(String s, TokenKind token) { |
mcimadamore@1113 | 94 | Name n = names.fromString(s); |
mcimadamore@1113 | 95 | tokenName[token.ordinal()] = n; |
mcimadamore@1113 | 96 | if (n.getIndex() > maxKey) maxKey = n.getIndex(); |
mcimadamore@1113 | 97 | } |
mcimadamore@1113 | 98 | |
mcimadamore@1113 | 99 | /** |
mcimadamore@1113 | 100 | * Create a new token given a name; if the name corresponds to a token name, |
mcimadamore@1113 | 101 | * a new token of the corresponding kind is returned; otherwise, an |
mcimadamore@1113 | 102 | * identifier token is returned. |
mcimadamore@1113 | 103 | */ |
mcimadamore@1113 | 104 | TokenKind lookupKind(Name name) { |
mcimadamore@1113 | 105 | return (name.getIndex() > maxKey) ? TokenKind.IDENTIFIER : key[name.getIndex()]; |
mcimadamore@1113 | 106 | } |
mcimadamore@1113 | 107 | |
mcimadamore@1113 | 108 | TokenKind lookupKind(String name) { |
mcimadamore@1113 | 109 | return lookupKind(names.fromString(name)); |
mcimadamore@1113 | 110 | } |
mcimadamore@1113 | 111 | |
mcimadamore@1113 | 112 | /** |
mcimadamore@1113 | 113 | * This enum defines all tokens used by the javac scanner. A token is |
mcimadamore@1113 | 114 | * optionally associated with a name. |
mcimadamore@1113 | 115 | */ |
mcimadamore@1503 | 116 | public enum TokenKind implements Formattable, Filter<TokenKind> { |
mcimadamore@1113 | 117 | EOF(), |
mcimadamore@1113 | 118 | ERROR(), |
mcimadamore@1113 | 119 | IDENTIFIER(Tag.NAMED), |
mcimadamore@1113 | 120 | ABSTRACT("abstract"), |
mcimadamore@1113 | 121 | ASSERT("assert", Tag.NAMED), |
mcimadamore@1113 | 122 | BOOLEAN("boolean", Tag.NAMED), |
mcimadamore@1113 | 123 | BREAK("break"), |
mcimadamore@1113 | 124 | BYTE("byte", Tag.NAMED), |
mcimadamore@1113 | 125 | CASE("case"), |
mcimadamore@1113 | 126 | CATCH("catch"), |
mcimadamore@1113 | 127 | CHAR("char", Tag.NAMED), |
mcimadamore@1113 | 128 | CLASS("class"), |
mcimadamore@1113 | 129 | CONST("const"), |
mcimadamore@1113 | 130 | CONTINUE("continue"), |
mcimadamore@1113 | 131 | DEFAULT("default"), |
mcimadamore@1113 | 132 | DO("do"), |
mcimadamore@1113 | 133 | DOUBLE("double", Tag.NAMED), |
mcimadamore@1113 | 134 | ELSE("else"), |
mcimadamore@1113 | 135 | ENUM("enum", Tag.NAMED), |
mcimadamore@1113 | 136 | EXTENDS("extends"), |
mcimadamore@1113 | 137 | FINAL("final"), |
mcimadamore@1113 | 138 | FINALLY("finally"), |
mcimadamore@1113 | 139 | FLOAT("float", Tag.NAMED), |
mcimadamore@1113 | 140 | FOR("for"), |
mcimadamore@1113 | 141 | GOTO("goto"), |
mcimadamore@1113 | 142 | IF("if"), |
mcimadamore@1113 | 143 | IMPLEMENTS("implements"), |
mcimadamore@1113 | 144 | IMPORT("import"), |
mcimadamore@1113 | 145 | INSTANCEOF("instanceof"), |
mcimadamore@1113 | 146 | INT("int", Tag.NAMED), |
mcimadamore@1113 | 147 | INTERFACE("interface"), |
mcimadamore@1113 | 148 | LONG("long", Tag.NAMED), |
mcimadamore@1113 | 149 | NATIVE("native"), |
mcimadamore@1113 | 150 | NEW("new"), |
mcimadamore@1113 | 151 | PACKAGE("package"), |
mcimadamore@1113 | 152 | PRIVATE("private"), |
mcimadamore@1113 | 153 | PROTECTED("protected"), |
mcimadamore@1113 | 154 | PUBLIC("public"), |
mcimadamore@1113 | 155 | RETURN("return"), |
mcimadamore@1113 | 156 | SHORT("short", Tag.NAMED), |
mcimadamore@1113 | 157 | STATIC("static"), |
mcimadamore@1113 | 158 | STRICTFP("strictfp"), |
mcimadamore@1113 | 159 | SUPER("super", Tag.NAMED), |
mcimadamore@1113 | 160 | SWITCH("switch"), |
mcimadamore@1113 | 161 | SYNCHRONIZED("synchronized"), |
mcimadamore@1113 | 162 | THIS("this", Tag.NAMED), |
mcimadamore@1113 | 163 | THROW("throw"), |
mcimadamore@1113 | 164 | THROWS("throws"), |
mcimadamore@1113 | 165 | TRANSIENT("transient"), |
mcimadamore@1113 | 166 | TRY("try"), |
mcimadamore@1113 | 167 | VOID("void", Tag.NAMED), |
mcimadamore@1113 | 168 | VOLATILE("volatile"), |
mcimadamore@1113 | 169 | WHILE("while"), |
mcimadamore@1113 | 170 | INTLITERAL(Tag.NUMERIC), |
mcimadamore@1113 | 171 | LONGLITERAL(Tag.NUMERIC), |
mcimadamore@1113 | 172 | FLOATLITERAL(Tag.NUMERIC), |
mcimadamore@1113 | 173 | DOUBLELITERAL(Tag.NUMERIC), |
mcimadamore@1113 | 174 | CHARLITERAL(Tag.NUMERIC), |
mcimadamore@1113 | 175 | STRINGLITERAL(Tag.STRING), |
mcimadamore@1113 | 176 | TRUE("true", Tag.NAMED), |
mcimadamore@1113 | 177 | FALSE("false", Tag.NAMED), |
mcimadamore@1113 | 178 | NULL("null", Tag.NAMED), |
mcimadamore@1503 | 179 | UNDERSCORE("_", Tag.NAMED), |
mcimadamore@1144 | 180 | ARROW("->"), |
mcimadamore@1352 | 181 | COLCOL("::"), |
mcimadamore@1113 | 182 | LPAREN("("), |
mcimadamore@1113 | 183 | RPAREN(")"), |
mcimadamore@1113 | 184 | LBRACE("{"), |
mcimadamore@1113 | 185 | RBRACE("}"), |
mcimadamore@1113 | 186 | LBRACKET("["), |
mcimadamore@1113 | 187 | RBRACKET("]"), |
mcimadamore@1113 | 188 | SEMI(";"), |
mcimadamore@1113 | 189 | COMMA(","), |
mcimadamore@1113 | 190 | DOT("."), |
mcimadamore@1113 | 191 | ELLIPSIS("..."), |
mcimadamore@1113 | 192 | EQ("="), |
mcimadamore@1113 | 193 | GT(">"), |
mcimadamore@1113 | 194 | LT("<"), |
mcimadamore@1113 | 195 | BANG("!"), |
mcimadamore@1113 | 196 | TILDE("~"), |
mcimadamore@1113 | 197 | QUES("?"), |
mcimadamore@1113 | 198 | COLON(":"), |
mcimadamore@1113 | 199 | EQEQ("=="), |
mcimadamore@1113 | 200 | LTEQ("<="), |
mcimadamore@1113 | 201 | GTEQ(">="), |
mcimadamore@1113 | 202 | BANGEQ("!="), |
mcimadamore@1113 | 203 | AMPAMP("&&"), |
mcimadamore@1113 | 204 | BARBAR("||"), |
mcimadamore@1113 | 205 | PLUSPLUS("++"), |
mcimadamore@1113 | 206 | SUBSUB("--"), |
mcimadamore@1113 | 207 | PLUS("+"), |
mcimadamore@1113 | 208 | SUB("-"), |
mcimadamore@1113 | 209 | STAR("*"), |
mcimadamore@1113 | 210 | SLASH("/"), |
mcimadamore@1113 | 211 | AMP("&"), |
mcimadamore@1113 | 212 | BAR("|"), |
mcimadamore@1113 | 213 | CARET("^"), |
mcimadamore@1113 | 214 | PERCENT("%"), |
mcimadamore@1113 | 215 | LTLT("<<"), |
mcimadamore@1113 | 216 | GTGT(">>"), |
mcimadamore@1113 | 217 | GTGTGT(">>>"), |
mcimadamore@1113 | 218 | PLUSEQ("+="), |
mcimadamore@1113 | 219 | SUBEQ("-="), |
mcimadamore@1113 | 220 | STAREQ("*="), |
mcimadamore@1113 | 221 | SLASHEQ("/="), |
mcimadamore@1113 | 222 | AMPEQ("&="), |
mcimadamore@1113 | 223 | BAREQ("|="), |
mcimadamore@1113 | 224 | CARETEQ("^="), |
mcimadamore@1113 | 225 | PERCENTEQ("%="), |
mcimadamore@1113 | 226 | LTLTEQ("<<="), |
mcimadamore@1113 | 227 | GTGTEQ(">>="), |
mcimadamore@1113 | 228 | GTGTGTEQ(">>>="), |
mcimadamore@1113 | 229 | MONKEYS_AT("@"), |
mcimadamore@1113 | 230 | CUSTOM; |
mcimadamore@1113 | 231 | |
mcimadamore@1113 | 232 | public final String name; |
mcimadamore@1113 | 233 | public final Tag tag; |
mcimadamore@1113 | 234 | |
mcimadamore@1113 | 235 | TokenKind() { |
mcimadamore@1113 | 236 | this(null, Tag.DEFAULT); |
mcimadamore@1113 | 237 | } |
mcimadamore@1113 | 238 | |
mcimadamore@1113 | 239 | TokenKind(String name) { |
mcimadamore@1113 | 240 | this(name, Tag.DEFAULT); |
mcimadamore@1113 | 241 | } |
mcimadamore@1113 | 242 | |
mcimadamore@1113 | 243 | TokenKind(Tag tag) { |
mcimadamore@1113 | 244 | this(null, tag); |
mcimadamore@1113 | 245 | } |
mcimadamore@1113 | 246 | |
mcimadamore@1113 | 247 | TokenKind(String name, Tag tag) { |
mcimadamore@1113 | 248 | this.name = name; |
mcimadamore@1113 | 249 | this.tag = tag; |
mcimadamore@1113 | 250 | } |
mcimadamore@1113 | 251 | |
mcimadamore@1113 | 252 | public String toString() { |
mcimadamore@1113 | 253 | switch (this) { |
mcimadamore@1113 | 254 | case IDENTIFIER: |
mcimadamore@1113 | 255 | return "token.identifier"; |
mcimadamore@1113 | 256 | case CHARLITERAL: |
mcimadamore@1113 | 257 | return "token.character"; |
mcimadamore@1113 | 258 | case STRINGLITERAL: |
mcimadamore@1113 | 259 | return "token.string"; |
mcimadamore@1113 | 260 | case INTLITERAL: |
mcimadamore@1113 | 261 | return "token.integer"; |
mcimadamore@1113 | 262 | case LONGLITERAL: |
mcimadamore@1113 | 263 | return "token.long-integer"; |
mcimadamore@1113 | 264 | case FLOATLITERAL: |
mcimadamore@1113 | 265 | return "token.float"; |
mcimadamore@1113 | 266 | case DOUBLELITERAL: |
mcimadamore@1113 | 267 | return "token.double"; |
mcimadamore@1113 | 268 | case ERROR: |
mcimadamore@1113 | 269 | return "token.bad-symbol"; |
mcimadamore@1113 | 270 | case EOF: |
mcimadamore@1113 | 271 | return "token.end-of-input"; |
mcimadamore@1113 | 272 | case DOT: case COMMA: case SEMI: case LPAREN: case RPAREN: |
mcimadamore@1113 | 273 | case LBRACKET: case RBRACKET: case LBRACE: case RBRACE: |
mcimadamore@1113 | 274 | return "'" + name + "'"; |
mcimadamore@1113 | 275 | default: |
mcimadamore@1113 | 276 | return name; |
mcimadamore@1113 | 277 | } |
mcimadamore@1113 | 278 | } |
mcimadamore@1113 | 279 | |
mcimadamore@1113 | 280 | public String getKind() { |
mcimadamore@1113 | 281 | return "Token"; |
mcimadamore@1113 | 282 | } |
mcimadamore@1113 | 283 | |
mcimadamore@1113 | 284 | public String toString(Locale locale, Messages messages) { |
mcimadamore@1113 | 285 | return name != null ? toString() : messages.getLocalizedString(locale, "compiler.misc." + toString()); |
mcimadamore@1113 | 286 | } |
mcimadamore@1503 | 287 | |
mcimadamore@1503 | 288 | @Override |
mcimadamore@1503 | 289 | public boolean accepts(TokenKind that) { |
mcimadamore@1503 | 290 | return this == that; |
mcimadamore@1503 | 291 | } |
mcimadamore@1113 | 292 | } |
mcimadamore@1113 | 293 | |
mcimadamore@1125 | 294 | public interface Comment { |
mcimadamore@1125 | 295 | |
mcimadamore@1125 | 296 | enum CommentStyle { |
mcimadamore@1125 | 297 | LINE, |
mcimadamore@1125 | 298 | BLOCK, |
mcimadamore@1125 | 299 | JAVADOC, |
mcimadamore@1125 | 300 | } |
mcimadamore@1125 | 301 | |
mcimadamore@1125 | 302 | String getText(); |
jjg@1281 | 303 | int getSourcePos(int index); |
mcimadamore@1125 | 304 | CommentStyle getStyle(); |
mcimadamore@1125 | 305 | boolean isDeprecated(); |
mcimadamore@1125 | 306 | } |
mcimadamore@1125 | 307 | |
mcimadamore@1113 | 308 | /** |
mcimadamore@1113 | 309 | * This is the class representing a javac token. Each token has several fields |
mcimadamore@1113 | 310 | * that are set by the javac lexer (i.e. start/end position, string value, etc). |
mcimadamore@1113 | 311 | */ |
mcimadamore@1113 | 312 | public static class Token { |
mcimadamore@1113 | 313 | |
mcimadamore@1113 | 314 | /** tags constants **/ |
mcimadamore@1113 | 315 | enum Tag { |
mcimadamore@1113 | 316 | DEFAULT, |
mcimadamore@1113 | 317 | NAMED, |
mcimadamore@1113 | 318 | STRING, |
mcimadamore@1113 | 319 | NUMERIC; |
mcimadamore@1113 | 320 | } |
mcimadamore@1113 | 321 | |
mcimadamore@1113 | 322 | /** The token kind */ |
mcimadamore@1113 | 323 | public final TokenKind kind; |
mcimadamore@1113 | 324 | |
mcimadamore@1113 | 325 | /** The start position of this token */ |
mcimadamore@1113 | 326 | public final int pos; |
mcimadamore@1113 | 327 | |
mcimadamore@1113 | 328 | /** The end position of this token */ |
mcimadamore@1113 | 329 | public final int endPos; |
mcimadamore@1113 | 330 | |
mcimadamore@1125 | 331 | /** Comment reader associated with this token */ |
mcimadamore@1125 | 332 | public final List<Comment> comments; |
mcimadamore@1113 | 333 | |
mcimadamore@1125 | 334 | Token(TokenKind kind, int pos, int endPos, List<Comment> comments) { |
mcimadamore@1113 | 335 | this.kind = kind; |
mcimadamore@1113 | 336 | this.pos = pos; |
mcimadamore@1113 | 337 | this.endPos = endPos; |
mcimadamore@1125 | 338 | this.comments = comments; |
mcimadamore@1113 | 339 | checkKind(); |
mcimadamore@1113 | 340 | } |
mcimadamore@1113 | 341 | |
mcimadamore@1113 | 342 | Token[] split(Tokens tokens) { |
mcimadamore@1113 | 343 | if (kind.name.length() < 2 || kind.tag != Tag.DEFAULT) { |
mcimadamore@1113 | 344 | throw new AssertionError("Cant split" + kind); |
mcimadamore@1113 | 345 | } |
mcimadamore@1113 | 346 | |
mcimadamore@1113 | 347 | TokenKind t1 = tokens.lookupKind(kind.name.substring(0, 1)); |
mcimadamore@1113 | 348 | TokenKind t2 = tokens.lookupKind(kind.name.substring(1)); |
mcimadamore@1113 | 349 | |
mcimadamore@1113 | 350 | if (t1 == null || t2 == null) { |
mcimadamore@1113 | 351 | throw new AssertionError("Cant split - bad subtokens"); |
mcimadamore@1113 | 352 | } |
mcimadamore@1113 | 353 | return new Token[] { |
mcimadamore@1125 | 354 | new Token(t1, pos, pos + t1.name.length(), comments), |
mcimadamore@1125 | 355 | new Token(t2, pos + t1.name.length(), endPos, null) |
mcimadamore@1113 | 356 | }; |
mcimadamore@1113 | 357 | } |
mcimadamore@1113 | 358 | |
mcimadamore@1113 | 359 | protected void checkKind() { |
mcimadamore@1113 | 360 | if (kind.tag != Tag.DEFAULT) { |
mcimadamore@1113 | 361 | throw new AssertionError("Bad token kind - expected " + Tag.STRING); |
mcimadamore@1113 | 362 | } |
mcimadamore@1113 | 363 | } |
mcimadamore@1113 | 364 | |
mcimadamore@1113 | 365 | public Name name() { |
mcimadamore@1113 | 366 | throw new UnsupportedOperationException(); |
mcimadamore@1113 | 367 | } |
mcimadamore@1113 | 368 | |
mcimadamore@1113 | 369 | public String stringVal() { |
mcimadamore@1113 | 370 | throw new UnsupportedOperationException(); |
mcimadamore@1113 | 371 | } |
mcimadamore@1113 | 372 | |
mcimadamore@1113 | 373 | public int radix() { |
mcimadamore@1113 | 374 | throw new UnsupportedOperationException(); |
mcimadamore@1113 | 375 | } |
mcimadamore@1125 | 376 | |
mcimadamore@1125 | 377 | /** |
mcimadamore@1125 | 378 | * Preserve classic semantics - if multiple javadocs are found on the token |
mcimadamore@1125 | 379 | * the last one is returned |
mcimadamore@1125 | 380 | */ |
jjg@1280 | 381 | public Comment comment(Comment.CommentStyle style) { |
jjg@1280 | 382 | List<Comment> comments = getComments(Comment.CommentStyle.JAVADOC); |
jjg@1280 | 383 | return comments.isEmpty() ? |
mcimadamore@1125 | 384 | null : |
jjg@1280 | 385 | comments.head; |
mcimadamore@1125 | 386 | } |
mcimadamore@1125 | 387 | |
mcimadamore@1125 | 388 | /** |
mcimadamore@1125 | 389 | * Preserve classic semantics - deprecated should be set if at least one |
mcimadamore@1125 | 390 | * javadoc comment attached to this token contains the '@deprecated' string |
mcimadamore@1125 | 391 | */ |
mcimadamore@1125 | 392 | public boolean deprecatedFlag() { |
jjg@1280 | 393 | for (Comment c : getComments(Comment.CommentStyle.JAVADOC)) { |
jjg@1280 | 394 | if (c.isDeprecated()) { |
mcimadamore@1125 | 395 | return true; |
mcimadamore@1125 | 396 | } |
mcimadamore@1125 | 397 | } |
mcimadamore@1125 | 398 | return false; |
mcimadamore@1125 | 399 | } |
mcimadamore@1125 | 400 | |
jjg@1280 | 401 | private List<Comment> getComments(Comment.CommentStyle style) { |
mcimadamore@1125 | 402 | if (comments == null) { |
mcimadamore@1125 | 403 | return List.nil(); |
mcimadamore@1125 | 404 | } else { |
alundblad@2047 | 405 | ListBuffer<Comment> buf = new ListBuffer<>(); |
jjg@1280 | 406 | for (Comment c : comments) { |
jjg@1280 | 407 | if (c.getStyle() == style) { |
jjg@1280 | 408 | buf.add(c); |
mcimadamore@1125 | 409 | } |
mcimadamore@1125 | 410 | } |
mcimadamore@1125 | 411 | return buf.toList(); |
mcimadamore@1125 | 412 | } |
mcimadamore@1125 | 413 | } |
mcimadamore@1113 | 414 | } |
mcimadamore@1113 | 415 | |
mcimadamore@1113 | 416 | final static class NamedToken extends Token { |
mcimadamore@1113 | 417 | /** The name of this token */ |
mcimadamore@1113 | 418 | public final Name name; |
mcimadamore@1113 | 419 | |
mcimadamore@1125 | 420 | public NamedToken(TokenKind kind, int pos, int endPos, Name name, List<Comment> comments) { |
mcimadamore@1125 | 421 | super(kind, pos, endPos, comments); |
mcimadamore@1113 | 422 | this.name = name; |
mcimadamore@1113 | 423 | } |
mcimadamore@1113 | 424 | |
mcimadamore@1113 | 425 | protected void checkKind() { |
mcimadamore@1113 | 426 | if (kind.tag != Tag.NAMED) { |
mcimadamore@1113 | 427 | throw new AssertionError("Bad token kind - expected " + Tag.NAMED); |
mcimadamore@1113 | 428 | } |
mcimadamore@1113 | 429 | } |
mcimadamore@1113 | 430 | |
mcimadamore@1113 | 431 | @Override |
mcimadamore@1113 | 432 | public Name name() { |
mcimadamore@1113 | 433 | return name; |
mcimadamore@1113 | 434 | } |
mcimadamore@1113 | 435 | } |
mcimadamore@1113 | 436 | |
mcimadamore@1113 | 437 | static class StringToken extends Token { |
mcimadamore@1113 | 438 | /** The string value of this token */ |
mcimadamore@1113 | 439 | public final String stringVal; |
mcimadamore@1113 | 440 | |
mcimadamore@1125 | 441 | public StringToken(TokenKind kind, int pos, int endPos, String stringVal, List<Comment> comments) { |
mcimadamore@1125 | 442 | super(kind, pos, endPos, comments); |
mcimadamore@1113 | 443 | this.stringVal = stringVal; |
mcimadamore@1113 | 444 | } |
mcimadamore@1113 | 445 | |
mcimadamore@1113 | 446 | protected void checkKind() { |
mcimadamore@1113 | 447 | if (kind.tag != Tag.STRING) { |
mcimadamore@1113 | 448 | throw new AssertionError("Bad token kind - expected " + Tag.STRING); |
mcimadamore@1113 | 449 | } |
mcimadamore@1113 | 450 | } |
mcimadamore@1113 | 451 | |
mcimadamore@1113 | 452 | @Override |
mcimadamore@1113 | 453 | public String stringVal() { |
mcimadamore@1113 | 454 | return stringVal; |
mcimadamore@1113 | 455 | } |
mcimadamore@1113 | 456 | } |
mcimadamore@1113 | 457 | |
mcimadamore@1113 | 458 | final static class NumericToken extends StringToken { |
mcimadamore@1113 | 459 | /** The 'radix' value of this token */ |
mcimadamore@1113 | 460 | public final int radix; |
mcimadamore@1113 | 461 | |
mcimadamore@1125 | 462 | public NumericToken(TokenKind kind, int pos, int endPos, String stringVal, int radix, List<Comment> comments) { |
mcimadamore@1125 | 463 | super(kind, pos, endPos, stringVal, comments); |
mcimadamore@1113 | 464 | this.radix = radix; |
mcimadamore@1113 | 465 | } |
mcimadamore@1113 | 466 | |
mcimadamore@1113 | 467 | protected void checkKind() { |
mcimadamore@1113 | 468 | if (kind.tag != Tag.NUMERIC) { |
mcimadamore@1113 | 469 | throw new AssertionError("Bad token kind - expected " + Tag.NUMERIC); |
mcimadamore@1113 | 470 | } |
mcimadamore@1113 | 471 | } |
mcimadamore@1113 | 472 | |
mcimadamore@1113 | 473 | @Override |
mcimadamore@1113 | 474 | public int radix() { |
mcimadamore@1113 | 475 | return radix; |
mcimadamore@1113 | 476 | } |
mcimadamore@1113 | 477 | } |
mcimadamore@1113 | 478 | |
mcimadamore@1113 | 479 | public static final Token DUMMY = |
mcimadamore@1125 | 480 | new Token(TokenKind.ERROR, 0, 0, null); |
mcimadamore@1113 | 481 | } |