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