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