src/share/classes/com/sun/tools/javac/code/SymbolMetadata.java

Mon, 16 Oct 2017 16:07:48 +0800

author
aoqi
date
Mon, 16 Oct 2017 16:07:48 +0800
changeset 2893
ca5783d9a597
parent 2525
2eb010b6cb22
child 3782
6dd9637926c7
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.javac.code;
aoqi@0 27
aoqi@0 28 import java.util.Map;
aoqi@0 29
aoqi@0 30 import javax.tools.JavaFileObject;
aoqi@0 31
aoqi@0 32 import com.sun.tools.javac.comp.Annotate;
aoqi@0 33 import com.sun.tools.javac.comp.AttrContext;
aoqi@0 34 import com.sun.tools.javac.comp.Env;
aoqi@0 35 import com.sun.tools.javac.util.*;
aoqi@0 36 import com.sun.tools.javac.util.Assert;
aoqi@0 37 import com.sun.tools.javac.util.List;
aoqi@0 38 import com.sun.tools.javac.util.Log;
aoqi@0 39 import com.sun.tools.javac.util.Pair;
aoqi@0 40 import static com.sun.tools.javac.code.Kinds.PCK;
aoqi@0 41
aoqi@0 42 /**
aoqi@0 43 * Container for all annotations (attributes in javac) on a Symbol.
aoqi@0 44 *
aoqi@0 45 * This class is explicitly mutable. Its contents will change when attributes
aoqi@0 46 * are annotated onto the Symbol. However this class depends on the facts that
aoqi@0 47 * List (in javac) is immutable.
aoqi@0 48 *
aoqi@0 49 * An instance of this class can be in one of three states:
aoqi@0 50 *
aoqi@0 51 * NOT_STARTED indicates that the Symbol this instance belongs to has not been
aoqi@0 52 * annotated (yet). Specifically if the declaration is not annotated this
aoqi@0 53 * instance will never move past NOT_STARTED. You can never go back to
aoqi@0 54 * NOT_STARTED.
aoqi@0 55 *
aoqi@0 56 * IN_PROGRESS annotations have been found on the declaration. Will be processed
aoqi@0 57 * later. You can reset to IN_PROGRESS. While IN_PROGRESS you can set the list
aoqi@0 58 * of attributes (and this moves out of the IN_PROGRESS state).
aoqi@0 59 *
aoqi@0 60 * "unnamed" this SymbolMetadata contains some attributes, possibly the final set.
aoqi@0 61 * While in this state you can only prepend or append to the attributes not set
aoqi@0 62 * it directly. You can also move back to the IN_PROGRESS state using reset().
aoqi@0 63 *
aoqi@0 64 * <p><b>This is NOT part of any supported API. If you write code that depends
aoqi@0 65 * on this, you do so at your own risk. This code and its internal interfaces
aoqi@0 66 * are subject to change or deletion without notice.</b>
aoqi@0 67 */
aoqi@0 68 public class SymbolMetadata {
aoqi@0 69
aoqi@0 70 private static final List<Attribute.Compound> DECL_NOT_STARTED = List.of(null);
aoqi@0 71 private static final List<Attribute.Compound> DECL_IN_PROGRESS = List.of(null);
aoqi@0 72
aoqi@0 73 /*
aoqi@0 74 * This field should never be null
aoqi@0 75 */
aoqi@0 76 private List<Attribute.Compound> attributes = DECL_NOT_STARTED;
aoqi@0 77
aoqi@0 78 /*
aoqi@0 79 * Type attributes for this symbol.
aoqi@0 80 * This field should never be null.
aoqi@0 81 */
aoqi@0 82 private List<Attribute.TypeCompound> type_attributes = List.<Attribute.TypeCompound>nil();
aoqi@0 83
aoqi@0 84 /*
aoqi@0 85 * Type attributes of initializers in this class.
aoqi@0 86 * Unused if the current symbol is not a ClassSymbol.
aoqi@0 87 */
aoqi@0 88 private List<Attribute.TypeCompound> init_type_attributes = List.<Attribute.TypeCompound>nil();
aoqi@0 89
aoqi@0 90 /*
aoqi@0 91 * Type attributes of class initializers in this class.
aoqi@0 92 * Unused if the current symbol is not a ClassSymbol.
aoqi@0 93 */
aoqi@0 94 private List<Attribute.TypeCompound> clinit_type_attributes = List.<Attribute.TypeCompound>nil();
aoqi@0 95
aoqi@0 96 /*
aoqi@0 97 * The Symbol this SymbolMetadata instance belongs to
aoqi@0 98 */
aoqi@0 99 private final Symbol sym;
aoqi@0 100
aoqi@0 101 public SymbolMetadata(Symbol sym) {
aoqi@0 102 this.sym = sym;
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 public List<Attribute.Compound> getDeclarationAttributes() {
aoqi@0 106 return filterDeclSentinels(attributes);
aoqi@0 107 }
aoqi@0 108
aoqi@0 109 public List<Attribute.TypeCompound> getTypeAttributes() {
aoqi@0 110 return type_attributes;
aoqi@0 111 }
aoqi@0 112
aoqi@0 113 public List<Attribute.TypeCompound> getInitTypeAttributes() {
aoqi@0 114 return init_type_attributes;
aoqi@0 115 }
aoqi@0 116
aoqi@0 117 public List<Attribute.TypeCompound> getClassInitTypeAttributes() {
aoqi@0 118 return clinit_type_attributes;
aoqi@0 119 }
aoqi@0 120
aoqi@0 121 public void setDeclarationAttributes(List<Attribute.Compound> a) {
aoqi@0 122 Assert.check(pendingCompletion() || !isStarted());
aoqi@0 123 if (a == null) {
aoqi@0 124 throw new NullPointerException();
aoqi@0 125 }
aoqi@0 126 attributes = a;
aoqi@0 127 }
aoqi@0 128
aoqi@0 129 public void setTypeAttributes(List<Attribute.TypeCompound> a) {
aoqi@0 130 if (a == null) {
aoqi@0 131 throw new NullPointerException();
aoqi@0 132 }
aoqi@0 133 type_attributes = a;
aoqi@0 134 }
aoqi@0 135
aoqi@0 136 public void setInitTypeAttributes(List<Attribute.TypeCompound> a) {
aoqi@0 137 if (a == null) {
aoqi@0 138 throw new NullPointerException();
aoqi@0 139 }
aoqi@0 140 init_type_attributes = a;
aoqi@0 141 }
aoqi@0 142
aoqi@0 143 public void setClassInitTypeAttributes(List<Attribute.TypeCompound> a) {
aoqi@0 144 if (a == null) {
aoqi@0 145 throw new NullPointerException();
aoqi@0 146 }
aoqi@0 147 clinit_type_attributes = a;
aoqi@0 148 }
aoqi@0 149
aoqi@0 150 public void setAttributes(SymbolMetadata other) {
aoqi@0 151 if (other == null) {
aoqi@0 152 throw new NullPointerException();
aoqi@0 153 }
aoqi@0 154 setDeclarationAttributes(other.getDeclarationAttributes());
aoqi@0 155 setTypeAttributes(other.getTypeAttributes());
aoqi@0 156 setInitTypeAttributes(other.getInitTypeAttributes());
aoqi@0 157 setClassInitTypeAttributes(other.getClassInitTypeAttributes());
aoqi@0 158 }
aoqi@0 159
aoqi@0 160 public void setDeclarationAttributesWithCompletion(final Annotate.AnnotateRepeatedContext<Attribute.Compound> ctx) {
aoqi@0 161 Assert.check(pendingCompletion() || (!isStarted() && sym.kind == PCK));
aoqi@0 162 this.setDeclarationAttributes(getAttributesForCompletion(ctx));
aoqi@0 163 }
aoqi@0 164
aoqi@0 165 public void appendTypeAttributesWithCompletion(final Annotate.AnnotateRepeatedContext<Attribute.TypeCompound> ctx) {
aoqi@0 166 this.appendUniqueTypes(getAttributesForCompletion(ctx));
aoqi@0 167 }
aoqi@0 168
aoqi@0 169 private <T extends Attribute.Compound> List<T> getAttributesForCompletion(
aoqi@0 170 final Annotate.AnnotateRepeatedContext<T> ctx) {
aoqi@0 171
aoqi@0 172 Map<Symbol.TypeSymbol, ListBuffer<T>> annotated = ctx.annotated;
aoqi@0 173 boolean atLeastOneRepeated = false;
aoqi@0 174 List<T> buf = List.<T>nil();
aoqi@0 175 for (ListBuffer<T> lb : annotated.values()) {
aoqi@0 176 if (lb.size() == 1) {
aoqi@0 177 buf = buf.prepend(lb.first());
aoqi@0 178 } else { // repeated
aoqi@0 179 // This will break when other subtypes of Attributs.Compound
aoqi@0 180 // are introduced, because PlaceHolder is a subtype of TypeCompound.
aoqi@0 181 T res;
aoqi@0 182 @SuppressWarnings("unchecked")
aoqi@0 183 T ph = (T) new Placeholder<T>(ctx, lb.toList(), sym);
aoqi@0 184 res = ph;
aoqi@0 185 buf = buf.prepend(res);
aoqi@0 186 atLeastOneRepeated = true;
aoqi@0 187 }
aoqi@0 188 }
aoqi@0 189
aoqi@0 190 if (atLeastOneRepeated) {
aoqi@0 191 // The Symbol s is now annotated with a combination of
aoqi@0 192 // finished non-repeating annotations and placeholders for
aoqi@0 193 // repeating annotations.
aoqi@0 194 //
aoqi@0 195 // We need to do this in two passes because when creating
aoqi@0 196 // a container for a repeating annotation we must
aoqi@0 197 // guarantee that the @Repeatable on the
aoqi@0 198 // contained annotation is fully annotated
aoqi@0 199 //
aoqi@0 200 // The way we force this order is to do all repeating
aoqi@0 201 // annotations in a pass after all non-repeating are
aoqi@0 202 // finished. This will work because @Repeatable
aoqi@0 203 // is non-repeating and therefore will be annotated in the
aoqi@0 204 // fist pass.
aoqi@0 205
aoqi@0 206 // Queue a pass that will replace Attribute.Placeholders
aoqi@0 207 // with Attribute.Compound (made from synthesized containers).
aoqi@0 208 ctx.annotateRepeated(new Annotate.Worker() {
aoqi@0 209 @Override
aoqi@0 210 public String toString() {
aoqi@0 211 return "repeated annotation pass of: " + sym + " in: " + sym.owner;
aoqi@0 212 }
aoqi@0 213
aoqi@0 214 @Override
aoqi@0 215 public void run() {
aoqi@0 216 complete(ctx);
aoqi@0 217 }
aoqi@0 218 });
aoqi@0 219 }
aoqi@0 220 // Add non-repeating attributes
aoqi@0 221 return buf.reverse();
aoqi@0 222 }
aoqi@0 223
aoqi@0 224 public SymbolMetadata reset() {
aoqi@0 225 attributes = DECL_IN_PROGRESS;
aoqi@0 226 return this;
aoqi@0 227 }
aoqi@0 228
aoqi@0 229 public boolean isEmpty() {
aoqi@0 230 return !isStarted()
aoqi@0 231 || pendingCompletion()
aoqi@0 232 || attributes.isEmpty();
aoqi@0 233 }
aoqi@0 234
aoqi@0 235 public boolean isTypesEmpty() {
aoqi@0 236 return type_attributes.isEmpty();
aoqi@0 237 }
aoqi@0 238
aoqi@0 239 public boolean pendingCompletion() {
aoqi@0 240 return attributes == DECL_IN_PROGRESS;
aoqi@0 241 }
aoqi@0 242
aoqi@0 243 public SymbolMetadata append(List<Attribute.Compound> l) {
aoqi@0 244 attributes = filterDeclSentinels(attributes);
aoqi@0 245
aoqi@0 246 if (l.isEmpty()) {
aoqi@0 247 ; // no-op
aoqi@0 248 } else if (attributes.isEmpty()) {
aoqi@0 249 attributes = l;
aoqi@0 250 } else {
aoqi@0 251 attributes = attributes.appendList(l);
aoqi@0 252 }
aoqi@0 253 return this;
aoqi@0 254 }
aoqi@0 255
aoqi@0 256 public SymbolMetadata appendUniqueTypes(List<Attribute.TypeCompound> l) {
aoqi@0 257 if (l.isEmpty()) {
aoqi@0 258 ; // no-op
aoqi@0 259 } else if (type_attributes.isEmpty()) {
aoqi@0 260 type_attributes = l;
aoqi@0 261 } else {
aoqi@0 262 // TODO: in case we expect a large number of annotations, this
aoqi@0 263 // might be inefficient.
aoqi@0 264 for (Attribute.TypeCompound tc : l) {
aoqi@0 265 if (!type_attributes.contains(tc))
aoqi@0 266 type_attributes = type_attributes.append(tc);
aoqi@0 267 }
aoqi@0 268 }
aoqi@0 269 return this;
aoqi@0 270 }
aoqi@0 271
aoqi@0 272 public SymbolMetadata appendInitTypeAttributes(List<Attribute.TypeCompound> l) {
aoqi@0 273 if (l.isEmpty()) {
aoqi@0 274 ; // no-op
aoqi@0 275 } else if (init_type_attributes.isEmpty()) {
aoqi@0 276 init_type_attributes = l;
aoqi@0 277 } else {
aoqi@0 278 init_type_attributes = init_type_attributes.appendList(l);
aoqi@0 279 }
aoqi@0 280 return this;
aoqi@0 281 }
aoqi@0 282
aoqi@0 283 public SymbolMetadata appendClassInitTypeAttributes(List<Attribute.TypeCompound> l) {
aoqi@0 284 if (l.isEmpty()) {
aoqi@0 285 ; // no-op
aoqi@0 286 } else if (clinit_type_attributes.isEmpty()) {
aoqi@0 287 clinit_type_attributes = l;
aoqi@0 288 } else {
aoqi@0 289 clinit_type_attributes = clinit_type_attributes.appendList(l);
aoqi@0 290 }
aoqi@0 291 return this;
aoqi@0 292 }
aoqi@0 293
aoqi@0 294 public SymbolMetadata prepend(List<Attribute.Compound> l) {
aoqi@0 295 attributes = filterDeclSentinels(attributes);
aoqi@0 296
aoqi@0 297 if (l.isEmpty()) {
aoqi@0 298 ; // no-op
aoqi@0 299 } else if (attributes.isEmpty()) {
aoqi@0 300 attributes = l;
aoqi@0 301 } else {
aoqi@0 302 attributes = attributes.prependList(l);
aoqi@0 303 }
aoqi@0 304 return this;
aoqi@0 305 }
aoqi@0 306
aoqi@0 307 private List<Attribute.Compound> filterDeclSentinels(List<Attribute.Compound> a) {
aoqi@0 308 return (a == DECL_IN_PROGRESS || a == DECL_NOT_STARTED)
aoqi@0 309 ? List.<Attribute.Compound>nil()
aoqi@0 310 : a;
aoqi@0 311 }
aoqi@0 312
aoqi@0 313 private boolean isStarted() {
aoqi@0 314 return attributes != DECL_NOT_STARTED;
aoqi@0 315 }
aoqi@0 316
aoqi@0 317 private List<Attribute.Compound> getPlaceholders() {
aoqi@0 318 List<Attribute.Compound> res = List.<Attribute.Compound>nil();
aoqi@0 319 for (Attribute.Compound a : filterDeclSentinels(attributes)) {
aoqi@0 320 if (a instanceof Placeholder) {
aoqi@0 321 res = res.prepend(a);
aoqi@0 322 }
aoqi@0 323 }
aoqi@0 324 return res.reverse();
aoqi@0 325 }
aoqi@0 326
aoqi@0 327 private List<Attribute.TypeCompound> getTypePlaceholders() {
aoqi@0 328 List<Attribute.TypeCompound> res = List.<Attribute.TypeCompound>nil();
aoqi@0 329 for (Attribute.TypeCompound a : type_attributes) {
aoqi@0 330 if (a instanceof Placeholder) {
aoqi@0 331 res = res.prepend(a);
aoqi@0 332 }
aoqi@0 333 }
aoqi@0 334 return res.reverse();
aoqi@0 335 }
aoqi@0 336
aoqi@0 337 /*
aoqi@0 338 * Replace Placeholders for repeating annotations with their containers
aoqi@0 339 */
aoqi@0 340 private <T extends Attribute.Compound> void complete(Annotate.AnnotateRepeatedContext<T> ctx) {
aoqi@0 341 Log log = ctx.log;
aoqi@0 342 Env<AttrContext> env = ctx.env;
aoqi@0 343 JavaFileObject oldSource = log.useSource(env.toplevel.sourcefile);
aoqi@0 344 try {
aoqi@0 345 // TODO: can we reduce duplication in the following branches?
aoqi@0 346 if (ctx.isTypeCompound) {
aoqi@0 347 Assert.check(!isTypesEmpty());
aoqi@0 348
aoqi@0 349 if (isTypesEmpty()) {
aoqi@0 350 return;
aoqi@0 351 }
aoqi@0 352
aoqi@0 353 List<Attribute.TypeCompound> result = List.nil();
aoqi@0 354 for (Attribute.TypeCompound a : getTypeAttributes()) {
aoqi@0 355 if (a instanceof Placeholder) {
aoqi@0 356 @SuppressWarnings("unchecked")
aoqi@0 357 Placeholder<Attribute.TypeCompound> ph = (Placeholder<Attribute.TypeCompound>) a;
aoqi@0 358 Attribute.TypeCompound replacement = replaceOne(ph, ph.getRepeatedContext());
aoqi@0 359
aoqi@0 360 if (null != replacement) {
aoqi@0 361 result = result.prepend(replacement);
aoqi@0 362 }
aoqi@0 363 } else {
aoqi@0 364 result = result.prepend(a);
aoqi@0 365 }
aoqi@0 366 }
aoqi@0 367
aoqi@0 368 type_attributes = result.reverse();
aoqi@0 369
aoqi@0 370 Assert.check(SymbolMetadata.this.getTypePlaceholders().isEmpty());
aoqi@0 371 } else {
aoqi@0 372 Assert.check(!pendingCompletion());
aoqi@0 373
aoqi@0 374 if (isEmpty()) {
aoqi@0 375 return;
aoqi@0 376 }
aoqi@0 377
aoqi@0 378 List<Attribute.Compound> result = List.nil();
aoqi@0 379 for (Attribute.Compound a : getDeclarationAttributes()) {
aoqi@0 380 if (a instanceof Placeholder) {
aoqi@0 381 @SuppressWarnings("unchecked")
aoqi@0 382 Attribute.Compound replacement = replaceOne((Placeholder<T>) a, ctx);
aoqi@0 383
aoqi@0 384 if (null != replacement) {
aoqi@0 385 result = result.prepend(replacement);
aoqi@0 386 }
aoqi@0 387 } else {
aoqi@0 388 result = result.prepend(a);
aoqi@0 389 }
aoqi@0 390 }
aoqi@0 391
aoqi@0 392 attributes = result.reverse();
aoqi@0 393
aoqi@0 394 Assert.check(SymbolMetadata.this.getPlaceholders().isEmpty());
aoqi@0 395 }
aoqi@0 396 } finally {
aoqi@0 397 log.useSource(oldSource);
aoqi@0 398 }
aoqi@0 399 }
aoqi@0 400
aoqi@0 401 private <T extends Attribute.Compound> T replaceOne(Placeholder<T> placeholder, Annotate.AnnotateRepeatedContext<T> ctx) {
aoqi@0 402 Log log = ctx.log;
aoqi@0 403
aoqi@0 404 // Process repeated annotations
aoqi@0 405 T validRepeated = ctx.processRepeatedAnnotations(placeholder.getPlaceholderFor(), sym);
aoqi@0 406
aoqi@0 407 if (validRepeated != null) {
aoqi@0 408 // Check that the container isn't manually
aoqi@0 409 // present along with repeated instances of
aoqi@0 410 // its contained annotation.
aoqi@0 411 ListBuffer<T> manualContainer = ctx.annotated.get(validRepeated.type.tsym);
aoqi@0 412 if (manualContainer != null) {
aoqi@0 413 log.error(ctx.pos.get(manualContainer.first()), "invalid.repeatable.annotation.repeated.and.container.present",
aoqi@0 414 manualContainer.first().type.tsym);
aoqi@0 415 }
aoqi@0 416 }
aoqi@0 417
aoqi@0 418 // A null return will delete the Placeholder
aoqi@0 419 return validRepeated;
aoqi@0 420 }
aoqi@0 421
aoqi@0 422 private static class Placeholder<T extends Attribute.Compound> extends Attribute.TypeCompound {
aoqi@0 423
aoqi@0 424 private final Annotate.AnnotateRepeatedContext<T> ctx;
aoqi@0 425 private final List<T> placeholderFor;
aoqi@0 426 private final Symbol on;
aoqi@0 427
aoqi@0 428 public Placeholder(Annotate.AnnotateRepeatedContext<T> ctx, List<T> placeholderFor, Symbol on) {
aoqi@0 429 super(on.type, List.<Pair<Symbol.MethodSymbol, Attribute>>nil(),
aoqi@0 430 ctx.isTypeCompound ?
aoqi@0 431 ((Attribute.TypeCompound)placeholderFor.head).position :
aoqi@0 432 new TypeAnnotationPosition());
aoqi@0 433 this.ctx = ctx;
aoqi@0 434 this.placeholderFor = placeholderFor;
aoqi@0 435 this.on = on;
aoqi@0 436 }
aoqi@0 437
aoqi@0 438 @Override
aoqi@0 439 public String toString() {
aoqi@0 440 return "<placeholder: " + placeholderFor + " on: " + on + ">";
aoqi@0 441 }
aoqi@0 442
aoqi@0 443 public List<T> getPlaceholderFor() {
aoqi@0 444 return placeholderFor;
aoqi@0 445 }
aoqi@0 446
aoqi@0 447 public Annotate.AnnotateRepeatedContext<T> getRepeatedContext() {
aoqi@0 448 return ctx;
aoqi@0 449 }
aoqi@0 450 }
aoqi@0 451 }

mercurial