23 * questions. |
23 * questions. |
24 */ |
24 */ |
25 |
25 |
26 package com.sun.tools.javac.code; |
26 package com.sun.tools.javac.code; |
27 |
27 |
28 import java.util.Map; |
28 |
29 |
|
30 import javax.tools.JavaFileObject; |
|
31 |
|
32 import com.sun.tools.javac.comp.Annotate; |
|
33 import com.sun.tools.javac.comp.AttrContext; |
|
34 import com.sun.tools.javac.comp.Env; |
|
35 import com.sun.tools.javac.util.*; |
|
36 import com.sun.tools.javac.util.Assert; |
29 import com.sun.tools.javac.util.Assert; |
37 import com.sun.tools.javac.util.List; |
30 import com.sun.tools.javac.util.List; |
38 import com.sun.tools.javac.util.Log; |
|
39 import com.sun.tools.javac.util.Pair; |
|
40 import static com.sun.tools.javac.code.Kinds.PCK; |
|
41 |
31 |
42 /** |
32 /** |
43 * Container for all annotations (attributes in javac) on a Symbol. |
33 * Container for all annotations (attributes in javac) on a Symbol. |
44 * |
34 * |
45 * This class is explicitly mutable. Its contents will change when attributes |
35 * This class is explicitly mutable. Its contents will change when attributes |
153 } |
143 } |
154 setDeclarationAttributes(other.getDeclarationAttributes()); |
144 setDeclarationAttributes(other.getDeclarationAttributes()); |
155 setTypeAttributes(other.getTypeAttributes()); |
145 setTypeAttributes(other.getTypeAttributes()); |
156 setInitTypeAttributes(other.getInitTypeAttributes()); |
146 setInitTypeAttributes(other.getInitTypeAttributes()); |
157 setClassInitTypeAttributes(other.getClassInitTypeAttributes()); |
147 setClassInitTypeAttributes(other.getClassInitTypeAttributes()); |
158 } |
|
159 |
|
160 public void setDeclarationAttributesWithCompletion(final Annotate.AnnotateRepeatedContext<Attribute.Compound> ctx) { |
|
161 Assert.check(pendingCompletion() || (!isStarted() && sym.kind == PCK)); |
|
162 this.setDeclarationAttributes(getAttributesForCompletion(ctx)); |
|
163 } |
|
164 |
|
165 public void appendTypeAttributesWithCompletion(final Annotate.AnnotateRepeatedContext<Attribute.TypeCompound> ctx) { |
|
166 this.appendUniqueTypes(getAttributesForCompletion(ctx)); |
|
167 } |
|
168 |
|
169 private <T extends Attribute.Compound> List<T> getAttributesForCompletion( |
|
170 final Annotate.AnnotateRepeatedContext<T> ctx) { |
|
171 |
|
172 Map<Symbol.TypeSymbol, ListBuffer<T>> annotated = ctx.annotated; |
|
173 boolean atLeastOneRepeated = false; |
|
174 List<T> buf = List.<T>nil(); |
|
175 for (ListBuffer<T> lb : annotated.values()) { |
|
176 if (lb.size() == 1) { |
|
177 buf = buf.prepend(lb.first()); |
|
178 } else { // repeated |
|
179 // This will break when other subtypes of Attributs.Compound |
|
180 // are introduced, because PlaceHolder is a subtype of TypeCompound. |
|
181 T res; |
|
182 @SuppressWarnings("unchecked") |
|
183 T ph = (T) new Placeholder<>(ctx, lb.toList(), sym); |
|
184 res = ph; |
|
185 buf = buf.prepend(res); |
|
186 atLeastOneRepeated = true; |
|
187 } |
|
188 } |
|
189 |
|
190 if (atLeastOneRepeated) { |
|
191 // The Symbol s is now annotated with a combination of |
|
192 // finished non-repeating annotations and placeholders for |
|
193 // repeating annotations. |
|
194 // |
|
195 // We need to do this in two passes because when creating |
|
196 // a container for a repeating annotation we must |
|
197 // guarantee that the @Repeatable on the |
|
198 // contained annotation is fully annotated |
|
199 // |
|
200 // The way we force this order is to do all repeating |
|
201 // annotations in a pass after all non-repeating are |
|
202 // finished. This will work because @Repeatable |
|
203 // is non-repeating and therefore will be annotated in the |
|
204 // fist pass. |
|
205 |
|
206 // Queue a pass that will replace Attribute.Placeholders |
|
207 // with Attribute.Compound (made from synthesized containers). |
|
208 ctx.annotateRepeated(new Annotate.Worker() { |
|
209 @Override |
|
210 public String toString() { |
|
211 return "repeated annotation pass of: " + sym + " in: " + sym.owner; |
|
212 } |
|
213 |
|
214 @Override |
|
215 public void run() { |
|
216 complete(ctx); |
|
217 } |
|
218 }); |
|
219 } |
|
220 // Add non-repeating attributes |
|
221 return buf.reverse(); |
|
222 } |
148 } |
223 |
149 |
224 public SymbolMetadata reset() { |
150 public SymbolMetadata reset() { |
225 attributes = DECL_IN_PROGRESS; |
151 attributes = DECL_IN_PROGRESS; |
226 return this; |
152 return this; |
311 } |
237 } |
312 |
238 |
313 private boolean isStarted() { |
239 private boolean isStarted() { |
314 return attributes != DECL_NOT_STARTED; |
240 return attributes != DECL_NOT_STARTED; |
315 } |
241 } |
316 |
|
317 private List<Attribute.Compound> getPlaceholders() { |
|
318 List<Attribute.Compound> res = List.<Attribute.Compound>nil(); |
|
319 for (Attribute.Compound a : filterDeclSentinels(attributes)) { |
|
320 if (a instanceof Placeholder) { |
|
321 res = res.prepend(a); |
|
322 } |
|
323 } |
|
324 return res.reverse(); |
|
325 } |
|
326 |
|
327 private List<Attribute.TypeCompound> getTypePlaceholders() { |
|
328 List<Attribute.TypeCompound> res = List.<Attribute.TypeCompound>nil(); |
|
329 for (Attribute.TypeCompound a : type_attributes) { |
|
330 if (a instanceof Placeholder) { |
|
331 res = res.prepend(a); |
|
332 } |
|
333 } |
|
334 return res.reverse(); |
|
335 } |
|
336 |
|
337 /* |
|
338 * Replace Placeholders for repeating annotations with their containers |
|
339 */ |
|
340 private <T extends Attribute.Compound> void complete(Annotate.AnnotateRepeatedContext<T> ctx) { |
|
341 Log log = ctx.log; |
|
342 Env<AttrContext> env = ctx.env; |
|
343 JavaFileObject oldSource = log.useSource(env.toplevel.sourcefile); |
|
344 try { |
|
345 // TODO: can we reduce duplication in the following branches? |
|
346 if (ctx.isTypeCompound) { |
|
347 Assert.check(!isTypesEmpty()); |
|
348 |
|
349 if (isTypesEmpty()) { |
|
350 return; |
|
351 } |
|
352 |
|
353 List<Attribute.TypeCompound> result = List.nil(); |
|
354 for (Attribute.TypeCompound a : getTypeAttributes()) { |
|
355 if (a instanceof Placeholder) { |
|
356 @SuppressWarnings("unchecked") |
|
357 Placeholder<Attribute.TypeCompound> ph = (Placeholder<Attribute.TypeCompound>) a; |
|
358 Attribute.TypeCompound replacement = replaceOne(ph, ph.getRepeatedContext()); |
|
359 |
|
360 if (null != replacement) { |
|
361 result = result.prepend(replacement); |
|
362 } |
|
363 } else { |
|
364 result = result.prepend(a); |
|
365 } |
|
366 } |
|
367 |
|
368 type_attributes = result.reverse(); |
|
369 |
|
370 Assert.check(SymbolMetadata.this.getTypePlaceholders().isEmpty()); |
|
371 } else { |
|
372 Assert.check(!pendingCompletion()); |
|
373 |
|
374 if (isEmpty()) { |
|
375 return; |
|
376 } |
|
377 |
|
378 List<Attribute.Compound> result = List.nil(); |
|
379 for (Attribute.Compound a : getDeclarationAttributes()) { |
|
380 if (a instanceof Placeholder) { |
|
381 @SuppressWarnings("unchecked") |
|
382 Attribute.Compound replacement = replaceOne((Placeholder<T>) a, ctx); |
|
383 |
|
384 if (null != replacement) { |
|
385 result = result.prepend(replacement); |
|
386 } |
|
387 } else { |
|
388 result = result.prepend(a); |
|
389 } |
|
390 } |
|
391 |
|
392 attributes = result.reverse(); |
|
393 |
|
394 Assert.check(SymbolMetadata.this.getPlaceholders().isEmpty()); |
|
395 } |
|
396 } finally { |
|
397 log.useSource(oldSource); |
|
398 } |
|
399 } |
|
400 |
|
401 private <T extends Attribute.Compound> T replaceOne(Placeholder<T> placeholder, Annotate.AnnotateRepeatedContext<T> ctx) { |
|
402 Log log = ctx.log; |
|
403 |
|
404 // Process repeated annotations |
|
405 T validRepeated = ctx.processRepeatedAnnotations(placeholder.getPlaceholderFor(), sym); |
|
406 |
|
407 if (validRepeated != null) { |
|
408 // Check that the container isn't manually |
|
409 // present along with repeated instances of |
|
410 // its contained annotation. |
|
411 ListBuffer<T> manualContainer = ctx.annotated.get(validRepeated.type.tsym); |
|
412 if (manualContainer != null) { |
|
413 log.error(ctx.pos.get(manualContainer.first()), "invalid.repeatable.annotation.repeated.and.container.present", |
|
414 manualContainer.first().type.tsym); |
|
415 } |
|
416 } |
|
417 |
|
418 // A null return will delete the Placeholder |
|
419 return validRepeated; |
|
420 } |
|
421 |
|
422 private static class Placeholder<T extends Attribute.Compound> extends Attribute.TypeCompound { |
|
423 |
|
424 private final Annotate.AnnotateRepeatedContext<T> ctx; |
|
425 private final List<T> placeholderFor; |
|
426 private final Symbol on; |
|
427 |
|
428 public Placeholder(Annotate.AnnotateRepeatedContext<T> ctx, |
|
429 List<T> placeholderFor, Symbol on) { |
|
430 super(on.type, List.<Pair<Symbol.MethodSymbol, Attribute>>nil(), |
|
431 ctx.isTypeCompound ? |
|
432 ((Attribute.TypeCompound)placeholderFor.head).position : |
|
433 // TODO: Eventually, we will need to get rid of this |
|
434 // use of unknown, either by using null, or by |
|
435 // throwing an assertion failure here. |
|
436 TypeAnnotationPosition.unknown); |
|
437 this.ctx = ctx; |
|
438 this.placeholderFor = placeholderFor; |
|
439 this.on = on; |
|
440 } |
|
441 |
|
442 @Override |
|
443 public String toString() { |
|
444 return "<placeholder: " + placeholderFor + " on: " + on + ">"; |
|
445 } |
|
446 |
|
447 public List<T> getPlaceholderFor() { |
|
448 return placeholderFor; |
|
449 } |
|
450 |
|
451 public Annotate.AnnotateRepeatedContext<T> getRepeatedContext() { |
|
452 return ctx; |
|
453 } |
|
454 } |
|
455 } |
242 } |