author | jjg |
Tue, 15 Oct 2013 15:57:13 -0700 | |
changeset 21041 | 99f5e5e97425 |
parent 21040 | 3e32f68d2151 |
child 22163 | 3651128c74eb |
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 |
* |
19928
adce50c2b76c
8022260: Rename javac.code.Annotations to javac.code.SymbolMetadata
jfranck
parents:
17578
diff
changeset
|
60 |
* "unnamed" this SymbolMetadata contains some attributes, possibly the final set. |
13689
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 |
*/ |
19928
adce50c2b76c
8022260: Rename javac.code.Annotations to javac.code.SymbolMetadata
jfranck
parents:
17578
diff
changeset
|
68 |
public class SymbolMetadata { |
13689
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 |
/* |
17578 | 79 |
* Type attributes for this symbol. |
80 |
* This field should never be null. |
|
15385 | 81 |
*/ |
82 |
private List<Attribute.TypeCompound> type_attributes = List.<Attribute.TypeCompound>nil(); |
|
83 |
||
84 |
/* |
|
17578 | 85 |
* Type attributes of initializers in this class. |
86 |
* Unused if the current symbol is not a ClassSymbol. |
|
87 |
*/ |
|
88 |
private List<Attribute.TypeCompound> init_type_attributes = List.<Attribute.TypeCompound>nil(); |
|
89 |
||
90 |
/* |
|
91 |
* Type attributes of class initializers in this class. |
|
92 |
* Unused if the current symbol is not a ClassSymbol. |
|
93 |
*/ |
|
94 |
private List<Attribute.TypeCompound> clinit_type_attributes = List.<Attribute.TypeCompound>nil(); |
|
95 |
||
96 |
/* |
|
19928
adce50c2b76c
8022260: Rename javac.code.Annotations to javac.code.SymbolMetadata
jfranck
parents:
17578
diff
changeset
|
97 |
* The Symbol this SymbolMetadata instance belongs to |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
98 |
*/ |
14804
f93a8d60b9a4
8001114: Container annotation is not checked for semantic correctness
jfranck
parents:
14258
diff
changeset
|
99 |
private final Symbol sym; |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
100 |
|
19928
adce50c2b76c
8022260: Rename javac.code.Annotations to javac.code.SymbolMetadata
jfranck
parents:
17578
diff
changeset
|
101 |
public SymbolMetadata(Symbol sym) { |
14804
f93a8d60b9a4
8001114: Container annotation is not checked for semantic correctness
jfranck
parents:
14258
diff
changeset
|
102 |
this.sym = sym; |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
103 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
104 |
|
15385 | 105 |
public List<Attribute.Compound> getDeclarationAttributes() { |
106 |
return filterDeclSentinels(attributes); |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
107 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
108 |
|
15385 | 109 |
public List<Attribute.TypeCompound> getTypeAttributes() { |
110 |
return type_attributes; |
|
111 |
} |
|
112 |
||
17578 | 113 |
public List<Attribute.TypeCompound> getInitTypeAttributes() { |
114 |
return init_type_attributes; |
|
115 |
} |
|
116 |
||
117 |
public List<Attribute.TypeCompound> getClassInitTypeAttributes() { |
|
118 |
return clinit_type_attributes; |
|
119 |
} |
|
120 |
||
15385 | 121 |
public void setDeclarationAttributes(List<Attribute.Compound> a) { |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
122 |
Assert.check(pendingCompletion() || !isStarted()); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
123 |
if (a == null) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
124 |
throw new NullPointerException(); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
125 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
126 |
attributes = a; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
127 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
128 |
|
15385 | 129 |
public void setTypeAttributes(List<Attribute.TypeCompound> a) { |
130 |
if (a == null) { |
|
131 |
throw new NullPointerException(); |
|
132 |
} |
|
133 |
type_attributes = a; |
|
134 |
} |
|
135 |
||
17578 | 136 |
public void setInitTypeAttributes(List<Attribute.TypeCompound> a) { |
137 |
if (a == null) { |
|
138 |
throw new NullPointerException(); |
|
139 |
} |
|
140 |
init_type_attributes = a; |
|
141 |
} |
|
142 |
||
143 |
public void setClassInitTypeAttributes(List<Attribute.TypeCompound> a) { |
|
144 |
if (a == null) { |
|
145 |
throw new NullPointerException(); |
|
146 |
} |
|
147 |
clinit_type_attributes = a; |
|
148 |
} |
|
149 |
||
19928
adce50c2b76c
8022260: Rename javac.code.Annotations to javac.code.SymbolMetadata
jfranck
parents:
17578
diff
changeset
|
150 |
public void setAttributes(SymbolMetadata other) { |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
151 |
if (other == null) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
152 |
throw new NullPointerException(); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
153 |
} |
15385 | 154 |
setDeclarationAttributes(other.getDeclarationAttributes()); |
155 |
setTypeAttributes(other.getTypeAttributes()); |
|
17578 | 156 |
setInitTypeAttributes(other.getInitTypeAttributes()); |
157 |
setClassInitTypeAttributes(other.getClassInitTypeAttributes()); |
|
15385 | 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)); |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
163 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
164 |
|
15385 | 165 |
public void appendTypeAttributesWithCompletion(final Annotate.AnnotateRepeatedContext<Attribute.TypeCompound> ctx) { |
166 |
this.appendUniqueTypes(getAttributesForCompletion(ctx)); |
|
167 |
} |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
168 |
|
15385 | 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; |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
173 |
boolean atLeastOneRepeated = false; |
15385 | 174 |
List<T> buf = List.<T>nil(); |
175 |
for (ListBuffer<T> lb : annotated.values()) { |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
176 |
if (lb.size() == 1) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
177 |
buf = buf.prepend(lb.first()); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
178 |
} else { // repeated |
15385 | 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<T>(ctx, lb.toList(), sym); |
|
184 |
res = ph; |
|
185 |
buf = buf.prepend(res); |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
186 |
atLeastOneRepeated = true; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
187 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
188 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
189 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
190 |
if (atLeastOneRepeated) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
191 |
// The Symbol s is now annotated with a combination of |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
192 |
// finished non-repeating annotations and placeholders for |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
193 |
// repeating annotations. |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
194 |
// |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
195 |
// We need to do this in two passes because when creating |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
196 |
// 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
|
197 |
// guarantee that the @Repeatable on the |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
198 |
// contained annotation is fully annotated |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
199 |
// |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
200 |
// The way we force this order is to do all repeating |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
201 |
// 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
|
202 |
// finished. This will work because @Repeatable |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
203 |
// is non-repeating and therefore will be annotated in the |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
204 |
// fist pass. |
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 |
// Queue a pass that will replace Attribute.Placeholders |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
207 |
// with Attribute.Compound (made from synthesized containers). |
21040
3e32f68d2151
8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents:
19928
diff
changeset
|
208 |
ctx.annotateRepeated(new Annotate.Worker() { |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
209 |
@Override |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
210 |
public String toString() { |
14804
f93a8d60b9a4
8001114: Container annotation is not checked for semantic correctness
jfranck
parents:
14258
diff
changeset
|
211 |
return "repeated annotation pass of: " + sym + " in: " + sym.owner; |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
212 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
213 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
214 |
@Override |
21040
3e32f68d2151
8026510: The name of com.sun.tools.javac.comp.Annotate.Annotator is confusing
jlahoda
parents:
19928
diff
changeset
|
215 |
public void run() { |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
216 |
complete(ctx); |
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 |
}); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
219 |
} |
15385 | 220 |
// Add non-repeating attributes |
221 |
return buf.reverse(); |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
222 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
223 |
|
19928
adce50c2b76c
8022260: Rename javac.code.Annotations to javac.code.SymbolMetadata
jfranck
parents:
17578
diff
changeset
|
224 |
public SymbolMetadata reset() { |
15385 | 225 |
attributes = DECL_IN_PROGRESS; |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
226 |
return this; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
227 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
228 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
229 |
public boolean isEmpty() { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
230 |
return !isStarted() |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
231 |
|| pendingCompletion() |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
232 |
|| attributes.isEmpty(); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
233 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
234 |
|
15385 | 235 |
public boolean isTypesEmpty() { |
236 |
return type_attributes.isEmpty(); |
|
237 |
} |
|
238 |
||
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
239 |
public boolean pendingCompletion() { |
15385 | 240 |
return attributes == DECL_IN_PROGRESS; |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
241 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
242 |
|
19928
adce50c2b76c
8022260: Rename javac.code.Annotations to javac.code.SymbolMetadata
jfranck
parents:
17578
diff
changeset
|
243 |
public SymbolMetadata append(List<Attribute.Compound> l) { |
15385 | 244 |
attributes = filterDeclSentinels(attributes); |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
245 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
246 |
if (l.isEmpty()) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
247 |
; // no-op |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
248 |
} else if (attributes.isEmpty()) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
249 |
attributes = l; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
250 |
} else { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
251 |
attributes = attributes.appendList(l); |
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 |
return this; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
254 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
255 |
|
19928
adce50c2b76c
8022260: Rename javac.code.Annotations to javac.code.SymbolMetadata
jfranck
parents:
17578
diff
changeset
|
256 |
public SymbolMetadata appendUniqueTypes(List<Attribute.TypeCompound> l) { |
15385 | 257 |
if (l.isEmpty()) { |
258 |
; // no-op |
|
259 |
} else if (type_attributes.isEmpty()) { |
|
260 |
type_attributes = l; |
|
261 |
} else { |
|
262 |
// TODO: in case we expect a large number of annotations, this |
|
263 |
// might be inefficient. |
|
264 |
for (Attribute.TypeCompound tc : l) { |
|
265 |
if (!type_attributes.contains(tc)) |
|
266 |
type_attributes = type_attributes.append(tc); |
|
267 |
} |
|
268 |
} |
|
269 |
return this; |
|
270 |
} |
|
271 |
||
19928
adce50c2b76c
8022260: Rename javac.code.Annotations to javac.code.SymbolMetadata
jfranck
parents:
17578
diff
changeset
|
272 |
public SymbolMetadata appendInitTypeAttributes(List<Attribute.TypeCompound> l) { |
17578 | 273 |
if (l.isEmpty()) { |
274 |
; // no-op |
|
275 |
} else if (init_type_attributes.isEmpty()) { |
|
276 |
init_type_attributes = l; |
|
277 |
} else { |
|
278 |
init_type_attributes = init_type_attributes.appendList(l); |
|
279 |
} |
|
280 |
return this; |
|
281 |
} |
|
282 |
||
19928
adce50c2b76c
8022260: Rename javac.code.Annotations to javac.code.SymbolMetadata
jfranck
parents:
17578
diff
changeset
|
283 |
public SymbolMetadata appendClassInitTypeAttributes(List<Attribute.TypeCompound> l) { |
17578 | 284 |
if (l.isEmpty()) { |
285 |
; // no-op |
|
286 |
} else if (clinit_type_attributes.isEmpty()) { |
|
287 |
clinit_type_attributes = l; |
|
288 |
} else { |
|
289 |
clinit_type_attributes = clinit_type_attributes.appendList(l); |
|
290 |
} |
|
291 |
return this; |
|
292 |
} |
|
293 |
||
19928
adce50c2b76c
8022260: Rename javac.code.Annotations to javac.code.SymbolMetadata
jfranck
parents:
17578
diff
changeset
|
294 |
public SymbolMetadata prepend(List<Attribute.Compound> l) { |
15385 | 295 |
attributes = filterDeclSentinels(attributes); |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
296 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
297 |
if (l.isEmpty()) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
298 |
; // no-op |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
299 |
} else if (attributes.isEmpty()) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
300 |
attributes = l; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
301 |
} else { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
302 |
attributes = attributes.prependList(l); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
303 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
304 |
return this; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
305 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
306 |
|
15385 | 307 |
private List<Attribute.Compound> filterDeclSentinels(List<Attribute.Compound> a) { |
308 |
return (a == DECL_IN_PROGRESS || a == DECL_NOT_STARTED) |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
309 |
? List.<Attribute.Compound>nil() |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
310 |
: a; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
311 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
312 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
313 |
private boolean isStarted() { |
15385 | 314 |
return attributes != DECL_NOT_STARTED; |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
315 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
316 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
317 |
private List<Attribute.Compound> getPlaceholders() { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
318 |
List<Attribute.Compound> res = List.<Attribute.Compound>nil(); |
15385 | 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) { |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
330 |
if (a instanceof Placeholder) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
331 |
res = res.prepend(a); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
332 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
333 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
334 |
return res.reverse(); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
335 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
336 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
337 |
/* |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
338 |
* Replace Placeholders for repeating annotations with their containers |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
339 |
*/ |
15385 | 340 |
private <T extends Attribute.Compound> void complete(Annotate.AnnotateRepeatedContext<T> ctx) { |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
341 |
Log log = ctx.log; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
342 |
Env<AttrContext> env = ctx.env; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
343 |
JavaFileObject oldSource = log.useSource(env.toplevel.sourcefile); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
344 |
try { |
15385 | 345 |
// TODO: can we reduce duplication in the following branches? |
346 |
if (ctx.isTypeCompound) { |
|
347 |
Assert.check(!isTypesEmpty()); |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
348 |
|
15385 | 349 |
if (isTypesEmpty()) { |
350 |
return; |
|
351 |
} |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
352 |
|
15385 | 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(); |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
369 |
|
19928
adce50c2b76c
8022260: Rename javac.code.Annotations to javac.code.SymbolMetadata
jfranck
parents:
17578
diff
changeset
|
370 |
Assert.check(SymbolMetadata.this.getTypePlaceholders().isEmpty()); |
15385 | 371 |
} else { |
372 |
Assert.check(!pendingCompletion()); |
|
373 |
||
374 |
if (isEmpty()) { |
|
375 |
return; |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
376 |
} |
15385 | 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); |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
383 |
|
15385 | 384 |
if (null != replacement) { |
385 |
result = result.prepend(replacement); |
|
386 |
} |
|
387 |
} else { |
|
388 |
result = result.prepend(a); |
|
389 |
} |
|
390 |
} |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
391 |
|
15385 | 392 |
attributes = result.reverse(); |
393 |
||
19928
adce50c2b76c
8022260: Rename javac.code.Annotations to javac.code.SymbolMetadata
jfranck
parents:
17578
diff
changeset
|
394 |
Assert.check(SymbolMetadata.this.getPlaceholders().isEmpty()); |
15385 | 395 |
} |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
396 |
} finally { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
397 |
log.useSource(oldSource); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
398 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
399 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
400 |
|
15385 | 401 |
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
|
402 |
Log log = ctx.log; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
403 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
404 |
// Process repeated annotations |
15385 | 405 |
T validRepeated = ctx.processRepeatedAnnotations(placeholder.getPlaceholderFor(), sym); |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
406 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
407 |
if (validRepeated != null) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
408 |
// Check that the container isn't manually |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
409 |
// present along with repeated instances of |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
410 |
// its contained annotation. |
15385 | 411 |
ListBuffer<T> manualContainer = ctx.annotated.get(validRepeated.type.tsym); |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
412 |
if (manualContainer != null) { |
15356
cf312dc54c60
8006119: update javac to follow latest spec for repeatable annotations
jjg
parents:
14804
diff
changeset
|
413 |
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
|
414 |
manualContainer.first().type.tsym); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
415 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
416 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
417 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
418 |
// A null return will delete the Placeholder |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
419 |
return validRepeated; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
420 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
421 |
|
15385 | 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; |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
427 |
|
15385 | 428 |
public Placeholder(Annotate.AnnotateRepeatedContext<T> ctx, List<T> placeholderFor, Symbol on) { |
429 |
super(on.type, List.<Pair<Symbol.MethodSymbol, Attribute>>nil(), |
|
430 |
ctx.isTypeCompound ? |
|
431 |
((Attribute.TypeCompound)placeholderFor.head).position : |
|
21041 | 432 |
new TypeAnnotationPosition()); |
15385 | 433 |
this.ctx = ctx; |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
434 |
this.placeholderFor = placeholderFor; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
435 |
this.on = on; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
436 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
437 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
438 |
@Override |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
439 |
public String toString() { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
440 |
return "<placeholder: " + placeholderFor + " on: " + on + ">"; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
441 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
442 |
|
15385 | 443 |
public List<T> getPlaceholderFor() { |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
444 |
return placeholderFor; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
445 |
} |
15385 | 446 |
|
447 |
public Annotate.AnnotateRepeatedContext<T> getRepeatedContext() { |
|
448 |
return ctx; |
|
449 |
} |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
450 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
diff
changeset
|
451 |
} |