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

Wed, 23 Jan 2013 13:27:24 -0800

author
jjg
date
Wed, 23 Jan 2013 13:27:24 -0800
changeset 1521
71f35e4b93a5
parent 1492
df694c775e8a
child 1755
ddb4a2bfcd82
permissions
-rw-r--r--

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

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

mercurial