author | mcimadamore |
Mon, 29 Sep 2008 12:00:29 +0100 | |
changeset 1358 | a51c5f89f8af |
parent 1260 | a772ba9ba43d |
child 1527 | 815e743a83ba |
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 |
||
31 |
import com.sun.tools.javac.code.*; |
|
32 |
import com.sun.tools.javac.jvm.*; |
|
33 |
import com.sun.tools.javac.tree.*; |
|
34 |
import com.sun.tools.javac.util.*; |
|
35 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
|
36 |
import com.sun.tools.javac.util.List; |
|
37 |
||
38 |
import com.sun.tools.javac.tree.JCTree.*; |
|
39 |
import com.sun.tools.javac.code.Lint; |
|
40 |
import com.sun.tools.javac.code.Lint.LintCategory; |
|
41 |
import com.sun.tools.javac.code.Type.*; |
|
42 |
import com.sun.tools.javac.code.Symbol.*; |
|
43 |
||
44 |
import static com.sun.tools.javac.code.Flags.*; |
|
45 |
import static com.sun.tools.javac.code.Kinds.*; |
|
46 |
import static com.sun.tools.javac.code.TypeTags.*; |
|
47 |
||
48 |
/** Type checking helper class for the attribution phase. |
|
49 |
* |
|
50 |
* <p><b>This is NOT part of any API supported by Sun Microsystems. If |
|
51 |
* you write code that depends on this, you do so at your own risk. |
|
52 |
* This code and its internal interfaces are subject to change or |
|
53 |
* deletion without notice.</b> |
|
54 |
*/ |
|
55 |
public class Check { |
|
56 |
protected static final Context.Key<Check> checkKey = |
|
57 |
new Context.Key<Check>(); |
|
58 |
||
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1257
diff
changeset
|
59 |
private final Names names; |
10 | 60 |
private final Log log; |
61 |
private final Symtab syms; |
|
62 |
private final Infer infer; |
|
63 |
private final Target target; |
|
64 |
private final Source source; |
|
65 |
private final Types types; |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
66 |
private final JCDiagnostic.Factory diags; |
10 | 67 |
private final boolean skipAnnotations; |
68 |
private final TreeInfo treeinfo; |
|
69 |
||
70 |
// The set of lint options currently in effect. It is initialized |
|
71 |
// from the context, and then is set/reset as needed by Attr as it |
|
72 |
// visits all the various parts of the trees during attribution. |
|
73 |
private Lint lint; |
|
74 |
||
75 |
public static Check instance(Context context) { |
|
76 |
Check instance = context.get(checkKey); |
|
77 |
if (instance == null) |
|
78 |
instance = new Check(context); |
|
79 |
return instance; |
|
80 |
} |
|
81 |
||
82 |
protected Check(Context context) { |
|
83 |
context.put(checkKey, this); |
|
84 |
||
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1257
diff
changeset
|
85 |
names = Names.instance(context); |
10 | 86 |
log = Log.instance(context); |
87 |
syms = Symtab.instance(context); |
|
88 |
infer = Infer.instance(context); |
|
89 |
this.types = Types.instance(context); |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
90 |
diags = JCDiagnostic.Factory.instance(context); |
10 | 91 |
Options options = Options.instance(context); |
92 |
target = Target.instance(context); |
|
93 |
source = Source.instance(context); |
|
94 |
lint = Lint.instance(context); |
|
95 |
treeinfo = TreeInfo.instance(context); |
|
96 |
||
97 |
Source source = Source.instance(context); |
|
98 |
allowGenerics = source.allowGenerics(); |
|
99 |
allowAnnotations = source.allowAnnotations(); |
|
100 |
complexInference = options.get("-complexinference") != null; |
|
101 |
skipAnnotations = options.get("skipAnnotations") != null; |
|
102 |
||
103 |
boolean verboseDeprecated = lint.isEnabled(LintCategory.DEPRECATION); |
|
104 |
boolean verboseUnchecked = lint.isEnabled(LintCategory.UNCHECKED); |
|
813 | 105 |
boolean enforceMandatoryWarnings = source.enforceMandatoryWarnings(); |
10 | 106 |
|
813 | 107 |
deprecationHandler = new MandatoryWarningHandler(log, verboseDeprecated, |
108 |
enforceMandatoryWarnings, "deprecated"); |
|
109 |
uncheckedHandler = new MandatoryWarningHandler(log, verboseUnchecked, |
|
110 |
enforceMandatoryWarnings, "unchecked"); |
|
10 | 111 |
} |
112 |
||
113 |
/** Switch: generics enabled? |
|
114 |
*/ |
|
115 |
boolean allowGenerics; |
|
116 |
||
117 |
/** Switch: annotations enabled? |
|
118 |
*/ |
|
119 |
boolean allowAnnotations; |
|
120 |
||
121 |
/** Switch: -complexinference option set? |
|
122 |
*/ |
|
123 |
boolean complexInference; |
|
124 |
||
125 |
/** A table mapping flat names of all compiled classes in this run to their |
|
126 |
* symbols; maintained from outside. |
|
127 |
*/ |
|
128 |
public Map<Name,ClassSymbol> compiled = new HashMap<Name, ClassSymbol>(); |
|
129 |
||
130 |
/** A handler for messages about deprecated usage. |
|
131 |
*/ |
|
132 |
private MandatoryWarningHandler deprecationHandler; |
|
133 |
||
134 |
/** A handler for messages about unchecked or unsafe usage. |
|
135 |
*/ |
|
136 |
private MandatoryWarningHandler uncheckedHandler; |
|
137 |
||
138 |
||
139 |
/* ************************************************************************* |
|
140 |
* Errors and Warnings |
|
141 |
**************************************************************************/ |
|
142 |
||
143 |
Lint setLint(Lint newLint) { |
|
144 |
Lint prev = lint; |
|
145 |
lint = newLint; |
|
146 |
return prev; |
|
147 |
} |
|
148 |
||
149 |
/** Warn about deprecated symbol. |
|
150 |
* @param pos Position to be used for error reporting. |
|
151 |
* @param sym The deprecated symbol. |
|
152 |
*/ |
|
153 |
void warnDeprecated(DiagnosticPosition pos, Symbol sym) { |
|
154 |
if (!lint.isSuppressed(LintCategory.DEPRECATION)) |
|
155 |
deprecationHandler.report(pos, "has.been.deprecated", sym, sym.location()); |
|
156 |
} |
|
157 |
||
158 |
/** Warn about unchecked operation. |
|
159 |
* @param pos Position to be used for error reporting. |
|
160 |
* @param msg A string describing the problem. |
|
161 |
*/ |
|
162 |
public void warnUnchecked(DiagnosticPosition pos, String msg, Object... args) { |
|
163 |
if (!lint.isSuppressed(LintCategory.UNCHECKED)) |
|
164 |
uncheckedHandler.report(pos, msg, args); |
|
165 |
} |
|
166 |
||
167 |
/** |
|
168 |
* Report any deferred diagnostics. |
|
169 |
*/ |
|
170 |
public void reportDeferredDiagnostics() { |
|
171 |
deprecationHandler.reportDeferredDiagnostic(); |
|
172 |
uncheckedHandler.reportDeferredDiagnostic(); |
|
173 |
} |
|
174 |
||
175 |
||
176 |
/** Report a failure to complete a class. |
|
177 |
* @param pos Position to be used for error reporting. |
|
178 |
* @param ex The failure to report. |
|
179 |
*/ |
|
180 |
public Type completionError(DiagnosticPosition pos, CompletionFailure ex) { |
|
169 | 181 |
log.error(pos, "cant.access", ex.sym, ex.getDetailValue()); |
10 | 182 |
if (ex instanceof ClassReader.BadClassFile) throw new Abort(); |
183 |
else return syms.errType; |
|
184 |
} |
|
185 |
||
186 |
/** Report a type error. |
|
187 |
* @param pos Position to be used for error reporting. |
|
188 |
* @param problem A string describing the error. |
|
189 |
* @param found The type that was found. |
|
190 |
* @param req The type that was required. |
|
191 |
*/ |
|
192 |
Type typeError(DiagnosticPosition pos, Object problem, Type found, Type req) { |
|
193 |
log.error(pos, "prob.found.req", |
|
194 |
problem, found, req); |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1040
diff
changeset
|
195 |
return types.createErrorType(found); |
10 | 196 |
} |
197 |
||
198 |
Type typeError(DiagnosticPosition pos, String problem, Type found, Type req, Object explanation) { |
|
199 |
log.error(pos, "prob.found.req.1", problem, found, req, explanation); |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1040
diff
changeset
|
200 |
return types.createErrorType(found); |
10 | 201 |
} |
202 |
||
203 |
/** Report an error that wrong type tag was found. |
|
204 |
* @param pos Position to be used for error reporting. |
|
205 |
* @param required An internationalized string describing the type tag |
|
206 |
* required. |
|
207 |
* @param found The type that was found. |
|
208 |
*/ |
|
209 |
Type typeTagError(DiagnosticPosition pos, Object required, Object found) { |
|
210 |
log.error(pos, "type.found.req", found, required); |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1040
diff
changeset
|
211 |
return types.createErrorType(found instanceof Type ? (Type)found : syms.errType); |
10 | 212 |
} |
213 |
||
214 |
/** Report an error that symbol cannot be referenced before super |
|
215 |
* has been called. |
|
216 |
* @param pos Position to be used for error reporting. |
|
217 |
* @param sym The referenced symbol. |
|
218 |
*/ |
|
219 |
void earlyRefError(DiagnosticPosition pos, Symbol sym) { |
|
220 |
log.error(pos, "cant.ref.before.ctor.called", sym); |
|
221 |
} |
|
222 |
||
223 |
/** Report duplicate declaration error. |
|
224 |
*/ |
|
225 |
void duplicateError(DiagnosticPosition pos, Symbol sym) { |
|
226 |
if (!sym.type.isErroneous()) { |
|
227 |
log.error(pos, "already.defined", sym, sym.location()); |
|
228 |
} |
|
229 |
} |
|
230 |
||
231 |
/** Report array/varargs duplicate declaration |
|
232 |
*/ |
|
233 |
void varargsDuplicateError(DiagnosticPosition pos, Symbol sym1, Symbol sym2) { |
|
234 |
if (!sym1.type.isErroneous() && !sym2.type.isErroneous()) { |
|
235 |
log.error(pos, "array.and.varargs", sym1, sym2, sym2.location()); |
|
236 |
} |
|
237 |
} |
|
238 |
||
239 |
/* ************************************************************************ |
|
240 |
* duplicate declaration checking |
|
241 |
*************************************************************************/ |
|
242 |
||
243 |
/** Check that variable does not hide variable with same name in |
|
244 |
* immediately enclosing local scope. |
|
245 |
* @param pos Position for error reporting. |
|
246 |
* @param v The symbol. |
|
247 |
* @param s The scope. |
|
248 |
*/ |
|
249 |
void checkTransparentVar(DiagnosticPosition pos, VarSymbol v, Scope s) { |
|
250 |
if (s.next != null) { |
|
251 |
for (Scope.Entry e = s.next.lookup(v.name); |
|
252 |
e.scope != null && e.sym.owner == v.owner; |
|
253 |
e = e.next()) { |
|
254 |
if (e.sym.kind == VAR && |
|
255 |
(e.sym.owner.kind & (VAR | MTH)) != 0 && |
|
256 |
v.name != names.error) { |
|
257 |
duplicateError(pos, e.sym); |
|
258 |
return; |
|
259 |
} |
|
260 |
} |
|
261 |
} |
|
262 |
} |
|
263 |
||
264 |
/** Check that a class or interface does not hide a class or |
|
265 |
* interface with same name in immediately enclosing local scope. |
|
266 |
* @param pos Position for error reporting. |
|
267 |
* @param c The symbol. |
|
268 |
* @param s The scope. |
|
269 |
*/ |
|
270 |
void checkTransparentClass(DiagnosticPosition pos, ClassSymbol c, Scope s) { |
|
271 |
if (s.next != null) { |
|
272 |
for (Scope.Entry e = s.next.lookup(c.name); |
|
273 |
e.scope != null && e.sym.owner == c.owner; |
|
274 |
e = e.next()) { |
|
275 |
if (e.sym.kind == TYP && |
|
276 |
(e.sym.owner.kind & (VAR | MTH)) != 0 && |
|
277 |
c.name != names.error) { |
|
278 |
duplicateError(pos, e.sym); |
|
279 |
return; |
|
280 |
} |
|
281 |
} |
|
282 |
} |
|
283 |
} |
|
284 |
||
285 |
/** Check that class does not have the same name as one of |
|
286 |
* its enclosing classes, or as a class defined in its enclosing scope. |
|
287 |
* return true if class is unique in its enclosing scope. |
|
288 |
* @param pos Position for error reporting. |
|
289 |
* @param name The class name. |
|
290 |
* @param s The enclosing scope. |
|
291 |
*/ |
|
292 |
boolean checkUniqueClassName(DiagnosticPosition pos, Name name, Scope s) { |
|
293 |
for (Scope.Entry e = s.lookup(name); e.scope == s; e = e.next()) { |
|
294 |
if (e.sym.kind == TYP && e.sym.name != names.error) { |
|
295 |
duplicateError(pos, e.sym); |
|
296 |
return false; |
|
297 |
} |
|
298 |
} |
|
299 |
for (Symbol sym = s.owner; sym != null; sym = sym.owner) { |
|
300 |
if (sym.kind == TYP && sym.name == name && sym.name != names.error) { |
|
301 |
duplicateError(pos, sym); |
|
302 |
return true; |
|
303 |
} |
|
304 |
} |
|
305 |
return true; |
|
306 |
} |
|
307 |
||
308 |
/* ************************************************************************* |
|
309 |
* Class name generation |
|
310 |
**************************************************************************/ |
|
311 |
||
312 |
/** Return name of local class. |
|
313 |
* This is of the form <enclClass> $ n <classname> |
|
314 |
* where |
|
315 |
* enclClass is the flat name of the enclosing class, |
|
316 |
* classname is the simple name of the local class |
|
317 |
*/ |
|
318 |
Name localClassName(ClassSymbol c) { |
|
319 |
for (int i=1; ; i++) { |
|
320 |
Name flatname = names. |
|
321 |
fromString("" + c.owner.enclClass().flatname + |
|
322 |
target.syntheticNameChar() + i + |
|
323 |
c.name); |
|
324 |
if (compiled.get(flatname) == null) return flatname; |
|
325 |
} |
|
326 |
} |
|
327 |
||
328 |
/* ************************************************************************* |
|
329 |
* Type Checking |
|
330 |
**************************************************************************/ |
|
331 |
||
332 |
/** Check that a given type is assignable to a given proto-type. |
|
333 |
* If it is, return the type, otherwise return errType. |
|
334 |
* @param pos Position to be used for error reporting. |
|
335 |
* @param found The type that was found. |
|
336 |
* @param req The type that was required. |
|
337 |
*/ |
|
338 |
Type checkType(DiagnosticPosition pos, Type found, Type req) { |
|
339 |
if (req.tag == ERROR) |
|
340 |
return req; |
|
341 |
if (found.tag == FORALL) |
|
342 |
return instantiatePoly(pos, (ForAll)found, req, convertWarner(pos, found, req)); |
|
343 |
if (req.tag == NONE) |
|
344 |
return found; |
|
345 |
if (types.isAssignable(found, req, convertWarner(pos, found, req))) |
|
346 |
return found; |
|
347 |
if (found.tag <= DOUBLE && req.tag <= DOUBLE) |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
348 |
return typeError(pos, diags.fragment("possible.loss.of.precision"), found, req); |
10 | 349 |
if (found.isSuperBound()) { |
350 |
log.error(pos, "assignment.from.super-bound", found); |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1040
diff
changeset
|
351 |
return types.createErrorType(found); |
10 | 352 |
} |
353 |
if (req.isExtendsBound()) { |
|
354 |
log.error(pos, "assignment.to.extends-bound", req); |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1040
diff
changeset
|
355 |
return types.createErrorType(found); |
10 | 356 |
} |
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
357 |
return typeError(pos, diags.fragment("incompatible.types"), found, req); |
10 | 358 |
} |
359 |
||
360 |
/** Instantiate polymorphic type to some prototype, unless |
|
361 |
* prototype is `anyPoly' in which case polymorphic type |
|
362 |
* is returned unchanged. |
|
363 |
*/ |
|
364 |
Type instantiatePoly(DiagnosticPosition pos, ForAll t, Type pt, Warner warn) { |
|
365 |
if (pt == Infer.anyPoly && complexInference) { |
|
366 |
return t; |
|
367 |
} else if (pt == Infer.anyPoly || pt.tag == NONE) { |
|
368 |
Type newpt = t.qtype.tag <= VOID ? t.qtype : syms.objectType; |
|
369 |
return instantiatePoly(pos, t, newpt, warn); |
|
370 |
} else if (pt.tag == ERROR) { |
|
371 |
return pt; |
|
372 |
} else { |
|
373 |
try { |
|
374 |
return infer.instantiateExpr(t, pt, warn); |
|
375 |
} catch (Infer.NoInstanceException ex) { |
|
376 |
if (ex.isAmbiguous) { |
|
377 |
JCDiagnostic d = ex.getDiagnostic(); |
|
378 |
log.error(pos, |
|
379 |
"undetermined.type" + (d!=null ? ".1" : ""), |
|
380 |
t, d); |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1040
diff
changeset
|
381 |
return types.createErrorType(pt); |
10 | 382 |
} else { |
383 |
JCDiagnostic d = ex.getDiagnostic(); |
|
384 |
return typeError(pos, |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
385 |
diags.fragment("incompatible.types" + (d!=null ? ".1" : ""), d), |
10 | 386 |
t, pt); |
387 |
} |
|
388 |
} |
|
389 |
} |
|
390 |
} |
|
391 |
||
392 |
/** Check that a given type can be cast to a given target type. |
|
393 |
* Return the result of the cast. |
|
394 |
* @param pos Position to be used for error reporting. |
|
395 |
* @param found The type that is being cast. |
|
396 |
* @param req The target type of the cast. |
|
397 |
*/ |
|
398 |
Type checkCastable(DiagnosticPosition pos, Type found, Type req) { |
|
399 |
if (found.tag == FORALL) { |
|
400 |
instantiatePoly(pos, (ForAll) found, req, castWarner(pos, found, req)); |
|
401 |
return req; |
|
402 |
} else if (types.isCastable(found, req, castWarner(pos, found, req))) { |
|
403 |
return req; |
|
404 |
} else { |
|
405 |
return typeError(pos, |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
406 |
diags.fragment("inconvertible.types"), |
10 | 407 |
found, req); |
408 |
} |
|
409 |
} |
|
410 |
//where |
|
411 |
/** Is type a type variable, or a (possibly multi-dimensional) array of |
|
412 |
* type variables? |
|
413 |
*/ |
|
414 |
boolean isTypeVar(Type t) { |
|
415 |
return t.tag == TYPEVAR || t.tag == ARRAY && isTypeVar(types.elemtype(t)); |
|
416 |
} |
|
417 |
||
418 |
/** Check that a type is within some bounds. |
|
419 |
* |
|
420 |
* Used in TypeApply to verify that, e.g., X in V<X> is a valid |
|
421 |
* type argument. |
|
422 |
* @param pos Position to be used for error reporting. |
|
423 |
* @param a The type that should be bounded by bs. |
|
424 |
* @param bs The bound. |
|
425 |
*/ |
|
426 |
private void checkExtends(DiagnosticPosition pos, Type a, TypeVar bs) { |
|
938
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
427 |
if (a.tag == TYPEVAR && ((TypeVar)a).isCaptured()) { |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
428 |
CapturedType ct = (CapturedType)a; |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
429 |
boolean ok; |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
430 |
if (ct.bound.isErroneous()) {//capture doesn't exist |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
431 |
ok = false; |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
432 |
} |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
433 |
else { |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
434 |
switch (ct.wildcard.kind) { |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
435 |
case EXTENDS: |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
436 |
ok = types.isCastable(bs.getUpperBound(), |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
437 |
types.upperBound(a), |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
438 |
Warner.noWarnings); |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
439 |
break; |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
440 |
case SUPER: |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
441 |
ok = !types.notSoftSubtype(types.lowerBound(a), |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
442 |
bs.getUpperBound()); |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
443 |
break; |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
444 |
case UNBOUND: |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
445 |
ok = true; |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
446 |
break; |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
447 |
default: |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
448 |
throw new AssertionError("Invalid bound kind"); |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
449 |
} |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
450 |
} |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
451 |
if (!ok) |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
452 |
log.error(pos, "not.within.bounds", a); |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
453 |
} |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
454 |
else { |
10 | 455 |
a = types.upperBound(a); |
456 |
for (List<Type> l = types.getBounds(bs); l.nonEmpty(); l = l.tail) { |
|
457 |
if (!types.isSubtype(a, l.head)) { |
|
458 |
log.error(pos, "not.within.bounds", a); |
|
459 |
return; |
|
460 |
} |
|
461 |
} |
|
937
457a11ae2e84
6651719: Compiler crashes possibly during forward reference of TypeParameter
mcimadamore
parents:
815
diff
changeset
|
462 |
} |
10 | 463 |
} |
464 |
||
465 |
/** Check that type is different from 'void'. |
|
466 |
* @param pos Position to be used for error reporting. |
|
467 |
* @param t The type to be checked. |
|
468 |
*/ |
|
469 |
Type checkNonVoid(DiagnosticPosition pos, Type t) { |
|
470 |
if (t.tag == VOID) { |
|
471 |
log.error(pos, "void.not.allowed.here"); |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1040
diff
changeset
|
472 |
return types.createErrorType(t); |
10 | 473 |
} else { |
474 |
return t; |
|
475 |
} |
|
476 |
} |
|
477 |
||
478 |
/** Check that type is a class or interface type. |
|
479 |
* @param pos Position to be used for error reporting. |
|
480 |
* @param t The type to be checked. |
|
481 |
*/ |
|
482 |
Type checkClassType(DiagnosticPosition pos, Type t) { |
|
483 |
if (t.tag != CLASS && t.tag != ERROR) |
|
484 |
return typeTagError(pos, |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
485 |
diags.fragment("type.req.class"), |
10 | 486 |
(t.tag == TYPEVAR) |
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
487 |
? diags.fragment("type.parameter", t) |
10 | 488 |
: t); |
489 |
else |
|
490 |
return t; |
|
491 |
} |
|
492 |
||
493 |
/** Check that type is a class or interface type. |
|
494 |
* @param pos Position to be used for error reporting. |
|
495 |
* @param t The type to be checked. |
|
496 |
* @param noBounds True if type bounds are illegal here. |
|
497 |
*/ |
|
498 |
Type checkClassType(DiagnosticPosition pos, Type t, boolean noBounds) { |
|
499 |
t = checkClassType(pos, t); |
|
500 |
if (noBounds && t.isParameterized()) { |
|
501 |
List<Type> args = t.getTypeArguments(); |
|
502 |
while (args.nonEmpty()) { |
|
503 |
if (args.head.tag == WILDCARD) |
|
504 |
return typeTagError(pos, |
|
505 |
log.getLocalizedString("type.req.exact"), |
|
506 |
args.head); |
|
507 |
args = args.tail; |
|
508 |
} |
|
509 |
} |
|
510 |
return t; |
|
511 |
} |
|
512 |
||
513 |
/** Check that type is a reifiable class, interface or array type. |
|
514 |
* @param pos Position to be used for error reporting. |
|
515 |
* @param t The type to be checked. |
|
516 |
*/ |
|
517 |
Type checkReifiableReferenceType(DiagnosticPosition pos, Type t) { |
|
518 |
if (t.tag != CLASS && t.tag != ARRAY && t.tag != ERROR) { |
|
519 |
return typeTagError(pos, |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
520 |
diags.fragment("type.req.class.array"), |
10 | 521 |
t); |
522 |
} else if (!types.isReifiable(t)) { |
|
523 |
log.error(pos, "illegal.generic.type.for.instof"); |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1040
diff
changeset
|
524 |
return types.createErrorType(t); |
10 | 525 |
} else { |
526 |
return t; |
|
527 |
} |
|
528 |
} |
|
529 |
||
530 |
/** Check that type is a reference type, i.e. a class, interface or array type |
|
531 |
* or a type variable. |
|
532 |
* @param pos Position to be used for error reporting. |
|
533 |
* @param t The type to be checked. |
|
534 |
*/ |
|
535 |
Type checkRefType(DiagnosticPosition pos, Type t) { |
|
536 |
switch (t.tag) { |
|
537 |
case CLASS: |
|
538 |
case ARRAY: |
|
539 |
case TYPEVAR: |
|
540 |
case WILDCARD: |
|
541 |
case ERROR: |
|
542 |
return t; |
|
543 |
default: |
|
544 |
return typeTagError(pos, |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
545 |
diags.fragment("type.req.ref"), |
10 | 546 |
t); |
547 |
} |
|
548 |
} |
|
549 |
||
550 |
/** Check that type is a null or reference type. |
|
551 |
* @param pos Position to be used for error reporting. |
|
552 |
* @param t The type to be checked. |
|
553 |
*/ |
|
554 |
Type checkNullOrRefType(DiagnosticPosition pos, Type t) { |
|
555 |
switch (t.tag) { |
|
556 |
case CLASS: |
|
557 |
case ARRAY: |
|
558 |
case TYPEVAR: |
|
559 |
case WILDCARD: |
|
560 |
case BOT: |
|
561 |
case ERROR: |
|
562 |
return t; |
|
563 |
default: |
|
564 |
return typeTagError(pos, |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
565 |
diags.fragment("type.req.ref"), |
10 | 566 |
t); |
567 |
} |
|
568 |
} |
|
569 |
||
570 |
/** Check that flag set does not contain elements of two conflicting sets. s |
|
571 |
* Return true if it doesn't. |
|
572 |
* @param pos Position to be used for error reporting. |
|
573 |
* @param flags The set of flags to be checked. |
|
574 |
* @param set1 Conflicting flags set #1. |
|
575 |
* @param set2 Conflicting flags set #2. |
|
576 |
*/ |
|
577 |
boolean checkDisjoint(DiagnosticPosition pos, long flags, long set1, long set2) { |
|
578 |
if ((flags & set1) != 0 && (flags & set2) != 0) { |
|
579 |
log.error(pos, |
|
580 |
"illegal.combination.of.modifiers", |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
938
diff
changeset
|
581 |
asFlagSet(TreeInfo.firstFlag(flags & set1)), |
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
938
diff
changeset
|
582 |
asFlagSet(TreeInfo.firstFlag(flags & set2))); |
10 | 583 |
return false; |
584 |
} else |
|
585 |
return true; |
|
586 |
} |
|
587 |
||
588 |
/** Check that given modifiers are legal for given symbol and |
|
589 |
* return modifiers together with any implicit modififiers for that symbol. |
|
590 |
* Warning: we can't use flags() here since this method |
|
591 |
* is called during class enter, when flags() would cause a premature |
|
592 |
* completion. |
|
593 |
* @param pos Position to be used for error reporting. |
|
594 |
* @param flags The set of modifiers given in a definition. |
|
595 |
* @param sym The defined symbol. |
|
596 |
*/ |
|
597 |
long checkFlags(DiagnosticPosition pos, long flags, Symbol sym, JCTree tree) { |
|
598 |
long mask; |
|
599 |
long implicit = 0; |
|
600 |
switch (sym.kind) { |
|
601 |
case VAR: |
|
602 |
if (sym.owner.kind != TYP) |
|
603 |
mask = LocalVarFlags; |
|
604 |
else if ((sym.owner.flags_field & INTERFACE) != 0) |
|
605 |
mask = implicit = InterfaceVarFlags; |
|
606 |
else |
|
607 |
mask = VarFlags; |
|
608 |
break; |
|
609 |
case MTH: |
|
610 |
if (sym.name == names.init) { |
|
611 |
if ((sym.owner.flags_field & ENUM) != 0) { |
|
612 |
// enum constructors cannot be declared public or |
|
613 |
// protected and must be implicitly or explicitly |
|
614 |
// private |
|
615 |
implicit = PRIVATE; |
|
616 |
mask = PRIVATE; |
|
617 |
} else |
|
618 |
mask = ConstructorFlags; |
|
619 |
} else if ((sym.owner.flags_field & INTERFACE) != 0) |
|
620 |
mask = implicit = InterfaceMethodFlags; |
|
621 |
else { |
|
622 |
mask = MethodFlags; |
|
623 |
} |
|
624 |
// Imply STRICTFP if owner has STRICTFP set. |
|
625 |
if (((flags|implicit) & Flags.ABSTRACT) == 0) |
|
626 |
implicit |= sym.owner.flags_field & STRICTFP; |
|
627 |
break; |
|
628 |
case TYP: |
|
629 |
if (sym.isLocal()) { |
|
630 |
mask = LocalClassFlags; |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1257
diff
changeset
|
631 |
if (sym.name.isEmpty()) { // Anonymous class |
10 | 632 |
// Anonymous classes in static methods are themselves static; |
633 |
// that's why we admit STATIC here. |
|
634 |
mask |= STATIC; |
|
635 |
// JLS: Anonymous classes are final. |
|
636 |
implicit |= FINAL; |
|
637 |
} |
|
638 |
if ((sym.owner.flags_field & STATIC) == 0 && |
|
639 |
(flags & ENUM) != 0) |
|
640 |
log.error(pos, "enums.must.be.static"); |
|
641 |
} else if (sym.owner.kind == TYP) { |
|
642 |
mask = MemberClassFlags; |
|
643 |
if (sym.owner.owner.kind == PCK || |
|
644 |
(sym.owner.flags_field & STATIC) != 0) |
|
645 |
mask |= STATIC; |
|
646 |
else if ((flags & ENUM) != 0) |
|
647 |
log.error(pos, "enums.must.be.static"); |
|
648 |
// Nested interfaces and enums are always STATIC (Spec ???) |
|
649 |
if ((flags & (INTERFACE | ENUM)) != 0 ) implicit = STATIC; |
|
650 |
} else { |
|
651 |
mask = ClassFlags; |
|
652 |
} |
|
653 |
// Interfaces are always ABSTRACT |
|
654 |
if ((flags & INTERFACE) != 0) implicit |= ABSTRACT; |
|
655 |
||
656 |
if ((flags & ENUM) != 0) { |
|
657 |
// enums can't be declared abstract or final |
|
658 |
mask &= ~(ABSTRACT | FINAL); |
|
659 |
implicit |= implicitEnumFinalFlag(tree); |
|
660 |
} |
|
661 |
// Imply STRICTFP if owner has STRICTFP set. |
|
662 |
implicit |= sym.owner.flags_field & STRICTFP; |
|
663 |
break; |
|
664 |
default: |
|
665 |
throw new AssertionError(); |
|
666 |
} |
|
667 |
long illegal = flags & StandardFlags & ~mask; |
|
668 |
if (illegal != 0) { |
|
669 |
if ((illegal & INTERFACE) != 0) { |
|
670 |
log.error(pos, "intf.not.allowed.here"); |
|
671 |
mask |= INTERFACE; |
|
672 |
} |
|
673 |
else { |
|
674 |
log.error(pos, |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
938
diff
changeset
|
675 |
"mod.not.allowed.here", asFlagSet(illegal)); |
10 | 676 |
} |
677 |
} |
|
678 |
else if ((sym.kind == TYP || |
|
679 |
// ISSUE: Disallowing abstract&private is no longer appropriate |
|
680 |
// in the presence of inner classes. Should it be deleted here? |
|
681 |
checkDisjoint(pos, flags, |
|
682 |
ABSTRACT, |
|
683 |
PRIVATE | STATIC)) |
|
684 |
&& |
|
685 |
checkDisjoint(pos, flags, |
|
686 |
ABSTRACT | INTERFACE, |
|
687 |
FINAL | NATIVE | SYNCHRONIZED) |
|
688 |
&& |
|
689 |
checkDisjoint(pos, flags, |
|
690 |
PUBLIC, |
|
691 |
PRIVATE | PROTECTED) |
|
692 |
&& |
|
693 |
checkDisjoint(pos, flags, |
|
694 |
PRIVATE, |
|
695 |
PUBLIC | PROTECTED) |
|
696 |
&& |
|
697 |
checkDisjoint(pos, flags, |
|
698 |
FINAL, |
|
699 |
VOLATILE) |
|
700 |
&& |
|
701 |
(sym.kind == TYP || |
|
702 |
checkDisjoint(pos, flags, |
|
703 |
ABSTRACT | NATIVE, |
|
704 |
STRICTFP))) { |
|
705 |
// skip |
|
706 |
} |
|
707 |
return flags & (mask | ~StandardFlags) | implicit; |
|
708 |
} |
|
709 |
||
710 |
||
711 |
/** Determine if this enum should be implicitly final. |
|
712 |
* |
|
713 |
* If the enum has no specialized enum contants, it is final. |
|
714 |
* |
|
715 |
* If the enum does have specialized enum contants, it is |
|
716 |
* <i>not</i> final. |
|
717 |
*/ |
|
718 |
private long implicitEnumFinalFlag(JCTree tree) { |
|
719 |
if (tree.getTag() != JCTree.CLASSDEF) return 0; |
|
720 |
class SpecialTreeVisitor extends JCTree.Visitor { |
|
721 |
boolean specialized; |
|
722 |
SpecialTreeVisitor() { |
|
723 |
this.specialized = false; |
|
724 |
}; |
|
725 |
||
726 |
public void visitTree(JCTree tree) { /* no-op */ } |
|
727 |
||
728 |
public void visitVarDef(JCVariableDecl tree) { |
|
729 |
if ((tree.mods.flags & ENUM) != 0) { |
|
730 |
if (tree.init instanceof JCNewClass && |
|
731 |
((JCNewClass) tree.init).def != null) { |
|
732 |
specialized = true; |
|
733 |
} |
|
734 |
} |
|
735 |
} |
|
736 |
} |
|
737 |
||
738 |
SpecialTreeVisitor sts = new SpecialTreeVisitor(); |
|
739 |
JCClassDecl cdef = (JCClassDecl) tree; |
|
740 |
for (JCTree defs: cdef.defs) { |
|
741 |
defs.accept(sts); |
|
742 |
if (sts.specialized) return 0; |
|
743 |
} |
|
744 |
return FINAL; |
|
745 |
} |
|
746 |
||
747 |
/* ************************************************************************* |
|
748 |
* Type Validation |
|
749 |
**************************************************************************/ |
|
750 |
||
751 |
/** Validate a type expression. That is, |
|
752 |
* check that all type arguments of a parametric type are within |
|
753 |
* their bounds. This must be done in a second phase after type attributon |
|
754 |
* since a class might have a subclass as type parameter bound. E.g: |
|
755 |
* |
|
756 |
* class B<A extends C> { ... } |
|
757 |
* class C extends B<C> { ... } |
|
758 |
* |
|
759 |
* and we can't make sure that the bound is already attributed because |
|
760 |
* of possible cycles. |
|
761 |
*/ |
|
762 |
private Validator validator = new Validator(); |
|
763 |
||
764 |
/** Visitor method: Validate a type expression, if it is not null, catching |
|
765 |
* and reporting any completion failures. |
|
766 |
*/ |
|
1358 | 767 |
void validate(JCTree tree, Env<AttrContext> env) { |
10 | 768 |
try { |
1358 | 769 |
if (tree != null) { |
770 |
validator.env = env; |
|
771 |
tree.accept(validator); |
|
772 |
checkRaw(tree, env); |
|
773 |
} |
|
10 | 774 |
} catch (CompletionFailure ex) { |
775 |
completionError(tree.pos(), ex); |
|
776 |
} |
|
777 |
} |
|
1358 | 778 |
//where |
779 |
void checkRaw(JCTree tree, Env<AttrContext> env) { |
|
780 |
if (lint.isEnabled(Lint.LintCategory.RAW) && |
|
781 |
tree.type.tag == CLASS && |
|
782 |
!env.enclClass.name.isEmpty() && //anonymous or intersection |
|
783 |
tree.type.isRaw()) { |
|
784 |
log.warning(tree.pos(), "raw.class.use", tree.type, tree.type.tsym.type); |
|
785 |
} |
|
786 |
} |
|
10 | 787 |
|
788 |
/** Visitor method: Validate a list of type expressions. |
|
789 |
*/ |
|
1358 | 790 |
void validate(List<? extends JCTree> trees, Env<AttrContext> env) { |
10 | 791 |
for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail) |
1358 | 792 |
validate(l.head, env); |
10 | 793 |
} |
794 |
||
795 |
/** A visitor class for type validation. |
|
796 |
*/ |
|
797 |
class Validator extends JCTree.Visitor { |
|
798 |
||
799 |
public void visitTypeArray(JCArrayTypeTree tree) { |
|
1358 | 800 |
validate(tree.elemtype, env); |
10 | 801 |
} |
802 |
||
803 |
public void visitTypeApply(JCTypeApply tree) { |
|
804 |
if (tree.type.tag == CLASS) { |
|
805 |
List<Type> formals = tree.type.tsym.type.getTypeArguments(); |
|
937
457a11ae2e84
6651719: Compiler crashes possibly during forward reference of TypeParameter
mcimadamore
parents:
815
diff
changeset
|
806 |
List<Type> actuals = types.capture(tree.type).getTypeArguments(); |
10 | 807 |
List<JCExpression> args = tree.arguments; |
808 |
List<Type> forms = formals; |
|
809 |
ListBuffer<TypeVar> tvars_buf = new ListBuffer<TypeVar>(); |
|
810 |
||
811 |
// For matching pairs of actual argument types `a' and |
|
812 |
// formal type parameters with declared bound `b' ... |
|
813 |
while (args.nonEmpty() && forms.nonEmpty()) { |
|
1358 | 814 |
validate(args.head, env); |
10 | 815 |
|
816 |
// exact type arguments needs to know their |
|
817 |
// bounds (for upper and lower bound |
|
818 |
// calculations). So we create new TypeVars with |
|
819 |
// bounds substed with actuals. |
|
820 |
tvars_buf.append(types.substBound(((TypeVar)forms.head), |
|
821 |
formals, |
|
937
457a11ae2e84
6651719: Compiler crashes possibly during forward reference of TypeParameter
mcimadamore
parents:
815
diff
changeset
|
822 |
actuals)); |
10 | 823 |
|
824 |
args = args.tail; |
|
825 |
forms = forms.tail; |
|
826 |
} |
|
827 |
||
828 |
args = tree.arguments; |
|
829 |
List<TypeVar> tvars = tvars_buf.toList(); |
|
830 |
while (args.nonEmpty() && tvars.nonEmpty()) { |
|
831 |
// Let the actual arguments know their bound |
|
832 |
args.head.type.withTypeVar(tvars.head); |
|
833 |
args = args.tail; |
|
834 |
tvars = tvars.tail; |
|
835 |
} |
|
836 |
||
837 |
args = tree.arguments; |
|
838 |
tvars = tvars_buf.toList(); |
|
839 |
while (args.nonEmpty() && tvars.nonEmpty()) { |
|
840 |
checkExtends(args.head.pos(), |
|
937
457a11ae2e84
6651719: Compiler crashes possibly during forward reference of TypeParameter
mcimadamore
parents:
815
diff
changeset
|
841 |
actuals.head, |
10 | 842 |
tvars.head); |
843 |
args = args.tail; |
|
844 |
tvars = tvars.tail; |
|
937
457a11ae2e84
6651719: Compiler crashes possibly during forward reference of TypeParameter
mcimadamore
parents:
815
diff
changeset
|
845 |
actuals = actuals.tail; |
10 | 846 |
} |
847 |
||
848 |
// Check that this type is either fully parameterized, or |
|
849 |
// not parameterized at all. |
|
850 |
if (tree.type.getEnclosingType().isRaw()) |
|
851 |
log.error(tree.pos(), "improperly.formed.type.inner.raw.param"); |
|
852 |
if (tree.clazz.getTag() == JCTree.SELECT) |
|
853 |
visitSelectInternal((JCFieldAccess)tree.clazz); |
|
854 |
} |
|
855 |
} |
|
856 |
||
857 |
public void visitTypeParameter(JCTypeParameter tree) { |
|
1358 | 858 |
validate(tree.bounds, env); |
10 | 859 |
checkClassBounds(tree.pos(), tree.type); |
860 |
} |
|
861 |
||
862 |
@Override |
|
863 |
public void visitWildcard(JCWildcard tree) { |
|
864 |
if (tree.inner != null) |
|
1358 | 865 |
validate(tree.inner, env); |
10 | 866 |
} |
867 |
||
868 |
public void visitSelect(JCFieldAccess tree) { |
|
869 |
if (tree.type.tag == CLASS) { |
|
870 |
visitSelectInternal(tree); |
|
871 |
||
872 |
// Check that this type is either fully parameterized, or |
|
873 |
// not parameterized at all. |
|
874 |
if (tree.selected.type.isParameterized() && tree.type.tsym.type.getTypeArguments().nonEmpty()) |
|
875 |
log.error(tree.pos(), "improperly.formed.type.param.missing"); |
|
876 |
} |
|
877 |
} |
|
878 |
public void visitSelectInternal(JCFieldAccess tree) { |
|
1358 | 879 |
if (tree.type.tsym.isStatic() && |
10 | 880 |
tree.selected.type.isParameterized()) { |
881 |
// The enclosing type is not a class, so we are |
|
882 |
// looking at a static member type. However, the |
|
883 |
// qualifying expression is parameterized. |
|
884 |
log.error(tree.pos(), "cant.select.static.class.from.param.type"); |
|
885 |
} else { |
|
886 |
// otherwise validate the rest of the expression |
|
1358 | 887 |
tree.selected.accept(this); |
10 | 888 |
} |
889 |
} |
|
890 |
||
891 |
/** Default visitor method: do nothing. |
|
892 |
*/ |
|
893 |
public void visitTree(JCTree tree) { |
|
894 |
} |
|
1358 | 895 |
|
896 |
Env<AttrContext> env; |
|
10 | 897 |
} |
898 |
||
899 |
/* ************************************************************************* |
|
900 |
* Exception checking |
|
901 |
**************************************************************************/ |
|
902 |
||
903 |
/* The following methods treat classes as sets that contain |
|
904 |
* the class itself and all their subclasses |
|
905 |
*/ |
|
906 |
||
907 |
/** Is given type a subtype of some of the types in given list? |
|
908 |
*/ |
|
909 |
boolean subset(Type t, List<Type> ts) { |
|
910 |
for (List<Type> l = ts; l.nonEmpty(); l = l.tail) |
|
911 |
if (types.isSubtype(t, l.head)) return true; |
|
912 |
return false; |
|
913 |
} |
|
914 |
||
915 |
/** Is given type a subtype or supertype of |
|
916 |
* some of the types in given list? |
|
917 |
*/ |
|
918 |
boolean intersects(Type t, List<Type> ts) { |
|
919 |
for (List<Type> l = ts; l.nonEmpty(); l = l.tail) |
|
920 |
if (types.isSubtype(t, l.head) || types.isSubtype(l.head, t)) return true; |
|
921 |
return false; |
|
922 |
} |
|
923 |
||
924 |
/** Add type set to given type list, unless it is a subclass of some class |
|
925 |
* in the list. |
|
926 |
*/ |
|
927 |
List<Type> incl(Type t, List<Type> ts) { |
|
928 |
return subset(t, ts) ? ts : excl(t, ts).prepend(t); |
|
929 |
} |
|
930 |
||
931 |
/** Remove type set from type set list. |
|
932 |
*/ |
|
933 |
List<Type> excl(Type t, List<Type> ts) { |
|
934 |
if (ts.isEmpty()) { |
|
935 |
return ts; |
|
936 |
} else { |
|
937 |
List<Type> ts1 = excl(t, ts.tail); |
|
938 |
if (types.isSubtype(ts.head, t)) return ts1; |
|
939 |
else if (ts1 == ts.tail) return ts; |
|
940 |
else return ts1.prepend(ts.head); |
|
941 |
} |
|
942 |
} |
|
943 |
||
944 |
/** Form the union of two type set lists. |
|
945 |
*/ |
|
946 |
List<Type> union(List<Type> ts1, List<Type> ts2) { |
|
947 |
List<Type> ts = ts1; |
|
948 |
for (List<Type> l = ts2; l.nonEmpty(); l = l.tail) |
|
949 |
ts = incl(l.head, ts); |
|
950 |
return ts; |
|
951 |
} |
|
952 |
||
953 |
/** Form the difference of two type lists. |
|
954 |
*/ |
|
955 |
List<Type> diff(List<Type> ts1, List<Type> ts2) { |
|
956 |
List<Type> ts = ts1; |
|
957 |
for (List<Type> l = ts2; l.nonEmpty(); l = l.tail) |
|
958 |
ts = excl(l.head, ts); |
|
959 |
return ts; |
|
960 |
} |
|
961 |
||
962 |
/** Form the intersection of two type lists. |
|
963 |
*/ |
|
964 |
public List<Type> intersect(List<Type> ts1, List<Type> ts2) { |
|
965 |
List<Type> ts = List.nil(); |
|
966 |
for (List<Type> l = ts1; l.nonEmpty(); l = l.tail) |
|
967 |
if (subset(l.head, ts2)) ts = incl(l.head, ts); |
|
968 |
for (List<Type> l = ts2; l.nonEmpty(); l = l.tail) |
|
969 |
if (subset(l.head, ts1)) ts = incl(l.head, ts); |
|
970 |
return ts; |
|
971 |
} |
|
972 |
||
973 |
/** Is exc an exception symbol that need not be declared? |
|
974 |
*/ |
|
975 |
boolean isUnchecked(ClassSymbol exc) { |
|
976 |
return |
|
977 |
exc.kind == ERR || |
|
978 |
exc.isSubClass(syms.errorType.tsym, types) || |
|
979 |
exc.isSubClass(syms.runtimeExceptionType.tsym, types); |
|
980 |
} |
|
981 |
||
982 |
/** Is exc an exception type that need not be declared? |
|
983 |
*/ |
|
984 |
boolean isUnchecked(Type exc) { |
|
985 |
return |
|
986 |
(exc.tag == TYPEVAR) ? isUnchecked(types.supertype(exc)) : |
|
987 |
(exc.tag == CLASS) ? isUnchecked((ClassSymbol)exc.tsym) : |
|
988 |
exc.tag == BOT; |
|
989 |
} |
|
990 |
||
991 |
/** Same, but handling completion failures. |
|
992 |
*/ |
|
993 |
boolean isUnchecked(DiagnosticPosition pos, Type exc) { |
|
994 |
try { |
|
995 |
return isUnchecked(exc); |
|
996 |
} catch (CompletionFailure ex) { |
|
997 |
completionError(pos, ex); |
|
998 |
return true; |
|
999 |
} |
|
1000 |
} |
|
1001 |
||
1002 |
/** Is exc handled by given exception list? |
|
1003 |
*/ |
|
1004 |
boolean isHandled(Type exc, List<Type> handled) { |
|
1005 |
return isUnchecked(exc) || subset(exc, handled); |
|
1006 |
} |
|
1007 |
||
1008 |
/** Return all exceptions in thrown list that are not in handled list. |
|
1009 |
* @param thrown The list of thrown exceptions. |
|
1010 |
* @param handled The list of handled exceptions. |
|
1011 |
*/ |
|
1012 |
List<Type> unHandled(List<Type> thrown, List<Type> handled) { |
|
1013 |
List<Type> unhandled = List.nil(); |
|
1014 |
for (List<Type> l = thrown; l.nonEmpty(); l = l.tail) |
|
1015 |
if (!isHandled(l.head, handled)) unhandled = unhandled.prepend(l.head); |
|
1016 |
return unhandled; |
|
1017 |
} |
|
1018 |
||
1019 |
/* ************************************************************************* |
|
1020 |
* Overriding/Implementation checking |
|
1021 |
**************************************************************************/ |
|
1022 |
||
1023 |
/** The level of access protection given by a flag set, |
|
1024 |
* where PRIVATE is highest and PUBLIC is lowest. |
|
1025 |
*/ |
|
1026 |
static int protection(long flags) { |
|
1027 |
switch ((short)(flags & AccessFlags)) { |
|
1028 |
case PRIVATE: return 3; |
|
1029 |
case PROTECTED: return 1; |
|
1030 |
default: |
|
1031 |
case PUBLIC: return 0; |
|
1032 |
case 0: return 2; |
|
1033 |
} |
|
1034 |
} |
|
1035 |
||
1036 |
/** A customized "cannot override" error message. |
|
1037 |
* @param m The overriding method. |
|
1038 |
* @param other The overridden method. |
|
1039 |
* @return An internationalized string. |
|
1040 |
*/ |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
1041 |
Object cannotOverride(MethodSymbol m, MethodSymbol other) { |
10 | 1042 |
String key; |
1043 |
if ((other.owner.flags() & INTERFACE) == 0) |
|
1044 |
key = "cant.override"; |
|
1045 |
else if ((m.owner.flags() & INTERFACE) == 0) |
|
1046 |
key = "cant.implement"; |
|
1047 |
else |
|
1048 |
key = "clashes.with"; |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
1049 |
return diags.fragment(key, m, m.location(), other, other.location()); |
10 | 1050 |
} |
1051 |
||
1052 |
/** A customized "override" warning message. |
|
1053 |
* @param m The overriding method. |
|
1054 |
* @param other The overridden method. |
|
1055 |
* @return An internationalized string. |
|
1056 |
*/ |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
1057 |
Object uncheckedOverrides(MethodSymbol m, MethodSymbol other) { |
10 | 1058 |
String key; |
1059 |
if ((other.owner.flags() & INTERFACE) == 0) |
|
1060 |
key = "unchecked.override"; |
|
1061 |
else if ((m.owner.flags() & INTERFACE) == 0) |
|
1062 |
key = "unchecked.implement"; |
|
1063 |
else |
|
1064 |
key = "unchecked.clash.with"; |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
1065 |
return diags.fragment(key, m, m.location(), other, other.location()); |
10 | 1066 |
} |
1067 |
||
1068 |
/** A customized "override" warning message. |
|
1069 |
* @param m The overriding method. |
|
1070 |
* @param other The overridden method. |
|
1071 |
* @return An internationalized string. |
|
1072 |
*/ |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
1073 |
Object varargsOverrides(MethodSymbol m, MethodSymbol other) { |
10 | 1074 |
String key; |
1075 |
if ((other.owner.flags() & INTERFACE) == 0) |
|
1076 |
key = "varargs.override"; |
|
1077 |
else if ((m.owner.flags() & INTERFACE) == 0) |
|
1078 |
key = "varargs.implement"; |
|
1079 |
else |
|
1080 |
key = "varargs.clash.with"; |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
1081 |
return diags.fragment(key, m, m.location(), other, other.location()); |
10 | 1082 |
} |
1083 |
||
1084 |
/** Check that this method conforms with overridden method 'other'. |
|
1085 |
* where `origin' is the class where checking started. |
|
1086 |
* Complications: |
|
1087 |
* (1) Do not check overriding of synthetic methods |
|
1088 |
* (reason: they might be final). |
|
1089 |
* todo: check whether this is still necessary. |
|
1090 |
* (2) Admit the case where an interface proxy throws fewer exceptions |
|
1091 |
* than the method it implements. Augment the proxy methods with the |
|
1092 |
* undeclared exceptions in this case. |
|
1093 |
* (3) When generics are enabled, admit the case where an interface proxy |
|
1094 |
* has a result type |
|
1095 |
* extended by the result type of the method it implements. |
|
1096 |
* Change the proxies result type to the smaller type in this case. |
|
1097 |
* |
|
1098 |
* @param tree The tree from which positions |
|
1099 |
* are extracted for errors. |
|
1100 |
* @param m The overriding method. |
|
1101 |
* @param other The overridden method. |
|
1102 |
* @param origin The class of which the overriding method |
|
1103 |
* is a member. |
|
1104 |
*/ |
|
1105 |
void checkOverride(JCTree tree, |
|
1106 |
MethodSymbol m, |
|
1107 |
MethodSymbol other, |
|
1108 |
ClassSymbol origin) { |
|
1109 |
// Don't check overriding of synthetic methods or by bridge methods. |
|
1110 |
if ((m.flags() & (SYNTHETIC|BRIDGE)) != 0 || (other.flags() & SYNTHETIC) != 0) { |
|
1111 |
return; |
|
1112 |
} |
|
1113 |
||
1114 |
// Error if static method overrides instance method (JLS 8.4.6.2). |
|
1115 |
if ((m.flags() & STATIC) != 0 && |
|
1116 |
(other.flags() & STATIC) == 0) { |
|
1117 |
log.error(TreeInfo.diagnosticPositionFor(m, tree), "override.static", |
|
1118 |
cannotOverride(m, other)); |
|
1119 |
return; |
|
1120 |
} |
|
1121 |
||
1122 |
// Error if instance method overrides static or final |
|
1123 |
// method (JLS 8.4.6.1). |
|
1124 |
if ((other.flags() & FINAL) != 0 || |
|
1125 |
(m.flags() & STATIC) == 0 && |
|
1126 |
(other.flags() & STATIC) != 0) { |
|
1127 |
log.error(TreeInfo.diagnosticPositionFor(m, tree), "override.meth", |
|
1128 |
cannotOverride(m, other), |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
938
diff
changeset
|
1129 |
asFlagSet(other.flags() & (FINAL | STATIC))); |
10 | 1130 |
return; |
1131 |
} |
|
1132 |
||
1133 |
if ((m.owner.flags() & ANNOTATION) != 0) { |
|
1134 |
// handled in validateAnnotationMethod |
|
1135 |
return; |
|
1136 |
} |
|
1137 |
||
1138 |
// Error if overriding method has weaker access (JLS 8.4.6.3). |
|
1139 |
if ((origin.flags() & INTERFACE) == 0 && |
|
1140 |
protection(m.flags()) > protection(other.flags())) { |
|
1141 |
log.error(TreeInfo.diagnosticPositionFor(m, tree), "override.weaker.access", |
|
1142 |
cannotOverride(m, other), |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
938
diff
changeset
|
1143 |
other.flags() == 0 ? |
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
938
diff
changeset
|
1144 |
Flag.PACKAGE : |
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
938
diff
changeset
|
1145 |
asFlagSet(other.flags() & AccessFlags)); |
10 | 1146 |
return; |
1147 |
} |
|
1148 |
||
1149 |
Type mt = types.memberType(origin.type, m); |
|
1150 |
Type ot = types.memberType(origin.type, other); |
|
1151 |
// Error if overriding result type is different |
|
1152 |
// (or, in the case of generics mode, not a subtype) of |
|
1153 |
// overridden result type. We have to rename any type parameters |
|
1154 |
// before comparing types. |
|
1155 |
List<Type> mtvars = mt.getTypeArguments(); |
|
1156 |
List<Type> otvars = ot.getTypeArguments(); |
|
1157 |
Type mtres = mt.getReturnType(); |
|
1158 |
Type otres = types.subst(ot.getReturnType(), otvars, mtvars); |
|
1159 |
||
1160 |
overrideWarner.warned = false; |
|
1161 |
boolean resultTypesOK = |
|
1162 |
types.returnTypeSubstitutable(mt, ot, otres, overrideWarner); |
|
1163 |
if (!resultTypesOK) { |
|
1164 |
if (!source.allowCovariantReturns() && |
|
1165 |
m.owner != origin && |
|
1166 |
m.owner.isSubClass(other.owner, types)) { |
|
1167 |
// allow limited interoperability with covariant returns |
|
1168 |
} else { |
|
1169 |
typeError(TreeInfo.diagnosticPositionFor(m, tree), |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
1170 |
diags.fragment("override.incompatible.ret", |
10 | 1171 |
cannotOverride(m, other)), |
1172 |
mtres, otres); |
|
1173 |
return; |
|
1174 |
} |
|
1175 |
} else if (overrideWarner.warned) { |
|
1176 |
warnUnchecked(TreeInfo.diagnosticPositionFor(m, tree), |
|
1177 |
"prob.found.req", |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
1178 |
diags.fragment("override.unchecked.ret", |
10 | 1179 |
uncheckedOverrides(m, other)), |
1180 |
mtres, otres); |
|
1181 |
} |
|
1182 |
||
1183 |
// Error if overriding method throws an exception not reported |
|
1184 |
// by overridden method. |
|
1185 |
List<Type> otthrown = types.subst(ot.getThrownTypes(), otvars, mtvars); |
|
1186 |
List<Type> unhandled = unHandled(mt.getThrownTypes(), otthrown); |
|
1187 |
if (unhandled.nonEmpty()) { |
|
1188 |
log.error(TreeInfo.diagnosticPositionFor(m, tree), |
|
1189 |
"override.meth.doesnt.throw", |
|
1190 |
cannotOverride(m, other), |
|
1191 |
unhandled.head); |
|
1192 |
return; |
|
1193 |
} |
|
1194 |
||
1195 |
// Optional warning if varargs don't agree |
|
1196 |
if ((((m.flags() ^ other.flags()) & Flags.VARARGS) != 0) |
|
1197 |
&& lint.isEnabled(Lint.LintCategory.OVERRIDES)) { |
|
1198 |
log.warning(TreeInfo.diagnosticPositionFor(m, tree), |
|
1199 |
((m.flags() & Flags.VARARGS) != 0) |
|
1200 |
? "override.varargs.missing" |
|
1201 |
: "override.varargs.extra", |
|
1202 |
varargsOverrides(m, other)); |
|
1203 |
} |
|
1204 |
||
1205 |
// Warn if instance method overrides bridge method (compiler spec ??) |
|
1206 |
if ((other.flags() & BRIDGE) != 0) { |
|
1207 |
log.warning(TreeInfo.diagnosticPositionFor(m, tree), "override.bridge", |
|
1208 |
uncheckedOverrides(m, other)); |
|
1209 |
} |
|
1210 |
||
1211 |
// Warn if a deprecated method overridden by a non-deprecated one. |
|
1212 |
if ((other.flags() & DEPRECATED) != 0 |
|
1213 |
&& (m.flags() & DEPRECATED) == 0 |
|
1214 |
&& m.outermostClass() != other.outermostClass() |
|
1215 |
&& !isDeprecatedOverrideIgnorable(other, origin)) { |
|
1216 |
warnDeprecated(TreeInfo.diagnosticPositionFor(m, tree), other); |
|
1217 |
} |
|
1218 |
} |
|
1219 |
// where |
|
1220 |
private boolean isDeprecatedOverrideIgnorable(MethodSymbol m, ClassSymbol origin) { |
|
1221 |
// If the method, m, is defined in an interface, then ignore the issue if the method |
|
1222 |
// is only inherited via a supertype and also implemented in the supertype, |
|
1223 |
// because in that case, we will rediscover the issue when examining the method |
|
1224 |
// in the supertype. |
|
1225 |
// If the method, m, is not defined in an interface, then the only time we need to |
|
1226 |
// address the issue is when the method is the supertype implemementation: any other |
|
1227 |
// case, we will have dealt with when examining the supertype classes |
|
1228 |
ClassSymbol mc = m.enclClass(); |
|
1229 |
Type st = types.supertype(origin.type); |
|
1230 |
if (st.tag != CLASS) |
|
1231 |
return true; |
|
1232 |
MethodSymbol stimpl = m.implementation((ClassSymbol)st.tsym, types, false); |
|
1233 |
||
1234 |
if (mc != null && ((mc.flags() & INTERFACE) != 0)) { |
|
1235 |
List<Type> intfs = types.interfaces(origin.type); |
|
1236 |
return (intfs.contains(mc.type) ? false : (stimpl != null)); |
|
1237 |
} |
|
1238 |
else |
|
1239 |
return (stimpl != m); |
|
1240 |
} |
|
1241 |
||
1242 |
||
1243 |
// used to check if there were any unchecked conversions |
|
1244 |
Warner overrideWarner = new Warner(); |
|
1245 |
||
1246 |
/** Check that a class does not inherit two concrete methods |
|
1247 |
* with the same signature. |
|
1248 |
* @param pos Position to be used for error reporting. |
|
1249 |
* @param site The class type to be checked. |
|
1250 |
*/ |
|
1251 |
public void checkCompatibleConcretes(DiagnosticPosition pos, Type site) { |
|
1252 |
Type sup = types.supertype(site); |
|
1253 |
if (sup.tag != CLASS) return; |
|
1254 |
||
1255 |
for (Type t1 = sup; |
|
1256 |
t1.tsym.type.isParameterized(); |
|
1257 |
t1 = types.supertype(t1)) { |
|
1258 |
for (Scope.Entry e1 = t1.tsym.members().elems; |
|
1259 |
e1 != null; |
|
1260 |
e1 = e1.sibling) { |
|
1261 |
Symbol s1 = e1.sym; |
|
1262 |
if (s1.kind != MTH || |
|
1263 |
(s1.flags() & (STATIC|SYNTHETIC|BRIDGE)) != 0 || |
|
1264 |
!s1.isInheritedIn(site.tsym, types) || |
|
1265 |
((MethodSymbol)s1).implementation(site.tsym, |
|
1266 |
types, |
|
1267 |
true) != s1) |
|
1268 |
continue; |
|
1269 |
Type st1 = types.memberType(t1, s1); |
|
1270 |
int s1ArgsLength = st1.getParameterTypes().length(); |
|
1271 |
if (st1 == s1.type) continue; |
|
1272 |
||
1273 |
for (Type t2 = sup; |
|
1274 |
t2.tag == CLASS; |
|
1275 |
t2 = types.supertype(t2)) { |
|
508
118e500e2e50
5009937: hiding versus generics versus binary compatibility
mcimadamore
parents:
169
diff
changeset
|
1276 |
for (Scope.Entry e2 = t2.tsym.members().lookup(s1.name); |
10 | 1277 |
e2.scope != null; |
1278 |
e2 = e2.next()) { |
|
1279 |
Symbol s2 = e2.sym; |
|
1280 |
if (s2 == s1 || |
|
1281 |
s2.kind != MTH || |
|
1282 |
(s2.flags() & (STATIC|SYNTHETIC|BRIDGE)) != 0 || |
|
1283 |
s2.type.getParameterTypes().length() != s1ArgsLength || |
|
1284 |
!s2.isInheritedIn(site.tsym, types) || |
|
1285 |
((MethodSymbol)s2).implementation(site.tsym, |
|
1286 |
types, |
|
1287 |
true) != s2) |
|
1288 |
continue; |
|
1289 |
Type st2 = types.memberType(t2, s2); |
|
1290 |
if (types.overrideEquivalent(st1, st2)) |
|
1291 |
log.error(pos, "concrete.inheritance.conflict", |
|
1292 |
s1, t1, s2, t2, sup); |
|
1293 |
} |
|
1294 |
} |
|
1295 |
} |
|
1296 |
} |
|
1297 |
} |
|
1298 |
||
1299 |
/** Check that classes (or interfaces) do not each define an abstract |
|
1300 |
* method with same name and arguments but incompatible return types. |
|
1301 |
* @param pos Position to be used for error reporting. |
|
1302 |
* @param t1 The first argument type. |
|
1303 |
* @param t2 The second argument type. |
|
1304 |
*/ |
|
1305 |
public boolean checkCompatibleAbstracts(DiagnosticPosition pos, |
|
1306 |
Type t1, |
|
1307 |
Type t2) { |
|
1308 |
return checkCompatibleAbstracts(pos, t1, t2, |
|
1309 |
types.makeCompoundType(t1, t2)); |
|
1310 |
} |
|
1311 |
||
1312 |
public boolean checkCompatibleAbstracts(DiagnosticPosition pos, |
|
1313 |
Type t1, |
|
1314 |
Type t2, |
|
1315 |
Type site) { |
|
1316 |
Symbol sym = firstIncompatibility(t1, t2, site); |
|
1317 |
if (sym != null) { |
|
1318 |
log.error(pos, "types.incompatible.diff.ret", |
|
1319 |
t1, t2, sym.name + |
|
1320 |
"(" + types.memberType(t2, sym).getParameterTypes() + ")"); |
|
1321 |
return false; |
|
1322 |
} |
|
1323 |
return true; |
|
1324 |
} |
|
1325 |
||
1326 |
/** Return the first method which is defined with same args |
|
1327 |
* but different return types in two given interfaces, or null if none |
|
1328 |
* exists. |
|
1329 |
* @param t1 The first type. |
|
1330 |
* @param t2 The second type. |
|
1331 |
* @param site The most derived type. |
|
1332 |
* @returns symbol from t2 that conflicts with one in t1. |
|
1333 |
*/ |
|
1334 |
private Symbol firstIncompatibility(Type t1, Type t2, Type site) { |
|
1335 |
Map<TypeSymbol,Type> interfaces1 = new HashMap<TypeSymbol,Type>(); |
|
1336 |
closure(t1, interfaces1); |
|
1337 |
Map<TypeSymbol,Type> interfaces2; |
|
1338 |
if (t1 == t2) |
|
1339 |
interfaces2 = interfaces1; |
|
1340 |
else |
|
1341 |
closure(t2, interfaces1, interfaces2 = new HashMap<TypeSymbol,Type>()); |
|
1342 |
||
1343 |
for (Type t3 : interfaces1.values()) { |
|
1344 |
for (Type t4 : interfaces2.values()) { |
|
1345 |
Symbol s = firstDirectIncompatibility(t3, t4, site); |
|
1346 |
if (s != null) return s; |
|
1347 |
} |
|
1348 |
} |
|
1349 |
return null; |
|
1350 |
} |
|
1351 |
||
1352 |
/** Compute all the supertypes of t, indexed by type symbol. */ |
|
1353 |
private void closure(Type t, Map<TypeSymbol,Type> typeMap) { |
|
1354 |
if (t.tag != CLASS) return; |
|
1355 |
if (typeMap.put(t.tsym, t) == null) { |
|
1356 |
closure(types.supertype(t), typeMap); |
|
1357 |
for (Type i : types.interfaces(t)) |
|
1358 |
closure(i, typeMap); |
|
1359 |
} |
|
1360 |
} |
|
1361 |
||
1362 |
/** Compute all the supertypes of t, indexed by type symbol (except thise in typesSkip). */ |
|
1363 |
private void closure(Type t, Map<TypeSymbol,Type> typesSkip, Map<TypeSymbol,Type> typeMap) { |
|
1364 |
if (t.tag != CLASS) return; |
|
1365 |
if (typesSkip.get(t.tsym) != null) return; |
|
1366 |
if (typeMap.put(t.tsym, t) == null) { |
|
1367 |
closure(types.supertype(t), typesSkip, typeMap); |
|
1368 |
for (Type i : types.interfaces(t)) |
|
1369 |
closure(i, typesSkip, typeMap); |
|
1370 |
} |
|
1371 |
} |
|
1372 |
||
1373 |
/** Return the first method in t2 that conflicts with a method from t1. */ |
|
1374 |
private Symbol firstDirectIncompatibility(Type t1, Type t2, Type site) { |
|
1375 |
for (Scope.Entry e1 = t1.tsym.members().elems; e1 != null; e1 = e1.sibling) { |
|
1376 |
Symbol s1 = e1.sym; |
|
1377 |
Type st1 = null; |
|
1378 |
if (s1.kind != MTH || !s1.isInheritedIn(site.tsym, types)) continue; |
|
1379 |
Symbol impl = ((MethodSymbol)s1).implementation(site.tsym, types, false); |
|
1380 |
if (impl != null && (impl.flags() & ABSTRACT) == 0) continue; |
|
1381 |
for (Scope.Entry e2 = t2.tsym.members().lookup(s1.name); e2.scope != null; e2 = e2.next()) { |
|
1382 |
Symbol s2 = e2.sym; |
|
1383 |
if (s1 == s2) continue; |
|
1384 |
if (s2.kind != MTH || !s2.isInheritedIn(site.tsym, types)) continue; |
|
1385 |
if (st1 == null) st1 = types.memberType(t1, s1); |
|
1386 |
Type st2 = types.memberType(t2, s2); |
|
1387 |
if (types.overrideEquivalent(st1, st2)) { |
|
1388 |
List<Type> tvars1 = st1.getTypeArguments(); |
|
1389 |
List<Type> tvars2 = st2.getTypeArguments(); |
|
1390 |
Type rt1 = st1.getReturnType(); |
|
1391 |
Type rt2 = types.subst(st2.getReturnType(), tvars2, tvars1); |
|
1392 |
boolean compat = |
|
1393 |
types.isSameType(rt1, rt2) || |
|
1394 |
rt1.tag >= CLASS && rt2.tag >= CLASS && |
|
1395 |
(types.covariantReturnType(rt1, rt2, Warner.noWarnings) || |
|
812
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1396 |
types.covariantReturnType(rt2, rt1, Warner.noWarnings)) || |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1397 |
checkCommonOverriderIn(s1,s2,site); |
10 | 1398 |
if (!compat) return s2; |
1399 |
} |
|
1400 |
} |
|
1401 |
} |
|
1402 |
return null; |
|
1403 |
} |
|
812
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1404 |
//WHERE |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1405 |
boolean checkCommonOverriderIn(Symbol s1, Symbol s2, Type site) { |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1406 |
Map<TypeSymbol,Type> supertypes = new HashMap<TypeSymbol,Type>(); |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1407 |
Type st1 = types.memberType(site, s1); |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1408 |
Type st2 = types.memberType(site, s2); |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1409 |
closure(site, supertypes); |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1410 |
for (Type t : supertypes.values()) { |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1411 |
for (Scope.Entry e = t.tsym.members().lookup(s1.name); e.scope != null; e = e.next()) { |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1412 |
Symbol s3 = e.sym; |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1413 |
if (s3 == s1 || s3 == s2 || s3.kind != MTH || (s3.flags() & (BRIDGE|SYNTHETIC)) != 0) continue; |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1414 |
Type st3 = types.memberType(site,s3); |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1415 |
if (types.overrideEquivalent(st3, st1) && types.overrideEquivalent(st3, st2)) { |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1416 |
if (s3.owner == site.tsym) { |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1417 |
return true; |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1418 |
} |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1419 |
List<Type> tvars1 = st1.getTypeArguments(); |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1420 |
List<Type> tvars2 = st2.getTypeArguments(); |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1421 |
List<Type> tvars3 = st3.getTypeArguments(); |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1422 |
Type rt1 = st1.getReturnType(); |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1423 |
Type rt2 = st2.getReturnType(); |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1424 |
Type rt13 = types.subst(st3.getReturnType(), tvars3, tvars1); |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1425 |
Type rt23 = types.subst(st3.getReturnType(), tvars3, tvars2); |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1426 |
boolean compat = |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1427 |
rt13.tag >= CLASS && rt23.tag >= CLASS && |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1428 |
(types.covariantReturnType(rt13, rt1, Warner.noWarnings) && |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1429 |
types.covariantReturnType(rt23, rt2, Warner.noWarnings)); |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1430 |
if (compat) |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1431 |
return true; |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1432 |
} |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1433 |
} |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1434 |
} |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1435 |
return false; |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1436 |
} |
10 | 1437 |
|
1438 |
/** Check that a given method conforms with any method it overrides. |
|
1439 |
* @param tree The tree from which positions are extracted |
|
1440 |
* for errors. |
|
1441 |
* @param m The overriding method. |
|
1442 |
*/ |
|
1443 |
void checkOverride(JCTree tree, MethodSymbol m) { |
|
1444 |
ClassSymbol origin = (ClassSymbol)m.owner; |
|
1445 |
if ((origin.flags() & ENUM) != 0 && names.finalize.equals(m.name)) |
|
1446 |
if (m.overrides(syms.enumFinalFinalize, origin, types, false)) { |
|
1447 |
log.error(tree.pos(), "enum.no.finalize"); |
|
1448 |
return; |
|
1449 |
} |
|
1450 |
for (Type t = types.supertype(origin.type); t.tag == CLASS; |
|
1451 |
t = types.supertype(t)) { |
|
1452 |
TypeSymbol c = t.tsym; |
|
1453 |
Scope.Entry e = c.members().lookup(m.name); |
|
1454 |
while (e.scope != null) { |
|
1455 |
if (m.overrides(e.sym, origin, types, false)) |
|
1456 |
checkOverride(tree, m, (MethodSymbol)e.sym, origin); |
|
508
118e500e2e50
5009937: hiding versus generics versus binary compatibility
mcimadamore
parents:
169
diff
changeset
|
1457 |
else if (e.sym.isInheritedIn(origin, types) && !m.isConstructor()) { |
118e500e2e50
5009937: hiding versus generics versus binary compatibility
mcimadamore
parents:
169
diff
changeset
|
1458 |
Type er1 = m.erasure(types); |
118e500e2e50
5009937: hiding versus generics versus binary compatibility
mcimadamore
parents:
169
diff
changeset
|
1459 |
Type er2 = e.sym.erasure(types); |
118e500e2e50
5009937: hiding versus generics versus binary compatibility
mcimadamore
parents:
169
diff
changeset
|
1460 |
if (types.isSameType(er1,er2)) { |
118e500e2e50
5009937: hiding versus generics versus binary compatibility
mcimadamore
parents:
169
diff
changeset
|
1461 |
log.error(TreeInfo.diagnosticPositionFor(m, tree), |
118e500e2e50
5009937: hiding versus generics versus binary compatibility
mcimadamore
parents:
169
diff
changeset
|
1462 |
"name.clash.same.erasure.no.override", |
118e500e2e50
5009937: hiding versus generics versus binary compatibility
mcimadamore
parents:
169
diff
changeset
|
1463 |
m, m.location(), |
118e500e2e50
5009937: hiding versus generics versus binary compatibility
mcimadamore
parents:
169
diff
changeset
|
1464 |
e.sym, e.sym.location()); |
118e500e2e50
5009937: hiding versus generics versus binary compatibility
mcimadamore
parents:
169
diff
changeset
|
1465 |
} |
118e500e2e50
5009937: hiding versus generics versus binary compatibility
mcimadamore
parents:
169
diff
changeset
|
1466 |
} |
10 | 1467 |
e = e.next(); |
1468 |
} |
|
1469 |
} |
|
1470 |
} |
|
1471 |
||
1472 |
/** Check that all abstract members of given class have definitions. |
|
1473 |
* @param pos Position to be used for error reporting. |
|
1474 |
* @param c The class. |
|
1475 |
*/ |
|
1476 |
void checkAllDefined(DiagnosticPosition pos, ClassSymbol c) { |
|
1477 |
try { |
|
1478 |
MethodSymbol undef = firstUndef(c, c); |
|
1479 |
if (undef != null) { |
|
1480 |
if ((c.flags() & ENUM) != 0 && |
|
1481 |
types.supertype(c.type).tsym == syms.enumSym && |
|
1482 |
(c.flags() & FINAL) == 0) { |
|
1483 |
// add the ABSTRACT flag to an enum |
|
1484 |
c.flags_field |= ABSTRACT; |
|
1485 |
} else { |
|
1486 |
MethodSymbol undef1 = |
|
1487 |
new MethodSymbol(undef.flags(), undef.name, |
|
1488 |
types.memberType(c.type, undef), undef.owner); |
|
1489 |
log.error(pos, "does.not.override.abstract", |
|
1490 |
c, undef1, undef1.location()); |
|
1491 |
} |
|
1492 |
} |
|
1493 |
} catch (CompletionFailure ex) { |
|
1494 |
completionError(pos, ex); |
|
1495 |
} |
|
1496 |
} |
|
1497 |
//where |
|
1498 |
/** Return first abstract member of class `c' that is not defined |
|
1499 |
* in `impl', null if there is none. |
|
1500 |
*/ |
|
1501 |
private MethodSymbol firstUndef(ClassSymbol impl, ClassSymbol c) { |
|
1502 |
MethodSymbol undef = null; |
|
1503 |
// Do not bother to search in classes that are not abstract, |
|
1504 |
// since they cannot have abstract members. |
|
1505 |
if (c == impl || (c.flags() & (ABSTRACT | INTERFACE)) != 0) { |
|
1506 |
Scope s = c.members(); |
|
1507 |
for (Scope.Entry e = s.elems; |
|
1508 |
undef == null && e != null; |
|
1509 |
e = e.sibling) { |
|
1510 |
if (e.sym.kind == MTH && |
|
1511 |
(e.sym.flags() & (ABSTRACT|IPROXY)) == ABSTRACT) { |
|
1512 |
MethodSymbol absmeth = (MethodSymbol)e.sym; |
|
1513 |
MethodSymbol implmeth = absmeth.implementation(impl, types, true); |
|
1514 |
if (implmeth == null || implmeth == absmeth) |
|
1515 |
undef = absmeth; |
|
1516 |
} |
|
1517 |
} |
|
1518 |
if (undef == null) { |
|
1519 |
Type st = types.supertype(c.type); |
|
1520 |
if (st.tag == CLASS) |
|
1521 |
undef = firstUndef(impl, (ClassSymbol)st.tsym); |
|
1522 |
} |
|
1523 |
for (List<Type> l = types.interfaces(c.type); |
|
1524 |
undef == null && l.nonEmpty(); |
|
1525 |
l = l.tail) { |
|
1526 |
undef = firstUndef(impl, (ClassSymbol)l.head.tsym); |
|
1527 |
} |
|
1528 |
} |
|
1529 |
return undef; |
|
1530 |
} |
|
1531 |
||
1532 |
/** Check for cyclic references. Issue an error if the |
|
1533 |
* symbol of the type referred to has a LOCKED flag set. |
|
1534 |
* |
|
1535 |
* @param pos Position to be used for error reporting. |
|
1536 |
* @param t The type referred to. |
|
1537 |
*/ |
|
1538 |
void checkNonCyclic(DiagnosticPosition pos, Type t) { |
|
1539 |
checkNonCyclicInternal(pos, t); |
|
1540 |
} |
|
1541 |
||
1542 |
||
1543 |
void checkNonCyclic(DiagnosticPosition pos, TypeVar t) { |
|
1544 |
checkNonCyclic1(pos, t, new HashSet<TypeVar>()); |
|
1545 |
} |
|
1546 |
||
1547 |
private void checkNonCyclic1(DiagnosticPosition pos, Type t, Set<TypeVar> seen) { |
|
1548 |
final TypeVar tv; |
|
661
9b2f1fe5c183
6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents:
508
diff
changeset
|
1549 |
if (t.tag == TYPEVAR && (t.tsym.flags() & UNATTRIBUTED) != 0) |
9b2f1fe5c183
6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents:
508
diff
changeset
|
1550 |
return; |
10 | 1551 |
if (seen.contains(t)) { |
1552 |
tv = (TypeVar)t; |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1040
diff
changeset
|
1553 |
tv.bound = types.createErrorType(t); |
10 | 1554 |
log.error(pos, "cyclic.inheritance", t); |
1555 |
} else if (t.tag == TYPEVAR) { |
|
1556 |
tv = (TypeVar)t; |
|
1557 |
seen.add(tv); |
|
1558 |
for (Type b : types.getBounds(tv)) |
|
1559 |
checkNonCyclic1(pos, b, seen); |
|
1560 |
} |
|
1561 |
} |
|
1562 |
||
1563 |
/** Check for cyclic references. Issue an error if the |
|
1564 |
* symbol of the type referred to has a LOCKED flag set. |
|
1565 |
* |
|
1566 |
* @param pos Position to be used for error reporting. |
|
1567 |
* @param t The type referred to. |
|
1568 |
* @returns True if the check completed on all attributed classes |
|
1569 |
*/ |
|
1570 |
private boolean checkNonCyclicInternal(DiagnosticPosition pos, Type t) { |
|
1571 |
boolean complete = true; // was the check complete? |
|
1572 |
//- System.err.println("checkNonCyclicInternal("+t+");");//DEBUG |
|
1573 |
Symbol c = t.tsym; |
|
1574 |
if ((c.flags_field & ACYCLIC) != 0) return true; |
|
1575 |
||
1576 |
if ((c.flags_field & LOCKED) != 0) { |
|
1577 |
noteCyclic(pos, (ClassSymbol)c); |
|
1578 |
} else if (!c.type.isErroneous()) { |
|
1579 |
try { |
|
1580 |
c.flags_field |= LOCKED; |
|
1581 |
if (c.type.tag == CLASS) { |
|
1582 |
ClassType clazz = (ClassType)c.type; |
|
1583 |
if (clazz.interfaces_field != null) |
|
1584 |
for (List<Type> l=clazz.interfaces_field; l.nonEmpty(); l=l.tail) |
|
1585 |
complete &= checkNonCyclicInternal(pos, l.head); |
|
1586 |
if (clazz.supertype_field != null) { |
|
1587 |
Type st = clazz.supertype_field; |
|
1588 |
if (st != null && st.tag == CLASS) |
|
1589 |
complete &= checkNonCyclicInternal(pos, st); |
|
1590 |
} |
|
1591 |
if (c.owner.kind == TYP) |
|
1592 |
complete &= checkNonCyclicInternal(pos, c.owner.type); |
|
1593 |
} |
|
1594 |
} finally { |
|
1595 |
c.flags_field &= ~LOCKED; |
|
1596 |
} |
|
1597 |
} |
|
1598 |
if (complete) |
|
1599 |
complete = ((c.flags_field & UNATTRIBUTED) == 0) && c.completer == null; |
|
1600 |
if (complete) c.flags_field |= ACYCLIC; |
|
1601 |
return complete; |
|
1602 |
} |
|
1603 |
||
1604 |
/** Note that we found an inheritance cycle. */ |
|
1605 |
private void noteCyclic(DiagnosticPosition pos, ClassSymbol c) { |
|
1606 |
log.error(pos, "cyclic.inheritance", c); |
|
1607 |
for (List<Type> l=types.interfaces(c.type); l.nonEmpty(); l=l.tail) |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1040
diff
changeset
|
1608 |
l.head = types.createErrorType((ClassSymbol)l.head.tsym, Type.noType); |
10 | 1609 |
Type st = types.supertype(c.type); |
1610 |
if (st.tag == CLASS) |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1040
diff
changeset
|
1611 |
((ClassType)c.type).supertype_field = types.createErrorType((ClassSymbol)st.tsym, Type.noType); |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1040
diff
changeset
|
1612 |
c.type = types.createErrorType(c, c.type); |
10 | 1613 |
c.flags_field |= ACYCLIC; |
1614 |
} |
|
1615 |
||
1616 |
/** Check that all methods which implement some |
|
1617 |
* method conform to the method they implement. |
|
1618 |
* @param tree The class definition whose members are checked. |
|
1619 |
*/ |
|
1620 |
void checkImplementations(JCClassDecl tree) { |
|
1621 |
checkImplementations(tree, tree.sym); |
|
1622 |
} |
|
1623 |
//where |
|
1624 |
/** Check that all methods which implement some |
|
1625 |
* method in `ic' conform to the method they implement. |
|
1626 |
*/ |
|
1627 |
void checkImplementations(JCClassDecl tree, ClassSymbol ic) { |
|
1628 |
ClassSymbol origin = tree.sym; |
|
1629 |
for (List<Type> l = types.closure(ic.type); l.nonEmpty(); l = l.tail) { |
|
1630 |
ClassSymbol lc = (ClassSymbol)l.head.tsym; |
|
1631 |
if ((allowGenerics || origin != lc) && (lc.flags() & ABSTRACT) != 0) { |
|
1632 |
for (Scope.Entry e=lc.members().elems; e != null; e=e.sibling) { |
|
1633 |
if (e.sym.kind == MTH && |
|
1634 |
(e.sym.flags() & (STATIC|ABSTRACT)) == ABSTRACT) { |
|
1635 |
MethodSymbol absmeth = (MethodSymbol)e.sym; |
|
1636 |
MethodSymbol implmeth = absmeth.implementation(origin, types, false); |
|
1637 |
if (implmeth != null && implmeth != absmeth && |
|
1638 |
(implmeth.owner.flags() & INTERFACE) == |
|
1639 |
(origin.flags() & INTERFACE)) { |
|
1640 |
// don't check if implmeth is in a class, yet |
|
1641 |
// origin is an interface. This case arises only |
|
1642 |
// if implmeth is declared in Object. The reason is |
|
1643 |
// that interfaces really don't inherit from |
|
1644 |
// Object it's just that the compiler represents |
|
1645 |
// things that way. |
|
1646 |
checkOverride(tree, implmeth, absmeth, origin); |
|
1647 |
} |
|
1648 |
} |
|
1649 |
} |
|
1650 |
} |
|
1651 |
} |
|
1652 |
} |
|
1653 |
||
1654 |
/** Check that all abstract methods implemented by a class are |
|
1655 |
* mutually compatible. |
|
1656 |
* @param pos Position to be used for error reporting. |
|
1657 |
* @param c The class whose interfaces are checked. |
|
1658 |
*/ |
|
1659 |
void checkCompatibleSupertypes(DiagnosticPosition pos, Type c) { |
|
1660 |
List<Type> supertypes = types.interfaces(c); |
|
1661 |
Type supertype = types.supertype(c); |
|
1662 |
if (supertype.tag == CLASS && |
|
1663 |
(supertype.tsym.flags() & ABSTRACT) != 0) |
|
1664 |
supertypes = supertypes.prepend(supertype); |
|
1665 |
for (List<Type> l = supertypes; l.nonEmpty(); l = l.tail) { |
|
1666 |
if (allowGenerics && !l.head.getTypeArguments().isEmpty() && |
|
1667 |
!checkCompatibleAbstracts(pos, l.head, l.head, c)) |
|
1668 |
return; |
|
1669 |
for (List<Type> m = supertypes; m != l; m = m.tail) |
|
1670 |
if (!checkCompatibleAbstracts(pos, l.head, m.head, c)) |
|
1671 |
return; |
|
1672 |
} |
|
1673 |
checkCompatibleConcretes(pos, c); |
|
1674 |
} |
|
1675 |
||
1676 |
/** Check that class c does not implement directly or indirectly |
|
1677 |
* the same parameterized interface with two different argument lists. |
|
1678 |
* @param pos Position to be used for error reporting. |
|
1679 |
* @param type The type whose interfaces are checked. |
|
1680 |
*/ |
|
1681 |
void checkClassBounds(DiagnosticPosition pos, Type type) { |
|
1682 |
checkClassBounds(pos, new HashMap<TypeSymbol,Type>(), type); |
|
1683 |
} |
|
1684 |
//where |
|
1685 |
/** Enter all interfaces of type `type' into the hash table `seensofar' |
|
1686 |
* with their class symbol as key and their type as value. Make |
|
1687 |
* sure no class is entered with two different types. |
|
1688 |
*/ |
|
1689 |
void checkClassBounds(DiagnosticPosition pos, |
|
1690 |
Map<TypeSymbol,Type> seensofar, |
|
1691 |
Type type) { |
|
1692 |
if (type.isErroneous()) return; |
|
1693 |
for (List<Type> l = types.interfaces(type); l.nonEmpty(); l = l.tail) { |
|
1694 |
Type it = l.head; |
|
1695 |
Type oldit = seensofar.put(it.tsym, it); |
|
1696 |
if (oldit != null) { |
|
1697 |
List<Type> oldparams = oldit.allparams(); |
|
1698 |
List<Type> newparams = it.allparams(); |
|
1699 |
if (!types.containsTypeEquivalent(oldparams, newparams)) |
|
1700 |
log.error(pos, "cant.inherit.diff.arg", |
|
1701 |
it.tsym, Type.toString(oldparams), |
|
1702 |
Type.toString(newparams)); |
|
1703 |
} |
|
1704 |
checkClassBounds(pos, seensofar, it); |
|
1705 |
} |
|
1706 |
Type st = types.supertype(type); |
|
1707 |
if (st != null) checkClassBounds(pos, seensofar, st); |
|
1708 |
} |
|
1709 |
||
1710 |
/** Enter interface into into set. |
|
1711 |
* If it existed already, issue a "repeated interface" error. |
|
1712 |
*/ |
|
1713 |
void checkNotRepeated(DiagnosticPosition pos, Type it, Set<Type> its) { |
|
1714 |
if (its.contains(it)) |
|
1715 |
log.error(pos, "repeated.interface"); |
|
1716 |
else { |
|
1717 |
its.add(it); |
|
1718 |
} |
|
1719 |
} |
|
1720 |
||
1721 |
/* ************************************************************************* |
|
1722 |
* Check annotations |
|
1723 |
**************************************************************************/ |
|
1724 |
||
1725 |
/** Annotation types are restricted to primitives, String, an |
|
1726 |
* enum, an annotation, Class, Class<?>, Class<? extends |
|
1727 |
* Anything>, arrays of the preceding. |
|
1728 |
*/ |
|
1729 |
void validateAnnotationType(JCTree restype) { |
|
1730 |
// restype may be null if an error occurred, so don't bother validating it |
|
1731 |
if (restype != null) { |
|
1732 |
validateAnnotationType(restype.pos(), restype.type); |
|
1733 |
} |
|
1734 |
} |
|
1735 |
||
1736 |
void validateAnnotationType(DiagnosticPosition pos, Type type) { |
|
1737 |
if (type.isPrimitive()) return; |
|
1738 |
if (types.isSameType(type, syms.stringType)) return; |
|
1739 |
if ((type.tsym.flags() & Flags.ENUM) != 0) return; |
|
1740 |
if ((type.tsym.flags() & Flags.ANNOTATION) != 0) return; |
|
1741 |
if (types.lowerBound(type).tsym == syms.classType.tsym) return; |
|
1742 |
if (types.isArray(type) && !types.isArray(types.elemtype(type))) { |
|
1743 |
validateAnnotationType(pos, types.elemtype(type)); |
|
1744 |
return; |
|
1745 |
} |
|
1746 |
log.error(pos, "invalid.annotation.member.type"); |
|
1747 |
} |
|
1748 |
||
1749 |
/** |
|
1750 |
* "It is also a compile-time error if any method declared in an |
|
1751 |
* annotation type has a signature that is override-equivalent to |
|
1752 |
* that of any public or protected method declared in class Object |
|
1753 |
* or in the interface annotation.Annotation." |
|
1754 |
* |
|
1755 |
* @jls3 9.6 Annotation Types |
|
1756 |
*/ |
|
1757 |
void validateAnnotationMethod(DiagnosticPosition pos, MethodSymbol m) { |
|
1758 |
for (Type sup = syms.annotationType; sup.tag == CLASS; sup = types.supertype(sup)) { |
|
1759 |
Scope s = sup.tsym.members(); |
|
1760 |
for (Scope.Entry e = s.lookup(m.name); e.scope != null; e = e.next()) { |
|
1761 |
if (e.sym.kind == MTH && |
|
1762 |
(e.sym.flags() & (PUBLIC | PROTECTED)) != 0 && |
|
1763 |
types.overrideEquivalent(m.type, e.sym.type)) |
|
1764 |
log.error(pos, "intf.annotation.member.clash", e.sym, sup); |
|
1765 |
} |
|
1766 |
} |
|
1767 |
} |
|
1768 |
||
1769 |
/** Check the annotations of a symbol. |
|
1770 |
*/ |
|
1771 |
public void validateAnnotations(List<JCAnnotation> annotations, Symbol s) { |
|
1772 |
if (skipAnnotations) return; |
|
1773 |
for (JCAnnotation a : annotations) |
|
1774 |
validateAnnotation(a, s); |
|
1775 |
} |
|
1776 |
||
1777 |
/** Check an annotation of a symbol. |
|
1778 |
*/ |
|
1779 |
public void validateAnnotation(JCAnnotation a, Symbol s) { |
|
1780 |
validateAnnotation(a); |
|
1781 |
||
1782 |
if (!annotationApplicable(a, s)) |
|
1783 |
log.error(a.pos(), "annotation.type.not.applicable"); |
|
1784 |
||
1785 |
if (a.annotationType.type.tsym == syms.overrideType.tsym) { |
|
1786 |
if (!isOverrider(s)) |
|
1787 |
log.error(a.pos(), "method.does.not.override.superclass"); |
|
1788 |
} |
|
1789 |
} |
|
1790 |
||
1791 |
/** Is s a method symbol that overrides a method in a superclass? */ |
|
1792 |
boolean isOverrider(Symbol s) { |
|
1793 |
if (s.kind != MTH || s.isStatic()) |
|
1794 |
return false; |
|
1795 |
MethodSymbol m = (MethodSymbol)s; |
|
1796 |
TypeSymbol owner = (TypeSymbol)m.owner; |
|
1797 |
for (Type sup : types.closure(owner.type)) { |
|
1798 |
if (sup == owner.type) |
|
1799 |
continue; // skip "this" |
|
1800 |
Scope scope = sup.tsym.members(); |
|
1801 |
for (Scope.Entry e = scope.lookup(m.name); e.scope != null; e = e.next()) { |
|
1802 |
if (!e.sym.isStatic() && m.overrides(e.sym, owner, types, true)) |
|
1803 |
return true; |
|
1804 |
} |
|
1805 |
} |
|
1806 |
return false; |
|
1807 |
} |
|
1808 |
||
1809 |
/** Is the annotation applicable to the symbol? */ |
|
1810 |
boolean annotationApplicable(JCAnnotation a, Symbol s) { |
|
1811 |
Attribute.Compound atTarget = |
|
1812 |
a.annotationType.type.tsym.attribute(syms.annotationTargetType.tsym); |
|
1813 |
if (atTarget == null) return true; |
|
1814 |
Attribute atValue = atTarget.member(names.value); |
|
1815 |
if (!(atValue instanceof Attribute.Array)) return true; // error recovery |
|
1816 |
Attribute.Array arr = (Attribute.Array) atValue; |
|
1817 |
for (Attribute app : arr.values) { |
|
1818 |
if (!(app instanceof Attribute.Enum)) return true; // recovery |
|
1819 |
Attribute.Enum e = (Attribute.Enum) app; |
|
1820 |
if (e.value.name == names.TYPE) |
|
1821 |
{ if (s.kind == TYP) return true; } |
|
1822 |
else if (e.value.name == names.FIELD) |
|
1823 |
{ if (s.kind == VAR && s.owner.kind != MTH) return true; } |
|
1824 |
else if (e.value.name == names.METHOD) |
|
1825 |
{ if (s.kind == MTH && !s.isConstructor()) return true; } |
|
1826 |
else if (e.value.name == names.PARAMETER) |
|
1827 |
{ if (s.kind == VAR && |
|
1828 |
s.owner.kind == MTH && |
|
1829 |
(s.flags() & PARAMETER) != 0) |
|
1830 |
return true; |
|
1831 |
} |
|
1832 |
else if (e.value.name == names.CONSTRUCTOR) |
|
1833 |
{ if (s.kind == MTH && s.isConstructor()) return true; } |
|
1834 |
else if (e.value.name == names.LOCAL_VARIABLE) |
|
1835 |
{ if (s.kind == VAR && s.owner.kind == MTH && |
|
1836 |
(s.flags() & PARAMETER) == 0) |
|
1837 |
return true; |
|
1838 |
} |
|
1839 |
else if (e.value.name == names.ANNOTATION_TYPE) |
|
1840 |
{ if (s.kind == TYP && (s.flags() & ANNOTATION) != 0) |
|
1841 |
return true; |
|
1842 |
} |
|
1843 |
else if (e.value.name == names.PACKAGE) |
|
1844 |
{ if (s.kind == PCK) return true; } |
|
1845 |
else |
|
1846 |
return true; // recovery |
|
1847 |
} |
|
1848 |
return false; |
|
1849 |
} |
|
1850 |
||
1851 |
/** Check an annotation value. |
|
1852 |
*/ |
|
1853 |
public void validateAnnotation(JCAnnotation a) { |
|
1854 |
if (a.type.isErroneous()) return; |
|
1855 |
||
1856 |
// collect an inventory of the members |
|
1857 |
Set<MethodSymbol> members = new HashSet<MethodSymbol>(); |
|
1858 |
for (Scope.Entry e = a.annotationType.type.tsym.members().elems; |
|
1859 |
e != null; |
|
1860 |
e = e.sibling) |
|
1861 |
if (e.sym.kind == MTH) |
|
1862 |
members.add((MethodSymbol) e.sym); |
|
1863 |
||
1864 |
// count them off as they're annotated |
|
1865 |
for (JCTree arg : a.args) { |
|
1866 |
if (arg.getTag() != JCTree.ASSIGN) continue; // recovery |
|
1867 |
JCAssign assign = (JCAssign) arg; |
|
1868 |
Symbol m = TreeInfo.symbol(assign.lhs); |
|
1869 |
if (m == null || m.type.isErroneous()) continue; |
|
1870 |
if (!members.remove(m)) |
|
1871 |
log.error(arg.pos(), "duplicate.annotation.member.value", |
|
1872 |
m.name, a.type); |
|
1873 |
if (assign.rhs.getTag() == ANNOTATION) |
|
1874 |
validateAnnotation((JCAnnotation)assign.rhs); |
|
1875 |
} |
|
1876 |
||
1877 |
// all the remaining ones better have default values |
|
1878 |
for (MethodSymbol m : members) |
|
1879 |
if (m.defaultValue == null && !m.type.isErroneous()) |
|
1880 |
log.error(a.pos(), "annotation.missing.default.value", |
|
1881 |
a.type, m.name); |
|
1882 |
||
1883 |
// special case: java.lang.annotation.Target must not have |
|
1884 |
// repeated values in its value member |
|
1885 |
if (a.annotationType.type.tsym != syms.annotationTargetType.tsym || |
|
1886 |
a.args.tail == null) |
|
1887 |
return; |
|
1888 |
||
1889 |
if (a.args.head.getTag() != JCTree.ASSIGN) return; // error recovery |
|
1890 |
JCAssign assign = (JCAssign) a.args.head; |
|
1891 |
Symbol m = TreeInfo.symbol(assign.lhs); |
|
1892 |
if (m.name != names.value) return; |
|
1893 |
JCTree rhs = assign.rhs; |
|
1894 |
if (rhs.getTag() != JCTree.NEWARRAY) return; |
|
1895 |
JCNewArray na = (JCNewArray) rhs; |
|
1896 |
Set<Symbol> targets = new HashSet<Symbol>(); |
|
1897 |
for (JCTree elem : na.elems) { |
|
1898 |
if (!targets.add(TreeInfo.symbol(elem))) { |
|
1899 |
log.error(elem.pos(), "repeated.annotation.target"); |
|
1900 |
} |
|
1901 |
} |
|
1902 |
} |
|
1903 |
||
1904 |
void checkDeprecatedAnnotation(DiagnosticPosition pos, Symbol s) { |
|
1905 |
if (allowAnnotations && |
|
1906 |
lint.isEnabled(Lint.LintCategory.DEP_ANN) && |
|
1907 |
(s.flags() & DEPRECATED) != 0 && |
|
1908 |
!syms.deprecatedType.isErroneous() && |
|
1909 |
s.attribute(syms.deprecatedType.tsym) == null) { |
|
1910 |
log.warning(pos, "missing.deprecated.annotation"); |
|
1911 |
} |
|
1912 |
} |
|
1913 |
||
1914 |
/* ************************************************************************* |
|
1915 |
* Check for recursive annotation elements. |
|
1916 |
**************************************************************************/ |
|
1917 |
||
1918 |
/** Check for cycles in the graph of annotation elements. |
|
1919 |
*/ |
|
1920 |
void checkNonCyclicElements(JCClassDecl tree) { |
|
1921 |
if ((tree.sym.flags_field & ANNOTATION) == 0) return; |
|
1922 |
assert (tree.sym.flags_field & LOCKED) == 0; |
|
1923 |
try { |
|
1924 |
tree.sym.flags_field |= LOCKED; |
|
1925 |
for (JCTree def : tree.defs) { |
|
1926 |
if (def.getTag() != JCTree.METHODDEF) continue; |
|
1927 |
JCMethodDecl meth = (JCMethodDecl)def; |
|
1928 |
checkAnnotationResType(meth.pos(), meth.restype.type); |
|
1929 |
} |
|
1930 |
} finally { |
|
1931 |
tree.sym.flags_field &= ~LOCKED; |
|
1932 |
tree.sym.flags_field |= ACYCLIC_ANN; |
|
1933 |
} |
|
1934 |
} |
|
1935 |
||
1936 |
void checkNonCyclicElementsInternal(DiagnosticPosition pos, TypeSymbol tsym) { |
|
1937 |
if ((tsym.flags_field & ACYCLIC_ANN) != 0) |
|
1938 |
return; |
|
1939 |
if ((tsym.flags_field & LOCKED) != 0) { |
|
1940 |
log.error(pos, "cyclic.annotation.element"); |
|
1941 |
return; |
|
1942 |
} |
|
1943 |
try { |
|
1944 |
tsym.flags_field |= LOCKED; |
|
1945 |
for (Scope.Entry e = tsym.members().elems; e != null; e = e.sibling) { |
|
1946 |
Symbol s = e.sym; |
|
1947 |
if (s.kind != Kinds.MTH) |
|
1948 |
continue; |
|
1949 |
checkAnnotationResType(pos, ((MethodSymbol)s).type.getReturnType()); |
|
1950 |
} |
|
1951 |
} finally { |
|
1952 |
tsym.flags_field &= ~LOCKED; |
|
1953 |
tsym.flags_field |= ACYCLIC_ANN; |
|
1954 |
} |
|
1955 |
} |
|
1956 |
||
1957 |
void checkAnnotationResType(DiagnosticPosition pos, Type type) { |
|
1958 |
switch (type.tag) { |
|
1959 |
case TypeTags.CLASS: |
|
1960 |
if ((type.tsym.flags() & ANNOTATION) != 0) |
|
1961 |
checkNonCyclicElementsInternal(pos, type.tsym); |
|
1962 |
break; |
|
1963 |
case TypeTags.ARRAY: |
|
1964 |
checkAnnotationResType(pos, types.elemtype(type)); |
|
1965 |
break; |
|
1966 |
default: |
|
1967 |
break; // int etc |
|
1968 |
} |
|
1969 |
} |
|
1970 |
||
1971 |
/* ************************************************************************* |
|
1972 |
* Check for cycles in the constructor call graph. |
|
1973 |
**************************************************************************/ |
|
1974 |
||
1975 |
/** Check for cycles in the graph of constructors calling other |
|
1976 |
* constructors. |
|
1977 |
*/ |
|
1978 |
void checkCyclicConstructors(JCClassDecl tree) { |
|
1979 |
Map<Symbol,Symbol> callMap = new HashMap<Symbol, Symbol>(); |
|
1980 |
||
1981 |
// enter each constructor this-call into the map |
|
1982 |
for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) { |
|
1983 |
JCMethodInvocation app = TreeInfo.firstConstructorCall(l.head); |
|
1984 |
if (app == null) continue; |
|
1985 |
JCMethodDecl meth = (JCMethodDecl) l.head; |
|
1986 |
if (TreeInfo.name(app.meth) == names._this) { |
|
1987 |
callMap.put(meth.sym, TreeInfo.symbol(app.meth)); |
|
1988 |
} else { |
|
1989 |
meth.sym.flags_field |= ACYCLIC; |
|
1990 |
} |
|
1991 |
} |
|
1992 |
||
1993 |
// Check for cycles in the map |
|
1994 |
Symbol[] ctors = new Symbol[0]; |
|
1995 |
ctors = callMap.keySet().toArray(ctors); |
|
1996 |
for (Symbol caller : ctors) { |
|
1997 |
checkCyclicConstructor(tree, caller, callMap); |
|
1998 |
} |
|
1999 |
} |
|
2000 |
||
2001 |
/** Look in the map to see if the given constructor is part of a |
|
2002 |
* call cycle. |
|
2003 |
*/ |
|
2004 |
private void checkCyclicConstructor(JCClassDecl tree, Symbol ctor, |
|
2005 |
Map<Symbol,Symbol> callMap) { |
|
2006 |
if (ctor != null && (ctor.flags_field & ACYCLIC) == 0) { |
|
2007 |
if ((ctor.flags_field & LOCKED) != 0) { |
|
2008 |
log.error(TreeInfo.diagnosticPositionFor(ctor, tree), |
|
2009 |
"recursive.ctor.invocation"); |
|
2010 |
} else { |
|
2011 |
ctor.flags_field |= LOCKED; |
|
2012 |
checkCyclicConstructor(tree, callMap.remove(ctor), callMap); |
|
2013 |
ctor.flags_field &= ~LOCKED; |
|
2014 |
} |
|
2015 |
ctor.flags_field |= ACYCLIC; |
|
2016 |
} |
|
2017 |
} |
|
2018 |
||
2019 |
/* ************************************************************************* |
|
2020 |
* Miscellaneous |
|
2021 |
**************************************************************************/ |
|
2022 |
||
2023 |
/** |
|
2024 |
* Return the opcode of the operator but emit an error if it is an |
|
2025 |
* error. |
|
2026 |
* @param pos position for error reporting. |
|
2027 |
* @param operator an operator |
|
2028 |
* @param tag a tree tag |
|
2029 |
* @param left type of left hand side |
|
2030 |
* @param right type of right hand side |
|
2031 |
*/ |
|
2032 |
int checkOperator(DiagnosticPosition pos, |
|
2033 |
OperatorSymbol operator, |
|
2034 |
int tag, |
|
2035 |
Type left, |
|
2036 |
Type right) { |
|
2037 |
if (operator.opcode == ByteCodes.error) { |
|
2038 |
log.error(pos, |
|
2039 |
"operator.cant.be.applied", |
|
2040 |
treeinfo.operatorName(tag), |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
938
diff
changeset
|
2041 |
List.of(left, right)); |
10 | 2042 |
} |
2043 |
return operator.opcode; |
|
2044 |
} |
|
2045 |
||
2046 |
||
2047 |
/** |
|
2048 |
* Check for division by integer constant zero |
|
2049 |
* @param pos Position for error reporting. |
|
2050 |
* @param operator The operator for the expression |
|
2051 |
* @param operand The right hand operand for the expression |
|
2052 |
*/ |
|
2053 |
void checkDivZero(DiagnosticPosition pos, Symbol operator, Type operand) { |
|
2054 |
if (operand.constValue() != null |
|
2055 |
&& lint.isEnabled(Lint.LintCategory.DIVZERO) |
|
2056 |
&& operand.tag <= LONG |
|
2057 |
&& ((Number) (operand.constValue())).longValue() == 0) { |
|
2058 |
int opc = ((OperatorSymbol)operator).opcode; |
|
2059 |
if (opc == ByteCodes.idiv || opc == ByteCodes.imod |
|
2060 |
|| opc == ByteCodes.ldiv || opc == ByteCodes.lmod) { |
|
2061 |
log.warning(pos, "div.zero"); |
|
2062 |
} |
|
2063 |
} |
|
2064 |
} |
|
2065 |
||
2066 |
/** |
|
2067 |
* Check for empty statements after if |
|
2068 |
*/ |
|
2069 |
void checkEmptyIf(JCIf tree) { |
|
2070 |
if (tree.thenpart.getTag() == JCTree.SKIP && tree.elsepart == null && lint.isEnabled(Lint.LintCategory.EMPTY)) |
|
2071 |
log.warning(tree.thenpart.pos(), "empty.if"); |
|
2072 |
} |
|
2073 |
||
2074 |
/** Check that symbol is unique in given scope. |
|
2075 |
* @param pos Position for error reporting. |
|
2076 |
* @param sym The symbol. |
|
2077 |
* @param s The scope. |
|
2078 |
*/ |
|
2079 |
boolean checkUnique(DiagnosticPosition pos, Symbol sym, Scope s) { |
|
2080 |
if (sym.type.isErroneous()) |
|
2081 |
return true; |
|
2082 |
if (sym.owner.name == names.any) return false; |
|
2083 |
for (Scope.Entry e = s.lookup(sym.name); e.scope == s; e = e.next()) { |
|
2084 |
if (sym != e.sym && |
|
2085 |
sym.kind == e.sym.kind && |
|
2086 |
sym.name != names.error && |
|
2087 |
(sym.kind != MTH || types.overrideEquivalent(sym.type, e.sym.type))) { |
|
2088 |
if ((sym.flags() & VARARGS) != (e.sym.flags() & VARARGS)) |
|
2089 |
varargsDuplicateError(pos, sym, e.sym); |
|
2090 |
else |
|
2091 |
duplicateError(pos, e.sym); |
|
2092 |
return false; |
|
2093 |
} |
|
2094 |
} |
|
2095 |
return true; |
|
2096 |
} |
|
2097 |
||
2098 |
/** Check that single-type import is not already imported or top-level defined, |
|
2099 |
* but make an exception for two single-type imports which denote the same type. |
|
2100 |
* @param pos Position for error reporting. |
|
2101 |
* @param sym The symbol. |
|
2102 |
* @param s The scope |
|
2103 |
*/ |
|
2104 |
boolean checkUniqueImport(DiagnosticPosition pos, Symbol sym, Scope s) { |
|
2105 |
return checkUniqueImport(pos, sym, s, false); |
|
2106 |
} |
|
2107 |
||
2108 |
/** Check that static single-type import is not already imported or top-level defined, |
|
2109 |
* but make an exception for two single-type imports which denote the same type. |
|
2110 |
* @param pos Position for error reporting. |
|
2111 |
* @param sym The symbol. |
|
2112 |
* @param s The scope |
|
2113 |
* @param staticImport Whether or not this was a static import |
|
2114 |
*/ |
|
2115 |
boolean checkUniqueStaticImport(DiagnosticPosition pos, Symbol sym, Scope s) { |
|
2116 |
return checkUniqueImport(pos, sym, s, true); |
|
2117 |
} |
|
2118 |
||
2119 |
/** Check that single-type import is not already imported or top-level defined, |
|
2120 |
* but make an exception for two single-type imports which denote the same type. |
|
2121 |
* @param pos Position for error reporting. |
|
2122 |
* @param sym The symbol. |
|
2123 |
* @param s The scope. |
|
2124 |
* @param staticImport Whether or not this was a static import |
|
2125 |
*/ |
|
2126 |
private boolean checkUniqueImport(DiagnosticPosition pos, Symbol sym, Scope s, boolean staticImport) { |
|
2127 |
for (Scope.Entry e = s.lookup(sym.name); e.scope != null; e = e.next()) { |
|
2128 |
// is encountered class entered via a class declaration? |
|
2129 |
boolean isClassDecl = e.scope == s; |
|
2130 |
if ((isClassDecl || sym != e.sym) && |
|
2131 |
sym.kind == e.sym.kind && |
|
2132 |
sym.name != names.error) { |
|
2133 |
if (!e.sym.type.isErroneous()) { |
|
2134 |
String what = e.sym.toString(); |
|
2135 |
if (!isClassDecl) { |
|
2136 |
if (staticImport) |
|
2137 |
log.error(pos, "already.defined.static.single.import", what); |
|
2138 |
else |
|
2139 |
log.error(pos, "already.defined.single.import", what); |
|
2140 |
} |
|
2141 |
else if (sym != e.sym) |
|
2142 |
log.error(pos, "already.defined.this.unit", what); |
|
2143 |
} |
|
2144 |
return false; |
|
2145 |
} |
|
2146 |
} |
|
2147 |
return true; |
|
2148 |
} |
|
2149 |
||
2150 |
/** Check that a qualified name is in canonical form (for import decls). |
|
2151 |
*/ |
|
2152 |
public void checkCanonical(JCTree tree) { |
|
2153 |
if (!isCanonical(tree)) |
|
2154 |
log.error(tree.pos(), "import.requires.canonical", |
|
2155 |
TreeInfo.symbol(tree)); |
|
2156 |
} |
|
2157 |
// where |
|
2158 |
private boolean isCanonical(JCTree tree) { |
|
2159 |
while (tree.getTag() == JCTree.SELECT) { |
|
2160 |
JCFieldAccess s = (JCFieldAccess) tree; |
|
2161 |
if (s.sym.owner != TreeInfo.symbol(s.selected)) |
|
2162 |
return false; |
|
2163 |
tree = s.selected; |
|
2164 |
} |
|
2165 |
return true; |
|
2166 |
} |
|
2167 |
||
2168 |
private class ConversionWarner extends Warner { |
|
2169 |
final String key; |
|
2170 |
final Type found; |
|
2171 |
final Type expected; |
|
2172 |
public ConversionWarner(DiagnosticPosition pos, String key, Type found, Type expected) { |
|
2173 |
super(pos); |
|
2174 |
this.key = key; |
|
2175 |
this.found = found; |
|
2176 |
this.expected = expected; |
|
2177 |
} |
|
2178 |
||
2179 |
public void warnUnchecked() { |
|
2180 |
boolean warned = this.warned; |
|
2181 |
super.warnUnchecked(); |
|
2182 |
if (warned) return; // suppress redundant diagnostics |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
2183 |
Object problem = diags.fragment(key); |
10 | 2184 |
Check.this.warnUnchecked(pos(), "prob.found.req", problem, found, expected); |
2185 |
} |
|
2186 |
} |
|
2187 |
||
2188 |
public Warner castWarner(DiagnosticPosition pos, Type found, Type expected) { |
|
2189 |
return new ConversionWarner(pos, "unchecked.cast.to.type", found, expected); |
|
2190 |
} |
|
2191 |
||
2192 |
public Warner convertWarner(DiagnosticPosition pos, Type found, Type expected) { |
|
2193 |
return new ConversionWarner(pos, "unchecked.assign", found, expected); |
|
2194 |
} |
|
2195 |
} |