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