author | mcimadamore |
Fri, 08 Aug 2008 17:43:24 +0100 | |
changeset 1045 | 56f6e84f7825 |
parent 1040 | c0f5acfd9d15 |
child 1257 | 873b053bf757 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
735 | 2 |
* Copyright 1999-2008 Sun Microsystems, Inc. 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 |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.tools.javac.comp; |
|
27 |
||
28 |
import java.util.*; |
|
29 |
import java.util.Set; |
|
30 |
import javax.lang.model.element.ElementKind; |
|
31 |
import javax.tools.JavaFileObject; |
|
32 |
||
33 |
import com.sun.tools.javac.code.*; |
|
34 |
import com.sun.tools.javac.jvm.*; |
|
35 |
import com.sun.tools.javac.tree.*; |
|
36 |
import com.sun.tools.javac.util.*; |
|
37 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
|
38 |
import com.sun.tools.javac.util.List; |
|
39 |
||
40 |
import com.sun.tools.javac.jvm.Target; |
|
41 |
import com.sun.tools.javac.code.Symbol.*; |
|
42 |
import com.sun.tools.javac.tree.JCTree.*; |
|
43 |
import com.sun.tools.javac.code.Type.*; |
|
44 |
||
45 |
import com.sun.source.tree.IdentifierTree; |
|
46 |
import com.sun.source.tree.MemberSelectTree; |
|
47 |
import com.sun.source.tree.TreeVisitor; |
|
48 |
import com.sun.source.util.SimpleTreeVisitor; |
|
49 |
||
50 |
import static com.sun.tools.javac.code.Flags.*; |
|
51 |
import static com.sun.tools.javac.code.Kinds.*; |
|
52 |
import static com.sun.tools.javac.code.TypeTags.*; |
|
53 |
||
54 |
/** This is the main context-dependent analysis phase in GJC. It |
|
55 |
* encompasses name resolution, type checking and constant folding as |
|
56 |
* subtasks. Some subtasks involve auxiliary classes. |
|
57 |
* @see Check |
|
58 |
* @see Resolve |
|
59 |
* @see ConstFold |
|
60 |
* @see Infer |
|
61 |
* |
|
62 |
* <p><b>This is NOT part of any API supported by Sun Microsystems. If |
|
63 |
* you write code that depends on this, you do so at your own risk. |
|
64 |
* This code and its internal interfaces are subject to change or |
|
65 |
* deletion without notice.</b> |
|
66 |
*/ |
|
67 |
public class Attr extends JCTree.Visitor { |
|
68 |
protected static final Context.Key<Attr> attrKey = |
|
69 |
new Context.Key<Attr>(); |
|
70 |
||
71 |
final Name.Table names; |
|
72 |
final Log log; |
|
73 |
final Symtab syms; |
|
74 |
final Resolve rs; |
|
75 |
final Check chk; |
|
76 |
final MemberEnter memberEnter; |
|
77 |
final TreeMaker make; |
|
78 |
final ConstFold cfolder; |
|
79 |
final Enter enter; |
|
80 |
final Target target; |
|
81 |
final Types types; |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
941
diff
changeset
|
82 |
final JCDiagnostic.Factory diags; |
10 | 83 |
final Annotate annotate; |
84 |
||
85 |
public static Attr instance(Context context) { |
|
86 |
Attr instance = context.get(attrKey); |
|
87 |
if (instance == null) |
|
88 |
instance = new Attr(context); |
|
89 |
return instance; |
|
90 |
} |
|
91 |
||
92 |
protected Attr(Context context) { |
|
93 |
context.put(attrKey, this); |
|
94 |
||
95 |
names = Name.Table.instance(context); |
|
96 |
log = Log.instance(context); |
|
97 |
syms = Symtab.instance(context); |
|
98 |
rs = Resolve.instance(context); |
|
99 |
chk = Check.instance(context); |
|
100 |
memberEnter = MemberEnter.instance(context); |
|
101 |
make = TreeMaker.instance(context); |
|
102 |
enter = Enter.instance(context); |
|
103 |
cfolder = ConstFold.instance(context); |
|
104 |
target = Target.instance(context); |
|
105 |
types = Types.instance(context); |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
941
diff
changeset
|
106 |
diags = JCDiagnostic.Factory.instance(context); |
10 | 107 |
annotate = Annotate.instance(context); |
108 |
||
109 |
Options options = Options.instance(context); |
|
110 |
||
111 |
Source source = Source.instance(context); |
|
112 |
allowGenerics = source.allowGenerics(); |
|
113 |
allowVarargs = source.allowVarargs(); |
|
114 |
allowEnums = source.allowEnums(); |
|
115 |
allowBoxing = source.allowBoxing(); |
|
116 |
allowCovariantReturns = source.allowCovariantReturns(); |
|
117 |
allowAnonOuterThis = source.allowAnonOuterThis(); |
|
118 |
relax = (options.get("-retrofit") != null || |
|
119 |
options.get("-relax") != null); |
|
120 |
useBeforeDeclarationWarning = options.get("useBeforeDeclarationWarning") != null; |
|
121 |
} |
|
122 |
||
123 |
/** Switch: relax some constraints for retrofit mode. |
|
124 |
*/ |
|
125 |
boolean relax; |
|
126 |
||
127 |
/** Switch: support generics? |
|
128 |
*/ |
|
129 |
boolean allowGenerics; |
|
130 |
||
131 |
/** Switch: allow variable-arity methods. |
|
132 |
*/ |
|
133 |
boolean allowVarargs; |
|
134 |
||
135 |
/** Switch: support enums? |
|
136 |
*/ |
|
137 |
boolean allowEnums; |
|
138 |
||
139 |
/** Switch: support boxing and unboxing? |
|
140 |
*/ |
|
141 |
boolean allowBoxing; |
|
142 |
||
143 |
/** Switch: support covariant result types? |
|
144 |
*/ |
|
145 |
boolean allowCovariantReturns; |
|
146 |
||
147 |
/** Switch: allow references to surrounding object from anonymous |
|
148 |
* objects during constructor call? |
|
149 |
*/ |
|
150 |
boolean allowAnonOuterThis; |
|
151 |
||
152 |
/** |
|
153 |
* Switch: warn about use of variable before declaration? |
|
154 |
* RFE: 6425594 |
|
155 |
*/ |
|
156 |
boolean useBeforeDeclarationWarning; |
|
157 |
||
158 |
/** Check kind and type of given tree against protokind and prototype. |
|
159 |
* If check succeeds, store type in tree and return it. |
|
160 |
* If check fails, store errType in tree and return it. |
|
161 |
* No checks are performed if the prototype is a method type. |
|
162 |
* Its not necessary in this case since we know that kind and type |
|
163 |
* are correct. |
|
164 |
* |
|
165 |
* @param tree The tree whose kind and type is checked |
|
166 |
* @param owntype The computed type of the tree |
|
167 |
* @param ownkind The computed kind of the tree |
|
168 |
* @param pkind The expected kind (or: protokind) of the tree |
|
169 |
* @param pt The expected type (or: prototype) of the tree |
|
170 |
*/ |
|
171 |
Type check(JCTree tree, Type owntype, int ownkind, int pkind, Type pt) { |
|
172 |
if (owntype.tag != ERROR && pt.tag != METHOD && pt.tag != FORALL) { |
|
173 |
if ((ownkind & ~pkind) == 0) { |
|
174 |
owntype = chk.checkType(tree.pos(), owntype, pt); |
|
175 |
} else { |
|
176 |
log.error(tree.pos(), "unexpected.type", |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
177 |
kindNames(pkind), |
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
735
diff
changeset
|
178 |
kindName(ownkind)); |
10 | 179 |
owntype = syms.errType; |
180 |
} |
|
181 |
} |
|
182 |
tree.type = owntype; |
|
183 |
return owntype; |
|
184 |
} |
|
185 |
||
186 |
/** Is given blank final variable assignable, i.e. in a scope where it |
|
187 |
* may be assigned to even though it is final? |
|
188 |
* @param v The blank final variable. |
|
189 |
* @param env The current environment. |
|
190 |
*/ |
|
191 |
boolean isAssignableAsBlankFinal(VarSymbol v, Env<AttrContext> env) { |
|
192 |
Symbol owner = env.info.scope.owner; |
|
193 |
// owner refers to the innermost variable, method or |
|
194 |
// initializer block declaration at this point. |
|
195 |
return |
|
196 |
v.owner == owner |
|
197 |
|| |
|
198 |
((owner.name == names.init || // i.e. we are in a constructor |
|
199 |
owner.kind == VAR || // i.e. we are in a variable initializer |
|
200 |
(owner.flags() & BLOCK) != 0) // i.e. we are in an initializer block |
|
201 |
&& |
|
202 |
v.owner == owner.owner |
|
203 |
&& |
|
204 |
((v.flags() & STATIC) != 0) == Resolve.isStatic(env)); |
|
205 |
} |
|
206 |
||
207 |
/** Check that variable can be assigned to. |
|
208 |
* @param pos The current source code position. |
|
209 |
* @param v The assigned varaible |
|
210 |
* @param base If the variable is referred to in a Select, the part |
|
211 |
* to the left of the `.', null otherwise. |
|
212 |
* @param env The current environment. |
|
213 |
*/ |
|
214 |
void checkAssignable(DiagnosticPosition pos, VarSymbol v, JCTree base, Env<AttrContext> env) { |
|
215 |
if ((v.flags() & FINAL) != 0 && |
|
216 |
((v.flags() & HASINIT) != 0 |
|
217 |
|| |
|
218 |
!((base == null || |
|
219 |
(base.getTag() == JCTree.IDENT && TreeInfo.name(base) == names._this)) && |
|
220 |
isAssignableAsBlankFinal(v, env)))) { |
|
221 |
log.error(pos, "cant.assign.val.to.final.var", v); |
|
222 |
} |
|
223 |
} |
|
224 |
||
225 |
/** Does tree represent a static reference to an identifier? |
|
226 |
* It is assumed that tree is either a SELECT or an IDENT. |
|
227 |
* We have to weed out selects from non-type names here. |
|
228 |
* @param tree The candidate tree. |
|
229 |
*/ |
|
230 |
boolean isStaticReference(JCTree tree) { |
|
231 |
if (tree.getTag() == JCTree.SELECT) { |
|
232 |
Symbol lsym = TreeInfo.symbol(((JCFieldAccess) tree).selected); |
|
233 |
if (lsym == null || lsym.kind != TYP) { |
|
234 |
return false; |
|
235 |
} |
|
236 |
} |
|
237 |
return true; |
|
238 |
} |
|
239 |
||
240 |
/** Is this symbol a type? |
|
241 |
*/ |
|
242 |
static boolean isType(Symbol sym) { |
|
243 |
return sym != null && sym.kind == TYP; |
|
244 |
} |
|
245 |
||
246 |
/** The current `this' symbol. |
|
247 |
* @param env The current environment. |
|
248 |
*/ |
|
249 |
Symbol thisSym(DiagnosticPosition pos, Env<AttrContext> env) { |
|
250 |
return rs.resolveSelf(pos, env, env.enclClass.sym, names._this); |
|
251 |
} |
|
252 |
||
253 |
/** Attribute a parsed identifier. |
|
254 |
* @param tree Parsed identifier name |
|
255 |
* @param topLevel The toplevel to use |
|
256 |
*/ |
|
257 |
public Symbol attribIdent(JCTree tree, JCCompilationUnit topLevel) { |
|
258 |
Env<AttrContext> localEnv = enter.topLevelEnv(topLevel); |
|
259 |
localEnv.enclClass = make.ClassDef(make.Modifiers(0), |
|
260 |
syms.errSymbol.name, |
|
261 |
null, null, null, null); |
|
262 |
localEnv.enclClass.sym = syms.errSymbol; |
|
263 |
return tree.accept(identAttributer, localEnv); |
|
264 |
} |
|
265 |
// where |
|
266 |
private TreeVisitor<Symbol,Env<AttrContext>> identAttributer = new IdentAttributer(); |
|
267 |
private class IdentAttributer extends SimpleTreeVisitor<Symbol,Env<AttrContext>> { |
|
268 |
@Override |
|
269 |
public Symbol visitMemberSelect(MemberSelectTree node, Env<AttrContext> env) { |
|
270 |
Symbol site = visit(node.getExpression(), env); |
|
271 |
if (site.kind == ERR) |
|
272 |
return site; |
|
273 |
Name name = (Name)node.getIdentifier(); |
|
274 |
if (site.kind == PCK) { |
|
275 |
env.toplevel.packge = (PackageSymbol)site; |
|
276 |
return rs.findIdentInPackage(env, (TypeSymbol)site, name, TYP | PCK); |
|
277 |
} else { |
|
278 |
env.enclClass.sym = (ClassSymbol)site; |
|
279 |
return rs.findMemberType(env, site.asType(), name, (TypeSymbol)site); |
|
280 |
} |
|
281 |
} |
|
282 |
||
283 |
@Override |
|
284 |
public Symbol visitIdentifier(IdentifierTree node, Env<AttrContext> env) { |
|
285 |
return rs.findIdent(env, (Name)node.getName(), TYP | PCK); |
|
286 |
} |
|
287 |
} |
|
288 |
||
289 |
public Type coerce(Type etype, Type ttype) { |
|
290 |
return cfolder.coerce(etype, ttype); |
|
291 |
} |
|
292 |
||
293 |
public Type attribType(JCTree node, TypeSymbol sym) { |
|
294 |
Env<AttrContext> env = enter.typeEnvs.get(sym); |
|
295 |
Env<AttrContext> localEnv = env.dup(node, env.info.dup()); |
|
296 |
return attribTree(node, localEnv, Kinds.TYP, Type.noType); |
|
297 |
} |
|
298 |
||
299 |
public Env<AttrContext> attribExprToTree(JCTree expr, Env<AttrContext> env, JCTree tree) { |
|
300 |
breakTree = tree; |
|
301 |
JavaFileObject prev = log.useSource(null); |
|
302 |
try { |
|
303 |
attribExpr(expr, env); |
|
304 |
} catch (BreakAttr b) { |
|
305 |
return b.env; |
|
306 |
} finally { |
|
307 |
breakTree = null; |
|
308 |
log.useSource(prev); |
|
309 |
} |
|
310 |
return env; |
|
311 |
} |
|
312 |
||
313 |
public Env<AttrContext> attribStatToTree(JCTree stmt, Env<AttrContext> env, JCTree tree) { |
|
314 |
breakTree = tree; |
|
315 |
JavaFileObject prev = log.useSource(null); |
|
316 |
try { |
|
317 |
attribStat(stmt, env); |
|
318 |
} catch (BreakAttr b) { |
|
319 |
return b.env; |
|
320 |
} finally { |
|
321 |
breakTree = null; |
|
322 |
log.useSource(prev); |
|
323 |
} |
|
324 |
return env; |
|
325 |
} |
|
326 |
||
327 |
private JCTree breakTree = null; |
|
328 |
||
329 |
private static class BreakAttr extends RuntimeException { |
|
330 |
static final long serialVersionUID = -6924771130405446405L; |
|
331 |
private Env<AttrContext> env; |
|
332 |
private BreakAttr(Env<AttrContext> env) { |
|
333 |
this.env = env; |
|
334 |
} |
|
335 |
} |
|
336 |
||
337 |
||
338 |
/* ************************************************************************ |
|
339 |
* Visitor methods |
|
340 |
*************************************************************************/ |
|
341 |
||
342 |
/** Visitor argument: the current environment. |
|
343 |
*/ |
|
344 |
Env<AttrContext> env; |
|
345 |
||
346 |
/** Visitor argument: the currently expected proto-kind. |
|
347 |
*/ |
|
348 |
int pkind; |
|
349 |
||
350 |
/** Visitor argument: the currently expected proto-type. |
|
351 |
*/ |
|
352 |
Type pt; |
|
353 |
||
354 |
/** Visitor result: the computed type. |
|
355 |
*/ |
|
356 |
Type result; |
|
357 |
||
358 |
/** Visitor method: attribute a tree, catching any completion failure |
|
359 |
* exceptions. Return the tree's type. |
|
360 |
* |
|
361 |
* @param tree The tree to be visited. |
|
362 |
* @param env The environment visitor argument. |
|
363 |
* @param pkind The protokind visitor argument. |
|
364 |
* @param pt The prototype visitor argument. |
|
365 |
*/ |
|
366 |
Type attribTree(JCTree tree, Env<AttrContext> env, int pkind, Type pt) { |
|
367 |
Env<AttrContext> prevEnv = this.env; |
|
368 |
int prevPkind = this.pkind; |
|
369 |
Type prevPt = this.pt; |
|
370 |
try { |
|
371 |
this.env = env; |
|
372 |
this.pkind = pkind; |
|
373 |
this.pt = pt; |
|
374 |
tree.accept(this); |
|
375 |
if (tree == breakTree) |
|
376 |
throw new BreakAttr(env); |
|
377 |
return result; |
|
378 |
} catch (CompletionFailure ex) { |
|
379 |
tree.type = syms.errType; |
|
380 |
return chk.completionError(tree.pos(), ex); |
|
381 |
} finally { |
|
382 |
this.env = prevEnv; |
|
383 |
this.pkind = prevPkind; |
|
384 |
this.pt = prevPt; |
|
385 |
} |
|
386 |
} |
|
387 |
||
388 |
/** Derived visitor method: attribute an expression tree. |
|
389 |
*/ |
|
390 |
public Type attribExpr(JCTree tree, Env<AttrContext> env, Type pt) { |
|
391 |
return attribTree(tree, env, VAL, pt.tag != ERROR ? pt : Type.noType); |
|
392 |
} |
|
393 |
||
394 |
/** Derived visitor method: attribute an expression tree with |
|
395 |
* no constraints on the computed type. |
|
396 |
*/ |
|
397 |
Type attribExpr(JCTree tree, Env<AttrContext> env) { |
|
398 |
return attribTree(tree, env, VAL, Type.noType); |
|
399 |
} |
|
400 |
||
401 |
/** Derived visitor method: attribute a type tree. |
|
402 |
*/ |
|
403 |
Type attribType(JCTree tree, Env<AttrContext> env) { |
|
404 |
Type result = attribTree(tree, env, TYP, Type.noType); |
|
405 |
return result; |
|
406 |
} |
|
407 |
||
408 |
/** Derived visitor method: attribute a statement or definition tree. |
|
409 |
*/ |
|
410 |
public Type attribStat(JCTree tree, Env<AttrContext> env) { |
|
411 |
return attribTree(tree, env, NIL, Type.noType); |
|
412 |
} |
|
413 |
||
414 |
/** Attribute a list of expressions, returning a list of types. |
|
415 |
*/ |
|
416 |
List<Type> attribExprs(List<JCExpression> trees, Env<AttrContext> env, Type pt) { |
|
417 |
ListBuffer<Type> ts = new ListBuffer<Type>(); |
|
418 |
for (List<JCExpression> l = trees; l.nonEmpty(); l = l.tail) |
|
419 |
ts.append(attribExpr(l.head, env, pt)); |
|
420 |
return ts.toList(); |
|
421 |
} |
|
422 |
||
423 |
/** Attribute a list of statements, returning nothing. |
|
424 |
*/ |
|
425 |
<T extends JCTree> void attribStats(List<T> trees, Env<AttrContext> env) { |
|
426 |
for (List<T> l = trees; l.nonEmpty(); l = l.tail) |
|
427 |
attribStat(l.head, env); |
|
428 |
} |
|
429 |
||
430 |
/** Attribute the arguments in a method call, returning a list of types. |
|
431 |
*/ |
|
432 |
List<Type> attribArgs(List<JCExpression> trees, Env<AttrContext> env) { |
|
433 |
ListBuffer<Type> argtypes = new ListBuffer<Type>(); |
|
434 |
for (List<JCExpression> l = trees; l.nonEmpty(); l = l.tail) |
|
435 |
argtypes.append(chk.checkNonVoid( |
|
436 |
l.head.pos(), types.upperBound(attribTree(l.head, env, VAL, Infer.anyPoly)))); |
|
437 |
return argtypes.toList(); |
|
438 |
} |
|
439 |
||
440 |
/** Attribute a type argument list, returning a list of types. |
|
441 |
*/ |
|
442 |
List<Type> attribTypes(List<JCExpression> trees, Env<AttrContext> env) { |
|
443 |
ListBuffer<Type> argtypes = new ListBuffer<Type>(); |
|
444 |
for (List<JCExpression> l = trees; l.nonEmpty(); l = l.tail) |
|
445 |
argtypes.append(chk.checkRefType(l.head.pos(), attribType(l.head, env))); |
|
446 |
return argtypes.toList(); |
|
447 |
} |
|
448 |
||
449 |
||
450 |
/** |
|
451 |
* Attribute type variables (of generic classes or methods). |
|
452 |
* Compound types are attributed later in attribBounds. |
|
453 |
* @param typarams the type variables to enter |
|
454 |
* @param env the current environment |
|
455 |
*/ |
|
456 |
void attribTypeVariables(List<JCTypeParameter> typarams, Env<AttrContext> env) { |
|
457 |
for (JCTypeParameter tvar : typarams) { |
|
458 |
TypeVar a = (TypeVar)tvar.type; |
|
661
9b2f1fe5c183
6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents:
512
diff
changeset
|
459 |
a.tsym.flags_field |= UNATTRIBUTED; |
9b2f1fe5c183
6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents:
512
diff
changeset
|
460 |
a.bound = Type.noType; |
10 | 461 |
if (!tvar.bounds.isEmpty()) { |
462 |
List<Type> bounds = List.of(attribType(tvar.bounds.head, env)); |
|
463 |
for (JCExpression bound : tvar.bounds.tail) |
|
464 |
bounds = bounds.prepend(attribType(bound, env)); |
|
465 |
types.setBounds(a, bounds.reverse()); |
|
466 |
} else { |
|
467 |
// if no bounds are given, assume a single bound of |
|
468 |
// java.lang.Object. |
|
469 |
types.setBounds(a, List.of(syms.objectType)); |
|
470 |
} |
|
661
9b2f1fe5c183
6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents:
512
diff
changeset
|
471 |
a.tsym.flags_field &= ~UNATTRIBUTED; |
10 | 472 |
} |
473 |
for (JCTypeParameter tvar : typarams) |
|
474 |
chk.checkNonCyclic(tvar.pos(), (TypeVar)tvar.type); |
|
475 |
attribStats(typarams, env); |
|
661
9b2f1fe5c183
6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents:
512
diff
changeset
|
476 |
} |
9b2f1fe5c183
6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents:
512
diff
changeset
|
477 |
|
9b2f1fe5c183
6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents:
512
diff
changeset
|
478 |
void attribBounds(List<JCTypeParameter> typarams) { |
10 | 479 |
for (JCTypeParameter typaram : typarams) { |
480 |
Type bound = typaram.type.getUpperBound(); |
|
481 |
if (bound != null && bound.tsym instanceof ClassSymbol) { |
|
482 |
ClassSymbol c = (ClassSymbol)bound.tsym; |
|
483 |
if ((c.flags_field & COMPOUND) != 0) { |
|
484 |
assert (c.flags_field & UNATTRIBUTED) != 0 : c; |
|
485 |
attribClass(typaram.pos(), c); |
|
486 |
} |
|
487 |
} |
|
488 |
} |
|
489 |
} |
|
490 |
||
491 |
/** |
|
492 |
* Attribute the type references in a list of annotations. |
|
493 |
*/ |
|
494 |
void attribAnnotationTypes(List<JCAnnotation> annotations, |
|
495 |
Env<AttrContext> env) { |
|
496 |
for (List<JCAnnotation> al = annotations; al.nonEmpty(); al = al.tail) { |
|
497 |
JCAnnotation a = al.head; |
|
498 |
attribType(a.annotationType, env); |
|
499 |
} |
|
500 |
} |
|
501 |
||
502 |
/** Attribute type reference in an `extends' or `implements' clause. |
|
503 |
* |
|
504 |
* @param tree The tree making up the type reference. |
|
505 |
* @param env The environment current at the reference. |
|
506 |
* @param classExpected true if only a class is expected here. |
|
507 |
* @param interfaceExpected true if only an interface is expected here. |
|
508 |
*/ |
|
509 |
Type attribBase(JCTree tree, |
|
510 |
Env<AttrContext> env, |
|
511 |
boolean classExpected, |
|
512 |
boolean interfaceExpected, |
|
513 |
boolean checkExtensible) { |
|
514 |
Type t = attribType(tree, env); |
|
515 |
return checkBase(t, tree, env, classExpected, interfaceExpected, checkExtensible); |
|
516 |
} |
|
517 |
Type checkBase(Type t, |
|
518 |
JCTree tree, |
|
519 |
Env<AttrContext> env, |
|
520 |
boolean classExpected, |
|
521 |
boolean interfaceExpected, |
|
522 |
boolean checkExtensible) { |
|
523 |
if (t.tag == TYPEVAR && !classExpected && !interfaceExpected) { |
|
524 |
// check that type variable is already visible |
|
525 |
if (t.getUpperBound() == null) { |
|
526 |
log.error(tree.pos(), "illegal.forward.ref"); |
|
527 |
return syms.errType; |
|
528 |
} |
|
529 |
} else { |
|
530 |
t = chk.checkClassType(tree.pos(), t, checkExtensible|!allowGenerics); |
|
531 |
} |
|
532 |
if (interfaceExpected && (t.tsym.flags() & INTERFACE) == 0) { |
|
533 |
log.error(tree.pos(), "intf.expected.here"); |
|
534 |
// return errType is necessary since otherwise there might |
|
535 |
// be undetected cycles which cause attribution to loop |
|
536 |
return syms.errType; |
|
537 |
} else if (checkExtensible && |
|
538 |
classExpected && |
|
539 |
(t.tsym.flags() & INTERFACE) != 0) { |
|
540 |
log.error(tree.pos(), "no.intf.expected.here"); |
|
541 |
return syms.errType; |
|
542 |
} |
|
543 |
if (checkExtensible && |
|
544 |
((t.tsym.flags() & FINAL) != 0)) { |
|
545 |
log.error(tree.pos(), |
|
546 |
"cant.inherit.from.final", t.tsym); |
|
547 |
} |
|
548 |
chk.checkNonCyclic(tree.pos(), t); |
|
549 |
return t; |
|
550 |
} |
|
551 |
||
552 |
public void visitClassDef(JCClassDecl tree) { |
|
553 |
// Local classes have not been entered yet, so we need to do it now: |
|
554 |
if ((env.info.scope.owner.kind & (VAR | MTH)) != 0) |
|
555 |
enter.classEnter(tree, env); |
|
556 |
||
557 |
ClassSymbol c = tree.sym; |
|
558 |
if (c == null) { |
|
559 |
// exit in case something drastic went wrong during enter. |
|
560 |
result = null; |
|
561 |
} else { |
|
562 |
// make sure class has been completed: |
|
563 |
c.complete(); |
|
564 |
||
565 |
// If this class appears as an anonymous class |
|
566 |
// in a superclass constructor call where |
|
567 |
// no explicit outer instance is given, |
|
568 |
// disable implicit outer instance from being passed. |
|
569 |
// (This would be an illegal access to "this before super"). |
|
570 |
if (env.info.isSelfCall && |
|
571 |
env.tree.getTag() == JCTree.NEWCLASS && |
|
572 |
((JCNewClass) env.tree).encl == null) |
|
573 |
{ |
|
574 |
c.flags_field |= NOOUTERTHIS; |
|
575 |
} |
|
576 |
attribClass(tree.pos(), c); |
|
577 |
result = tree.type = c.type; |
|
578 |
} |
|
579 |
} |
|
580 |
||
581 |
public void visitMethodDef(JCMethodDecl tree) { |
|
582 |
MethodSymbol m = tree.sym; |
|
583 |
||
584 |
Lint lint = env.info.lint.augment(m.attributes_field, m.flags()); |
|
585 |
Lint prevLint = chk.setLint(lint); |
|
586 |
try { |
|
587 |
chk.checkDeprecatedAnnotation(tree.pos(), m); |
|
588 |
||
661
9b2f1fe5c183
6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents:
512
diff
changeset
|
589 |
attribBounds(tree.typarams); |
10 | 590 |
|
591 |
// If we override any other methods, check that we do so properly. |
|
592 |
// JLS ??? |
|
593 |
chk.checkOverride(tree, m); |
|
594 |
||
595 |
// Create a new environment with local scope |
|
596 |
// for attributing the method. |
|
597 |
Env<AttrContext> localEnv = memberEnter.methodEnv(tree, env); |
|
598 |
||
599 |
localEnv.info.lint = lint; |
|
600 |
||
601 |
// Enter all type parameters into the local method scope. |
|
602 |
for (List<JCTypeParameter> l = tree.typarams; l.nonEmpty(); l = l.tail) |
|
603 |
localEnv.info.scope.enterIfAbsent(l.head.type.tsym); |
|
604 |
||
605 |
ClassSymbol owner = env.enclClass.sym; |
|
606 |
if ((owner.flags() & ANNOTATION) != 0 && |
|
607 |
tree.params.nonEmpty()) |
|
608 |
log.error(tree.params.head.pos(), |
|
609 |
"intf.annotation.members.cant.have.params"); |
|
610 |
||
611 |
// Attribute all value parameters. |
|
612 |
for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) { |
|
613 |
attribStat(l.head, localEnv); |
|
614 |
} |
|
615 |
||
616 |
// Check that type parameters are well-formed. |
|
617 |
chk.validateTypeParams(tree.typarams); |
|
618 |
if ((owner.flags() & ANNOTATION) != 0 && |
|
619 |
tree.typarams.nonEmpty()) |
|
620 |
log.error(tree.typarams.head.pos(), |
|
621 |
"intf.annotation.members.cant.have.type.params"); |
|
622 |
||
623 |
// Check that result type is well-formed. |
|
624 |
chk.validate(tree.restype); |
|
625 |
if ((owner.flags() & ANNOTATION) != 0) |
|
626 |
chk.validateAnnotationType(tree.restype); |
|
627 |
||
628 |
if ((owner.flags() & ANNOTATION) != 0) |
|
629 |
chk.validateAnnotationMethod(tree.pos(), m); |
|
630 |
||
631 |
// Check that all exceptions mentioned in the throws clause extend |
|
632 |
// java.lang.Throwable. |
|
633 |
if ((owner.flags() & ANNOTATION) != 0 && tree.thrown.nonEmpty()) |
|
634 |
log.error(tree.thrown.head.pos(), |
|
635 |
"throws.not.allowed.in.intf.annotation"); |
|
636 |
for (List<JCExpression> l = tree.thrown; l.nonEmpty(); l = l.tail) |
|
637 |
chk.checkType(l.head.pos(), l.head.type, syms.throwableType); |
|
638 |
||
639 |
if (tree.body == null) { |
|
640 |
// Empty bodies are only allowed for |
|
641 |
// abstract, native, or interface methods, or for methods |
|
642 |
// in a retrofit signature class. |
|
643 |
if ((owner.flags() & INTERFACE) == 0 && |
|
644 |
(tree.mods.flags & (ABSTRACT | NATIVE)) == 0 && |
|
645 |
!relax) |
|
646 |
log.error(tree.pos(), "missing.meth.body.or.decl.abstract"); |
|
647 |
if (tree.defaultValue != null) { |
|
648 |
if ((owner.flags() & ANNOTATION) == 0) |
|
649 |
log.error(tree.pos(), |
|
650 |
"default.allowed.in.intf.annotation.member"); |
|
651 |
} |
|
652 |
} else if ((owner.flags() & INTERFACE) != 0) { |
|
653 |
log.error(tree.body.pos(), "intf.meth.cant.have.body"); |
|
654 |
} else if ((tree.mods.flags & ABSTRACT) != 0) { |
|
655 |
log.error(tree.pos(), "abstract.meth.cant.have.body"); |
|
656 |
} else if ((tree.mods.flags & NATIVE) != 0) { |
|
657 |
log.error(tree.pos(), "native.meth.cant.have.body"); |
|
658 |
} else { |
|
659 |
// Add an implicit super() call unless an explicit call to |
|
660 |
// super(...) or this(...) is given |
|
661 |
// or we are compiling class java.lang.Object. |
|
662 |
if (tree.name == names.init && owner.type != syms.objectType) { |
|
663 |
JCBlock body = tree.body; |
|
664 |
if (body.stats.isEmpty() || |
|
665 |
!TreeInfo.isSelfCall(body.stats.head)) { |
|
666 |
body.stats = body.stats. |
|
667 |
prepend(memberEnter.SuperCall(make.at(body.pos), |
|
668 |
List.<Type>nil(), |
|
669 |
List.<JCVariableDecl>nil(), |
|
670 |
false)); |
|
671 |
} else if ((env.enclClass.sym.flags() & ENUM) != 0 && |
|
672 |
(tree.mods.flags & GENERATEDCONSTR) == 0 && |
|
673 |
TreeInfo.isSuperCall(body.stats.head)) { |
|
674 |
// enum constructors are not allowed to call super |
|
675 |
// directly, so make sure there aren't any super calls |
|
676 |
// in enum constructors, except in the compiler |
|
677 |
// generated one. |
|
678 |
log.error(tree.body.stats.head.pos(), |
|
679 |
"call.to.super.not.allowed.in.enum.ctor", |
|
680 |
env.enclClass.sym); |
|
681 |
} |
|
682 |
} |
|
683 |
||
684 |
// Attribute method body. |
|
685 |
attribStat(tree.body, localEnv); |
|
686 |
} |
|
687 |
localEnv.info.scope.leave(); |
|
688 |
result = tree.type = m.type; |
|
689 |
chk.validateAnnotations(tree.mods.annotations, m); |
|
690 |
||
691 |
} |
|
692 |
finally { |
|
693 |
chk.setLint(prevLint); |
|
694 |
} |
|
695 |
} |
|
696 |
||
697 |
public void visitVarDef(JCVariableDecl tree) { |
|
698 |
// Local variables have not been entered yet, so we need to do it now: |
|
699 |
if (env.info.scope.owner.kind == MTH) { |
|
700 |
if (tree.sym != null) { |
|
701 |
// parameters have already been entered |
|
702 |
env.info.scope.enter(tree.sym); |
|
703 |
} else { |
|
704 |
memberEnter.memberEnter(tree, env); |
|
705 |
annotate.flush(); |
|
706 |
} |
|
707 |
} |
|
708 |
||
709 |
// Check that the variable's declared type is well-formed. |
|
710 |
chk.validate(tree.vartype); |
|
711 |
||
712 |
VarSymbol v = tree.sym; |
|
713 |
Lint lint = env.info.lint.augment(v.attributes_field, v.flags()); |
|
714 |
Lint prevLint = chk.setLint(lint); |
|
715 |
||
716 |
try { |
|
717 |
chk.checkDeprecatedAnnotation(tree.pos(), v); |
|
718 |
||
719 |
if (tree.init != null) { |
|
720 |
if ((v.flags_field & FINAL) != 0 && tree.init.getTag() != JCTree.NEWCLASS) { |
|
721 |
// In this case, `v' is final. Ensure that it's initializer is |
|
722 |
// evaluated. |
|
723 |
v.getConstValue(); // ensure initializer is evaluated |
|
724 |
} else { |
|
725 |
// Attribute initializer in a new environment |
|
726 |
// with the declared variable as owner. |
|
727 |
// Check that initializer conforms to variable's declared type. |
|
728 |
Env<AttrContext> initEnv = memberEnter.initEnv(tree, env); |
|
729 |
initEnv.info.lint = lint; |
|
730 |
// In order to catch self-references, we set the variable's |
|
731 |
// declaration position to maximal possible value, effectively |
|
732 |
// marking the variable as undefined. |
|
1045
56f6e84f7825
6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents:
1040
diff
changeset
|
733 |
initEnv.info.enclVar = v; |
10 | 734 |
attribExpr(tree.init, initEnv, v.type); |
735 |
} |
|
736 |
} |
|
737 |
result = tree.type = v.type; |
|
738 |
chk.validateAnnotations(tree.mods.annotations, v); |
|
739 |
} |
|
740 |
finally { |
|
741 |
chk.setLint(prevLint); |
|
742 |
} |
|
743 |
} |
|
744 |
||
745 |
public void visitSkip(JCSkip tree) { |
|
746 |
result = null; |
|
747 |
} |
|
748 |
||
749 |
public void visitBlock(JCBlock tree) { |
|
750 |
if (env.info.scope.owner.kind == TYP) { |
|
751 |
// Block is a static or instance initializer; |
|
752 |
// let the owner of the environment be a freshly |
|
753 |
// created BLOCK-method. |
|
754 |
Env<AttrContext> localEnv = |
|
755 |
env.dup(tree, env.info.dup(env.info.scope.dupUnshared())); |
|
756 |
localEnv.info.scope.owner = |
|
757 |
new MethodSymbol(tree.flags | BLOCK, names.empty, null, |
|
758 |
env.info.scope.owner); |
|
759 |
if ((tree.flags & STATIC) != 0) localEnv.info.staticLevel++; |
|
760 |
attribStats(tree.stats, localEnv); |
|
761 |
} else { |
|
762 |
// Create a new local environment with a local scope. |
|
763 |
Env<AttrContext> localEnv = |
|
764 |
env.dup(tree, env.info.dup(env.info.scope.dup())); |
|
765 |
attribStats(tree.stats, localEnv); |
|
766 |
localEnv.info.scope.leave(); |
|
767 |
} |
|
768 |
result = null; |
|
769 |
} |
|
770 |
||
771 |
public void visitDoLoop(JCDoWhileLoop tree) { |
|
772 |
attribStat(tree.body, env.dup(tree)); |
|
773 |
attribExpr(tree.cond, env, syms.booleanType); |
|
774 |
result = null; |
|
775 |
} |
|
776 |
||
777 |
public void visitWhileLoop(JCWhileLoop tree) { |
|
778 |
attribExpr(tree.cond, env, syms.booleanType); |
|
779 |
attribStat(tree.body, env.dup(tree)); |
|
780 |
result = null; |
|
781 |
} |
|
782 |
||
783 |
public void visitForLoop(JCForLoop tree) { |
|
784 |
Env<AttrContext> loopEnv = |
|
785 |
env.dup(env.tree, env.info.dup(env.info.scope.dup())); |
|
786 |
attribStats(tree.init, loopEnv); |
|
787 |
if (tree.cond != null) attribExpr(tree.cond, loopEnv, syms.booleanType); |
|
788 |
loopEnv.tree = tree; // before, we were not in loop! |
|
789 |
attribStats(tree.step, loopEnv); |
|
790 |
attribStat(tree.body, loopEnv); |
|
791 |
loopEnv.info.scope.leave(); |
|
792 |
result = null; |
|
793 |
} |
|
794 |
||
795 |
public void visitForeachLoop(JCEnhancedForLoop tree) { |
|
796 |
Env<AttrContext> loopEnv = |
|
797 |
env.dup(env.tree, env.info.dup(env.info.scope.dup())); |
|
798 |
attribStat(tree.var, loopEnv); |
|
799 |
Type exprType = types.upperBound(attribExpr(tree.expr, loopEnv)); |
|
800 |
chk.checkNonVoid(tree.pos(), exprType); |
|
801 |
Type elemtype = types.elemtype(exprType); // perhaps expr is an array? |
|
802 |
if (elemtype == null) { |
|
803 |
// or perhaps expr implements Iterable<T>? |
|
804 |
Type base = types.asSuper(exprType, syms.iterableType.tsym); |
|
805 |
if (base == null) { |
|
806 |
log.error(tree.expr.pos(), "foreach.not.applicable.to.type"); |
|
807 |
elemtype = syms.errType; |
|
808 |
} else { |
|
809 |
List<Type> iterableParams = base.allparams(); |
|
810 |
elemtype = iterableParams.isEmpty() |
|
811 |
? syms.objectType |
|
812 |
: types.upperBound(iterableParams.head); |
|
813 |
} |
|
814 |
} |
|
815 |
chk.checkType(tree.expr.pos(), elemtype, tree.var.sym.type); |
|
816 |
loopEnv.tree = tree; // before, we were not in loop! |
|
817 |
attribStat(tree.body, loopEnv); |
|
818 |
loopEnv.info.scope.leave(); |
|
819 |
result = null; |
|
820 |
} |
|
821 |
||
822 |
public void visitLabelled(JCLabeledStatement tree) { |
|
823 |
// Check that label is not used in an enclosing statement |
|
824 |
Env<AttrContext> env1 = env; |
|
825 |
while (env1 != null && env1.tree.getTag() != JCTree.CLASSDEF) { |
|
826 |
if (env1.tree.getTag() == JCTree.LABELLED && |
|
827 |
((JCLabeledStatement) env1.tree).label == tree.label) { |
|
828 |
log.error(tree.pos(), "label.already.in.use", |
|
829 |
tree.label); |
|
830 |
break; |
|
831 |
} |
|
832 |
env1 = env1.next; |
|
833 |
} |
|
834 |
||
835 |
attribStat(tree.body, env.dup(tree)); |
|
836 |
result = null; |
|
837 |
} |
|
838 |
||
839 |
public void visitSwitch(JCSwitch tree) { |
|
840 |
Type seltype = attribExpr(tree.selector, env); |
|
841 |
||
842 |
Env<AttrContext> switchEnv = |
|
843 |
env.dup(tree, env.info.dup(env.info.scope.dup())); |
|
844 |
||
845 |
boolean enumSwitch = |
|
846 |
allowEnums && |
|
847 |
(seltype.tsym.flags() & Flags.ENUM) != 0; |
|
848 |
if (!enumSwitch) |
|
849 |
seltype = chk.checkType(tree.selector.pos(), seltype, syms.intType); |
|
850 |
||
851 |
// Attribute all cases and |
|
852 |
// check that there are no duplicate case labels or default clauses. |
|
853 |
Set<Object> labels = new HashSet<Object>(); // The set of case labels. |
|
854 |
boolean hasDefault = false; // Is there a default label? |
|
855 |
for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) { |
|
856 |
JCCase c = l.head; |
|
857 |
Env<AttrContext> caseEnv = |
|
858 |
switchEnv.dup(c, env.info.dup(switchEnv.info.scope.dup())); |
|
859 |
if (c.pat != null) { |
|
860 |
if (enumSwitch) { |
|
861 |
Symbol sym = enumConstant(c.pat, seltype); |
|
862 |
if (sym == null) { |
|
863 |
log.error(c.pat.pos(), "enum.const.req"); |
|
864 |
} else if (!labels.add(sym)) { |
|
865 |
log.error(c.pos(), "duplicate.case.label"); |
|
866 |
} |
|
867 |
} else { |
|
868 |
Type pattype = attribExpr(c.pat, switchEnv, seltype); |
|
869 |
if (pattype.tag != ERROR) { |
|
870 |
if (pattype.constValue() == null) { |
|
871 |
log.error(c.pat.pos(), "const.expr.req"); |
|
872 |
} else if (labels.contains(pattype.constValue())) { |
|
873 |
log.error(c.pos(), "duplicate.case.label"); |
|
874 |
} else { |
|
875 |
labels.add(pattype.constValue()); |
|
876 |
} |
|
877 |
} |
|
878 |
} |
|
879 |
} else if (hasDefault) { |
|
880 |
log.error(c.pos(), "duplicate.default.label"); |
|
881 |
} else { |
|
882 |
hasDefault = true; |
|
883 |
} |
|
884 |
attribStats(c.stats, caseEnv); |
|
885 |
caseEnv.info.scope.leave(); |
|
886 |
addVars(c.stats, switchEnv.info.scope); |
|
887 |
} |
|
888 |
||
889 |
switchEnv.info.scope.leave(); |
|
890 |
result = null; |
|
891 |
} |
|
892 |
// where |
|
893 |
/** Add any variables defined in stats to the switch scope. */ |
|
894 |
private static void addVars(List<JCStatement> stats, Scope switchScope) { |
|
895 |
for (;stats.nonEmpty(); stats = stats.tail) { |
|
896 |
JCTree stat = stats.head; |
|
897 |
if (stat.getTag() == JCTree.VARDEF) |
|
898 |
switchScope.enter(((JCVariableDecl) stat).sym); |
|
899 |
} |
|
900 |
} |
|
901 |
// where |
|
902 |
/** Return the selected enumeration constant symbol, or null. */ |
|
903 |
private Symbol enumConstant(JCTree tree, Type enumType) { |
|
904 |
if (tree.getTag() != JCTree.IDENT) { |
|
905 |
log.error(tree.pos(), "enum.label.must.be.unqualified.enum"); |
|
906 |
return syms.errSymbol; |
|
907 |
} |
|
908 |
JCIdent ident = (JCIdent)tree; |
|
909 |
Name name = ident.name; |
|
910 |
for (Scope.Entry e = enumType.tsym.members().lookup(name); |
|
911 |
e.scope != null; e = e.next()) { |
|
912 |
if (e.sym.kind == VAR) { |
|
913 |
Symbol s = ident.sym = e.sym; |
|
914 |
((VarSymbol)s).getConstValue(); // ensure initializer is evaluated |
|
915 |
ident.type = s.type; |
|
916 |
return ((s.flags_field & Flags.ENUM) == 0) |
|
917 |
? null : s; |
|
918 |
} |
|
919 |
} |
|
920 |
return null; |
|
921 |
} |
|
922 |
||
923 |
public void visitSynchronized(JCSynchronized tree) { |
|
924 |
chk.checkRefType(tree.pos(), attribExpr(tree.lock, env)); |
|
925 |
attribStat(tree.body, env); |
|
926 |
result = null; |
|
927 |
} |
|
928 |
||
929 |
public void visitTry(JCTry tree) { |
|
930 |
// Attribute body |
|
931 |
attribStat(tree.body, env.dup(tree, env.info.dup())); |
|
932 |
||
933 |
// Attribute catch clauses |
|
934 |
for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) { |
|
935 |
JCCatch c = l.head; |
|
936 |
Env<AttrContext> catchEnv = |
|
937 |
env.dup(c, env.info.dup(env.info.scope.dup())); |
|
938 |
Type ctype = attribStat(c.param, catchEnv); |
|
939 |
if (c.param.type.tsym.kind == Kinds.VAR) { |
|
940 |
c.param.sym.setData(ElementKind.EXCEPTION_PARAMETER); |
|
941 |
} |
|
942 |
chk.checkType(c.param.vartype.pos(), |
|
943 |
chk.checkClassType(c.param.vartype.pos(), ctype), |
|
944 |
syms.throwableType); |
|
945 |
attribStat(c.body, catchEnv); |
|
946 |
catchEnv.info.scope.leave(); |
|
947 |
} |
|
948 |
||
949 |
// Attribute finalizer |
|
950 |
if (tree.finalizer != null) attribStat(tree.finalizer, env); |
|
951 |
result = null; |
|
952 |
} |
|
953 |
||
954 |
public void visitConditional(JCConditional tree) { |
|
955 |
attribExpr(tree.cond, env, syms.booleanType); |
|
956 |
attribExpr(tree.truepart, env); |
|
957 |
attribExpr(tree.falsepart, env); |
|
958 |
result = check(tree, |
|
959 |
capture(condType(tree.pos(), tree.cond.type, |
|
960 |
tree.truepart.type, tree.falsepart.type)), |
|
961 |
VAL, pkind, pt); |
|
962 |
} |
|
963 |
//where |
|
964 |
/** Compute the type of a conditional expression, after |
|
965 |
* checking that it exists. See Spec 15.25. |
|
966 |
* |
|
967 |
* @param pos The source position to be used for |
|
968 |
* error diagnostics. |
|
969 |
* @param condtype The type of the expression's condition. |
|
970 |
* @param thentype The type of the expression's then-part. |
|
971 |
* @param elsetype The type of the expression's else-part. |
|
972 |
*/ |
|
973 |
private Type condType(DiagnosticPosition pos, |
|
974 |
Type condtype, |
|
975 |
Type thentype, |
|
976 |
Type elsetype) { |
|
977 |
Type ctype = condType1(pos, condtype, thentype, elsetype); |
|
978 |
||
979 |
// If condition and both arms are numeric constants, |
|
980 |
// evaluate at compile-time. |
|
981 |
return ((condtype.constValue() != null) && |
|
982 |
(thentype.constValue() != null) && |
|
983 |
(elsetype.constValue() != null)) |
|
984 |
? cfolder.coerce(condtype.isTrue()?thentype:elsetype, ctype) |
|
985 |
: ctype; |
|
986 |
} |
|
987 |
/** Compute the type of a conditional expression, after |
|
988 |
* checking that it exists. Does not take into |
|
989 |
* account the special case where condition and both arms |
|
990 |
* are constants. |
|
991 |
* |
|
992 |
* @param pos The source position to be used for error |
|
993 |
* diagnostics. |
|
994 |
* @param condtype The type of the expression's condition. |
|
995 |
* @param thentype The type of the expression's then-part. |
|
996 |
* @param elsetype The type of the expression's else-part. |
|
997 |
*/ |
|
998 |
private Type condType1(DiagnosticPosition pos, Type condtype, |
|
999 |
Type thentype, Type elsetype) { |
|
1000 |
// If same type, that is the result |
|
1001 |
if (types.isSameType(thentype, elsetype)) |
|
1002 |
return thentype.baseType(); |
|
1003 |
||
1004 |
Type thenUnboxed = (!allowBoxing || thentype.isPrimitive()) |
|
1005 |
? thentype : types.unboxedType(thentype); |
|
1006 |
Type elseUnboxed = (!allowBoxing || elsetype.isPrimitive()) |
|
1007 |
? elsetype : types.unboxedType(elsetype); |
|
1008 |
||
1009 |
// Otherwise, if both arms can be converted to a numeric |
|
1010 |
// type, return the least numeric type that fits both arms |
|
1011 |
// (i.e. return larger of the two, or return int if one |
|
1012 |
// arm is short, the other is char). |
|
1013 |
if (thenUnboxed.isPrimitive() && elseUnboxed.isPrimitive()) { |
|
1014 |
// If one arm has an integer subrange type (i.e., byte, |
|
1015 |
// short, or char), and the other is an integer constant |
|
1016 |
// that fits into the subrange, return the subrange type. |
|
1017 |
if (thenUnboxed.tag < INT && elseUnboxed.tag == INT && |
|
1018 |
types.isAssignable(elseUnboxed, thenUnboxed)) |
|
1019 |
return thenUnboxed.baseType(); |
|
1020 |
if (elseUnboxed.tag < INT && thenUnboxed.tag == INT && |
|
1021 |
types.isAssignable(thenUnboxed, elseUnboxed)) |
|
1022 |
return elseUnboxed.baseType(); |
|
1023 |
||
1024 |
for (int i = BYTE; i < VOID; i++) { |
|
1025 |
Type candidate = syms.typeOfTag[i]; |
|
1026 |
if (types.isSubtype(thenUnboxed, candidate) && |
|
1027 |
types.isSubtype(elseUnboxed, candidate)) |
|
1028 |
return candidate; |
|
1029 |
} |
|
1030 |
} |
|
1031 |
||
1032 |
// Those were all the cases that could result in a primitive |
|
1033 |
if (allowBoxing) { |
|
1034 |
if (thentype.isPrimitive()) |
|
1035 |
thentype = types.boxedClass(thentype).type; |
|
1036 |
if (elsetype.isPrimitive()) |
|
1037 |
elsetype = types.boxedClass(elsetype).type; |
|
1038 |
} |
|
1039 |
||
1040 |
if (types.isSubtype(thentype, elsetype)) |
|
1041 |
return elsetype.baseType(); |
|
1042 |
if (types.isSubtype(elsetype, thentype)) |
|
1043 |
return thentype.baseType(); |
|
1044 |
||
1045 |
if (!allowBoxing || thentype.tag == VOID || elsetype.tag == VOID) { |
|
1046 |
log.error(pos, "neither.conditional.subtype", |
|
1047 |
thentype, elsetype); |
|
1048 |
return thentype.baseType(); |
|
1049 |
} |
|
1050 |
||
1051 |
// both are known to be reference types. The result is |
|
1052 |
// lub(thentype,elsetype). This cannot fail, as it will |
|
1053 |
// always be possible to infer "Object" if nothing better. |
|
1054 |
return types.lub(thentype.baseType(), elsetype.baseType()); |
|
1055 |
} |
|
1056 |
||
1057 |
public void visitIf(JCIf tree) { |
|
1058 |
attribExpr(tree.cond, env, syms.booleanType); |
|
1059 |
attribStat(tree.thenpart, env); |
|
1060 |
if (tree.elsepart != null) |
|
1061 |
attribStat(tree.elsepart, env); |
|
1062 |
chk.checkEmptyIf(tree); |
|
1063 |
result = null; |
|
1064 |
} |
|
1065 |
||
1066 |
public void visitExec(JCExpressionStatement tree) { |
|
1067 |
attribExpr(tree.expr, env); |
|
1068 |
result = null; |
|
1069 |
} |
|
1070 |
||
1071 |
public void visitBreak(JCBreak tree) { |
|
1072 |
tree.target = findJumpTarget(tree.pos(), tree.getTag(), tree.label, env); |
|
1073 |
result = null; |
|
1074 |
} |
|
1075 |
||
1076 |
public void visitContinue(JCContinue tree) { |
|
1077 |
tree.target = findJumpTarget(tree.pos(), tree.getTag(), tree.label, env); |
|
1078 |
result = null; |
|
1079 |
} |
|
1080 |
//where |
|
1081 |
/** Return the target of a break or continue statement, if it exists, |
|
1082 |
* report an error if not. |
|
1083 |
* Note: The target of a labelled break or continue is the |
|
1084 |
* (non-labelled) statement tree referred to by the label, |
|
1085 |
* not the tree representing the labelled statement itself. |
|
1086 |
* |
|
1087 |
* @param pos The position to be used for error diagnostics |
|
1088 |
* @param tag The tag of the jump statement. This is either |
|
1089 |
* Tree.BREAK or Tree.CONTINUE. |
|
1090 |
* @param label The label of the jump statement, or null if no |
|
1091 |
* label is given. |
|
1092 |
* @param env The environment current at the jump statement. |
|
1093 |
*/ |
|
1094 |
private JCTree findJumpTarget(DiagnosticPosition pos, |
|
1095 |
int tag, |
|
1096 |
Name label, |
|
1097 |
Env<AttrContext> env) { |
|
1098 |
// Search environments outwards from the point of jump. |
|
1099 |
Env<AttrContext> env1 = env; |
|
1100 |
LOOP: |
|
1101 |
while (env1 != null) { |
|
1102 |
switch (env1.tree.getTag()) { |
|
1103 |
case JCTree.LABELLED: |
|
1104 |
JCLabeledStatement labelled = (JCLabeledStatement)env1.tree; |
|
1105 |
if (label == labelled.label) { |
|
1106 |
// If jump is a continue, check that target is a loop. |
|
1107 |
if (tag == JCTree.CONTINUE) { |
|
1108 |
if (labelled.body.getTag() != JCTree.DOLOOP && |
|
1109 |
labelled.body.getTag() != JCTree.WHILELOOP && |
|
1110 |
labelled.body.getTag() != JCTree.FORLOOP && |
|
1111 |
labelled.body.getTag() != JCTree.FOREACHLOOP) |
|
1112 |
log.error(pos, "not.loop.label", label); |
|
1113 |
// Found labelled statement target, now go inwards |
|
1114 |
// to next non-labelled tree. |
|
1115 |
return TreeInfo.referencedStatement(labelled); |
|
1116 |
} else { |
|
1117 |
return labelled; |
|
1118 |
} |
|
1119 |
} |
|
1120 |
break; |
|
1121 |
case JCTree.DOLOOP: |
|
1122 |
case JCTree.WHILELOOP: |
|
1123 |
case JCTree.FORLOOP: |
|
1124 |
case JCTree.FOREACHLOOP: |
|
1125 |
if (label == null) return env1.tree; |
|
1126 |
break; |
|
1127 |
case JCTree.SWITCH: |
|
1128 |
if (label == null && tag == JCTree.BREAK) return env1.tree; |
|
1129 |
break; |
|
1130 |
case JCTree.METHODDEF: |
|
1131 |
case JCTree.CLASSDEF: |
|
1132 |
break LOOP; |
|
1133 |
default: |
|
1134 |
} |
|
1135 |
env1 = env1.next; |
|
1136 |
} |
|
1137 |
if (label != null) |
|
1138 |
log.error(pos, "undef.label", label); |
|
1139 |
else if (tag == JCTree.CONTINUE) |
|
1140 |
log.error(pos, "cont.outside.loop"); |
|
1141 |
else |
|
1142 |
log.error(pos, "break.outside.switch.loop"); |
|
1143 |
return null; |
|
1144 |
} |
|
1145 |
||
1146 |
public void visitReturn(JCReturn tree) { |
|
1147 |
// Check that there is an enclosing method which is |
|
1148 |
// nested within than the enclosing class. |
|
1149 |
if (env.enclMethod == null || |
|
1150 |
env.enclMethod.sym.owner != env.enclClass.sym) { |
|
1151 |
log.error(tree.pos(), "ret.outside.meth"); |
|
1152 |
||
1153 |
} else { |
|
1154 |
// Attribute return expression, if it exists, and check that |
|
1155 |
// it conforms to result type of enclosing method. |
|
1156 |
Symbol m = env.enclMethod.sym; |
|
1157 |
if (m.type.getReturnType().tag == VOID) { |
|
1158 |
if (tree.expr != null) |
|
1159 |
log.error(tree.expr.pos(), |
|
1160 |
"cant.ret.val.from.meth.decl.void"); |
|
1161 |
} else if (tree.expr == null) { |
|
1162 |
log.error(tree.pos(), "missing.ret.val"); |
|
1163 |
} else { |
|
1164 |
attribExpr(tree.expr, env, m.type.getReturnType()); |
|
1165 |
} |
|
1166 |
} |
|
1167 |
result = null; |
|
1168 |
} |
|
1169 |
||
1170 |
public void visitThrow(JCThrow tree) { |
|
1171 |
attribExpr(tree.expr, env, syms.throwableType); |
|
1172 |
result = null; |
|
1173 |
} |
|
1174 |
||
1175 |
public void visitAssert(JCAssert tree) { |
|
1176 |
attribExpr(tree.cond, env, syms.booleanType); |
|
1177 |
if (tree.detail != null) { |
|
1178 |
chk.checkNonVoid(tree.detail.pos(), attribExpr(tree.detail, env)); |
|
1179 |
} |
|
1180 |
result = null; |
|
1181 |
} |
|
1182 |
||
1183 |
/** Visitor method for method invocations. |
|
1184 |
* NOTE: The method part of an application will have in its type field |
|
1185 |
* the return type of the method, not the method's type itself! |
|
1186 |
*/ |
|
1187 |
public void visitApply(JCMethodInvocation tree) { |
|
1188 |
// The local environment of a method application is |
|
1189 |
// a new environment nested in the current one. |
|
1190 |
Env<AttrContext> localEnv = env.dup(tree, env.info.dup()); |
|
1191 |
||
1192 |
// The types of the actual method arguments. |
|
1193 |
List<Type> argtypes; |
|
1194 |
||
1195 |
// The types of the actual method type arguments. |
|
1196 |
List<Type> typeargtypes = null; |
|
1197 |
||
1198 |
Name methName = TreeInfo.name(tree.meth); |
|
1199 |
||
1200 |
boolean isConstructorCall = |
|
1201 |
methName == names._this || methName == names._super; |
|
1202 |
||
1203 |
if (isConstructorCall) { |
|
1204 |
// We are seeing a ...this(...) or ...super(...) call. |
|
1205 |
// Check that this is the first statement in a constructor. |
|
1206 |
if (checkFirstConstructorStat(tree, env)) { |
|
1207 |
||
1208 |
// Record the fact |
|
1209 |
// that this is a constructor call (using isSelfCall). |
|
1210 |
localEnv.info.isSelfCall = true; |
|
1211 |
||
1212 |
// Attribute arguments, yielding list of argument types. |
|
1213 |
argtypes = attribArgs(tree.args, localEnv); |
|
1214 |
typeargtypes = attribTypes(tree.typeargs, localEnv); |
|
1215 |
||
1216 |
// Variable `site' points to the class in which the called |
|
1217 |
// constructor is defined. |
|
1218 |
Type site = env.enclClass.sym.type; |
|
1219 |
if (methName == names._super) { |
|
1220 |
if (site == syms.objectType) { |
|
1221 |
log.error(tree.meth.pos(), "no.superclass", site); |
|
1222 |
site = syms.errType; |
|
1223 |
} else { |
|
1224 |
site = types.supertype(site); |
|
1225 |
} |
|
1226 |
} |
|
1227 |
||
1228 |
if (site.tag == CLASS) { |
|
1229 |
if (site.getEnclosingType().tag == CLASS) { |
|
1230 |
// we are calling a nested class |
|
1231 |
||
1232 |
if (tree.meth.getTag() == JCTree.SELECT) { |
|
1233 |
JCTree qualifier = ((JCFieldAccess) tree.meth).selected; |
|
1234 |
||
1235 |
// We are seeing a prefixed call, of the form |
|
1236 |
// <expr>.super(...). |
|
1237 |
// Check that the prefix expression conforms |
|
1238 |
// to the outer instance type of the class. |
|
1239 |
chk.checkRefType(qualifier.pos(), |
|
1240 |
attribExpr(qualifier, localEnv, |
|
1241 |
site.getEnclosingType())); |
|
1242 |
} else if (methName == names._super) { |
|
1243 |
// qualifier omitted; check for existence |
|
1244 |
// of an appropriate implicit qualifier. |
|
1245 |
rs.resolveImplicitThis(tree.meth.pos(), |
|
1246 |
localEnv, site); |
|
1247 |
} |
|
1248 |
} else if (tree.meth.getTag() == JCTree.SELECT) { |
|
1249 |
log.error(tree.meth.pos(), "illegal.qual.not.icls", |
|
1250 |
site.tsym); |
|
1251 |
} |
|
1252 |
||
1253 |
// if we're calling a java.lang.Enum constructor, |
|
1254 |
// prefix the implicit String and int parameters |
|
1255 |
if (site.tsym == syms.enumSym && allowEnums) |
|
1256 |
argtypes = argtypes.prepend(syms.intType).prepend(syms.stringType); |
|
1257 |
||
1258 |
// Resolve the called constructor under the assumption |
|
1259 |
// that we are referring to a superclass instance of the |
|
1260 |
// current instance (JLS ???). |
|
1261 |
boolean selectSuperPrev = localEnv.info.selectSuper; |
|
1262 |
localEnv.info.selectSuper = true; |
|
1263 |
localEnv.info.varArgs = false; |
|
1264 |
Symbol sym = rs.resolveConstructor( |
|
1265 |
tree.meth.pos(), localEnv, site, argtypes, typeargtypes); |
|
1266 |
localEnv.info.selectSuper = selectSuperPrev; |
|
1267 |
||
1268 |
// Set method symbol to resolved constructor... |
|
1269 |
TreeInfo.setSymbol(tree.meth, sym); |
|
1270 |
||
1271 |
// ...and check that it is legal in the current context. |
|
1272 |
// (this will also set the tree's type) |
|
1273 |
Type mpt = newMethTemplate(argtypes, typeargtypes); |
|
1274 |
checkId(tree.meth, site, sym, localEnv, MTH, |
|
1275 |
mpt, tree.varargsElement != null); |
|
1276 |
} |
|
1277 |
// Otherwise, `site' is an error type and we do nothing |
|
1278 |
} |
|
1279 |
result = tree.type = syms.voidType; |
|
1280 |
} else { |
|
1281 |
// Otherwise, we are seeing a regular method call. |
|
1282 |
// Attribute the arguments, yielding list of argument types, ... |
|
1283 |
argtypes = attribArgs(tree.args, localEnv); |
|
1284 |
typeargtypes = attribTypes(tree.typeargs, localEnv); |
|
1285 |
||
1286 |
// ... and attribute the method using as a prototype a methodtype |
|
1287 |
// whose formal argument types is exactly the list of actual |
|
1288 |
// arguments (this will also set the method symbol). |
|
1289 |
Type mpt = newMethTemplate(argtypes, typeargtypes); |
|
1290 |
localEnv.info.varArgs = false; |
|
1291 |
Type mtype = attribExpr(tree.meth, localEnv, mpt); |
|
1292 |
if (localEnv.info.varArgs) |
|
1293 |
assert mtype.isErroneous() || tree.varargsElement != null; |
|
1294 |
||
1295 |
// Compute the result type. |
|
1296 |
Type restype = mtype.getReturnType(); |
|
1297 |
assert restype.tag != WILDCARD : mtype; |
|
1298 |
||
1299 |
// as a special case, array.clone() has a result that is |
|
1300 |
// the same as static type of the array being cloned |
|
1301 |
if (tree.meth.getTag() == JCTree.SELECT && |
|
1302 |
allowCovariantReturns && |
|
1303 |
methName == names.clone && |
|
1304 |
types.isArray(((JCFieldAccess) tree.meth).selected.type)) |
|
1305 |
restype = ((JCFieldAccess) tree.meth).selected.type; |
|
1306 |
||
1307 |
// as a special case, x.getClass() has type Class<? extends |X|> |
|
1308 |
if (allowGenerics && |
|
1309 |
methName == names.getClass && tree.args.isEmpty()) { |
|
1310 |
Type qualifier = (tree.meth.getTag() == JCTree.SELECT) |
|
1311 |
? ((JCFieldAccess) tree.meth).selected.type |
|
1312 |
: env.enclClass.sym.type; |
|
1313 |
restype = new |
|
1314 |
ClassType(restype.getEnclosingType(), |
|
1315 |
List.<Type>of(new WildcardType(types.erasure(qualifier), |
|
1316 |
BoundKind.EXTENDS, |
|
1317 |
syms.boundClass)), |
|
1318 |
restype.tsym); |
|
1319 |
} |
|
1320 |
||
1321 |
// Check that value of resulting type is admissible in the |
|
1322 |
// current context. Also, capture the return type |
|
1323 |
result = check(tree, capture(restype), VAL, pkind, pt); |
|
1324 |
} |
|
1325 |
chk.validate(tree.typeargs); |
|
1326 |
} |
|
1327 |
//where |
|
1328 |
/** Check that given application node appears as first statement |
|
1329 |
* in a constructor call. |
|
1330 |
* @param tree The application node |
|
1331 |
* @param env The environment current at the application. |
|
1332 |
*/ |
|
1333 |
boolean checkFirstConstructorStat(JCMethodInvocation tree, Env<AttrContext> env) { |
|
1334 |
JCMethodDecl enclMethod = env.enclMethod; |
|
1335 |
if (enclMethod != null && enclMethod.name == names.init) { |
|
1336 |
JCBlock body = enclMethod.body; |
|
1337 |
if (body.stats.head.getTag() == JCTree.EXEC && |
|
1338 |
((JCExpressionStatement) body.stats.head).expr == tree) |
|
1339 |
return true; |
|
1340 |
} |
|
1341 |
log.error(tree.pos(),"call.must.be.first.stmt.in.ctor", |
|
1342 |
TreeInfo.name(tree.meth)); |
|
1343 |
return false; |
|
1344 |
} |
|
1345 |
||
1346 |
/** Obtain a method type with given argument types. |
|
1347 |
*/ |
|
1348 |
Type newMethTemplate(List<Type> argtypes, List<Type> typeargtypes) { |
|
1349 |
MethodType mt = new MethodType(argtypes, null, null, syms.methodClass); |
|
1350 |
return (typeargtypes == null) ? mt : (Type)new ForAll(typeargtypes, mt); |
|
1351 |
} |
|
1352 |
||
1353 |
public void visitNewClass(JCNewClass tree) { |
|
1354 |
Type owntype = syms.errType; |
|
1355 |
||
1356 |
// The local environment of a class creation is |
|
1357 |
// a new environment nested in the current one. |
|
1358 |
Env<AttrContext> localEnv = env.dup(tree, env.info.dup()); |
|
1359 |
||
1360 |
// The anonymous inner class definition of the new expression, |
|
1361 |
// if one is defined by it. |
|
1362 |
JCClassDecl cdef = tree.def; |
|
1363 |
||
1364 |
// If enclosing class is given, attribute it, and |
|
1365 |
// complete class name to be fully qualified |
|
1366 |
JCExpression clazz = tree.clazz; // Class field following new |
|
1367 |
JCExpression clazzid = // Identifier in class field |
|
1368 |
(clazz.getTag() == JCTree.TYPEAPPLY) |
|
1369 |
? ((JCTypeApply) clazz).clazz |
|
1370 |
: clazz; |
|
1371 |
||
1372 |
JCExpression clazzid1 = clazzid; // The same in fully qualified form |
|
1373 |
||
1374 |
if (tree.encl != null) { |
|
1375 |
// We are seeing a qualified new, of the form |
|
1376 |
// <expr>.new C <...> (...) ... |
|
1377 |
// In this case, we let clazz stand for the name of the |
|
1378 |
// allocated class C prefixed with the type of the qualifier |
|
1379 |
// expression, so that we can |
|
1380 |
// resolve it with standard techniques later. I.e., if |
|
1381 |
// <expr> has type T, then <expr>.new C <...> (...) |
|
1382 |
// yields a clazz T.C. |
|
1383 |
Type encltype = chk.checkRefType(tree.encl.pos(), |
|
1384 |
attribExpr(tree.encl, env)); |
|
1385 |
clazzid1 = make.at(clazz.pos).Select(make.Type(encltype), |
|
1386 |
((JCIdent) clazzid).name); |
|
1387 |
if (clazz.getTag() == JCTree.TYPEAPPLY) |
|
1388 |
clazz = make.at(tree.pos). |
|
1389 |
TypeApply(clazzid1, |
|
1390 |
((JCTypeApply) clazz).arguments); |
|
1391 |
else |
|
1392 |
clazz = clazzid1; |
|
1393 |
// System.out.println(clazz + " generated.");//DEBUG |
|
1394 |
} |
|
1395 |
||
1396 |
// Attribute clazz expression and store |
|
1397 |
// symbol + type back into the attributed tree. |
|
1398 |
Type clazztype = chk.checkClassType( |
|
1399 |
tree.clazz.pos(), attribType(clazz, env), true); |
|
1400 |
chk.validate(clazz); |
|
1401 |
if (tree.encl != null) { |
|
1402 |
// We have to work in this case to store |
|
1403 |
// symbol + type back into the attributed tree. |
|
1404 |
tree.clazz.type = clazztype; |
|
1405 |
TreeInfo.setSymbol(clazzid, TreeInfo.symbol(clazzid1)); |
|
1406 |
clazzid.type = ((JCIdent) clazzid).sym.type; |
|
1407 |
if (!clazztype.isErroneous()) { |
|
1408 |
if (cdef != null && clazztype.tsym.isInterface()) { |
|
1409 |
log.error(tree.encl.pos(), "anon.class.impl.intf.no.qual.for.new"); |
|
1410 |
} else if (clazztype.tsym.isStatic()) { |
|
1411 |
log.error(tree.encl.pos(), "qualified.new.of.static.class", clazztype.tsym); |
|
1412 |
} |
|
1413 |
} |
|
1414 |
} else if (!clazztype.tsym.isInterface() && |
|
1415 |
clazztype.getEnclosingType().tag == CLASS) { |
|
1416 |
// Check for the existence of an apropos outer instance |
|
1417 |
rs.resolveImplicitThis(tree.pos(), env, clazztype); |
|
1418 |
} |
|
1419 |
||
1420 |
// Attribute constructor arguments. |
|
1421 |
List<Type> argtypes = attribArgs(tree.args, localEnv); |
|
1422 |
List<Type> typeargtypes = attribTypes(tree.typeargs, localEnv); |
|
1423 |
||
1424 |
// If we have made no mistakes in the class type... |
|
1425 |
if (clazztype.tag == CLASS) { |
|
1426 |
// Enums may not be instantiated except implicitly |
|
1427 |
if (allowEnums && |
|
1428 |
(clazztype.tsym.flags_field&Flags.ENUM) != 0 && |
|
1429 |
(env.tree.getTag() != JCTree.VARDEF || |
|
1430 |
(((JCVariableDecl) env.tree).mods.flags&Flags.ENUM) == 0 || |
|
1431 |
((JCVariableDecl) env.tree).init != tree)) |
|
1432 |
log.error(tree.pos(), "enum.cant.be.instantiated"); |
|
1433 |
// Check that class is not abstract |
|
1434 |
if (cdef == null && |
|
1435 |
(clazztype.tsym.flags() & (ABSTRACT | INTERFACE)) != 0) { |
|
1436 |
log.error(tree.pos(), "abstract.cant.be.instantiated", |
|
1437 |
clazztype.tsym); |
|
1438 |
} else if (cdef != null && clazztype.tsym.isInterface()) { |
|
1439 |
// Check that no constructor arguments are given to |
|
1440 |
// anonymous classes implementing an interface |
|
1441 |
if (!argtypes.isEmpty()) |
|
1442 |
log.error(tree.args.head.pos(), "anon.class.impl.intf.no.args"); |
|
1443 |
||
1444 |
if (!typeargtypes.isEmpty()) |
|
1445 |
log.error(tree.typeargs.head.pos(), "anon.class.impl.intf.no.typeargs"); |
|
1446 |
||
1447 |
// Error recovery: pretend no arguments were supplied. |
|
1448 |
argtypes = List.nil(); |
|
1449 |
typeargtypes = List.nil(); |
|
1450 |
} |
|
1451 |
||
1452 |
// Resolve the called constructor under the assumption |
|
1453 |
// that we are referring to a superclass instance of the |
|
1454 |
// current instance (JLS ???). |
|
1455 |
else { |
|
1456 |
localEnv.info.selectSuper = cdef != null; |
|
1457 |
localEnv.info.varArgs = false; |
|
1458 |
tree.constructor = rs.resolveConstructor( |
|
1459 |
tree.pos(), localEnv, clazztype, argtypes, typeargtypes); |
|
1460 |
Type ctorType = checkMethod(clazztype, |
|
1461 |
tree.constructor, |
|
1462 |
localEnv, |
|
1463 |
tree.args, |
|
1464 |
argtypes, |
|
1465 |
typeargtypes, |
|
1466 |
localEnv.info.varArgs); |
|
1467 |
if (localEnv.info.varArgs) |
|
1468 |
assert ctorType.isErroneous() || tree.varargsElement != null; |
|
1469 |
} |
|
1470 |
||
1471 |
if (cdef != null) { |
|
1472 |
// We are seeing an anonymous class instance creation. |
|
1473 |
// In this case, the class instance creation |
|
1474 |
// expression |
|
1475 |
// |
|
1476 |
// E.new <typeargs1>C<typargs2>(args) { ... } |
|
1477 |
// |
|
1478 |
// is represented internally as |
|
1479 |
// |
|
1480 |
// E . new <typeargs1>C<typargs2>(args) ( class <empty-name> { ... } ) . |
|
1481 |
// |
|
1482 |
// This expression is then *transformed* as follows: |
|
1483 |
// |
|
1484 |
// (1) add a STATIC flag to the class definition |
|
1485 |
// if the current environment is static |
|
1486 |
// (2) add an extends or implements clause |
|
1487 |
// (3) add a constructor. |
|
1488 |
// |
|
1489 |
// For instance, if C is a class, and ET is the type of E, |
|
1490 |
// the expression |
|
1491 |
// |
|
1492 |
// E.new <typeargs1>C<typargs2>(args) { ... } |
|
1493 |
// |
|
1494 |
// is translated to (where X is a fresh name and typarams is the |
|
1495 |
// parameter list of the super constructor): |
|
1496 |
// |
|
1497 |
// new <typeargs1>X(<*nullchk*>E, args) where |
|
1498 |
// X extends C<typargs2> { |
|
1499 |
// <typarams> X(ET e, args) { |
|
1500 |
// e.<typeargs1>super(args) |
|
1501 |
// } |
|
1502 |
// ... |
|
1503 |
// } |
|
1504 |
if (Resolve.isStatic(env)) cdef.mods.flags |= STATIC; |
|
1505 |
||
1506 |
if (clazztype.tsym.isInterface()) { |
|
1507 |
cdef.implementing = List.of(clazz); |
|
1508 |
} else { |
|
1509 |
cdef.extending = clazz; |
|
1510 |
} |
|
1511 |
||
1512 |
attribStat(cdef, localEnv); |
|
1513 |
||
1514 |
// If an outer instance is given, |
|
1515 |
// prefix it to the constructor arguments |
|
1516 |
// and delete it from the new expression |
|
1517 |
if (tree.encl != null && !clazztype.tsym.isInterface()) { |
|
1518 |
tree.args = tree.args.prepend(makeNullCheck(tree.encl)); |
|
1519 |
argtypes = argtypes.prepend(tree.encl.type); |
|
1520 |
tree.encl = null; |
|
1521 |
} |
|
1522 |
||
1523 |
// Reassign clazztype and recompute constructor. |
|
1524 |
clazztype = cdef.sym.type; |
|
1525 |
Symbol sym = rs.resolveConstructor( |
|
1526 |
tree.pos(), localEnv, clazztype, argtypes, |
|
1527 |
typeargtypes, true, tree.varargsElement != null); |
|
1528 |
assert sym.kind < AMBIGUOUS || tree.constructor.type.isErroneous(); |
|
1529 |
tree.constructor = sym; |
|
1530 |
} |
|
1531 |
||
1532 |
if (tree.constructor != null && tree.constructor.kind == MTH) |
|
1533 |
owntype = clazztype; |
|
1534 |
} |
|
1535 |
result = check(tree, owntype, VAL, pkind, pt); |
|
1536 |
chk.validate(tree.typeargs); |
|
1537 |
} |
|
1538 |
||
1539 |
/** Make an attributed null check tree. |
|
1540 |
*/ |
|
1541 |
public JCExpression makeNullCheck(JCExpression arg) { |
|
1542 |
// optimization: X.this is never null; skip null check |
|
1543 |
Name name = TreeInfo.name(arg); |
|
1544 |
if (name == names._this || name == names._super) return arg; |
|
1545 |
||
1546 |
int optag = JCTree.NULLCHK; |
|
1547 |
JCUnary tree = make.at(arg.pos).Unary(optag, arg); |
|
1548 |
tree.operator = syms.nullcheck; |
|
1549 |
tree.type = arg.type; |
|
1550 |
return tree; |
|
1551 |
} |
|
1552 |
||
1553 |
public void visitNewArray(JCNewArray tree) { |
|
1554 |
Type owntype = syms.errType; |
|
1555 |
Type elemtype; |
|
1556 |
if (tree.elemtype != null) { |
|
1557 |
elemtype = attribType(tree.elemtype, env); |
|
1558 |
chk.validate(tree.elemtype); |
|
1559 |
owntype = elemtype; |
|
1560 |
for (List<JCExpression> l = tree.dims; l.nonEmpty(); l = l.tail) { |
|
1561 |
attribExpr(l.head, env, syms.intType); |
|
1562 |
owntype = new ArrayType(owntype, syms.arrayClass); |
|
1563 |
} |
|
1564 |
} else { |
|
1565 |
// we are seeing an untyped aggregate { ... } |
|
1566 |
// this is allowed only if the prototype is an array |
|
1567 |
if (pt.tag == ARRAY) { |
|
1568 |
elemtype = types.elemtype(pt); |
|
1569 |
} else { |
|
1570 |
if (pt.tag != ERROR) { |
|
1571 |
log.error(tree.pos(), "illegal.initializer.for.type", |
|
1572 |
pt); |
|
1573 |
} |
|
1574 |
elemtype = syms.errType; |
|
1575 |
} |
|
1576 |
} |
|
1577 |
if (tree.elems != null) { |
|
1578 |
attribExprs(tree.elems, env, elemtype); |
|
1579 |
owntype = new ArrayType(elemtype, syms.arrayClass); |
|
1580 |
} |
|
1581 |
if (!types.isReifiable(elemtype)) |
|
1582 |
log.error(tree.pos(), "generic.array.creation"); |
|
1583 |
result = check(tree, owntype, VAL, pkind, pt); |
|
1584 |
} |
|
1585 |
||
1586 |
public void visitParens(JCParens tree) { |
|
1587 |
Type owntype = attribTree(tree.expr, env, pkind, pt); |
|
1588 |
result = check(tree, owntype, pkind, pkind, pt); |
|
1589 |
Symbol sym = TreeInfo.symbol(tree); |
|
1590 |
if (sym != null && (sym.kind&(TYP|PCK)) != 0) |
|
1591 |
log.error(tree.pos(), "illegal.start.of.type"); |
|
1592 |
} |
|
1593 |
||
1594 |
public void visitAssign(JCAssign tree) { |
|
1595 |
Type owntype = attribTree(tree.lhs, env.dup(tree), VAR, Type.noType); |
|
1596 |
Type capturedType = capture(owntype); |
|
1597 |
attribExpr(tree.rhs, env, owntype); |
|
1598 |
result = check(tree, capturedType, VAL, pkind, pt); |
|
1599 |
} |
|
1600 |
||
1601 |
public void visitAssignop(JCAssignOp tree) { |
|
1602 |
// Attribute arguments. |
|
1603 |
Type owntype = attribTree(tree.lhs, env, VAR, Type.noType); |
|
1604 |
Type operand = attribExpr(tree.rhs, env); |
|
1605 |
// Find operator. |
|
1606 |
Symbol operator = tree.operator = rs.resolveBinaryOperator( |
|
1607 |
tree.pos(), tree.getTag() - JCTree.ASGOffset, env, |
|
1608 |
owntype, operand); |
|
1609 |
||
1610 |
if (operator.kind == MTH) { |
|
1611 |
chk.checkOperator(tree.pos(), |
|
1612 |
(OperatorSymbol)operator, |
|
1613 |
tree.getTag() - JCTree.ASGOffset, |
|
1614 |
owntype, |
|
1615 |
operand); |
|
166 | 1616 |
chk.checkDivZero(tree.rhs.pos(), operator, operand); |
1617 |
chk.checkCastable(tree.rhs.pos(), |
|
1618 |
operator.type.getReturnType(), |
|
1619 |
owntype); |
|
10 | 1620 |
} |
1621 |
result = check(tree, owntype, VAL, pkind, pt); |
|
1622 |
} |
|
1623 |
||
1624 |
public void visitUnary(JCUnary tree) { |
|
1625 |
// Attribute arguments. |
|
1626 |
Type argtype = (JCTree.PREINC <= tree.getTag() && tree.getTag() <= JCTree.POSTDEC) |
|
1627 |
? attribTree(tree.arg, env, VAR, Type.noType) |
|
1628 |
: chk.checkNonVoid(tree.arg.pos(), attribExpr(tree.arg, env)); |
|
1629 |
||
1630 |
// Find operator. |
|
1631 |
Symbol operator = tree.operator = |
|
1632 |
rs.resolveUnaryOperator(tree.pos(), tree.getTag(), env, argtype); |
|
1633 |
||
1634 |
Type owntype = syms.errType; |
|
1635 |
if (operator.kind == MTH) { |
|
1636 |
owntype = (JCTree.PREINC <= tree.getTag() && tree.getTag() <= JCTree.POSTDEC) |
|
1637 |
? tree.arg.type |
|
1638 |
: operator.type.getReturnType(); |
|
1639 |
int opc = ((OperatorSymbol)operator).opcode; |
|
1640 |
||
1641 |
// If the argument is constant, fold it. |
|
1642 |
if (argtype.constValue() != null) { |
|
1643 |
Type ctype = cfolder.fold1(opc, argtype); |
|
1644 |
if (ctype != null) { |
|
1645 |
owntype = cfolder.coerce(ctype, owntype); |
|
1646 |
||
1647 |
// Remove constant types from arguments to |
|
1648 |
// conserve space. The parser will fold concatenations |
|
1649 |
// of string literals; the code here also |
|
1650 |
// gets rid of intermediate results when some of the |
|
1651 |
// operands are constant identifiers. |
|
1652 |
if (tree.arg.type.tsym == syms.stringType.tsym) { |
|
1653 |
tree.arg.type = syms.stringType; |
|
1654 |
} |
|
1655 |
} |
|
1656 |
} |
|
1657 |
} |
|
1658 |
result = check(tree, owntype, VAL, pkind, pt); |
|
1659 |
} |
|
1660 |
||
1661 |
public void visitBinary(JCBinary tree) { |
|
1662 |
// Attribute arguments. |
|
1663 |
Type left = chk.checkNonVoid(tree.lhs.pos(), attribExpr(tree.lhs, env)); |
|
1664 |
Type right = chk.checkNonVoid(tree.lhs.pos(), attribExpr(tree.rhs, env)); |
|
1665 |
||
1666 |
// Find operator. |
|
1667 |
Symbol operator = tree.operator = |
|
1668 |
rs.resolveBinaryOperator(tree.pos(), tree.getTag(), env, left, right); |
|
1669 |
||
1670 |
Type owntype = syms.errType; |
|
1671 |
if (operator.kind == MTH) { |
|
1672 |
owntype = operator.type.getReturnType(); |
|
1673 |
int opc = chk.checkOperator(tree.lhs.pos(), |
|
1674 |
(OperatorSymbol)operator, |
|
1675 |
tree.getTag(), |
|
1676 |
left, |
|
1677 |
right); |
|
1678 |
||
1679 |
// If both arguments are constants, fold them. |
|
1680 |
if (left.constValue() != null && right.constValue() != null) { |
|
1681 |
Type ctype = cfolder.fold2(opc, left, right); |
|
1682 |
if (ctype != null) { |
|
1683 |
owntype = cfolder.coerce(ctype, owntype); |
|
1684 |
||
1685 |
// Remove constant types from arguments to |
|
1686 |
// conserve space. The parser will fold concatenations |
|
1687 |
// of string literals; the code here also |
|
1688 |
// gets rid of intermediate results when some of the |
|
1689 |
// operands are constant identifiers. |
|
1690 |
if (tree.lhs.type.tsym == syms.stringType.tsym) { |
|
1691 |
tree.lhs.type = syms.stringType; |
|
1692 |
} |
|
1693 |
if (tree.rhs.type.tsym == syms.stringType.tsym) { |
|
1694 |
tree.rhs.type = syms.stringType; |
|
1695 |
} |
|
1696 |
} |
|
1697 |
} |
|
1698 |
||
1699 |
// Check that argument types of a reference ==, != are |
|
1700 |
// castable to each other, (JLS???). |
|
1701 |
if ((opc == ByteCodes.if_acmpeq || opc == ByteCodes.if_acmpne)) { |
|
1702 |
if (!types.isCastable(left, right, new Warner(tree.pos()))) { |
|
1703 |
log.error(tree.pos(), "incomparable.types", left, right); |
|
1704 |
} |
|
1705 |
} |
|
1706 |
||
1707 |
chk.checkDivZero(tree.rhs.pos(), operator, right); |
|
1708 |
} |
|
1709 |
result = check(tree, owntype, VAL, pkind, pt); |
|
1710 |
} |
|
1711 |
||
1712 |
public void visitTypeCast(JCTypeCast tree) { |
|
1713 |
Type clazztype = attribType(tree.clazz, env); |
|
1714 |
Type exprtype = attribExpr(tree.expr, env, Infer.anyPoly); |
|
1715 |
Type owntype = chk.checkCastable(tree.expr.pos(), exprtype, clazztype); |
|
1716 |
if (exprtype.constValue() != null) |
|
1717 |
owntype = cfolder.coerce(exprtype, owntype); |
|
1718 |
result = check(tree, capture(owntype), VAL, pkind, pt); |
|
1719 |
} |
|
1720 |
||
1721 |
public void visitTypeTest(JCInstanceOf tree) { |
|
1722 |
Type exprtype = chk.checkNullOrRefType( |
|
1723 |
tree.expr.pos(), attribExpr(tree.expr, env)); |
|
1724 |
Type clazztype = chk.checkReifiableReferenceType( |
|
1725 |
tree.clazz.pos(), attribType(tree.clazz, env)); |
|
1726 |
chk.checkCastable(tree.expr.pos(), exprtype, clazztype); |
|
1727 |
result = check(tree, syms.booleanType, VAL, pkind, pt); |
|
1728 |
} |
|
1729 |
||
1730 |
public void visitIndexed(JCArrayAccess tree) { |
|
1731 |
Type owntype = syms.errType; |
|
1732 |
Type atype = attribExpr(tree.indexed, env); |
|
1733 |
attribExpr(tree.index, env, syms.intType); |
|
1734 |
if (types.isArray(atype)) |
|
1735 |
owntype = types.elemtype(atype); |
|
1736 |
else if (atype.tag != ERROR) |
|
1737 |
log.error(tree.pos(), "array.req.but.found", atype); |
|
1738 |
if ((pkind & VAR) == 0) owntype = capture(owntype); |
|
1739 |
result = check(tree, owntype, VAR, pkind, pt); |
|
1740 |
} |
|
1741 |
||
1742 |
public void visitIdent(JCIdent tree) { |
|
1743 |
Symbol sym; |
|
1744 |
boolean varArgs = false; |
|
1745 |
||
1746 |
// Find symbol |
|
1747 |
if (pt.tag == METHOD || pt.tag == FORALL) { |
|
1748 |
// If we are looking for a method, the prototype `pt' will be a |
|
1749 |
// method type with the type of the call's arguments as parameters. |
|
1750 |
env.info.varArgs = false; |
|
1751 |
sym = rs.resolveMethod(tree.pos(), env, tree.name, pt.getParameterTypes(), pt.getTypeArguments()); |
|
1752 |
varArgs = env.info.varArgs; |
|
1753 |
} else if (tree.sym != null && tree.sym.kind != VAR) { |
|
1754 |
sym = tree.sym; |
|
1755 |
} else { |
|
1756 |
sym = rs.resolveIdent(tree.pos(), env, tree.name, pkind); |
|
1757 |
} |
|
1758 |
tree.sym = sym; |
|
1759 |
||
1760 |
// (1) Also find the environment current for the class where |
|
1761 |
// sym is defined (`symEnv'). |
|
1762 |
// Only for pre-tiger versions (1.4 and earlier): |
|
1763 |
// (2) Also determine whether we access symbol out of an anonymous |
|
1764 |
// class in a this or super call. This is illegal for instance |
|
1765 |
// members since such classes don't carry a this$n link. |
|
1766 |
// (`noOuterThisPath'). |
|
1767 |
Env<AttrContext> symEnv = env; |
|
1768 |
boolean noOuterThisPath = false; |
|
1769 |
if (env.enclClass.sym.owner.kind != PCK && // we are in an inner class |
|
1770 |
(sym.kind & (VAR | MTH | TYP)) != 0 && |
|
1771 |
sym.owner.kind == TYP && |
|
1772 |
tree.name != names._this && tree.name != names._super) { |
|
1773 |
||
1774 |
// Find environment in which identifier is defined. |
|
1775 |
while (symEnv.outer != null && |
|
1776 |
!sym.isMemberOf(symEnv.enclClass.sym, types)) { |
|
1777 |
if ((symEnv.enclClass.sym.flags() & NOOUTERTHIS) != 0) |
|
1778 |
noOuterThisPath = !allowAnonOuterThis; |
|
1779 |
symEnv = symEnv.outer; |
|
1780 |
} |
|
1781 |
} |
|
1782 |
||
1783 |
// If symbol is a variable, ... |
|
1784 |
if (sym.kind == VAR) { |
|
1785 |
VarSymbol v = (VarSymbol)sym; |
|
1786 |
||
1787 |
// ..., evaluate its initializer, if it has one, and check for |
|
1788 |
// illegal forward reference. |
|
1789 |
checkInit(tree, env, v, false); |
|
1790 |
||
1791 |
// If symbol is a local variable accessed from an embedded |
|
1792 |
// inner class check that it is final. |
|
1793 |
if (v.owner.kind == MTH && |
|
1794 |
v.owner != env.info.scope.owner && |
|
1795 |
(v.flags_field & FINAL) == 0) { |
|
1796 |
log.error(tree.pos(), |
|
1797 |
"local.var.accessed.from.icls.needs.final", |
|
1798 |
v); |
|
1799 |
} |
|
1800 |
||
1801 |
// If we are expecting a variable (as opposed to a value), check |
|
1802 |
// that the variable is assignable in the current environment. |
|
1803 |
if (pkind == VAR) |
|
1804 |
checkAssignable(tree.pos(), v, null, env); |
|
1805 |
} |
|
1806 |
||
1807 |
// In a constructor body, |
|
1808 |
// if symbol is a field or instance method, check that it is |
|
1809 |
// not accessed before the supertype constructor is called. |
|
1810 |
if ((symEnv.info.isSelfCall || noOuterThisPath) && |
|
1811 |
(sym.kind & (VAR | MTH)) != 0 && |
|
1812 |
sym.owner.kind == TYP && |
|
1813 |
(sym.flags() & STATIC) == 0) { |
|
1814 |
chk.earlyRefError(tree.pos(), sym.kind == VAR ? sym : thisSym(tree.pos(), env)); |
|
1815 |
} |
|
1816 |
Env<AttrContext> env1 = env; |
|
512
53e498fa5c0e
6657499: javac 1.6.0 fails to compile class with inner class
mcimadamore
parents:
511
diff
changeset
|
1817 |
if (sym.kind != ERR && sym.kind != TYP && sym.owner != null && sym.owner != env1.enclClass.sym) { |
10 | 1818 |
// If the found symbol is inaccessible, then it is |
1819 |
// accessed through an enclosing instance. Locate this |
|
1820 |
// enclosing instance: |
|
1821 |
while (env1.outer != null && !rs.isAccessible(env, env1.enclClass.sym.type, sym)) |
|
1822 |
env1 = env1.outer; |
|
1823 |
} |
|
1824 |
result = checkId(tree, env1.enclClass.sym.type, sym, env, pkind, pt, varArgs); |
|
1825 |
} |
|
1826 |
||
1827 |
public void visitSelect(JCFieldAccess tree) { |
|
1828 |
// Determine the expected kind of the qualifier expression. |
|
1829 |
int skind = 0; |
|
1830 |
if (tree.name == names._this || tree.name == names._super || |
|
1831 |
tree.name == names._class) |
|
1832 |
{ |
|
1833 |
skind = TYP; |
|
1834 |
} else { |
|
1835 |
if ((pkind & PCK) != 0) skind = skind | PCK; |
|
1836 |
if ((pkind & TYP) != 0) skind = skind | TYP | PCK; |
|
1837 |
if ((pkind & (VAL | MTH)) != 0) skind = skind | VAL | TYP; |
|
1838 |
} |
|
1839 |
||
1840 |
// Attribute the qualifier expression, and determine its symbol (if any). |
|
1841 |
Type site = attribTree(tree.selected, env, skind, Infer.anyPoly); |
|
1842 |
if ((pkind & (PCK | TYP)) == 0) |
|
1843 |
site = capture(site); // Capture field access |
|
1844 |
||
1845 |
// don't allow T.class T[].class, etc |
|
1846 |
if (skind == TYP) { |
|
1847 |
Type elt = site; |
|
1848 |
while (elt.tag == ARRAY) |
|
1849 |
elt = ((ArrayType)elt).elemtype; |
|
1850 |
if (elt.tag == TYPEVAR) { |
|
1851 |
log.error(tree.pos(), "type.var.cant.be.deref"); |
|
1852 |
result = syms.errType; |
|
1853 |
return; |
|
1854 |
} |
|
1855 |
} |
|
1856 |
||
1857 |
// If qualifier symbol is a type or `super', assert `selectSuper' |
|
1858 |
// for the selection. This is relevant for determining whether |
|
1859 |
// protected symbols are accessible. |
|
1860 |
Symbol sitesym = TreeInfo.symbol(tree.selected); |
|
1861 |
boolean selectSuperPrev = env.info.selectSuper; |
|
1862 |
env.info.selectSuper = |
|
1863 |
sitesym != null && |
|
1864 |
sitesym.name == names._super; |
|
1865 |
||
1866 |
// If selected expression is polymorphic, strip |
|
1867 |
// type parameters and remember in env.info.tvars, so that |
|
1868 |
// they can be added later (in Attr.checkId and Infer.instantiateMethod). |
|
1869 |
if (tree.selected.type.tag == FORALL) { |
|
1870 |
ForAll pstype = (ForAll)tree.selected.type; |
|
1871 |
env.info.tvars = pstype.tvars; |
|
1872 |
site = tree.selected.type = pstype.qtype; |
|
1873 |
} |
|
1874 |
||
1875 |
// Determine the symbol represented by the selection. |
|
1876 |
env.info.varArgs = false; |
|
1877 |
Symbol sym = selectSym(tree, site, env, pt, pkind); |
|
1878 |
if (sym.exists() && !isType(sym) && (pkind & (PCK | TYP)) != 0) { |
|
1879 |
site = capture(site); |
|
1880 |
sym = selectSym(tree, site, env, pt, pkind); |
|
1881 |
} |
|
1882 |
boolean varArgs = env.info.varArgs; |
|
1883 |
tree.sym = sym; |
|
1884 |
||
511
b3b5eadd2bca
6450290: Capture of nested wildcards causes type error
mcimadamore
parents:
325
diff
changeset
|
1885 |
if (site.tag == TYPEVAR && !isType(sym) && sym.kind != ERR) { |
b3b5eadd2bca
6450290: Capture of nested wildcards causes type error
mcimadamore
parents:
325
diff
changeset
|
1886 |
while (site.tag == TYPEVAR) site = site.getUpperBound(); |
b3b5eadd2bca
6450290: Capture of nested wildcards causes type error
mcimadamore
parents:
325
diff
changeset
|
1887 |
site = capture(site); |
b3b5eadd2bca
6450290: Capture of nested wildcards causes type error
mcimadamore
parents:
325
diff
changeset
|
1888 |
} |
10 | 1889 |
|
1890 |
// If that symbol is a variable, ... |
|
1891 |
if (sym.kind == VAR) { |
|
1892 |
VarSymbol v = (VarSymbol)sym; |
|
1893 |
||
1894 |
// ..., evaluate its initializer, if it has one, and check for |
|
1895 |
// illegal forward reference. |
|
1896 |
checkInit(tree, env, v, true); |
|
1897 |
||
1898 |
// If we are expecting a variable (as opposed to a value), check |
|
1899 |
// that the variable is assignable in the current environment. |
|
1900 |
if (pkind == VAR) |
|
1901 |
checkAssignable(tree.pos(), v, tree.selected, env); |
|
1902 |
} |
|
1903 |
||
1904 |
// Disallow selecting a type from an expression |
|
1905 |
if (isType(sym) && (sitesym==null || (sitesym.kind&(TYP|PCK)) == 0)) { |
|
1906 |
tree.type = check(tree.selected, pt, |
|
1907 |
sitesym == null ? VAL : sitesym.kind, TYP|PCK, pt); |
|
1908 |
} |
|
1909 |
||
1910 |
if (isType(sitesym)) { |
|
1911 |
if (sym.name == names._this) { |
|
1912 |
// If `C' is the currently compiled class, check that |
|
1913 |
// C.this' does not appear in a call to a super(...) |
|
1914 |
if (env.info.isSelfCall && |
|
1915 |
site.tsym == env.enclClass.sym) { |
|
1916 |
chk.earlyRefError(tree.pos(), sym); |
|
1917 |
} |
|
1918 |
} else { |
|
1919 |
// Check if type-qualified fields or methods are static (JLS) |
|
1920 |
if ((sym.flags() & STATIC) == 0 && |
|
1921 |
sym.name != names._super && |
|
1922 |
(sym.kind == VAR || sym.kind == MTH)) { |
|
1923 |
rs.access(rs.new StaticError(sym), |
|
1924 |
tree.pos(), site, sym.name, true); |
|
1925 |
} |
|
1926 |
} |
|
1927 |
} |
|
1928 |
||
1929 |
// If we are selecting an instance member via a `super', ... |
|
1930 |
if (env.info.selectSuper && (sym.flags() & STATIC) == 0) { |
|
1931 |
||
1932 |
// Check that super-qualified symbols are not abstract (JLS) |
|
1933 |
rs.checkNonAbstract(tree.pos(), sym); |
|
1934 |
||
1935 |
if (site.isRaw()) { |
|
1936 |
// Determine argument types for site. |
|
1937 |
Type site1 = types.asSuper(env.enclClass.sym.type, site.tsym); |
|
1938 |
if (site1 != null) site = site1; |
|
1939 |
} |
|
1940 |
} |
|
1941 |
||
1942 |
env.info.selectSuper = selectSuperPrev; |
|
1943 |
result = checkId(tree, site, sym, env, pkind, pt, varArgs); |
|
1944 |
env.info.tvars = List.nil(); |
|
1945 |
} |
|
1946 |
//where |
|
1947 |
/** Determine symbol referenced by a Select expression, |
|
1948 |
* |
|
1949 |
* @param tree The select tree. |
|
1950 |
* @param site The type of the selected expression, |
|
1951 |
* @param env The current environment. |
|
1952 |
* @param pt The current prototype. |
|
1953 |
* @param pkind The expected kind(s) of the Select expression. |
|
1954 |
*/ |
|
1955 |
private Symbol selectSym(JCFieldAccess tree, |
|
1956 |
Type site, |
|
1957 |
Env<AttrContext> env, |
|
1958 |
Type pt, |
|
1959 |
int pkind) { |
|
1960 |
DiagnosticPosition pos = tree.pos(); |
|
1961 |
Name name = tree.name; |
|
1962 |
||
1963 |
switch (site.tag) { |
|
1964 |
case PACKAGE: |
|
1965 |
return rs.access( |
|
1966 |
rs.findIdentInPackage(env, site.tsym, name, pkind), |
|
1967 |
pos, site, name, true); |
|
1968 |
case ARRAY: |
|
1969 |
case CLASS: |
|
1970 |
if (pt.tag == METHOD || pt.tag == FORALL) { |
|
1971 |
return rs.resolveQualifiedMethod( |
|
1972 |
pos, env, site, name, pt.getParameterTypes(), pt.getTypeArguments()); |
|
1973 |
} else if (name == names._this || name == names._super) { |
|
1974 |
return rs.resolveSelf(pos, env, site.tsym, name); |
|
1975 |
} else if (name == names._class) { |
|
1976 |
// In this case, we have already made sure in |
|
1977 |
// visitSelect that qualifier expression is a type. |
|
1978 |
Type t = syms.classType; |
|
1979 |
List<Type> typeargs = allowGenerics |
|
1980 |
? List.of(types.erasure(site)) |
|
1981 |
: List.<Type>nil(); |
|
1982 |
t = new ClassType(t.getEnclosingType(), typeargs, t.tsym); |
|
1983 |
return new VarSymbol( |
|
1984 |
STATIC | PUBLIC | FINAL, names._class, t, site.tsym); |
|
1985 |
} else { |
|
1986 |
// We are seeing a plain identifier as selector. |
|
1987 |
Symbol sym = rs.findIdentInType(env, site, name, pkind); |
|
1988 |
if ((pkind & ERRONEOUS) == 0) |
|
1989 |
sym = rs.access(sym, pos, site, name, true); |
|
1990 |
return sym; |
|
1991 |
} |
|
1992 |
case WILDCARD: |
|
1993 |
throw new AssertionError(tree); |
|
1994 |
case TYPEVAR: |
|
1995 |
// Normally, site.getUpperBound() shouldn't be null. |
|
1996 |
// It should only happen during memberEnter/attribBase |
|
1997 |
// when determining the super type which *must* be |
|
1998 |
// done before attributing the type variables. In |
|
1999 |
// other words, we are seeing this illegal program: |
|
2000 |
// class B<T> extends A<T.foo> {} |
|
2001 |
Symbol sym = (site.getUpperBound() != null) |
|
2002 |
? selectSym(tree, capture(site.getUpperBound()), env, pt, pkind) |
|
2003 |
: null; |
|
2004 |
if (sym == null || isType(sym)) { |
|
2005 |
log.error(pos, "type.var.cant.be.deref"); |
|
2006 |
return syms.errSymbol; |
|
2007 |
} else { |
|
2008 |
return sym; |
|
2009 |
} |
|
2010 |
case ERROR: |
|
2011 |
// preserve identifier names through errors |
|
2012 |
return new ErrorType(name, site.tsym).tsym; |
|
2013 |
default: |
|
2014 |
// The qualifier expression is of a primitive type -- only |
|
2015 |
// .class is allowed for these. |
|
2016 |
if (name == names._class) { |
|
2017 |
// In this case, we have already made sure in Select that |
|
2018 |
// qualifier expression is a type. |
|
2019 |
Type t = syms.classType; |
|
2020 |
Type arg = types.boxedClass(site).type; |
|
2021 |
t = new ClassType(t.getEnclosingType(), List.of(arg), t.tsym); |
|
2022 |
return new VarSymbol( |
|
2023 |
STATIC | PUBLIC | FINAL, names._class, t, site.tsym); |
|
2024 |
} else { |
|
2025 |
log.error(pos, "cant.deref", site); |
|
2026 |
return syms.errSymbol; |
|
2027 |
} |
|
2028 |
} |
|
2029 |
} |
|
2030 |
||
2031 |
/** Determine type of identifier or select expression and check that |
|
2032 |
* (1) the referenced symbol is not deprecated |
|
2033 |
* (2) the symbol's type is safe (@see checkSafe) |
|
2034 |
* (3) if symbol is a variable, check that its type and kind are |
|
2035 |
* compatible with the prototype and protokind. |
|
2036 |
* (4) if symbol is an instance field of a raw type, |
|
2037 |
* which is being assigned to, issue an unchecked warning if its |
|
2038 |
* type changes under erasure. |
|
2039 |
* (5) if symbol is an instance method of a raw type, issue an |
|
2040 |
* unchecked warning if its argument types change under erasure. |
|
2041 |
* If checks succeed: |
|
2042 |
* If symbol is a constant, return its constant type |
|
2043 |
* else if symbol is a method, return its result type |
|
2044 |
* otherwise return its type. |
|
2045 |
* Otherwise return errType. |
|
2046 |
* |
|
2047 |
* @param tree The syntax tree representing the identifier |
|
2048 |
* @param site If this is a select, the type of the selected |
|
2049 |
* expression, otherwise the type of the current class. |
|
2050 |
* @param sym The symbol representing the identifier. |
|
2051 |
* @param env The current environment. |
|
2052 |
* @param pkind The set of expected kinds. |
|
2053 |
* @param pt The expected type. |
|
2054 |
*/ |
|
2055 |
Type checkId(JCTree tree, |
|
2056 |
Type site, |
|
2057 |
Symbol sym, |
|
2058 |
Env<AttrContext> env, |
|
2059 |
int pkind, |
|
2060 |
Type pt, |
|
2061 |
boolean useVarargs) { |
|
2062 |
if (pt.isErroneous()) return syms.errType; |
|
2063 |
Type owntype; // The computed type of this identifier occurrence. |
|
2064 |
switch (sym.kind) { |
|
2065 |
case TYP: |
|
2066 |
// For types, the computed type equals the symbol's type, |
|
2067 |
// except for two situations: |
|
2068 |
owntype = sym.type; |
|
2069 |
if (owntype.tag == CLASS) { |
|
2070 |
Type ownOuter = owntype.getEnclosingType(); |
|
2071 |
||
2072 |
// (a) If the symbol's type is parameterized, erase it |
|
2073 |
// because no type parameters were given. |
|
2074 |
// We recover generic outer type later in visitTypeApply. |
|
2075 |
if (owntype.tsym.type.getTypeArguments().nonEmpty()) { |
|
2076 |
owntype = types.erasure(owntype); |
|
2077 |
} |
|
2078 |
||
2079 |
// (b) If the symbol's type is an inner class, then |
|
2080 |
// we have to interpret its outer type as a superclass |
|
2081 |
// of the site type. Example: |
|
2082 |
// |
|
2083 |
// class Tree<A> { class Visitor { ... } } |
|
2084 |
// class PointTree extends Tree<Point> { ... } |
|
2085 |
// ...PointTree.Visitor... |
|
2086 |
// |
|
2087 |
// Then the type of the last expression above is |
|
2088 |
// Tree<Point>.Visitor. |
|
2089 |
else if (ownOuter.tag == CLASS && site != ownOuter) { |
|
2090 |
Type normOuter = site; |
|
2091 |
if (normOuter.tag == CLASS) |
|
2092 |
normOuter = types.asEnclosingSuper(site, ownOuter.tsym); |
|
2093 |
if (normOuter == null) // perhaps from an import |
|
2094 |
normOuter = types.erasure(ownOuter); |
|
2095 |
if (normOuter != ownOuter) |
|
2096 |
owntype = new ClassType( |
|
2097 |
normOuter, List.<Type>nil(), owntype.tsym); |
|
2098 |
} |
|
2099 |
} |
|
2100 |
break; |
|
2101 |
case VAR: |
|
2102 |
VarSymbol v = (VarSymbol)sym; |
|
2103 |
// Test (4): if symbol is an instance field of a raw type, |
|
2104 |
// which is being assigned to, issue an unchecked warning if |
|
2105 |
// its type changes under erasure. |
|
2106 |
if (allowGenerics && |
|
2107 |
pkind == VAR && |
|
2108 |
v.owner.kind == TYP && |
|
2109 |
(v.flags() & STATIC) == 0 && |
|
2110 |
(site.tag == CLASS || site.tag == TYPEVAR)) { |
|
2111 |
Type s = types.asOuterSuper(site, v.owner); |
|
2112 |
if (s != null && |
|
2113 |
s.isRaw() && |
|
2114 |
!types.isSameType(v.type, v.erasure(types))) { |
|
2115 |
chk.warnUnchecked(tree.pos(), |
|
2116 |
"unchecked.assign.to.var", |
|
2117 |
v, s); |
|
2118 |
} |
|
2119 |
} |
|
2120 |
// The computed type of a variable is the type of the |
|
2121 |
// variable symbol, taken as a member of the site type. |
|
2122 |
owntype = (sym.owner.kind == TYP && |
|
2123 |
sym.name != names._this && sym.name != names._super) |
|
2124 |
? types.memberType(site, sym) |
|
2125 |
: sym.type; |
|
2126 |
||
2127 |
if (env.info.tvars.nonEmpty()) { |
|
2128 |
Type owntype1 = new ForAll(env.info.tvars, owntype); |
|
2129 |
for (List<Type> l = env.info.tvars; l.nonEmpty(); l = l.tail) |
|
2130 |
if (!owntype.contains(l.head)) { |
|
2131 |
log.error(tree.pos(), "undetermined.type", owntype1); |
|
2132 |
owntype1 = syms.errType; |
|
2133 |
} |
|
2134 |
owntype = owntype1; |
|
2135 |
} |
|
2136 |
||
2137 |
// If the variable is a constant, record constant value in |
|
2138 |
// computed type. |
|
2139 |
if (v.getConstValue() != null && isStaticReference(tree)) |
|
2140 |
owntype = owntype.constType(v.getConstValue()); |
|
2141 |
||
2142 |
if (pkind == VAL) { |
|
2143 |
owntype = capture(owntype); // capture "names as expressions" |
|
2144 |
} |
|
2145 |
break; |
|
2146 |
case MTH: { |
|
2147 |
JCMethodInvocation app = (JCMethodInvocation)env.tree; |
|
2148 |
owntype = checkMethod(site, sym, env, app.args, |
|
2149 |
pt.getParameterTypes(), pt.getTypeArguments(), |
|
2150 |
env.info.varArgs); |
|
2151 |
break; |
|
2152 |
} |
|
2153 |
case PCK: case ERR: |
|
2154 |
owntype = sym.type; |
|
2155 |
break; |
|
2156 |
default: |
|
2157 |
throw new AssertionError("unexpected kind: " + sym.kind + |
|
2158 |
" in tree " + tree); |
|
2159 |
} |
|
2160 |
||
2161 |
// Test (1): emit a `deprecation' warning if symbol is deprecated. |
|
2162 |
// (for constructors, the error was given when the constructor was |
|
2163 |
// resolved) |
|
2164 |
if (sym.name != names.init && |
|
2165 |
(sym.flags() & DEPRECATED) != 0 && |
|
2166 |
(env.info.scope.owner.flags() & DEPRECATED) == 0 && |
|
2167 |
sym.outermostClass() != env.info.scope.owner.outermostClass()) |
|
2168 |
chk.warnDeprecated(tree.pos(), sym); |
|
2169 |
||
2170 |
if ((sym.flags() & PROPRIETARY) != 0) |
|
2171 |
log.strictWarning(tree.pos(), "sun.proprietary", sym); |
|
2172 |
||
2173 |
// Test (3): if symbol is a variable, check that its type and |
|
2174 |
// kind are compatible with the prototype and protokind. |
|
2175 |
return check(tree, owntype, sym.kind, pkind, pt); |
|
2176 |
} |
|
2177 |
||
2178 |
/** Check that variable is initialized and evaluate the variable's |
|
2179 |
* initializer, if not yet done. Also check that variable is not |
|
2180 |
* referenced before it is defined. |
|
2181 |
* @param tree The tree making up the variable reference. |
|
2182 |
* @param env The current environment. |
|
2183 |
* @param v The variable's symbol. |
|
2184 |
*/ |
|
2185 |
private void checkInit(JCTree tree, |
|
2186 |
Env<AttrContext> env, |
|
2187 |
VarSymbol v, |
|
2188 |
boolean onlyWarning) { |
|
2189 |
// System.err.println(v + " " + ((v.flags() & STATIC) != 0) + " " + |
|
2190 |
// tree.pos + " " + v.pos + " " + |
|
2191 |
// Resolve.isStatic(env));//DEBUG |
|
2192 |
||
2193 |
// A forward reference is diagnosed if the declaration position |
|
2194 |
// of the variable is greater than the current tree position |
|
2195 |
// and the tree and variable definition occur in the same class |
|
2196 |
// definition. Note that writes don't count as references. |
|
2197 |
// This check applies only to class and instance |
|
2198 |
// variables. Local variables follow different scope rules, |
|
2199 |
// and are subject to definite assignment checking. |
|
1045
56f6e84f7825
6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents:
1040
diff
changeset
|
2200 |
if ((env.info.enclVar == v || v.pos > tree.pos) && |
10 | 2201 |
v.owner.kind == TYP && |
2202 |
canOwnInitializer(env.info.scope.owner) && |
|
2203 |
v.owner == env.info.scope.owner.enclClass() && |
|
2204 |
((v.flags() & STATIC) != 0) == Resolve.isStatic(env) && |
|
2205 |
(env.tree.getTag() != JCTree.ASSIGN || |
|
2206 |
TreeInfo.skipParens(((JCAssign) env.tree).lhs) != tree)) { |
|
1045
56f6e84f7825
6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents:
1040
diff
changeset
|
2207 |
String suffix = (env.info.enclVar == v) ? |
56f6e84f7825
6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents:
1040
diff
changeset
|
2208 |
"self.ref" : "forward.ref"; |
325
44df0e9643b4
6509042: javac rejects class literals in enum constructors
mcimadamore
parents:
166
diff
changeset
|
2209 |
if (!onlyWarning || isStaticEnumField(v)) { |
1045
56f6e84f7825
6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents:
1040
diff
changeset
|
2210 |
log.error(tree.pos(), "illegal." + suffix); |
10 | 2211 |
} else if (useBeforeDeclarationWarning) { |
1045
56f6e84f7825
6676362: Spurious forward reference error with final var + instance variable initializer
mcimadamore
parents:
1040
diff
changeset
|
2212 |
log.warning(tree.pos(), suffix, v); |
10 | 2213 |
} |
2214 |
} |
|
2215 |
||
2216 |
v.getConstValue(); // ensure initializer is evaluated |
|
2217 |
||
2218 |
checkEnumInitializer(tree, env, v); |
|
2219 |
} |
|
2220 |
||
2221 |
/** |
|
2222 |
* Check for illegal references to static members of enum. In |
|
2223 |
* an enum type, constructors and initializers may not |
|
2224 |
* reference its static members unless they are constant. |
|
2225 |
* |
|
2226 |
* @param tree The tree making up the variable reference. |
|
2227 |
* @param env The current environment. |
|
2228 |
* @param v The variable's symbol. |
|
2229 |
* @see JLS 3rd Ed. (8.9 Enums) |
|
2230 |
*/ |
|
2231 |
private void checkEnumInitializer(JCTree tree, Env<AttrContext> env, VarSymbol v) { |
|
2232 |
// JLS 3rd Ed.: |
|
2233 |
// |
|
2234 |
// "It is a compile-time error to reference a static field |
|
2235 |
// of an enum type that is not a compile-time constant |
|
2236 |
// (15.28) from constructors, instance initializer blocks, |
|
2237 |
// or instance variable initializer expressions of that |
|
2238 |
// type. It is a compile-time error for the constructors, |
|
2239 |
// instance initializer blocks, or instance variable |
|
2240 |
// initializer expressions of an enum constant e to refer |
|
2241 |
// to itself or to an enum constant of the same type that |
|
2242 |
// is declared to the right of e." |
|
325
44df0e9643b4
6509042: javac rejects class literals in enum constructors
mcimadamore
parents:
166
diff
changeset
|
2243 |
if (isStaticEnumField(v)) { |
10 | 2244 |
ClassSymbol enclClass = env.info.scope.owner.enclClass(); |
2245 |
||
2246 |
if (enclClass == null || enclClass.owner == null) |
|
2247 |
return; |
|
2248 |
||
2249 |
// See if the enclosing class is the enum (or a |
|
2250 |
// subclass thereof) declaring v. If not, this |
|
2251 |
// reference is OK. |
|
2252 |
if (v.owner != enclClass && !types.isSubtype(enclClass.type, v.owner.type)) |
|
2253 |
return; |
|
2254 |
||
2255 |
// If the reference isn't from an initializer, then |
|
2256 |
// the reference is OK. |
|
2257 |
if (!Resolve.isInitializer(env)) |
|
2258 |
return; |
|
2259 |
||
2260 |
log.error(tree.pos(), "illegal.enum.static.ref"); |
|
2261 |
} |
|
2262 |
} |
|
2263 |
||
325
44df0e9643b4
6509042: javac rejects class literals in enum constructors
mcimadamore
parents:
166
diff
changeset
|
2264 |
/** Is the given symbol a static, non-constant field of an Enum? |
44df0e9643b4
6509042: javac rejects class literals in enum constructors
mcimadamore
parents:
166
diff
changeset
|
2265 |
* Note: enum literals should not be regarded as such |
44df0e9643b4
6509042: javac rejects class literals in enum constructors
mcimadamore
parents:
166
diff
changeset
|
2266 |
*/ |
44df0e9643b4
6509042: javac rejects class literals in enum constructors
mcimadamore
parents:
166
diff
changeset
|
2267 |
private boolean isStaticEnumField(VarSymbol v) { |
44df0e9643b4
6509042: javac rejects class literals in enum constructors
mcimadamore
parents:
166
diff
changeset
|
2268 |
return Flags.isEnum(v.owner) && |
44df0e9643b4
6509042: javac rejects class literals in enum constructors
mcimadamore
parents:
166
diff
changeset
|
2269 |
Flags.isStatic(v) && |
44df0e9643b4
6509042: javac rejects class literals in enum constructors
mcimadamore
parents:
166
diff
changeset
|
2270 |
!Flags.isConstant(v) && |
44df0e9643b4
6509042: javac rejects class literals in enum constructors
mcimadamore
parents:
166
diff
changeset
|
2271 |
v.name != names._class; |
10 | 2272 |
} |
2273 |
||
2274 |
/** Can the given symbol be the owner of code which forms part |
|
2275 |
* if class initialization? This is the case if the symbol is |
|
2276 |
* a type or field, or if the symbol is the synthetic method. |
|
2277 |
* owning a block. |
|
2278 |
*/ |
|
2279 |
private boolean canOwnInitializer(Symbol sym) { |
|
2280 |
return |
|
2281 |
(sym.kind & (VAR | TYP)) != 0 || |
|
2282 |
(sym.kind == MTH && (sym.flags() & BLOCK) != 0); |
|
2283 |
} |
|
2284 |
||
2285 |
Warner noteWarner = new Warner(); |
|
2286 |
||
2287 |
/** |
|
2288 |
* Check that method arguments conform to its instantation. |
|
2289 |
**/ |
|
2290 |
public Type checkMethod(Type site, |
|
2291 |
Symbol sym, |
|
2292 |
Env<AttrContext> env, |
|
2293 |
final List<JCExpression> argtrees, |
|
2294 |
List<Type> argtypes, |
|
2295 |
List<Type> typeargtypes, |
|
2296 |
boolean useVarargs) { |
|
2297 |
// Test (5): if symbol is an instance method of a raw type, issue |
|
2298 |
// an unchecked warning if its argument types change under erasure. |
|
2299 |
if (allowGenerics && |
|
2300 |
(sym.flags() & STATIC) == 0 && |
|
2301 |
(site.tag == CLASS || site.tag == TYPEVAR)) { |
|
2302 |
Type s = types.asOuterSuper(site, sym.owner); |
|
2303 |
if (s != null && s.isRaw() && |
|
2304 |
!types.isSameTypes(sym.type.getParameterTypes(), |
|
2305 |
sym.erasure(types).getParameterTypes())) { |
|
2306 |
chk.warnUnchecked(env.tree.pos(), |
|
2307 |
"unchecked.call.mbr.of.raw.type", |
|
2308 |
sym, s); |
|
2309 |
} |
|
2310 |
} |
|
2311 |
||
2312 |
// Compute the identifier's instantiated type. |
|
2313 |
// For methods, we need to compute the instance type by |
|
2314 |
// Resolve.instantiate from the symbol's type as well as |
|
2315 |
// any type arguments and value arguments. |
|
2316 |
noteWarner.warned = false; |
|
2317 |
Type owntype = rs.instantiate(env, |
|
2318 |
site, |
|
2319 |
sym, |
|
2320 |
argtypes, |
|
2321 |
typeargtypes, |
|
2322 |
true, |
|
2323 |
useVarargs, |
|
2324 |
noteWarner); |
|
2325 |
boolean warned = noteWarner.warned; |
|
2326 |
||
2327 |
// If this fails, something went wrong; we should not have |
|
2328 |
// found the identifier in the first place. |
|
2329 |
if (owntype == null) { |
|
2330 |
if (!pt.isErroneous()) |
|
2331 |
log.error(env.tree.pos(), |
|
2332 |
"internal.error.cant.instantiate", |
|
2333 |
sym, site, |
|
2334 |
Type.toString(pt.getParameterTypes())); |
|
2335 |
owntype = syms.errType; |
|
2336 |
} else { |
|
2337 |
// System.out.println("call : " + env.tree); |
|
2338 |
// System.out.println("method : " + owntype); |
|
2339 |
// System.out.println("actuals: " + argtypes); |
|
2340 |
List<Type> formals = owntype.getParameterTypes(); |
|
2341 |
Type last = useVarargs ? formals.last() : null; |
|
2342 |
if (sym.name==names.init && |
|
2343 |
sym.owner == syms.enumSym) |
|
2344 |
formals = formals.tail.tail; |
|
2345 |
List<JCExpression> args = argtrees; |
|
2346 |
while (formals.head != last) { |
|
2347 |
JCTree arg = args.head; |
|
2348 |
Warner warn = chk.convertWarner(arg.pos(), arg.type, formals.head); |
|
2349 |
assertConvertible(arg, arg.type, formals.head, warn); |
|
2350 |
warned |= warn.warned; |
|
2351 |
args = args.tail; |
|
2352 |
formals = formals.tail; |
|
2353 |
} |
|
2354 |
if (useVarargs) { |
|
2355 |
Type varArg = types.elemtype(last); |
|
2356 |
while (args.tail != null) { |
|
2357 |
JCTree arg = args.head; |
|
2358 |
Warner warn = chk.convertWarner(arg.pos(), arg.type, varArg); |
|
2359 |
assertConvertible(arg, arg.type, varArg, warn); |
|
2360 |
warned |= warn.warned; |
|
2361 |
args = args.tail; |
|
2362 |
} |
|
2363 |
} else if ((sym.flags() & VARARGS) != 0 && allowVarargs) { |
|
2364 |
// non-varargs call to varargs method |
|
2365 |
Type varParam = owntype.getParameterTypes().last(); |
|
2366 |
Type lastArg = argtypes.last(); |
|
2367 |
if (types.isSubtypeUnchecked(lastArg, types.elemtype(varParam)) && |
|
2368 |
!types.isSameType(types.erasure(varParam), types.erasure(lastArg))) |
|
2369 |
log.warning(argtrees.last().pos(), "inexact.non-varargs.call", |
|
2370 |
types.elemtype(varParam), |
|
2371 |
varParam); |
|
2372 |
} |
|
2373 |
||
2374 |
if (warned && sym.type.tag == FORALL) { |
|
2375 |
String typeargs = ""; |
|
2376 |
if (typeargtypes != null && typeargtypes.nonEmpty()) { |
|
2377 |
typeargs = "<" + Type.toString(typeargtypes) + ">"; |
|
2378 |
} |
|
2379 |
chk.warnUnchecked(env.tree.pos(), |
|
2380 |
"unchecked.meth.invocation.applied", |
|
2381 |
sym, |
|
2382 |
sym.location(), |
|
2383 |
typeargs, |
|
2384 |
Type.toString(argtypes)); |
|
2385 |
owntype = new MethodType(owntype.getParameterTypes(), |
|
2386 |
types.erasure(owntype.getReturnType()), |
|
2387 |
owntype.getThrownTypes(), |
|
2388 |
syms.methodClass); |
|
2389 |
} |
|
2390 |
if (useVarargs) { |
|
2391 |
JCTree tree = env.tree; |
|
2392 |
Type argtype = owntype.getParameterTypes().last(); |
|
2393 |
if (!types.isReifiable(argtype)) |
|
2394 |
chk.warnUnchecked(env.tree.pos(), |
|
2395 |
"unchecked.generic.array.creation", |
|
2396 |
argtype); |
|
2397 |
Type elemtype = types.elemtype(argtype); |
|
2398 |
switch (tree.getTag()) { |
|
2399 |
case JCTree.APPLY: |
|
2400 |
((JCMethodInvocation) tree).varargsElement = elemtype; |
|
2401 |
break; |
|
2402 |
case JCTree.NEWCLASS: |
|
2403 |
((JCNewClass) tree).varargsElement = elemtype; |
|
2404 |
break; |
|
2405 |
default: |
|
2406 |
throw new AssertionError(""+tree); |
|
2407 |
} |
|
2408 |
} |
|
2409 |
} |
|
2410 |
return owntype; |
|
2411 |
} |
|
2412 |
||
2413 |
private void assertConvertible(JCTree tree, Type actual, Type formal, Warner warn) { |
|
2414 |
if (types.isConvertible(actual, formal, warn)) |
|
2415 |
return; |
|
2416 |
||
2417 |
if (formal.isCompound() |
|
2418 |
&& types.isSubtype(actual, types.supertype(formal)) |
|
2419 |
&& types.isSubtypeUnchecked(actual, types.interfaces(formal), warn)) |
|
2420 |
return; |
|
2421 |
||
2422 |
if (false) { |
|
2423 |
// TODO: make assertConvertible work |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
941
diff
changeset
|
2424 |
chk.typeError(tree.pos(), diags.fragment("incompatible.types"), actual, formal); |
10 | 2425 |
throw new AssertionError("Tree: " + tree |
2426 |
+ " actual:" + actual |
|
2427 |
+ " formal: " + formal); |
|
2428 |
} |
|
2429 |
} |
|
2430 |
||
2431 |
public void visitLiteral(JCLiteral tree) { |
|
2432 |
result = check( |
|
2433 |
tree, litType(tree.typetag).constType(tree.value), VAL, pkind, pt); |
|
2434 |
} |
|
2435 |
//where |
|
2436 |
/** Return the type of a literal with given type tag. |
|
2437 |
*/ |
|
2438 |
Type litType(int tag) { |
|
2439 |
return (tag == TypeTags.CLASS) ? syms.stringType : syms.typeOfTag[tag]; |
|
2440 |
} |
|
2441 |
||
2442 |
public void visitTypeIdent(JCPrimitiveTypeTree tree) { |
|
2443 |
result = check(tree, syms.typeOfTag[tree.typetag], TYP, pkind, pt); |
|
2444 |
} |
|
2445 |
||
2446 |
public void visitTypeArray(JCArrayTypeTree tree) { |
|
2447 |
Type etype = attribType(tree.elemtype, env); |
|
2448 |
Type type = new ArrayType(etype, syms.arrayClass); |
|
2449 |
result = check(tree, type, TYP, pkind, pt); |
|
2450 |
} |
|
2451 |
||
2452 |
/** Visitor method for parameterized types. |
|
2453 |
* Bound checking is left until later, since types are attributed |
|
2454 |
* before supertype structure is completely known |
|
2455 |
*/ |
|
2456 |
public void visitTypeApply(JCTypeApply tree) { |
|
2457 |
Type owntype = syms.errType; |
|
2458 |
||
2459 |
// Attribute functor part of application and make sure it's a class. |
|
2460 |
Type clazztype = chk.checkClassType(tree.clazz.pos(), attribType(tree.clazz, env)); |
|
2461 |
||
2462 |
// Attribute type parameters |
|
2463 |
List<Type> actuals = attribTypes(tree.arguments, env); |
|
2464 |
||
2465 |
if (clazztype.tag == CLASS) { |
|
2466 |
List<Type> formals = clazztype.tsym.type.getTypeArguments(); |
|
2467 |
||
2468 |
if (actuals.length() == formals.length()) { |
|
2469 |
List<Type> a = actuals; |
|
2470 |
List<Type> f = formals; |
|
2471 |
while (a.nonEmpty()) { |
|
2472 |
a.head = a.head.withTypeVar(f.head); |
|
2473 |
a = a.tail; |
|
2474 |
f = f.tail; |
|
2475 |
} |
|
2476 |
// Compute the proper generic outer |
|
2477 |
Type clazzOuter = clazztype.getEnclosingType(); |
|
2478 |
if (clazzOuter.tag == CLASS) { |
|
2479 |
Type site; |
|
2480 |
if (tree.clazz.getTag() == JCTree.IDENT) { |
|
2481 |
site = env.enclClass.sym.type; |
|
2482 |
} else if (tree.clazz.getTag() == JCTree.SELECT) { |
|
2483 |
site = ((JCFieldAccess) tree.clazz).selected.type; |
|
2484 |
} else throw new AssertionError(""+tree); |
|
2485 |
if (clazzOuter.tag == CLASS && site != clazzOuter) { |
|
2486 |
if (site.tag == CLASS) |
|
2487 |
site = types.asOuterSuper(site, clazzOuter.tsym); |
|
2488 |
if (site == null) |
|
2489 |
site = types.erasure(clazzOuter); |
|
2490 |
clazzOuter = site; |
|
2491 |
} |
|
2492 |
} |
|
2493 |
owntype = new ClassType(clazzOuter, actuals, clazztype.tsym); |
|
2494 |
} else { |
|
2495 |
if (formals.length() != 0) { |
|
2496 |
log.error(tree.pos(), "wrong.number.type.args", |
|
2497 |
Integer.toString(formals.length())); |
|
2498 |
} else { |
|
2499 |
log.error(tree.pos(), "type.doesnt.take.params", clazztype.tsym); |
|
2500 |
} |
|
2501 |
owntype = syms.errType; |
|
2502 |
} |
|
2503 |
} |
|
2504 |
result = check(tree, owntype, TYP, pkind, pt); |
|
2505 |
} |
|
2506 |
||
2507 |
public void visitTypeParameter(JCTypeParameter tree) { |
|
2508 |
TypeVar a = (TypeVar)tree.type; |
|
2509 |
Set<Type> boundSet = new HashSet<Type>(); |
|
2510 |
if (a.bound.isErroneous()) |
|
2511 |
return; |
|
2512 |
List<Type> bs = types.getBounds(a); |
|
2513 |
if (tree.bounds.nonEmpty()) { |
|
2514 |
// accept class or interface or typevar as first bound. |
|
2515 |
Type b = checkBase(bs.head, tree.bounds.head, env, false, false, false); |
|
2516 |
boundSet.add(types.erasure(b)); |
|
2517 |
if (b.tag == TYPEVAR) { |
|
2518 |
// if first bound was a typevar, do not accept further bounds. |
|
2519 |
if (tree.bounds.tail.nonEmpty()) { |
|
2520 |
log.error(tree.bounds.tail.head.pos(), |
|
2521 |
"type.var.may.not.be.followed.by.other.bounds"); |
|
2522 |
tree.bounds = List.of(tree.bounds.head); |
|
164
c1e219636b4e
6608214: Exception throw while analysing a file with error
mcimadamore
parents:
163
diff
changeset
|
2523 |
a.bound = bs.head; |
10 | 2524 |
} |
2525 |
} else { |
|
2526 |
// if first bound was a class or interface, accept only interfaces |
|
2527 |
// as further bounds. |
|
2528 |
for (JCExpression bound : tree.bounds.tail) { |
|
2529 |
bs = bs.tail; |
|
2530 |
Type i = checkBase(bs.head, bound, env, false, true, false); |
|
2531 |
if (i.tag == CLASS) |
|
2532 |
chk.checkNotRepeated(bound.pos(), types.erasure(i), boundSet); |
|
2533 |
} |
|
2534 |
} |
|
2535 |
} |
|
2536 |
bs = types.getBounds(a); |
|
2537 |
||
2538 |
// in case of multiple bounds ... |
|
2539 |
if (bs.length() > 1) { |
|
2540 |
// ... the variable's bound is a class type flagged COMPOUND |
|
2541 |
// (see comment for TypeVar.bound). |
|
2542 |
// In this case, generate a class tree that represents the |
|
2543 |
// bound class, ... |
|
2544 |
JCTree extending; |
|
2545 |
List<JCExpression> implementing; |
|
2546 |
if ((bs.head.tsym.flags() & INTERFACE) == 0) { |
|
2547 |
extending = tree.bounds.head; |
|
2548 |
implementing = tree.bounds.tail; |
|
2549 |
} else { |
|
2550 |
extending = null; |
|
2551 |
implementing = tree.bounds; |
|
2552 |
} |
|
2553 |
JCClassDecl cd = make.at(tree.pos).ClassDef( |
|
2554 |
make.Modifiers(PUBLIC | ABSTRACT), |
|
2555 |
tree.name, List.<JCTypeParameter>nil(), |
|
2556 |
extending, implementing, List.<JCTree>nil()); |
|
2557 |
||
2558 |
ClassSymbol c = (ClassSymbol)a.getUpperBound().tsym; |
|
2559 |
assert (c.flags() & COMPOUND) != 0; |
|
2560 |
cd.sym = c; |
|
2561 |
c.sourcefile = env.toplevel.sourcefile; |
|
2562 |
||
2563 |
// ... and attribute the bound class |
|
2564 |
c.flags_field |= UNATTRIBUTED; |
|
2565 |
Env<AttrContext> cenv = enter.classEnv(cd, env); |
|
2566 |
enter.typeEnvs.put(c, cenv); |
|
2567 |
} |
|
2568 |
} |
|
2569 |
||
2570 |
||
2571 |
public void visitWildcard(JCWildcard tree) { |
|
2572 |
//- System.err.println("visitWildcard("+tree+");");//DEBUG |
|
2573 |
Type type = (tree.kind.kind == BoundKind.UNBOUND) |
|
2574 |
? syms.objectType |
|
2575 |
: attribType(tree.inner, env); |
|
2576 |
result = check(tree, new WildcardType(chk.checkRefType(tree.pos(), type), |
|
2577 |
tree.kind.kind, |
|
2578 |
syms.boundClass), |
|
2579 |
TYP, pkind, pt); |
|
2580 |
} |
|
2581 |
||
2582 |
public void visitAnnotation(JCAnnotation tree) { |
|
2583 |
log.error(tree.pos(), "annotation.not.valid.for.type", pt); |
|
2584 |
result = tree.type = syms.errType; |
|
2585 |
} |
|
2586 |
||
2587 |
public void visitErroneous(JCErroneous tree) { |
|
2588 |
if (tree.errs != null) |
|
2589 |
for (JCTree err : tree.errs) |
|
2590 |
attribTree(err, env, ERR, pt); |
|
2591 |
result = tree.type = syms.errType; |
|
2592 |
} |
|
2593 |
||
2594 |
/** Default visitor method for all other trees. |
|
2595 |
*/ |
|
2596 |
public void visitTree(JCTree tree) { |
|
2597 |
throw new AssertionError(); |
|
2598 |
} |
|
2599 |
||
2600 |
/** Main method: attribute class definition associated with given class symbol. |
|
2601 |
* reporting completion failures at the given position. |
|
2602 |
* @param pos The source position at which completion errors are to be |
|
2603 |
* reported. |
|
2604 |
* @param c The class symbol whose definition will be attributed. |
|
2605 |
*/ |
|
2606 |
public void attribClass(DiagnosticPosition pos, ClassSymbol c) { |
|
2607 |
try { |
|
2608 |
annotate.flush(); |
|
2609 |
attribClass(c); |
|
2610 |
} catch (CompletionFailure ex) { |
|
2611 |
chk.completionError(pos, ex); |
|
2612 |
} |
|
2613 |
} |
|
2614 |
||
2615 |
/** Attribute class definition associated with given class symbol. |
|
2616 |
* @param c The class symbol whose definition will be attributed. |
|
2617 |
*/ |
|
2618 |
void attribClass(ClassSymbol c) throws CompletionFailure { |
|
2619 |
if (c.type.tag == ERROR) return; |
|
2620 |
||
2621 |
// Check for cycles in the inheritance graph, which can arise from |
|
2622 |
// ill-formed class files. |
|
2623 |
chk.checkNonCyclic(null, c.type); |
|
2624 |
||
2625 |
Type st = types.supertype(c.type); |
|
2626 |
if ((c.flags_field & Flags.COMPOUND) == 0) { |
|
2627 |
// First, attribute superclass. |
|
2628 |
if (st.tag == CLASS) |
|
2629 |
attribClass((ClassSymbol)st.tsym); |
|
2630 |
||
2631 |
// Next attribute owner, if it is a class. |
|
2632 |
if (c.owner.kind == TYP && c.owner.type.tag == CLASS) |
|
2633 |
attribClass((ClassSymbol)c.owner); |
|
2634 |
} |
|
2635 |
||
2636 |
// The previous operations might have attributed the current class |
|
2637 |
// if there was a cycle. So we test first whether the class is still |
|
2638 |
// UNATTRIBUTED. |
|
2639 |
if ((c.flags_field & UNATTRIBUTED) != 0) { |
|
2640 |
c.flags_field &= ~UNATTRIBUTED; |
|
2641 |
||
2642 |
// Get environment current at the point of class definition. |
|
2643 |
Env<AttrContext> env = enter.typeEnvs.get(c); |
|
2644 |
||
2645 |
// The info.lint field in the envs stored in enter.typeEnvs is deliberately uninitialized, |
|
2646 |
// because the annotations were not available at the time the env was created. Therefore, |
|
2647 |
// we look up the environment chain for the first enclosing environment for which the |
|
2648 |
// lint value is set. Typically, this is the parent env, but might be further if there |
|
2649 |
// are any envs created as a result of TypeParameter nodes. |
|
2650 |
Env<AttrContext> lintEnv = env; |
|
2651 |
while (lintEnv.info.lint == null) |
|
2652 |
lintEnv = lintEnv.next; |
|
2653 |
||
2654 |
// Having found the enclosing lint value, we can initialize the lint value for this class |
|
2655 |
env.info.lint = lintEnv.info.lint.augment(c.attributes_field, c.flags()); |
|
2656 |
||
2657 |
Lint prevLint = chk.setLint(env.info.lint); |
|
2658 |
JavaFileObject prev = log.useSource(c.sourcefile); |
|
2659 |
||
2660 |
try { |
|
2661 |
// java.lang.Enum may not be subclassed by a non-enum |
|
2662 |
if (st.tsym == syms.enumSym && |
|
2663 |
((c.flags_field & (Flags.ENUM|Flags.COMPOUND)) == 0)) |
|
2664 |
log.error(env.tree.pos(), "enum.no.subclassing"); |
|
2665 |
||
2666 |
// Enums may not be extended by source-level classes |
|
2667 |
if (st.tsym != null && |
|
2668 |
((st.tsym.flags_field & Flags.ENUM) != 0) && |
|
941
2589047a9c5d
6675483: Javac rejects multiple type-variable bound declarations starting with an enum type
mcimadamore
parents:
939
diff
changeset
|
2669 |
((c.flags_field & (Flags.ENUM | Flags.COMPOUND)) == 0) && |
10 | 2670 |
!target.compilerBootstrap(c)) { |
2671 |
log.error(env.tree.pos(), "enum.types.not.extensible"); |
|
2672 |
} |
|
2673 |
attribClassBody(env, c); |
|
2674 |
||
2675 |
chk.checkDeprecatedAnnotation(env.tree.pos(), c); |
|
2676 |
} finally { |
|
2677 |
log.useSource(prev); |
|
2678 |
chk.setLint(prevLint); |
|
2679 |
} |
|
2680 |
||
2681 |
} |
|
2682 |
} |
|
2683 |
||
2684 |
public void visitImport(JCImport tree) { |
|
2685 |
// nothing to do |
|
2686 |
} |
|
2687 |
||
2688 |
/** Finish the attribution of a class. */ |
|
2689 |
private void attribClassBody(Env<AttrContext> env, ClassSymbol c) { |
|
2690 |
JCClassDecl tree = (JCClassDecl)env.tree; |
|
2691 |
assert c == tree.sym; |
|
2692 |
||
2693 |
// Validate annotations |
|
2694 |
chk.validateAnnotations(tree.mods.annotations, c); |
|
2695 |
||
2696 |
// Validate type parameters, supertype and interfaces. |
|
661
9b2f1fe5c183
6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents:
512
diff
changeset
|
2697 |
attribBounds(tree.typarams); |
10 | 2698 |
chk.validateTypeParams(tree.typarams); |
2699 |
chk.validate(tree.extending); |
|
2700 |
chk.validate(tree.implementing); |
|
2701 |
||
2702 |
// If this is a non-abstract class, check that it has no abstract |
|
2703 |
// methods or unimplemented methods of an implemented interface. |
|
2704 |
if ((c.flags() & (ABSTRACT | INTERFACE)) == 0) { |
|
2705 |
if (!relax) |
|
2706 |
chk.checkAllDefined(tree.pos(), c); |
|
2707 |
} |
|
2708 |
||
2709 |
if ((c.flags() & ANNOTATION) != 0) { |
|
2710 |
if (tree.implementing.nonEmpty()) |
|
2711 |
log.error(tree.implementing.head.pos(), |
|
2712 |
"cant.extend.intf.annotation"); |
|
2713 |
if (tree.typarams.nonEmpty()) |
|
2714 |
log.error(tree.typarams.head.pos(), |
|
2715 |
"intf.annotation.cant.have.type.params"); |
|
2716 |
} else { |
|
2717 |
// Check that all extended classes and interfaces |
|
2718 |
// are compatible (i.e. no two define methods with same arguments |
|
2719 |
// yet different return types). (JLS 8.4.6.3) |
|
2720 |
chk.checkCompatibleSupertypes(tree.pos(), c.type); |
|
2721 |
} |
|
2722 |
||
2723 |
// Check that class does not import the same parameterized interface |
|
2724 |
// with two different argument lists. |
|
2725 |
chk.checkClassBounds(tree.pos(), c.type); |
|
2726 |
||
2727 |
tree.type = c.type; |
|
2728 |
||
2729 |
boolean assertsEnabled = false; |
|
2730 |
assert assertsEnabled = true; |
|
2731 |
if (assertsEnabled) { |
|
2732 |
for (List<JCTypeParameter> l = tree.typarams; |
|
2733 |
l.nonEmpty(); l = l.tail) |
|
2734 |
assert env.info.scope.lookup(l.head.name).scope != null; |
|
2735 |
} |
|
2736 |
||
2737 |
// Check that a generic class doesn't extend Throwable |
|
2738 |
if (!c.type.allparams().isEmpty() && types.isSubtype(c.type, syms.throwableType)) |
|
2739 |
log.error(tree.extending.pos(), "generic.throwable"); |
|
2740 |
||
2741 |
// Check that all methods which implement some |
|
2742 |
// method conform to the method they implement. |
|
2743 |
chk.checkImplementations(tree); |
|
2744 |
||
2745 |
for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) { |
|
2746 |
// Attribute declaration |
|
2747 |
attribStat(l.head, env); |
|
2748 |
// Check that declarations in inner classes are not static (JLS 8.1.2) |
|
2749 |
// Make an exception for static constants. |
|
2750 |
if (c.owner.kind != PCK && |
|
2751 |
((c.flags() & STATIC) == 0 || c.name == names.empty) && |
|
2752 |
(TreeInfo.flags(l.head) & (STATIC | INTERFACE)) != 0) { |
|
2753 |
Symbol sym = null; |
|
2754 |
if (l.head.getTag() == JCTree.VARDEF) sym = ((JCVariableDecl) l.head).sym; |
|
2755 |
if (sym == null || |
|
2756 |
sym.kind != VAR || |
|
2757 |
((VarSymbol) sym).getConstValue() == null) |
|
2758 |
log.error(l.head.pos(), "icls.cant.have.static.decl"); |
|
2759 |
} |
|
2760 |
} |
|
2761 |
||
2762 |
// Check for cycles among non-initial constructors. |
|
2763 |
chk.checkCyclicConstructors(tree); |
|
2764 |
||
2765 |
// Check for cycles among annotation elements. |
|
2766 |
chk.checkNonCyclicElements(tree); |
|
2767 |
||
2768 |
// Check for proper use of serialVersionUID |
|
2769 |
if (env.info.lint.isEnabled(Lint.LintCategory.SERIAL) && |
|
2770 |
isSerializable(c) && |
|
2771 |
(c.flags() & Flags.ENUM) == 0 && |
|
2772 |
(c.flags() & ABSTRACT) == 0) { |
|
2773 |
checkSerialVersionUID(tree, c); |
|
2774 |
} |
|
2775 |
} |
|
2776 |
// where |
|
2777 |
/** check if a class is a subtype of Serializable, if that is available. */ |
|
2778 |
private boolean isSerializable(ClassSymbol c) { |
|
2779 |
try { |
|
2780 |
syms.serializableType.complete(); |
|
2781 |
} |
|
2782 |
catch (CompletionFailure e) { |
|
2783 |
return false; |
|
2784 |
} |
|
2785 |
return types.isSubtype(c.type, syms.serializableType); |
|
2786 |
} |
|
2787 |
||
2788 |
/** Check that an appropriate serialVersionUID member is defined. */ |
|
2789 |
private void checkSerialVersionUID(JCClassDecl tree, ClassSymbol c) { |
|
2790 |
||
2791 |
// check for presence of serialVersionUID |
|
2792 |
Scope.Entry e = c.members().lookup(names.serialVersionUID); |
|
2793 |
while (e.scope != null && e.sym.kind != VAR) e = e.next(); |
|
2794 |
if (e.scope == null) { |
|
2795 |
log.warning(tree.pos(), "missing.SVUID", c); |
|
2796 |
return; |
|
2797 |
} |
|
2798 |
||
2799 |
// check that it is static final |
|
2800 |
VarSymbol svuid = (VarSymbol)e.sym; |
|
2801 |
if ((svuid.flags() & (STATIC | FINAL)) != |
|
2802 |
(STATIC | FINAL)) |
|
2803 |
log.warning(TreeInfo.diagnosticPositionFor(svuid, tree), "improper.SVUID", c); |
|
2804 |
||
2805 |
// check that it is long |
|
2806 |
else if (svuid.type.tag != TypeTags.LONG) |
|
2807 |
log.warning(TreeInfo.diagnosticPositionFor(svuid, tree), "long.SVUID", c); |
|
2808 |
||
2809 |
// check constant |
|
2810 |
else if (svuid.getConstValue() == null) |
|
2811 |
log.warning(TreeInfo.diagnosticPositionFor(svuid, tree), "constant.SVUID", c); |
|
2812 |
} |
|
2813 |
||
2814 |
private Type capture(Type type) { |
|
2815 |
return types.capture(type); |
|
2816 |
} |
|
2817 |
} |