author | jlahoda |
Wed, 17 Jul 2013 15:08:58 +0200 | |
changeset 18919 | 7d1f1448a9db |
parent 17578 | 46ac954e4a84 |
child 19122 | 1841f2fa76de |
permissions | -rw-r--r-- |
10 | 1 |
/* |
15356
cf312dc54c60
8006119: update javac to follow latest spec for repeatable annotations
jjg
parents:
14961
diff
changeset
|
2 |
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 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 |
* |
|
5520 | 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. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.comp; |
|
27 |
||
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
28 |
import java.util.Map; |
15385 | 29 |
|
10 | 30 |
import com.sun.tools.javac.util.*; |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
31 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
10 | 32 |
import com.sun.tools.javac.code.*; |
33 |
import com.sun.tools.javac.code.Symbol.*; |
|
34 |
import com.sun.tools.javac.tree.*; |
|
35 |
import com.sun.tools.javac.tree.JCTree.*; |
|
36 |
||
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14054
diff
changeset
|
37 |
import static com.sun.tools.javac.code.TypeTag.ARRAY; |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14054
diff
changeset
|
38 |
import static com.sun.tools.javac.code.TypeTag.CLASS; |
10950 | 39 |
import static com.sun.tools.javac.tree.JCTree.Tag.*; |
40 |
||
10 | 41 |
/** Enter annotations on symbols. Annotations accumulate in a queue, |
42 |
* which is processed at the top level of any set of recursive calls |
|
43 |
* requesting it be processed. |
|
44 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
45 |
* <p><b>This is NOT part of any supported API. |
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
46 |
* If you write code that depends on this, you do so at your own risk. |
10 | 47 |
* This code and its internal interfaces are subject to change or |
48 |
* deletion without notice.</b> |
|
49 |
*/ |
|
50 |
public class Annotate { |
|
51 |
protected static final Context.Key<Annotate> annotateKey = |
|
52 |
new Context.Key<Annotate>(); |
|
53 |
||
54 |
public static Annotate instance(Context context) { |
|
55 |
Annotate instance = context.get(annotateKey); |
|
56 |
if (instance == null) |
|
57 |
instance = new Annotate(context); |
|
58 |
return instance; |
|
59 |
} |
|
60 |
||
61 |
final Attr attr; |
|
62 |
final TreeMaker make; |
|
63 |
final Log log; |
|
64 |
final Symtab syms; |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
10
diff
changeset
|
65 |
final Names names; |
10 | 66 |
final Resolve rs; |
67 |
final Types types; |
|
68 |
final ConstFold cfolder; |
|
69 |
final Check chk; |
|
70 |
||
71 |
protected Annotate(Context context) { |
|
72 |
context.put(annotateKey, this); |
|
73 |
attr = Attr.instance(context); |
|
74 |
make = TreeMaker.instance(context); |
|
75 |
log = Log.instance(context); |
|
76 |
syms = Symtab.instance(context); |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
10
diff
changeset
|
77 |
names = Names.instance(context); |
10 | 78 |
rs = Resolve.instance(context); |
79 |
types = Types.instance(context); |
|
80 |
cfolder = ConstFold.instance(context); |
|
81 |
chk = Check.instance(context); |
|
82 |
} |
|
83 |
||
84 |
/* ******************************************************************** |
|
85 |
* Queue maintenance |
|
86 |
*********************************************************************/ |
|
87 |
||
88 |
private int enterCount = 0; |
|
89 |
||
90 |
ListBuffer<Annotator> q = new ListBuffer<Annotator>(); |
|
15385 | 91 |
ListBuffer<Annotator> typesQ = new ListBuffer<Annotator>(); |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
92 |
ListBuffer<Annotator> repeatedQ = new ListBuffer<Annotator>(); |
15385 | 93 |
ListBuffer<Annotator> afterRepeatedQ = new ListBuffer<Annotator>(); |
94 |
||
95 |
public void earlier(Annotator a) { |
|
96 |
q.prepend(a); |
|
97 |
} |
|
10 | 98 |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
99 |
public void normal(Annotator a) { |
10 | 100 |
q.append(a); |
101 |
} |
|
102 |
||
15385 | 103 |
public void typeAnnotation(Annotator a) { |
104 |
typesQ.append(a); |
|
10 | 105 |
} |
106 |
||
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
107 |
public void repeated(Annotator a) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
108 |
repeatedQ.append(a); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
109 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
110 |
|
15385 | 111 |
public void afterRepeated(Annotator a) { |
112 |
afterRepeatedQ.append(a); |
|
113 |
} |
|
114 |
||
10 | 115 |
/** Called when the Enter phase starts. */ |
116 |
public void enterStart() { |
|
117 |
enterCount++; |
|
118 |
} |
|
119 |
||
120 |
/** Called after the Enter phase completes. */ |
|
121 |
public void enterDone() { |
|
122 |
enterCount--; |
|
123 |
flush(); |
|
124 |
} |
|
125 |
||
126 |
public void flush() { |
|
127 |
if (enterCount != 0) return; |
|
128 |
enterCount++; |
|
129 |
try { |
|
15385 | 130 |
while (q.nonEmpty()) { |
10 | 131 |
q.next().enterAnnotation(); |
15385 | 132 |
} |
133 |
while (typesQ.nonEmpty()) { |
|
134 |
typesQ.next().enterAnnotation(); |
|
135 |
} |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
136 |
while (repeatedQ.nonEmpty()) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
137 |
repeatedQ.next().enterAnnotation(); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
138 |
} |
15385 | 139 |
while (afterRepeatedQ.nonEmpty()) { |
140 |
afterRepeatedQ.next().enterAnnotation(); |
|
141 |
} |
|
10 | 142 |
} finally { |
143 |
enterCount--; |
|
144 |
} |
|
145 |
} |
|
146 |
||
147 |
/** A client that has annotations to add registers an annotator, |
|
148 |
* the method it will use to add the annotation. There are no |
|
149 |
* parameters; any needed data should be captured by the |
|
150 |
* Annotator. |
|
151 |
*/ |
|
152 |
public interface Annotator { |
|
153 |
void enterAnnotation(); |
|
154 |
String toString(); |
|
155 |
} |
|
156 |
||
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
157 |
/** |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
158 |
* This context contains all the information needed to synthesize new |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
159 |
* annotations trees by the completer for repeating annotations. |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
160 |
*/ |
15385 | 161 |
public class AnnotateRepeatedContext<T extends Attribute.Compound> { |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
162 |
public final Env<AttrContext> env; |
15385 | 163 |
public final Map<Symbol.TypeSymbol, ListBuffer<T>> annotated; |
164 |
public final Map<T, JCDiagnostic.DiagnosticPosition> pos; |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
165 |
public final Log log; |
15385 | 166 |
public final boolean isTypeCompound; |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
167 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
168 |
public AnnotateRepeatedContext(Env<AttrContext> env, |
15385 | 169 |
Map<Symbol.TypeSymbol, ListBuffer<T>> annotated, |
170 |
Map<T, JCDiagnostic.DiagnosticPosition> pos, |
|
171 |
Log log, |
|
172 |
boolean isTypeCompound) { |
|
13840
a365c561f4de
7196774: javac cannot be built with JDK 6 after 7151010
jjg
parents:
13689
diff
changeset
|
173 |
Assert.checkNonNull(env); |
a365c561f4de
7196774: javac cannot be built with JDK 6 after 7151010
jjg
parents:
13689
diff
changeset
|
174 |
Assert.checkNonNull(annotated); |
a365c561f4de
7196774: javac cannot be built with JDK 6 after 7151010
jjg
parents:
13689
diff
changeset
|
175 |
Assert.checkNonNull(pos); |
a365c561f4de
7196774: javac cannot be built with JDK 6 after 7151010
jjg
parents:
13689
diff
changeset
|
176 |
Assert.checkNonNull(log); |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
177 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
178 |
this.env = env; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
179 |
this.annotated = annotated; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
180 |
this.pos = pos; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
181 |
this.log = log; |
15385 | 182 |
this.isTypeCompound = isTypeCompound; |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
183 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
184 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
185 |
/** |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
186 |
* Process a list of repeating annotations returning a new |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
187 |
* Attribute.Compound that is the attribute for the synthesized tree |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
188 |
* for the container. |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
189 |
* |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
190 |
* @param repeatingAnnotations a List of repeating annotations |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
191 |
* @return a new Attribute.Compound that is the container for the repeatingAnnotations |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
192 |
*/ |
15385 | 193 |
public T processRepeatedAnnotations(List<T> repeatingAnnotations, Symbol sym) { |
14804
f93a8d60b9a4
8001114: Container annotation is not checked for semantic correctness
jfranck
parents:
14359
diff
changeset
|
194 |
return Annotate.this.processRepeatedAnnotations(repeatingAnnotations, this, sym); |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
195 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
196 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
197 |
/** |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
198 |
* Queue the Annotator a on the repeating annotations queue of the |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
199 |
* Annotate instance this context belongs to. |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
200 |
* |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
201 |
* @param a the Annotator to enqueue for repeating annotation annotating |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
202 |
*/ |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
203 |
public void annotateRepeated(Annotator a) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
204 |
Annotate.this.repeated(a); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
205 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
206 |
} |
10 | 207 |
|
208 |
/* ******************************************************************** |
|
209 |
* Compute an attribute from its annotation. |
|
210 |
*********************************************************************/ |
|
211 |
||
212 |
/** Process a single compound annotation, returning its |
|
213 |
* Attribute. Used from MemberEnter for attaching the attributes |
|
214 |
* to the annotated symbol. |
|
215 |
*/ |
|
216 |
Attribute.Compound enterAnnotation(JCAnnotation a, |
|
217 |
Type expected, |
|
218 |
Env<AttrContext> env) { |
|
17578 | 219 |
return enterAnnotation(a, expected, env, false); |
220 |
} |
|
221 |
||
222 |
Attribute.TypeCompound enterTypeAnnotation(JCAnnotation a, |
|
223 |
Type expected, |
|
224 |
Env<AttrContext> env) { |
|
225 |
return (Attribute.TypeCompound) enterAnnotation(a, expected, env, true); |
|
226 |
} |
|
227 |
||
228 |
// boolean typeAnnotation determines whether the method returns |
|
229 |
// a Compound (false) or TypeCompound (true). |
|
230 |
Attribute.Compound enterAnnotation(JCAnnotation a, |
|
231 |
Type expected, |
|
232 |
Env<AttrContext> env, |
|
233 |
boolean typeAnnotation) { |
|
10 | 234 |
// The annotation might have had its type attributed (but not checked) |
235 |
// by attr.attribAnnotationTypes during MemberEnter, in which case we do not |
|
236 |
// need to do it again. |
|
237 |
Type at = (a.annotationType.type != null ? a.annotationType.type |
|
238 |
: attr.attribType(a.annotationType, env)); |
|
239 |
a.type = chk.checkType(a.annotationType.pos(), at, expected); |
|
17578 | 240 |
if (a.type.isErroneous()) { |
241 |
if (typeAnnotation) { |
|
242 |
return new Attribute.TypeCompound(a.type, List.<Pair<MethodSymbol,Attribute>>nil(), null); |
|
243 |
} else { |
|
244 |
return new Attribute.Compound(a.type, List.<Pair<MethodSymbol,Attribute>>nil()); |
|
245 |
} |
|
246 |
} |
|
10 | 247 |
if ((a.type.tsym.flags() & Flags.ANNOTATION) == 0) { |
248 |
log.error(a.annotationType.pos(), |
|
249 |
"not.annotation.type", a.type.toString()); |
|
17578 | 250 |
if (typeAnnotation) { |
251 |
return new Attribute.TypeCompound(a.type, List.<Pair<MethodSymbol,Attribute>>nil(), null); |
|
252 |
} else { |
|
253 |
return new Attribute.Compound(a.type, List.<Pair<MethodSymbol,Attribute>>nil()); |
|
254 |
} |
|
10 | 255 |
} |
256 |
List<JCExpression> args = a.args; |
|
10950 | 257 |
if (args.length() == 1 && !args.head.hasTag(ASSIGN)) { |
10 | 258 |
// special case: elided "value=" assumed |
259 |
args.head = make.at(args.head.pos). |
|
260 |
Assign(make.Ident(names.value), args.head); |
|
261 |
} |
|
262 |
ListBuffer<Pair<MethodSymbol,Attribute>> buf = |
|
263 |
new ListBuffer<Pair<MethodSymbol,Attribute>>(); |
|
264 |
for (List<JCExpression> tl = args; tl.nonEmpty(); tl = tl.tail) { |
|
265 |
JCExpression t = tl.head; |
|
10950 | 266 |
if (!t.hasTag(ASSIGN)) { |
10 | 267 |
log.error(t.pos(), "annotation.value.must.be.name.value"); |
268 |
continue; |
|
269 |
} |
|
270 |
JCAssign assign = (JCAssign)t; |
|
10950 | 271 |
if (!assign.lhs.hasTag(IDENT)) { |
10 | 272 |
log.error(t.pos(), "annotation.value.must.be.name.value"); |
273 |
continue; |
|
274 |
} |
|
275 |
JCIdent left = (JCIdent)assign.lhs; |
|
276 |
Symbol method = rs.resolveQualifiedMethod(left.pos(), |
|
9603
fa337b87574b
6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents:
7681
diff
changeset
|
277 |
env, |
fa337b87574b
6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents:
7681
diff
changeset
|
278 |
a.type, |
fa337b87574b
6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents:
7681
diff
changeset
|
279 |
left.name, |
fa337b87574b
6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents:
7681
diff
changeset
|
280 |
List.<Type>nil(), |
fa337b87574b
6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents:
7681
diff
changeset
|
281 |
null); |
10 | 282 |
left.sym = method; |
283 |
left.type = method.type; |
|
284 |
if (method.owner != a.type.tsym) |
|
285 |
log.error(left.pos(), "no.annotation.member", left.name, a.type); |
|
286 |
Type result = method.type.getReturnType(); |
|
287 |
Attribute value = enterAttributeValue(result, assign.rhs, env); |
|
288 |
if (!method.type.isErroneous()) |
|
289 |
buf.append(new Pair<MethodSymbol,Attribute> |
|
290 |
((MethodSymbol)method, value)); |
|
6594
d43f068fba19
6970584: Flow.java should be more error-friendly
mcimadamore
parents:
5847
diff
changeset
|
291 |
t.type = result; |
10 | 292 |
} |
17578 | 293 |
if (typeAnnotation) { |
294 |
if (a.attribute == null || !(a.attribute instanceof Attribute.TypeCompound)) { |
|
295 |
// Create a new TypeCompound |
|
296 |
Attribute.TypeCompound tc = new Attribute.TypeCompound(a.type, buf.toList(), new TypeAnnotationPosition()); |
|
297 |
a.attribute = tc; |
|
298 |
return tc; |
|
299 |
} else { |
|
300 |
// Use an existing TypeCompound |
|
301 |
return a.attribute; |
|
302 |
} |
|
303 |
} else { |
|
304 |
Attribute.Compound ac = new Attribute.Compound(a.type, buf.toList()); |
|
305 |
a.attribute = ac; |
|
306 |
return ac; |
|
307 |
} |
|
10 | 308 |
} |
309 |
||
310 |
Attribute enterAttributeValue(Type expected, |
|
311 |
JCExpression tree, |
|
312 |
Env<AttrContext> env) { |
|
9603
fa337b87574b
6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents:
7681
diff
changeset
|
313 |
//first, try completing the attribution value sym - if a completion |
fa337b87574b
6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents:
7681
diff
changeset
|
314 |
//error is thrown, we should recover gracefully, and display an |
fa337b87574b
6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents:
7681
diff
changeset
|
315 |
//ordinary resolution diagnostic. |
fa337b87574b
6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents:
7681
diff
changeset
|
316 |
try { |
fa337b87574b
6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents:
7681
diff
changeset
|
317 |
expected.tsym.complete(); |
fa337b87574b
6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents:
7681
diff
changeset
|
318 |
} catch(CompletionFailure e) { |
fa337b87574b
6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents:
7681
diff
changeset
|
319 |
log.error(tree.pos(), "cant.resolve", Kinds.kindName(e.sym), e.sym); |
fa337b87574b
6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents:
7681
diff
changeset
|
320 |
return new Attribute.Error(expected); |
fa337b87574b
6550655: com.sun.tools.javac.code.Symbol$CompletionFailure
mcimadamore
parents:
7681
diff
changeset
|
321 |
} |
10 | 322 |
if (expected.isPrimitive() || types.isSameType(expected, syms.stringType)) { |
323 |
Type result = attr.attribExpr(tree, env, expected); |
|
324 |
if (result.isErroneous()) |
|
325 |
return new Attribute.Error(expected); |
|
326 |
if (result.constValue() == null) { |
|
327 |
log.error(tree.pos(), "attribute.value.must.be.constant"); |
|
328 |
return new Attribute.Error(expected); |
|
329 |
} |
|
330 |
result = cfolder.coerce(result, expected); |
|
331 |
return new Attribute.Constant(expected, result.constValue()); |
|
332 |
} |
|
333 |
if (expected.tsym == syms.classType.tsym) { |
|
334 |
Type result = attr.attribExpr(tree, env, expected); |
|
335 |
if (result.isErroneous()) |
|
336 |
return new Attribute.Error(expected); |
|
337 |
if (TreeInfo.name(tree) != names._class) { |
|
338 |
log.error(tree.pos(), "annotation.value.must.be.class.literal"); |
|
339 |
return new Attribute.Error(expected); |
|
340 |
} |
|
341 |
return new Attribute.Class(types, |
|
342 |
(((JCFieldAccess) tree).selected).type); |
|
343 |
} |
|
344 |
if ((expected.tsym.flags() & Flags.ANNOTATION) != 0) { |
|
10950 | 345 |
if (!tree.hasTag(ANNOTATION)) { |
10 | 346 |
log.error(tree.pos(), "annotation.value.must.be.annotation"); |
347 |
expected = syms.errorType; |
|
348 |
} |
|
349 |
return enterAnnotation((JCAnnotation)tree, expected, env); |
|
350 |
} |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14054
diff
changeset
|
351 |
if (expected.hasTag(ARRAY)) { // should really be isArray() |
10950 | 352 |
if (!tree.hasTag(NEWARRAY)) { |
10 | 353 |
tree = make.at(tree.pos). |
354 |
NewArray(null, List.<JCExpression>nil(), List.of(tree)); |
|
355 |
} |
|
356 |
JCNewArray na = (JCNewArray)tree; |
|
357 |
if (na.elemtype != null) { |
|
358 |
log.error(na.elemtype.pos(), "new.not.allowed.in.annotation"); |
|
359 |
return new Attribute.Error(expected); |
|
360 |
} |
|
361 |
ListBuffer<Attribute> buf = new ListBuffer<Attribute>(); |
|
362 |
for (List<JCExpression> l = na.elems; l.nonEmpty(); l=l.tail) { |
|
363 |
buf.append(enterAttributeValue(types.elemtype(expected), |
|
364 |
l.head, |
|
365 |
env)); |
|
366 |
} |
|
6594
d43f068fba19
6970584: Flow.java should be more error-friendly
mcimadamore
parents:
5847
diff
changeset
|
367 |
na.type = expected; |
10 | 368 |
return new Attribute. |
369 |
Array(expected, buf.toArray(new Attribute[buf.length()])); |
|
370 |
} |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14054
diff
changeset
|
371 |
if (expected.hasTag(CLASS) && |
10 | 372 |
(expected.tsym.flags() & Flags.ENUM) != 0) { |
373 |
attr.attribExpr(tree, env, expected); |
|
374 |
Symbol sym = TreeInfo.symbol(tree); |
|
375 |
if (sym == null || |
|
376 |
TreeInfo.nonstaticSelect(tree) || |
|
377 |
sym.kind != Kinds.VAR || |
|
378 |
(sym.flags() & Flags.ENUM) == 0) { |
|
379 |
log.error(tree.pos(), "enum.annotation.must.be.enum.constant"); |
|
380 |
return new Attribute.Error(expected); |
|
381 |
} |
|
382 |
VarSymbol enumerator = (VarSymbol) sym; |
|
383 |
return new Attribute.Enum(expected, enumerator); |
|
384 |
} |
|
385 |
if (!expected.isErroneous()) |
|
386 |
log.error(tree.pos(), "annotation.value.not.allowable.type"); |
|
387 |
return new Attribute.Error(attr.attribExpr(tree, env, expected)); |
|
388 |
} |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
389 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
390 |
/* ********************************* |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
391 |
* Support for repeating annotations |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
392 |
***********************************/ |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
393 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
394 |
/* Process repeated annotations. This method returns the |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
395 |
* synthesized container annotation or null IFF all repeating |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
396 |
* annotation are invalid. This method reports errors/warnings. |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
397 |
*/ |
15385 | 398 |
private <T extends Attribute.Compound> T processRepeatedAnnotations(List<T> annotations, |
399 |
AnnotateRepeatedContext<T> ctx, |
|
400 |
Symbol on) { |
|
401 |
T firstOccurrence = annotations.head; |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
402 |
List<Attribute> repeated = List.nil(); |
14804
f93a8d60b9a4
8001114: Container annotation is not checked for semantic correctness
jfranck
parents:
14359
diff
changeset
|
403 |
Type origAnnoType = null; |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
404 |
Type arrayOfOrigAnnoType = null; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
405 |
Type targetContainerType = null; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
406 |
MethodSymbol containerValueSymbol = null; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
407 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
408 |
Assert.check(!annotations.isEmpty() && |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
409 |
!annotations.tail.isEmpty()); // i.e. size() > 1 |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
410 |
|
16342
28d6ae9ed67c
7196531: Duplicate error messages on repeating annotations
jfranck
parents:
15385
diff
changeset
|
411 |
int count = 0; |
15385 | 412 |
for (List<T> al = annotations; |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
413 |
!al.isEmpty(); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
414 |
al = al.tail) |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
415 |
{ |
16342
28d6ae9ed67c
7196531: Duplicate error messages on repeating annotations
jfranck
parents:
15385
diff
changeset
|
416 |
count++; |
28d6ae9ed67c
7196531: Duplicate error messages on repeating annotations
jfranck
parents:
15385
diff
changeset
|
417 |
|
28d6ae9ed67c
7196531: Duplicate error messages on repeating annotations
jfranck
parents:
15385
diff
changeset
|
418 |
// There must be more than a single anno in the annotation list |
28d6ae9ed67c
7196531: Duplicate error messages on repeating annotations
jfranck
parents:
15385
diff
changeset
|
419 |
Assert.check(count > 1 || !al.tail.isEmpty()); |
28d6ae9ed67c
7196531: Duplicate error messages on repeating annotations
jfranck
parents:
15385
diff
changeset
|
420 |
|
15385 | 421 |
T currentAnno = al.head; |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
422 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
423 |
origAnnoType = currentAnno.type; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
424 |
if (arrayOfOrigAnnoType == null) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
425 |
arrayOfOrigAnnoType = types.makeArrayType(origAnnoType); |
15385 | 426 |
} |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
427 |
|
16342
28d6ae9ed67c
7196531: Duplicate error messages on repeating annotations
jfranck
parents:
15385
diff
changeset
|
428 |
// Only report errors if this isn't the first occurrence I.E. count > 1 |
28d6ae9ed67c
7196531: Duplicate error messages on repeating annotations
jfranck
parents:
15385
diff
changeset
|
429 |
boolean reportError = count > 1; |
28d6ae9ed67c
7196531: Duplicate error messages on repeating annotations
jfranck
parents:
15385
diff
changeset
|
430 |
Type currentContainerType = getContainingType(currentAnno, ctx.pos.get(currentAnno), reportError); |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
431 |
if (currentContainerType == null) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
432 |
continue; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
433 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
434 |
// Assert that the target Container is == for all repeated |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
435 |
// annos of the same annotation type, the types should |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
436 |
// come from the same Symbol, i.e. be '==' |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
437 |
Assert.check(targetContainerType == null || currentContainerType == targetContainerType); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
438 |
targetContainerType = currentContainerType; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
439 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
440 |
containerValueSymbol = validateContainer(targetContainerType, origAnnoType, ctx.pos.get(currentAnno)); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
441 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
442 |
if (containerValueSymbol == null) { // Check of CA type failed |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
443 |
// errors are already reported |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
444 |
continue; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
445 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
446 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
447 |
repeated = repeated.prepend(currentAnno); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
448 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
449 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
450 |
if (!repeated.isEmpty()) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
451 |
repeated = repeated.reverse(); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
452 |
TreeMaker m = make.at(ctx.pos.get(firstOccurrence)); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
453 |
Pair<MethodSymbol, Attribute> p = |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
454 |
new Pair<MethodSymbol, Attribute>(containerValueSymbol, |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
455 |
new Attribute.Array(arrayOfOrigAnnoType, repeated)); |
15385 | 456 |
if (ctx.isTypeCompound) { |
457 |
/* TODO: the following code would be cleaner: |
|
458 |
Attribute.TypeCompound at = new Attribute.TypeCompound(targetContainerType, List.of(p), |
|
459 |
((Attribute.TypeCompound)annotations.head).position); |
|
460 |
JCTypeAnnotation annoTree = m.TypeAnnotation(at); |
|
461 |
at = enterTypeAnnotation(annoTree, targetContainerType, ctx.env); |
|
462 |
*/ |
|
463 |
// However, we directly construct the TypeCompound to keep the |
|
464 |
// direct relation to the contained TypeCompounds. |
|
465 |
Attribute.TypeCompound at = new Attribute.TypeCompound(targetContainerType, List.of(p), |
|
466 |
((Attribute.TypeCompound)annotations.head).position); |
|
14804
f93a8d60b9a4
8001114: Container annotation is not checked for semantic correctness
jfranck
parents:
14359
diff
changeset
|
467 |
|
15385 | 468 |
// TODO: annotation applicability checks from below? |
469 |
||
470 |
at.setSynthesized(true); |
|
14804
f93a8d60b9a4
8001114: Container annotation is not checked for semantic correctness
jfranck
parents:
14359
diff
changeset
|
471 |
|
15385 | 472 |
@SuppressWarnings("unchecked") |
473 |
T x = (T) at; |
|
474 |
return x; |
|
475 |
} else { |
|
476 |
Attribute.Compound c = new Attribute.Compound(targetContainerType, List.of(p)); |
|
477 |
JCAnnotation annoTree = m.Annotation(c); |
|
478 |
||
479 |
if (!chk.annotationApplicable(annoTree, on)) |
|
480 |
log.error(annoTree.pos(), "invalid.repeatable.annotation.incompatible.target", targetContainerType, origAnnoType); |
|
14804
f93a8d60b9a4
8001114: Container annotation is not checked for semantic correctness
jfranck
parents:
14359
diff
changeset
|
481 |
|
15385 | 482 |
if (!chk.validateAnnotationDeferErrors(annoTree)) |
483 |
log.error(annoTree.pos(), "duplicate.annotation.invalid.repeated", origAnnoType); |
|
484 |
||
485 |
c = enterAnnotation(annoTree, targetContainerType, ctx.env); |
|
486 |
c.setSynthesized(true); |
|
487 |
||
488 |
@SuppressWarnings("unchecked") |
|
489 |
T x = (T) c; |
|
490 |
return x; |
|
491 |
} |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
492 |
} else { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
493 |
return null; // errors should have been reported elsewhere |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
494 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
495 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
496 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
497 |
/** Fetches the actual Type that should be the containing annotation. */ |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
498 |
private Type getContainingType(Attribute.Compound currentAnno, |
16342
28d6ae9ed67c
7196531: Duplicate error messages on repeating annotations
jfranck
parents:
15385
diff
changeset
|
499 |
DiagnosticPosition pos, |
28d6ae9ed67c
7196531: Duplicate error messages on repeating annotations
jfranck
parents:
15385
diff
changeset
|
500 |
boolean reportError) |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
501 |
{ |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
502 |
Type origAnnoType = currentAnno.type; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
503 |
TypeSymbol origAnnoDecl = origAnnoType.tsym; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
504 |
|
15356
cf312dc54c60
8006119: update javac to follow latest spec for repeatable annotations
jjg
parents:
14961
diff
changeset
|
505 |
// Fetch the Repeatable annotation from the current |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
506 |
// annotation's declaration, or null if it has none |
15356
cf312dc54c60
8006119: update javac to follow latest spec for repeatable annotations
jjg
parents:
14961
diff
changeset
|
507 |
Attribute.Compound ca = origAnnoDecl.attribute(syms.repeatableType.tsym); |
cf312dc54c60
8006119: update javac to follow latest spec for repeatable annotations
jjg
parents:
14961
diff
changeset
|
508 |
if (ca == null) { // has no Repeatable annotation |
16342
28d6ae9ed67c
7196531: Duplicate error messages on repeating annotations
jfranck
parents:
15385
diff
changeset
|
509 |
if (reportError) |
28d6ae9ed67c
7196531: Duplicate error messages on repeating annotations
jfranck
parents:
15385
diff
changeset
|
510 |
log.error(pos, "duplicate.annotation.missing.container", origAnnoType, syms.repeatableType); |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
511 |
return null; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
512 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
513 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
514 |
return filterSame(extractContainingType(ca, pos, origAnnoDecl), |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
515 |
origAnnoType); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
516 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
517 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
518 |
// returns null if t is same as 's', returns 't' otherwise |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
519 |
private Type filterSame(Type t, Type s) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
520 |
if (t == null || s == null) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
521 |
return t; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
522 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
523 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
524 |
return types.isSameType(t, s) ? null : t; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
525 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
526 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
527 |
/** Extract the actual Type to be used for a containing annotation. */ |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
528 |
private Type extractContainingType(Attribute.Compound ca, |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
529 |
DiagnosticPosition pos, |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
530 |
TypeSymbol annoDecl) |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
531 |
{ |
15356
cf312dc54c60
8006119: update javac to follow latest spec for repeatable annotations
jjg
parents:
14961
diff
changeset
|
532 |
// The next three checks check that the Repeatable annotation |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
533 |
// on the declaration of the annotation type that is repeating is |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
534 |
// valid. |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
535 |
|
15356
cf312dc54c60
8006119: update javac to follow latest spec for repeatable annotations
jjg
parents:
14961
diff
changeset
|
536 |
// Repeatable must have at least one element |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
537 |
if (ca.values.isEmpty()) { |
15356
cf312dc54c60
8006119: update javac to follow latest spec for repeatable annotations
jjg
parents:
14961
diff
changeset
|
538 |
log.error(pos, "invalid.repeatable.annotation", annoDecl); |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
539 |
return null; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
540 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
541 |
Pair<MethodSymbol,Attribute> p = ca.values.head; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
542 |
Name name = p.fst.name; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
543 |
if (name != names.value) { // should contain only one element, named "value" |
15356
cf312dc54c60
8006119: update javac to follow latest spec for repeatable annotations
jjg
parents:
14961
diff
changeset
|
544 |
log.error(pos, "invalid.repeatable.annotation", annoDecl); |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
545 |
return null; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
546 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
547 |
if (!(p.snd instanceof Attribute.Class)) { // check that the value of "value" is an Attribute.Class |
15356
cf312dc54c60
8006119: update javac to follow latest spec for repeatable annotations
jjg
parents:
14961
diff
changeset
|
548 |
log.error(pos, "invalid.repeatable.annotation", annoDecl); |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
549 |
return null; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
550 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
551 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
552 |
return ((Attribute.Class)p.snd).getValue(); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
553 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
554 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
555 |
/* Validate that the suggested targetContainerType Type is a valid |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
556 |
* container type for repeated instances of originalAnnoType |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
557 |
* annotations. Return null and report errors if this is not the |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
558 |
* case, return the MethodSymbol of the value element in |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
559 |
* targetContainerType if it is suitable (this is needed to |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
560 |
* synthesize the container). */ |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
561 |
private MethodSymbol validateContainer(Type targetContainerType, |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
562 |
Type originalAnnoType, |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
563 |
DiagnosticPosition pos) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
564 |
MethodSymbol containerValueSymbol = null; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
565 |
boolean fatalError = false; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
566 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
567 |
// Validate that there is a (and only 1) value method |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
568 |
Scope scope = targetContainerType.tsym.members(); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
569 |
int nr_value_elems = 0; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
570 |
boolean error = false; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
571 |
for(Symbol elm : scope.getElementsByName(names.value)) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
572 |
nr_value_elems++; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
573 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
574 |
if (nr_value_elems == 1 && |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
575 |
elm.kind == Kinds.MTH) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
576 |
containerValueSymbol = (MethodSymbol)elm; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
577 |
} else { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
578 |
error = true; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
579 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
580 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
581 |
if (error) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
582 |
log.error(pos, |
15356
cf312dc54c60
8006119: update javac to follow latest spec for repeatable annotations
jjg
parents:
14961
diff
changeset
|
583 |
"invalid.repeatable.annotation.multiple.values", |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
584 |
targetContainerType, |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
585 |
nr_value_elems); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
586 |
return null; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
587 |
} else if (nr_value_elems == 0) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
588 |
log.error(pos, |
15356
cf312dc54c60
8006119: update javac to follow latest spec for repeatable annotations
jjg
parents:
14961
diff
changeset
|
589 |
"invalid.repeatable.annotation.no.value", |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
590 |
targetContainerType); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
591 |
return null; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
592 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
593 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
594 |
// validate that the 'value' element is a method |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
595 |
// probably "impossible" to fail this |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
596 |
if (containerValueSymbol.kind != Kinds.MTH) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
597 |
log.error(pos, |
15356
cf312dc54c60
8006119: update javac to follow latest spec for repeatable annotations
jjg
parents:
14961
diff
changeset
|
598 |
"invalid.repeatable.annotation.invalid.value", |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
599 |
targetContainerType); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
600 |
fatalError = true; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
601 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
602 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
603 |
// validate that the 'value' element has the correct return type |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
604 |
// i.e. array of original anno |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
605 |
Type valueRetType = containerValueSymbol.type.getReturnType(); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
606 |
Type expectedType = types.makeArrayType(originalAnnoType); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
607 |
if (!(types.isArray(valueRetType) && |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
608 |
types.isSameType(expectedType, valueRetType))) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
609 |
log.error(pos, |
15356
cf312dc54c60
8006119: update javac to follow latest spec for repeatable annotations
jjg
parents:
14961
diff
changeset
|
610 |
"invalid.repeatable.annotation.value.return", |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
611 |
targetContainerType, |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
612 |
valueRetType, |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
613 |
expectedType); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
614 |
fatalError = true; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
615 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
616 |
if (error) { |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
617 |
fatalError = true; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
618 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
619 |
|
15356
cf312dc54c60
8006119: update javac to follow latest spec for repeatable annotations
jjg
parents:
14961
diff
changeset
|
620 |
// The conditions for a valid containing annotation are made |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
621 |
// in Check.validateRepeatedAnnotaton(); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
622 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
623 |
return fatalError ? null : containerValueSymbol; |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
624 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
10950
diff
changeset
|
625 |
} |