author | jjg |
Thu, 30 Sep 2010 10:47:12 -0700 | |
changeset 6721 | d92073844278 |
parent 6711 | fc9732adc790 |
child 7214 | 26dfa4a69593 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
5520 | 2 |
* Copyright (c) 1999, 2009, Oracle and/or its affiliates. All rights reserved. |
10 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5520 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.comp; |
|
27 |
||
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 |
||
6721 | 48 |
import static com.sun.tools.javac.main.OptionName.*; |
49 |
||
10 | 50 |
/** Type checking helper class for the attribution phase. |
51 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5846
diff
changeset
|
52 |
* <p><b>This is NOT part of any supported API. |
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5846
diff
changeset
|
53 |
* If you write code that depends on this, you do so at your own risk. |
10 | 54 |
* This code and its internal interfaces are subject to change or |
55 |
* deletion without notice.</b> |
|
56 |
*/ |
|
57 |
public class Check { |
|
58 |
protected static final Context.Key<Check> checkKey = |
|
59 |
new Context.Key<Check>(); |
|
60 |
||
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1257
diff
changeset
|
61 |
private final Names names; |
10 | 62 |
private final Log log; |
63 |
private final Symtab syms; |
|
6711
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
64 |
private final Enter enter; |
10 | 65 |
private final Infer infer; |
66 |
private final Types types; |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
67 |
private final JCDiagnostic.Factory diags; |
10 | 68 |
private final boolean skipAnnotations; |
3557
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
69 |
private boolean warnOnSyntheticConflicts; |
5842
714e365b4b1b
6955264: add option to suppress Abort in Check.completionError
jjg
parents:
5654
diff
changeset
|
70 |
private boolean suppressAbortOnBadClassFile; |
10 | 71 |
private final TreeInfo treeinfo; |
72 |
||
73 |
// The set of lint options currently in effect. It is initialized |
|
74 |
// from the context, and then is set/reset as needed by Attr as it |
|
75 |
// visits all the various parts of the trees during attribution. |
|
76 |
private Lint lint; |
|
77 |
||
78 |
public static Check instance(Context context) { |
|
79 |
Check instance = context.get(checkKey); |
|
80 |
if (instance == null) |
|
81 |
instance = new Check(context); |
|
82 |
return instance; |
|
83 |
} |
|
84 |
||
85 |
protected Check(Context context) { |
|
86 |
context.put(checkKey, this); |
|
87 |
||
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1257
diff
changeset
|
88 |
names = Names.instance(context); |
10 | 89 |
log = Log.instance(context); |
90 |
syms = Symtab.instance(context); |
|
6711
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
91 |
enter = Enter.instance(context); |
10 | 92 |
infer = Infer.instance(context); |
93 |
this.types = Types.instance(context); |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
94 |
diags = JCDiagnostic.Factory.instance(context); |
10 | 95 |
Options options = Options.instance(context); |
96 |
lint = Lint.instance(context); |
|
97 |
treeinfo = TreeInfo.instance(context); |
|
98 |
||
99 |
Source source = Source.instance(context); |
|
100 |
allowGenerics = source.allowGenerics(); |
|
101 |
allowAnnotations = source.allowAnnotations(); |
|
3780
368edd3d2082
6874249: Check has duplicate local variable and field for "source"
jjg
parents:
3765
diff
changeset
|
102 |
allowCovariantReturns = source.allowCovariantReturns(); |
6721 | 103 |
complexInference = options.isSet(COMPLEXINFERENCE); |
104 |
skipAnnotations = options.isSet("skipAnnotations"); |
|
105 |
warnOnSyntheticConflicts = options.isSet("warnOnSyntheticConflicts"); |
|
106 |
suppressAbortOnBadClassFile = options.isSet("suppressAbortOnBadClassFile"); |
|
10 | 107 |
|
3780
368edd3d2082
6874249: Check has duplicate local variable and field for "source"
jjg
parents:
3765
diff
changeset
|
108 |
Target target = Target.instance(context); |
368edd3d2082
6874249: Check has duplicate local variable and field for "source"
jjg
parents:
3765
diff
changeset
|
109 |
syntheticNameChar = target.syntheticNameChar(); |
368edd3d2082
6874249: Check has duplicate local variable and field for "source"
jjg
parents:
3765
diff
changeset
|
110 |
|
10 | 111 |
boolean verboseDeprecated = lint.isEnabled(LintCategory.DEPRECATION); |
112 |
boolean verboseUnchecked = lint.isEnabled(LintCategory.UNCHECKED); |
|
5846
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
113 |
boolean verboseVarargs = lint.isEnabled(LintCategory.VARARGS); |
3661 | 114 |
boolean verboseSunApi = lint.isEnabled(LintCategory.SUNAPI); |
813 | 115 |
boolean enforceMandatoryWarnings = source.enforceMandatoryWarnings(); |
10 | 116 |
|
813 | 117 |
deprecationHandler = new MandatoryWarningHandler(log, verboseDeprecated, |
6151
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
6148
diff
changeset
|
118 |
enforceMandatoryWarnings, "deprecated", LintCategory.DEPRECATION); |
813 | 119 |
uncheckedHandler = new MandatoryWarningHandler(log, verboseUnchecked, |
6151
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
6148
diff
changeset
|
120 |
enforceMandatoryWarnings, "unchecked", LintCategory.UNCHECKED); |
5846
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
121 |
unsafeVarargsHandler = new MandatoryWarningHandler(log, verboseVarargs, |
6151
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
6148
diff
changeset
|
122 |
enforceMandatoryWarnings, "varargs", LintCategory.VARARGS); |
3661 | 123 |
sunApiHandler = new MandatoryWarningHandler(log, verboseSunApi, |
6151
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
6148
diff
changeset
|
124 |
enforceMandatoryWarnings, "sunapi", null); |
10 | 125 |
} |
126 |
||
127 |
/** Switch: generics enabled? |
|
128 |
*/ |
|
129 |
boolean allowGenerics; |
|
130 |
||
131 |
/** Switch: annotations enabled? |
|
132 |
*/ |
|
133 |
boolean allowAnnotations; |
|
134 |
||
3780
368edd3d2082
6874249: Check has duplicate local variable and field for "source"
jjg
parents:
3765
diff
changeset
|
135 |
/** Switch: covariant returns enabled? |
368edd3d2082
6874249: Check has duplicate local variable and field for "source"
jjg
parents:
3765
diff
changeset
|
136 |
*/ |
368edd3d2082
6874249: Check has duplicate local variable and field for "source"
jjg
parents:
3765
diff
changeset
|
137 |
boolean allowCovariantReturns; |
368edd3d2082
6874249: Check has duplicate local variable and field for "source"
jjg
parents:
3765
diff
changeset
|
138 |
|
10 | 139 |
/** Switch: -complexinference option set? |
140 |
*/ |
|
141 |
boolean complexInference; |
|
142 |
||
3780
368edd3d2082
6874249: Check has duplicate local variable and field for "source"
jjg
parents:
3765
diff
changeset
|
143 |
/** Character for synthetic names |
368edd3d2082
6874249: Check has duplicate local variable and field for "source"
jjg
parents:
3765
diff
changeset
|
144 |
*/ |
368edd3d2082
6874249: Check has duplicate local variable and field for "source"
jjg
parents:
3765
diff
changeset
|
145 |
char syntheticNameChar; |
368edd3d2082
6874249: Check has duplicate local variable and field for "source"
jjg
parents:
3765
diff
changeset
|
146 |
|
10 | 147 |
/** A table mapping flat names of all compiled classes in this run to their |
148 |
* symbols; maintained from outside. |
|
149 |
*/ |
|
150 |
public Map<Name,ClassSymbol> compiled = new HashMap<Name, ClassSymbol>(); |
|
151 |
||
152 |
/** A handler for messages about deprecated usage. |
|
153 |
*/ |
|
154 |
private MandatoryWarningHandler deprecationHandler; |
|
155 |
||
156 |
/** A handler for messages about unchecked or unsafe usage. |
|
157 |
*/ |
|
158 |
private MandatoryWarningHandler uncheckedHandler; |
|
159 |
||
5846
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
160 |
/** A handler for messages about unchecked or unsafe vararg method decl. |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
161 |
*/ |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
162 |
private MandatoryWarningHandler unsafeVarargsHandler; |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
163 |
|
5848
c5a4ce47e780
6960407: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5847
diff
changeset
|
164 |
/** A handler for messages about using proprietary API. |
3661 | 165 |
*/ |
166 |
private MandatoryWarningHandler sunApiHandler; |
|
10 | 167 |
|
168 |
/* ************************************************************************* |
|
169 |
* Errors and Warnings |
|
170 |
**************************************************************************/ |
|
171 |
||
172 |
Lint setLint(Lint newLint) { |
|
173 |
Lint prev = lint; |
|
174 |
lint = newLint; |
|
175 |
return prev; |
|
176 |
} |
|
177 |
||
178 |
/** Warn about deprecated symbol. |
|
179 |
* @param pos Position to be used for error reporting. |
|
180 |
* @param sym The deprecated symbol. |
|
181 |
*/ |
|
182 |
void warnDeprecated(DiagnosticPosition pos, Symbol sym) { |
|
183 |
if (!lint.isSuppressed(LintCategory.DEPRECATION)) |
|
184 |
deprecationHandler.report(pos, "has.been.deprecated", sym, sym.location()); |
|
185 |
} |
|
186 |
||
187 |
/** Warn about unchecked operation. |
|
188 |
* @param pos Position to be used for error reporting. |
|
189 |
* @param msg A string describing the problem. |
|
190 |
*/ |
|
191 |
public void warnUnchecked(DiagnosticPosition pos, String msg, Object... args) { |
|
192 |
if (!lint.isSuppressed(LintCategory.UNCHECKED)) |
|
193 |
uncheckedHandler.report(pos, msg, args); |
|
194 |
} |
|
195 |
||
5846
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
196 |
/** Warn about unsafe vararg method decl. |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
197 |
* @param pos Position to be used for error reporting. |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
198 |
* @param sym The deprecated symbol. |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
199 |
*/ |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
200 |
void warnUnsafeVararg(DiagnosticPosition pos, Type elemType) { |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
201 |
if (!lint.isSuppressed(LintCategory.VARARGS)) |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
202 |
unsafeVarargsHandler.report(pos, "varargs.non.reifiable.type", elemType); |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
203 |
} |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
204 |
|
5848
c5a4ce47e780
6960407: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5847
diff
changeset
|
205 |
/** Warn about using proprietary API. |
3661 | 206 |
* @param pos Position to be used for error reporting. |
207 |
* @param msg A string describing the problem. |
|
208 |
*/ |
|
209 |
public void warnSunApi(DiagnosticPosition pos, String msg, Object... args) { |
|
210 |
if (!lint.isSuppressed(LintCategory.SUNAPI)) |
|
211 |
sunApiHandler.report(pos, msg, args); |
|
212 |
} |
|
213 |
||
5002
12a9e8562200
4880220: Add a warning when accessing a static method via an reference
jjg
parents:
4867
diff
changeset
|
214 |
public void warnStatic(DiagnosticPosition pos, String msg, Object... args) { |
12a9e8562200
4880220: Add a warning when accessing a static method via an reference
jjg
parents:
4867
diff
changeset
|
215 |
if (lint.isEnabled(LintCategory.STATIC)) |
6151
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
6148
diff
changeset
|
216 |
log.warning(LintCategory.STATIC, pos, msg, args); |
5002
12a9e8562200
4880220: Add a warning when accessing a static method via an reference
jjg
parents:
4867
diff
changeset
|
217 |
} |
12a9e8562200
4880220: Add a warning when accessing a static method via an reference
jjg
parents:
4867
diff
changeset
|
218 |
|
10 | 219 |
/** |
220 |
* Report any deferred diagnostics. |
|
221 |
*/ |
|
222 |
public void reportDeferredDiagnostics() { |
|
223 |
deprecationHandler.reportDeferredDiagnostic(); |
|
224 |
uncheckedHandler.reportDeferredDiagnostic(); |
|
5846
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
225 |
unsafeVarargsHandler.reportDeferredDiagnostic(); |
3661 | 226 |
sunApiHandler.reportDeferredDiagnostic(); |
10 | 227 |
} |
228 |
||
229 |
||
230 |
/** Report a failure to complete a class. |
|
231 |
* @param pos Position to be used for error reporting. |
|
232 |
* @param ex The failure to report. |
|
233 |
*/ |
|
234 |
public Type completionError(DiagnosticPosition pos, CompletionFailure ex) { |
|
169 | 235 |
log.error(pos, "cant.access", ex.sym, ex.getDetailValue()); |
5842
714e365b4b1b
6955264: add option to suppress Abort in Check.completionError
jjg
parents:
5654
diff
changeset
|
236 |
if (ex instanceof ClassReader.BadClassFile |
714e365b4b1b
6955264: add option to suppress Abort in Check.completionError
jjg
parents:
5654
diff
changeset
|
237 |
&& !suppressAbortOnBadClassFile) throw new Abort(); |
10 | 238 |
else return syms.errType; |
239 |
} |
|
240 |
||
241 |
/** Report a type error. |
|
242 |
* @param pos Position to be used for error reporting. |
|
243 |
* @param problem A string describing the error. |
|
244 |
* @param found The type that was found. |
|
245 |
* @param req The type that was required. |
|
246 |
*/ |
|
247 |
Type typeError(DiagnosticPosition pos, Object problem, Type found, Type req) { |
|
248 |
log.error(pos, "prob.found.req", |
|
249 |
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
|
250 |
return types.createErrorType(found); |
10 | 251 |
} |
252 |
||
253 |
Type typeError(DiagnosticPosition pos, String problem, Type found, Type req, Object explanation) { |
|
254 |
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
|
255 |
return types.createErrorType(found); |
10 | 256 |
} |
257 |
||
258 |
/** Report an error that wrong type tag was found. |
|
259 |
* @param pos Position to be used for error reporting. |
|
260 |
* @param required An internationalized string describing the type tag |
|
261 |
* required. |
|
262 |
* @param found The type that was found. |
|
263 |
*/ |
|
264 |
Type typeTagError(DiagnosticPosition pos, Object required, Object found) { |
|
2723
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2509
diff
changeset
|
265 |
// this error used to be raised by the parser, |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2509
diff
changeset
|
266 |
// but has been delayed to this point: |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2509
diff
changeset
|
267 |
if (found instanceof Type && ((Type)found).tag == VOID) { |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2509
diff
changeset
|
268 |
log.error(pos, "illegal.start.of.type"); |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2509
diff
changeset
|
269 |
return syms.errType; |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2509
diff
changeset
|
270 |
} |
10 | 271 |
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
|
272 |
return types.createErrorType(found instanceof Type ? (Type)found : syms.errType); |
10 | 273 |
} |
274 |
||
275 |
/** Report an error that symbol cannot be referenced before super |
|
276 |
* has been called. |
|
277 |
* @param pos Position to be used for error reporting. |
|
278 |
* @param sym The referenced symbol. |
|
279 |
*/ |
|
280 |
void earlyRefError(DiagnosticPosition pos, Symbol sym) { |
|
281 |
log.error(pos, "cant.ref.before.ctor.called", sym); |
|
282 |
} |
|
283 |
||
284 |
/** Report duplicate declaration error. |
|
285 |
*/ |
|
286 |
void duplicateError(DiagnosticPosition pos, Symbol sym) { |
|
287 |
if (!sym.type.isErroneous()) { |
|
288 |
log.error(pos, "already.defined", sym, sym.location()); |
|
289 |
} |
|
290 |
} |
|
291 |
||
292 |
/** Report array/varargs duplicate declaration |
|
293 |
*/ |
|
294 |
void varargsDuplicateError(DiagnosticPosition pos, Symbol sym1, Symbol sym2) { |
|
295 |
if (!sym1.type.isErroneous() && !sym2.type.isErroneous()) { |
|
296 |
log.error(pos, "array.and.varargs", sym1, sym2, sym2.location()); |
|
297 |
} |
|
298 |
} |
|
299 |
||
300 |
/* ************************************************************************ |
|
301 |
* duplicate declaration checking |
|
302 |
*************************************************************************/ |
|
303 |
||
304 |
/** Check that variable does not hide variable with same name in |
|
305 |
* immediately enclosing local scope. |
|
306 |
* @param pos Position for error reporting. |
|
307 |
* @param v The symbol. |
|
308 |
* @param s The scope. |
|
309 |
*/ |
|
310 |
void checkTransparentVar(DiagnosticPosition pos, VarSymbol v, Scope s) { |
|
311 |
if (s.next != null) { |
|
312 |
for (Scope.Entry e = s.next.lookup(v.name); |
|
313 |
e.scope != null && e.sym.owner == v.owner; |
|
314 |
e = e.next()) { |
|
315 |
if (e.sym.kind == VAR && |
|
316 |
(e.sym.owner.kind & (VAR | MTH)) != 0 && |
|
317 |
v.name != names.error) { |
|
318 |
duplicateError(pos, e.sym); |
|
319 |
return; |
|
320 |
} |
|
321 |
} |
|
322 |
} |
|
323 |
} |
|
324 |
||
325 |
/** Check that a class or interface does not hide a class or |
|
326 |
* interface with same name in immediately enclosing local scope. |
|
327 |
* @param pos Position for error reporting. |
|
328 |
* @param c The symbol. |
|
329 |
* @param s The scope. |
|
330 |
*/ |
|
331 |
void checkTransparentClass(DiagnosticPosition pos, ClassSymbol c, Scope s) { |
|
332 |
if (s.next != null) { |
|
333 |
for (Scope.Entry e = s.next.lookup(c.name); |
|
334 |
e.scope != null && e.sym.owner == c.owner; |
|
335 |
e = e.next()) { |
|
6352
217d5a69681a
6977800: Regression: invalid resolution of supertype for local class
mcimadamore
parents:
6351
diff
changeset
|
336 |
if (e.sym.kind == TYP && e.sym.type.tag != TYPEVAR && |
10 | 337 |
(e.sym.owner.kind & (VAR | MTH)) != 0 && |
338 |
c.name != names.error) { |
|
339 |
duplicateError(pos, e.sym); |
|
340 |
return; |
|
341 |
} |
|
342 |
} |
|
343 |
} |
|
344 |
} |
|
345 |
||
346 |
/** Check that class does not have the same name as one of |
|
347 |
* its enclosing classes, or as a class defined in its enclosing scope. |
|
348 |
* return true if class is unique in its enclosing scope. |
|
349 |
* @param pos Position for error reporting. |
|
350 |
* @param name The class name. |
|
351 |
* @param s The enclosing scope. |
|
352 |
*/ |
|
353 |
boolean checkUniqueClassName(DiagnosticPosition pos, Name name, Scope s) { |
|
354 |
for (Scope.Entry e = s.lookup(name); e.scope == s; e = e.next()) { |
|
355 |
if (e.sym.kind == TYP && e.sym.name != names.error) { |
|
356 |
duplicateError(pos, e.sym); |
|
357 |
return false; |
|
358 |
} |
|
359 |
} |
|
360 |
for (Symbol sym = s.owner; sym != null; sym = sym.owner) { |
|
361 |
if (sym.kind == TYP && sym.name == name && sym.name != names.error) { |
|
362 |
duplicateError(pos, sym); |
|
363 |
return true; |
|
364 |
} |
|
365 |
} |
|
366 |
return true; |
|
367 |
} |
|
368 |
||
369 |
/* ************************************************************************* |
|
370 |
* Class name generation |
|
371 |
**************************************************************************/ |
|
372 |
||
373 |
/** Return name of local class. |
|
374 |
* This is of the form <enclClass> $ n <classname> |
|
375 |
* where |
|
376 |
* enclClass is the flat name of the enclosing class, |
|
377 |
* classname is the simple name of the local class |
|
378 |
*/ |
|
379 |
Name localClassName(ClassSymbol c) { |
|
380 |
for (int i=1; ; i++) { |
|
381 |
Name flatname = names. |
|
382 |
fromString("" + c.owner.enclClass().flatname + |
|
3780
368edd3d2082
6874249: Check has duplicate local variable and field for "source"
jjg
parents:
3765
diff
changeset
|
383 |
syntheticNameChar + i + |
10 | 384 |
c.name); |
385 |
if (compiled.get(flatname) == null) return flatname; |
|
386 |
} |
|
387 |
} |
|
388 |
||
389 |
/* ************************************************************************* |
|
390 |
* Type Checking |
|
391 |
**************************************************************************/ |
|
392 |
||
393 |
/** Check that a given type is assignable to a given proto-type. |
|
394 |
* If it is, return the type, otherwise return errType. |
|
395 |
* @param pos Position to be used for error reporting. |
|
396 |
* @param found The type that was found. |
|
397 |
* @param req The type that was required. |
|
398 |
*/ |
|
399 |
Type checkType(DiagnosticPosition pos, Type found, Type req) { |
|
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6147
diff
changeset
|
400 |
return checkType(pos, found, req, "incompatible.types"); |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6147
diff
changeset
|
401 |
} |
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6147
diff
changeset
|
402 |
|
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6147
diff
changeset
|
403 |
Type checkType(DiagnosticPosition pos, Type found, Type req, String errKey) { |
10 | 404 |
if (req.tag == ERROR) |
405 |
return req; |
|
5320
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
406 |
if (found.tag == FORALL) |
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
407 |
return instantiatePoly(pos, (ForAll)found, req, convertWarner(pos, found, req)); |
10 | 408 |
if (req.tag == NONE) |
409 |
return found; |
|
410 |
if (types.isAssignable(found, req, convertWarner(pos, found, req))) |
|
411 |
return found; |
|
412 |
if (found.tag <= DOUBLE && req.tag <= DOUBLE) |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
413 |
return typeError(pos, diags.fragment("possible.loss.of.precision"), found, req); |
10 | 414 |
if (found.isSuperBound()) { |
415 |
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
|
416 |
return types.createErrorType(found); |
10 | 417 |
} |
418 |
if (req.isExtendsBound()) { |
|
419 |
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
|
420 |
return types.createErrorType(found); |
10 | 421 |
} |
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
6147
diff
changeset
|
422 |
return typeError(pos, diags.fragment(errKey), found, req); |
10 | 423 |
} |
424 |
||
425 |
/** Instantiate polymorphic type to some prototype, unless |
|
426 |
* prototype is `anyPoly' in which case polymorphic type |
|
427 |
* is returned unchanged. |
|
428 |
*/ |
|
3765
9548183897cb
6840638: Project Coin: Improved Type Inference for Generic Instance Creation (aka 'diamond')
mcimadamore
parents:
3661
diff
changeset
|
429 |
Type instantiatePoly(DiagnosticPosition pos, ForAll t, Type pt, Warner warn) throws Infer.NoInstanceException { |
10 | 430 |
if (pt == Infer.anyPoly && complexInference) { |
431 |
return t; |
|
432 |
} else if (pt == Infer.anyPoly || pt.tag == NONE) { |
|
433 |
Type newpt = t.qtype.tag <= VOID ? t.qtype : syms.objectType; |
|
434 |
return instantiatePoly(pos, t, newpt, warn); |
|
435 |
} else if (pt.tag == ERROR) { |
|
436 |
return pt; |
|
437 |
} else { |
|
5320
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
438 |
try { |
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
439 |
return infer.instantiateExpr(t, pt, warn); |
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
440 |
} catch (Infer.NoInstanceException ex) { |
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
441 |
if (ex.isAmbiguous) { |
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
442 |
JCDiagnostic d = ex.getDiagnostic(); |
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
443 |
log.error(pos, |
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
444 |
"undetermined.type" + (d!=null ? ".1" : ""), |
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
445 |
t, d); |
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
446 |
return types.createErrorType(pt); |
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
447 |
} else { |
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
448 |
JCDiagnostic d = ex.getDiagnostic(); |
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
449 |
return typeError(pos, |
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
450 |
diags.fragment("incompatible.types" + (d!=null ? ".1" : ""), d), |
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
451 |
t, pt); |
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
452 |
} |
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
453 |
} catch (Infer.InvalidInstanceException ex) { |
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
454 |
JCDiagnostic d = ex.getDiagnostic(); |
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
455 |
log.error(pos, "invalid.inferred.types", t.tvars, d); |
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
456 |
return types.createErrorType(pt); |
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
457 |
} |
10 | 458 |
} |
5320
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
459 |
} |
10 | 460 |
|
461 |
/** Check that a given type can be cast to a given target type. |
|
462 |
* Return the result of the cast. |
|
463 |
* @param pos Position to be used for error reporting. |
|
464 |
* @param found The type that is being cast. |
|
465 |
* @param req The target type of the cast. |
|
466 |
*/ |
|
467 |
Type checkCastable(DiagnosticPosition pos, Type found, Type req) { |
|
468 |
if (found.tag == FORALL) { |
|
469 |
instantiatePoly(pos, (ForAll) found, req, castWarner(pos, found, req)); |
|
470 |
return req; |
|
471 |
} else if (types.isCastable(found, req, castWarner(pos, found, req))) { |
|
472 |
return req; |
|
473 |
} else { |
|
474 |
return typeError(pos, |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
475 |
diags.fragment("inconvertible.types"), |
10 | 476 |
found, req); |
477 |
} |
|
478 |
} |
|
479 |
//where |
|
480 |
/** Is type a type variable, or a (possibly multi-dimensional) array of |
|
481 |
* type variables? |
|
482 |
*/ |
|
483 |
boolean isTypeVar(Type t) { |
|
484 |
return t.tag == TYPEVAR || t.tag == ARRAY && isTypeVar(types.elemtype(t)); |
|
485 |
} |
|
486 |
||
487 |
/** Check that a type is within some bounds. |
|
488 |
* |
|
489 |
* Used in TypeApply to verify that, e.g., X in V<X> is a valid |
|
490 |
* type argument. |
|
491 |
* @param pos Position to be used for error reporting. |
|
492 |
* @param a The type that should be bounded by bs. |
|
493 |
* @param bs The bound. |
|
494 |
*/ |
|
495 |
private void checkExtends(DiagnosticPosition pos, Type a, TypeVar bs) { |
|
1527 | 496 |
if (a.isUnbound()) { |
497 |
return; |
|
498 |
} else if (a.tag != WILDCARD) { |
|
499 |
a = types.upperBound(a); |
|
500 |
for (List<Type> l = types.getBounds(bs); l.nonEmpty(); l = l.tail) { |
|
501 |
if (!types.isSubtype(a, l.head)) { |
|
502 |
log.error(pos, "not.within.bounds", a); |
|
503 |
return; |
|
504 |
} |
|
505 |
} |
|
506 |
} else if (a.isExtendsBound()) { |
|
507 |
if (!types.isCastable(bs.getUpperBound(), types.upperBound(a), Warner.noWarnings)) |
|
508 |
log.error(pos, "not.within.bounds", a); |
|
509 |
} else if (a.isSuperBound()) { |
|
510 |
if (types.notSoftSubtype(types.lowerBound(a), bs.getUpperBound())) |
|
511 |
log.error(pos, "not.within.bounds", a); |
|
512 |
} |
|
513 |
} |
|
514 |
||
515 |
/** Check that a type is within some bounds. |
|
516 |
* |
|
517 |
* Used in TypeApply to verify that, e.g., X in V<X> is a valid |
|
518 |
* type argument. |
|
519 |
* @param pos Position to be used for error reporting. |
|
520 |
* @param a The type that should be bounded by bs. |
|
521 |
* @param bs The bound. |
|
522 |
*/ |
|
523 |
private void checkCapture(JCTypeApply tree) { |
|
524 |
List<JCExpression> args = tree.getTypeArguments(); |
|
525 |
for (Type arg : types.capture(tree.type).getTypeArguments()) { |
|
526 |
if (arg.tag == TYPEVAR && arg.getUpperBound().isErroneous()) { |
|
527 |
log.error(args.head.pos, "not.within.bounds", args.head.type); |
|
528 |
break; |
|
938
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
529 |
} |
1527 | 530 |
args = args.tail; |
938
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
937
diff
changeset
|
531 |
} |
1527 | 532 |
} |
10 | 533 |
|
534 |
/** Check that type is different from 'void'. |
|
535 |
* @param pos Position to be used for error reporting. |
|
536 |
* @param t The type to be checked. |
|
537 |
*/ |
|
538 |
Type checkNonVoid(DiagnosticPosition pos, Type t) { |
|
539 |
if (t.tag == VOID) { |
|
540 |
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
|
541 |
return types.createErrorType(t); |
10 | 542 |
} else { |
543 |
return t; |
|
544 |
} |
|
545 |
} |
|
546 |
||
547 |
/** Check that type is a class or interface type. |
|
548 |
* @param pos Position to be used for error reporting. |
|
549 |
* @param t The type to be checked. |
|
550 |
*/ |
|
551 |
Type checkClassType(DiagnosticPosition pos, Type t) { |
|
552 |
if (t.tag != CLASS && t.tag != ERROR) |
|
553 |
return typeTagError(pos, |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
554 |
diags.fragment("type.req.class"), |
10 | 555 |
(t.tag == TYPEVAR) |
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
556 |
? diags.fragment("type.parameter", t) |
10 | 557 |
: t); |
558 |
else |
|
559 |
return t; |
|
560 |
} |
|
561 |
||
562 |
/** Check that type is a class or interface type. |
|
563 |
* @param pos Position to be used for error reporting. |
|
564 |
* @param t The type to be checked. |
|
565 |
* @param noBounds True if type bounds are illegal here. |
|
566 |
*/ |
|
567 |
Type checkClassType(DiagnosticPosition pos, Type t, boolean noBounds) { |
|
568 |
t = checkClassType(pos, t); |
|
569 |
if (noBounds && t.isParameterized()) { |
|
570 |
List<Type> args = t.getTypeArguments(); |
|
571 |
while (args.nonEmpty()) { |
|
572 |
if (args.head.tag == WILDCARD) |
|
573 |
return typeTagError(pos, |
|
6032 | 574 |
diags.fragment("type.req.exact"), |
10 | 575 |
args.head); |
576 |
args = args.tail; |
|
577 |
} |
|
578 |
} |
|
579 |
return t; |
|
580 |
} |
|
581 |
||
582 |
/** Check that type is a reifiable class, interface or array type. |
|
583 |
* @param pos Position to be used for error reporting. |
|
584 |
* @param t The type to be checked. |
|
585 |
*/ |
|
586 |
Type checkReifiableReferenceType(DiagnosticPosition pos, Type t) { |
|
587 |
if (t.tag != CLASS && t.tag != ARRAY && t.tag != ERROR) { |
|
588 |
return typeTagError(pos, |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
589 |
diags.fragment("type.req.class.array"), |
10 | 590 |
t); |
591 |
} else if (!types.isReifiable(t)) { |
|
592 |
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
|
593 |
return types.createErrorType(t); |
10 | 594 |
} else { |
595 |
return t; |
|
596 |
} |
|
597 |
} |
|
598 |
||
599 |
/** Check that type is a reference type, i.e. a class, interface or array type |
|
600 |
* or a type variable. |
|
601 |
* @param pos Position to be used for error reporting. |
|
602 |
* @param t The type to be checked. |
|
603 |
*/ |
|
604 |
Type checkRefType(DiagnosticPosition pos, Type t) { |
|
605 |
switch (t.tag) { |
|
606 |
case CLASS: |
|
607 |
case ARRAY: |
|
608 |
case TYPEVAR: |
|
609 |
case WILDCARD: |
|
610 |
case ERROR: |
|
611 |
return t; |
|
612 |
default: |
|
613 |
return typeTagError(pos, |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
614 |
diags.fragment("type.req.ref"), |
10 | 615 |
t); |
616 |
} |
|
617 |
} |
|
618 |
||
2723
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2509
diff
changeset
|
619 |
/** Check that each type is a reference type, i.e. a class, interface or array type |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2509
diff
changeset
|
620 |
* or a type variable. |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2509
diff
changeset
|
621 |
* @param trees Original trees, used for error reporting. |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2509
diff
changeset
|
622 |
* @param types The types to be checked. |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2509
diff
changeset
|
623 |
*/ |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2509
diff
changeset
|
624 |
List<Type> checkRefTypes(List<JCExpression> trees, List<Type> types) { |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2509
diff
changeset
|
625 |
List<JCExpression> tl = trees; |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2509
diff
changeset
|
626 |
for (List<Type> l = types; l.nonEmpty(); l = l.tail) { |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2509
diff
changeset
|
627 |
l.head = checkRefType(tl.head.pos(), l.head); |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2509
diff
changeset
|
628 |
tl = tl.tail; |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2509
diff
changeset
|
629 |
} |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2509
diff
changeset
|
630 |
return types; |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2509
diff
changeset
|
631 |
} |
b659ca23d5f5
6829189: Java programming with JSR 292 needs language support
jrose
parents:
2509
diff
changeset
|
632 |
|
10 | 633 |
/** Check that type is a null or reference type. |
634 |
* @param pos Position to be used for error reporting. |
|
635 |
* @param t The type to be checked. |
|
636 |
*/ |
|
637 |
Type checkNullOrRefType(DiagnosticPosition pos, Type t) { |
|
638 |
switch (t.tag) { |
|
639 |
case CLASS: |
|
640 |
case ARRAY: |
|
641 |
case TYPEVAR: |
|
642 |
case WILDCARD: |
|
643 |
case BOT: |
|
644 |
case ERROR: |
|
645 |
return t; |
|
646 |
default: |
|
647 |
return typeTagError(pos, |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
648 |
diags.fragment("type.req.ref"), |
10 | 649 |
t); |
650 |
} |
|
651 |
} |
|
652 |
||
653 |
/** Check that flag set does not contain elements of two conflicting sets. s |
|
654 |
* Return true if it doesn't. |
|
655 |
* @param pos Position to be used for error reporting. |
|
656 |
* @param flags The set of flags to be checked. |
|
657 |
* @param set1 Conflicting flags set #1. |
|
658 |
* @param set2 Conflicting flags set #2. |
|
659 |
*/ |
|
660 |
boolean checkDisjoint(DiagnosticPosition pos, long flags, long set1, long set2) { |
|
661 |
if ((flags & set1) != 0 && (flags & set2) != 0) { |
|
662 |
log.error(pos, |
|
663 |
"illegal.combination.of.modifiers", |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
938
diff
changeset
|
664 |
asFlagSet(TreeInfo.firstFlag(flags & set1)), |
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
938
diff
changeset
|
665 |
asFlagSet(TreeInfo.firstFlag(flags & set2))); |
10 | 666 |
return false; |
667 |
} else |
|
668 |
return true; |
|
669 |
} |
|
670 |
||
5321
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
671 |
/** Check that the type inferred using the diamond operator does not contain |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
672 |
* non-denotable types such as captured types or intersection types. |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
673 |
* @param t the type inferred using the diamond operator |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
674 |
*/ |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
675 |
List<Type> checkDiamond(ClassType t) { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
676 |
DiamondTypeChecker dtc = new DiamondTypeChecker(); |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
677 |
ListBuffer<Type> buf = ListBuffer.lb(); |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
678 |
for (Type arg : t.getTypeArguments()) { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
679 |
if (!dtc.visit(arg, null)) { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
680 |
buf.append(arg); |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
681 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
682 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
683 |
return buf.toList(); |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
684 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
685 |
|
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
686 |
static class DiamondTypeChecker extends Types.SimpleVisitor<Boolean, Void> { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
687 |
public Boolean visitType(Type t, Void s) { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
688 |
return true; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
689 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
690 |
@Override |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
691 |
public Boolean visitClassType(ClassType t, Void s) { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
692 |
if (t.isCompound()) { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
693 |
return false; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
694 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
695 |
for (Type targ : t.getTypeArguments()) { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
696 |
if (!visit(targ, s)) { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
697 |
return false; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
698 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
699 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
700 |
return true; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
701 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
702 |
@Override |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
703 |
public Boolean visitCapturedType(CapturedType t, Void s) { |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
704 |
return false; |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
705 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
706 |
} |
c8efe769cb3b
6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents:
5320
diff
changeset
|
707 |
|
5846
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
708 |
void checkVarargMethodDecl(JCMethodDecl tree) { |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
709 |
MethodSymbol m = tree.sym; |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
710 |
//check the element type of the vararg |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
711 |
if (m.isVarArgs()) { |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
712 |
Type varargElemType = types.elemtype(tree.params.last().type); |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
713 |
if (!types.isReifiable(varargElemType)) { |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
714 |
warnUnsafeVararg(tree.params.head.pos(), varargElemType); |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
715 |
} |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
716 |
} |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
717 |
} |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
718 |
|
5489
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
5321
diff
changeset
|
719 |
/** |
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
5321
diff
changeset
|
720 |
* Check that vararg method call is sound |
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
5321
diff
changeset
|
721 |
* @param pos Position to be used for error reporting. |
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
5321
diff
changeset
|
722 |
* @param argtypes Actual arguments supplied to vararg method. |
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
5321
diff
changeset
|
723 |
*/ |
5846
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
724 |
void checkVararg(DiagnosticPosition pos, List<Type> argtypes, Symbol msym, Env<AttrContext> env) { |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
725 |
Env<AttrContext> calleeLintEnv = env; |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
726 |
while (calleeLintEnv.info.lint == null) |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
727 |
calleeLintEnv = calleeLintEnv.next; |
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
728 |
Lint calleeLint = calleeLintEnv.info.lint.augment(msym.attributes_field, msym.flags()); |
5489
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
5321
diff
changeset
|
729 |
Type argtype = argtypes.last(); |
5846
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
730 |
if (!types.isReifiable(argtype) && !calleeLint.isSuppressed(Lint.LintCategory.VARARGS)) { |
5489
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
5321
diff
changeset
|
731 |
warnUnchecked(pos, |
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
5321
diff
changeset
|
732 |
"unchecked.generic.array.creation", |
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
5321
diff
changeset
|
733 |
argtype); |
5846
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5842
diff
changeset
|
734 |
} |
5489
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
5321
diff
changeset
|
735 |
} |
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
5321
diff
changeset
|
736 |
|
10 | 737 |
/** Check that given modifiers are legal for given symbol and |
738 |
* return modifiers together with any implicit modififiers for that symbol. |
|
739 |
* Warning: we can't use flags() here since this method |
|
740 |
* is called during class enter, when flags() would cause a premature |
|
741 |
* completion. |
|
742 |
* @param pos Position to be used for error reporting. |
|
743 |
* @param flags The set of modifiers given in a definition. |
|
744 |
* @param sym The defined symbol. |
|
745 |
*/ |
|
746 |
long checkFlags(DiagnosticPosition pos, long flags, Symbol sym, JCTree tree) { |
|
747 |
long mask; |
|
748 |
long implicit = 0; |
|
749 |
switch (sym.kind) { |
|
750 |
case VAR: |
|
751 |
if (sym.owner.kind != TYP) |
|
752 |
mask = LocalVarFlags; |
|
753 |
else if ((sym.owner.flags_field & INTERFACE) != 0) |
|
754 |
mask = implicit = InterfaceVarFlags; |
|
755 |
else |
|
756 |
mask = VarFlags; |
|
757 |
break; |
|
758 |
case MTH: |
|
759 |
if (sym.name == names.init) { |
|
760 |
if ((sym.owner.flags_field & ENUM) != 0) { |
|
761 |
// enum constructors cannot be declared public or |
|
762 |
// protected and must be implicitly or explicitly |
|
763 |
// private |
|
764 |
implicit = PRIVATE; |
|
765 |
mask = PRIVATE; |
|
766 |
} else |
|
767 |
mask = ConstructorFlags; |
|
768 |
} else if ((sym.owner.flags_field & INTERFACE) != 0) |
|
769 |
mask = implicit = InterfaceMethodFlags; |
|
770 |
else { |
|
771 |
mask = MethodFlags; |
|
772 |
} |
|
773 |
// Imply STRICTFP if owner has STRICTFP set. |
|
774 |
if (((flags|implicit) & Flags.ABSTRACT) == 0) |
|
775 |
implicit |= sym.owner.flags_field & STRICTFP; |
|
776 |
break; |
|
777 |
case TYP: |
|
778 |
if (sym.isLocal()) { |
|
779 |
mask = LocalClassFlags; |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1257
diff
changeset
|
780 |
if (sym.name.isEmpty()) { // Anonymous class |
10 | 781 |
// Anonymous classes in static methods are themselves static; |
782 |
// that's why we admit STATIC here. |
|
783 |
mask |= STATIC; |
|
784 |
// JLS: Anonymous classes are final. |
|
785 |
implicit |= FINAL; |
|
786 |
} |
|
787 |
if ((sym.owner.flags_field & STATIC) == 0 && |
|
788 |
(flags & ENUM) != 0) |
|
789 |
log.error(pos, "enums.must.be.static"); |
|
790 |
} else if (sym.owner.kind == TYP) { |
|
791 |
mask = MemberClassFlags; |
|
792 |
if (sym.owner.owner.kind == PCK || |
|
793 |
(sym.owner.flags_field & STATIC) != 0) |
|
794 |
mask |= STATIC; |
|
795 |
else if ((flags & ENUM) != 0) |
|
796 |
log.error(pos, "enums.must.be.static"); |
|
797 |
// Nested interfaces and enums are always STATIC (Spec ???) |
|
798 |
if ((flags & (INTERFACE | ENUM)) != 0 ) implicit = STATIC; |
|
799 |
} else { |
|
800 |
mask = ClassFlags; |
|
801 |
} |
|
802 |
// Interfaces are always ABSTRACT |
|
803 |
if ((flags & INTERFACE) != 0) implicit |= ABSTRACT; |
|
804 |
||
805 |
if ((flags & ENUM) != 0) { |
|
806 |
// enums can't be declared abstract or final |
|
807 |
mask &= ~(ABSTRACT | FINAL); |
|
808 |
implicit |= implicitEnumFinalFlag(tree); |
|
809 |
} |
|
810 |
// Imply STRICTFP if owner has STRICTFP set. |
|
811 |
implicit |= sym.owner.flags_field & STRICTFP; |
|
812 |
break; |
|
813 |
default: |
|
814 |
throw new AssertionError(); |
|
815 |
} |
|
816 |
long illegal = flags & StandardFlags & ~mask; |
|
817 |
if (illegal != 0) { |
|
818 |
if ((illegal & INTERFACE) != 0) { |
|
819 |
log.error(pos, "intf.not.allowed.here"); |
|
820 |
mask |= INTERFACE; |
|
821 |
} |
|
822 |
else { |
|
823 |
log.error(pos, |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
938
diff
changeset
|
824 |
"mod.not.allowed.here", asFlagSet(illegal)); |
10 | 825 |
} |
826 |
} |
|
827 |
else if ((sym.kind == TYP || |
|
828 |
// ISSUE: Disallowing abstract&private is no longer appropriate |
|
829 |
// in the presence of inner classes. Should it be deleted here? |
|
830 |
checkDisjoint(pos, flags, |
|
831 |
ABSTRACT, |
|
832 |
PRIVATE | STATIC)) |
|
833 |
&& |
|
834 |
checkDisjoint(pos, flags, |
|
835 |
ABSTRACT | INTERFACE, |
|
836 |
FINAL | NATIVE | SYNCHRONIZED) |
|
837 |
&& |
|
838 |
checkDisjoint(pos, flags, |
|
839 |
PUBLIC, |
|
840 |
PRIVATE | PROTECTED) |
|
841 |
&& |
|
842 |
checkDisjoint(pos, flags, |
|
843 |
PRIVATE, |
|
844 |
PUBLIC | PROTECTED) |
|
845 |
&& |
|
846 |
checkDisjoint(pos, flags, |
|
847 |
FINAL, |
|
848 |
VOLATILE) |
|
849 |
&& |
|
850 |
(sym.kind == TYP || |
|
851 |
checkDisjoint(pos, flags, |
|
852 |
ABSTRACT | NATIVE, |
|
853 |
STRICTFP))) { |
|
854 |
// skip |
|
855 |
} |
|
856 |
return flags & (mask | ~StandardFlags) | implicit; |
|
857 |
} |
|
858 |
||
859 |
||
860 |
/** Determine if this enum should be implicitly final. |
|
861 |
* |
|
862 |
* If the enum has no specialized enum contants, it is final. |
|
863 |
* |
|
864 |
* If the enum does have specialized enum contants, it is |
|
865 |
* <i>not</i> final. |
|
866 |
*/ |
|
867 |
private long implicitEnumFinalFlag(JCTree tree) { |
|
868 |
if (tree.getTag() != JCTree.CLASSDEF) return 0; |
|
869 |
class SpecialTreeVisitor extends JCTree.Visitor { |
|
870 |
boolean specialized; |
|
871 |
SpecialTreeVisitor() { |
|
872 |
this.specialized = false; |
|
873 |
}; |
|
874 |
||
3780
368edd3d2082
6874249: Check has duplicate local variable and field for "source"
jjg
parents:
3765
diff
changeset
|
875 |
@Override |
10 | 876 |
public void visitTree(JCTree tree) { /* no-op */ } |
877 |
||
3780
368edd3d2082
6874249: Check has duplicate local variable and field for "source"
jjg
parents:
3765
diff
changeset
|
878 |
@Override |
10 | 879 |
public void visitVarDef(JCVariableDecl tree) { |
880 |
if ((tree.mods.flags & ENUM) != 0) { |
|
881 |
if (tree.init instanceof JCNewClass && |
|
882 |
((JCNewClass) tree.init).def != null) { |
|
883 |
specialized = true; |
|
884 |
} |
|
885 |
} |
|
886 |
} |
|
887 |
} |
|
888 |
||
889 |
SpecialTreeVisitor sts = new SpecialTreeVisitor(); |
|
890 |
JCClassDecl cdef = (JCClassDecl) tree; |
|
891 |
for (JCTree defs: cdef.defs) { |
|
892 |
defs.accept(sts); |
|
893 |
if (sts.specialized) return 0; |
|
894 |
} |
|
895 |
return FINAL; |
|
896 |
} |
|
897 |
||
898 |
/* ************************************************************************* |
|
899 |
* Type Validation |
|
900 |
**************************************************************************/ |
|
901 |
||
902 |
/** Validate a type expression. That is, |
|
903 |
* check that all type arguments of a parametric type are within |
|
904 |
* their bounds. This must be done in a second phase after type attributon |
|
905 |
* since a class might have a subclass as type parameter bound. E.g: |
|
906 |
* |
|
907 |
* class B<A extends C> { ... } |
|
908 |
* class C extends B<C> { ... } |
|
909 |
* |
|
910 |
* and we can't make sure that the bound is already attributed because |
|
911 |
* of possible cycles. |
|
6351 | 912 |
* |
913 |
* Visitor method: Validate a type expression, if it is not null, catching |
|
10 | 914 |
* and reporting any completion failures. |
915 |
*/ |
|
1358 | 916 |
void validate(JCTree tree, Env<AttrContext> env) { |
6351 | 917 |
validate(tree, env, true); |
10 | 918 |
} |
6351 | 919 |
void validate(JCTree tree, Env<AttrContext> env, boolean checkRaw) { |
920 |
new Validator(env).validateTree(tree, checkRaw, true); |
|
1358 | 921 |
} |
10 | 922 |
|
923 |
/** Visitor method: Validate a list of type expressions. |
|
924 |
*/ |
|
1358 | 925 |
void validate(List<? extends JCTree> trees, Env<AttrContext> env) { |
10 | 926 |
for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail) |
1358 | 927 |
validate(l.head, env); |
10 | 928 |
} |
929 |
||
930 |
/** A visitor class for type validation. |
|
931 |
*/ |
|
932 |
class Validator extends JCTree.Visitor { |
|
933 |
||
6351 | 934 |
boolean isOuter; |
935 |
Env<AttrContext> env; |
|
936 |
||
937 |
Validator(Env<AttrContext> env) { |
|
938 |
this.env = env; |
|
939 |
} |
|
940 |
||
3780
368edd3d2082
6874249: Check has duplicate local variable and field for "source"
jjg
parents:
3765
diff
changeset
|
941 |
@Override |
10 | 942 |
public void visitTypeArray(JCArrayTypeTree tree) { |
6351 | 943 |
tree.elemtype.accept(this); |
10 | 944 |
} |
945 |
||
3780
368edd3d2082
6874249: Check has duplicate local variable and field for "source"
jjg
parents:
3765
diff
changeset
|
946 |
@Override |
10 | 947 |
public void visitTypeApply(JCTypeApply tree) { |
948 |
if (tree.type.tag == CLASS) { |
|
1531
37df4e42719a
6557954: Inner class type parameters doesn't get substituted when checking type well-formedness
mcimadamore
parents:
1527
diff
changeset
|
949 |
List<Type> formals = tree.type.tsym.type.allparams(); |
37df4e42719a
6557954: Inner class type parameters doesn't get substituted when checking type well-formedness
mcimadamore
parents:
1527
diff
changeset
|
950 |
List<Type> actuals = tree.type.allparams(); |
10 | 951 |
List<JCExpression> args = tree.arguments; |
1531
37df4e42719a
6557954: Inner class type parameters doesn't get substituted when checking type well-formedness
mcimadamore
parents:
1527
diff
changeset
|
952 |
List<Type> forms = tree.type.tsym.type.getTypeArguments(); |
5648
e8aa492874b5
6880344: Recursive type parameters do not compile
mcimadamore
parents:
5489
diff
changeset
|
953 |
ListBuffer<Type> tvars_buf = new ListBuffer<Type>(); |
10 | 954 |
|
6351 | 955 |
boolean is_java_lang_Class = tree.type.tsym.flatName() == names.java_lang_Class; |
956 |
||
10 | 957 |
// For matching pairs of actual argument types `a' and |
958 |
// formal type parameters with declared bound `b' ... |
|
959 |
while (args.nonEmpty() && forms.nonEmpty()) { |
|
6351 | 960 |
validateTree(args.head, |
961 |
!(isOuter && is_java_lang_Class), |
|
962 |
false); |
|
10 | 963 |
|
964 |
// exact type arguments needs to know their |
|
965 |
// bounds (for upper and lower bound |
|
966 |
// calculations). So we create new TypeVars with |
|
967 |
// bounds substed with actuals. |
|
968 |
tvars_buf.append(types.substBound(((TypeVar)forms.head), |
|
969 |
formals, |
|
937
457a11ae2e84
6651719: Compiler crashes possibly during forward reference of TypeParameter
mcimadamore
parents:
815
diff
changeset
|
970 |
actuals)); |
10 | 971 |
|
972 |
args = args.tail; |
|
973 |
forms = forms.tail; |
|
974 |
} |
|
975 |
||
976 |
args = tree.arguments; |
|
1527 | 977 |
List<Type> tvars_cap = types.substBounds(formals, |
978 |
formals, |
|
1531
37df4e42719a
6557954: Inner class type parameters doesn't get substituted when checking type well-formedness
mcimadamore
parents:
1527
diff
changeset
|
979 |
types.capture(tree.type).allparams()); |
1527 | 980 |
while (args.nonEmpty() && tvars_cap.nonEmpty()) { |
981 |
// Let the actual arguments know their bound |
|
982 |
args.head.type.withTypeVar((TypeVar)tvars_cap.head); |
|
983 |
args = args.tail; |
|
984 |
tvars_cap = tvars_cap.tail; |
|
985 |
} |
|
986 |
||
987 |
args = tree.arguments; |
|
5648
e8aa492874b5
6880344: Recursive type parameters do not compile
mcimadamore
parents:
5489
diff
changeset
|
988 |
List<Type> tvars = tvars_buf.toList(); |
1527 | 989 |
|
10 | 990 |
while (args.nonEmpty() && tvars.nonEmpty()) { |
5648
e8aa492874b5
6880344: Recursive type parameters do not compile
mcimadamore
parents:
5489
diff
changeset
|
991 |
Type actual = types.subst(args.head.type, |
e8aa492874b5
6880344: Recursive type parameters do not compile
mcimadamore
parents:
5489
diff
changeset
|
992 |
tree.type.tsym.type.getTypeArguments(), |
e8aa492874b5
6880344: Recursive type parameters do not compile
mcimadamore
parents:
5489
diff
changeset
|
993 |
tvars_buf.toList()); |
1527 | 994 |
checkExtends(args.head.pos(), |
5648
e8aa492874b5
6880344: Recursive type parameters do not compile
mcimadamore
parents:
5489
diff
changeset
|
995 |
actual, |
e8aa492874b5
6880344: Recursive type parameters do not compile
mcimadamore
parents:
5489
diff
changeset
|
996 |
(TypeVar)tvars.head); |
10 | 997 |
args = args.tail; |
998 |
tvars = tvars.tail; |
|
999 |
} |
|
1000 |
||
1527 | 1001 |
checkCapture(tree); |
5320
e2aaa958b02d
6939618: Revert 'simple' diamond implementation
mcimadamore
parents:
5002
diff
changeset
|
1002 |
|
10 | 1003 |
// Check that this type is either fully parameterized, or |
1004 |
// not parameterized at all. |
|
1005 |
if (tree.type.getEnclosingType().isRaw()) |
|
1006 |
log.error(tree.pos(), "improperly.formed.type.inner.raw.param"); |
|
1007 |
if (tree.clazz.getTag() == JCTree.SELECT) |
|
1008 |
visitSelectInternal((JCFieldAccess)tree.clazz); |
|
1009 |
} |
|
1010 |
} |
|
1011 |
||
3780
368edd3d2082
6874249: Check has duplicate local variable and field for "source"
jjg
parents:
3765
diff
changeset
|
1012 |
@Override |
10 | 1013 |
public void visitTypeParameter(JCTypeParameter tree) { |
6351 | 1014 |
validateTrees(tree.bounds, true, isOuter); |
10 | 1015 |
checkClassBounds(tree.pos(), tree.type); |
1016 |
} |
|
1017 |
||
1018 |
@Override |
|
1019 |
public void visitWildcard(JCWildcard tree) { |
|
1020 |
if (tree.inner != null) |
|
6351 | 1021 |
validateTree(tree.inner, true, isOuter); |
10 | 1022 |
} |
1023 |
||
3780
368edd3d2082
6874249: Check has duplicate local variable and field for "source"
jjg
parents:
3765
diff
changeset
|
1024 |
@Override |
10 | 1025 |
public void visitSelect(JCFieldAccess tree) { |
1026 |
if (tree.type.tag == CLASS) { |
|
1027 |
visitSelectInternal(tree); |
|
1028 |
||
1029 |
// Check that this type is either fully parameterized, or |
|
1030 |
// not parameterized at all. |
|
1031 |
if (tree.selected.type.isParameterized() && tree.type.tsym.type.getTypeArguments().nonEmpty()) |
|
1032 |
log.error(tree.pos(), "improperly.formed.type.param.missing"); |
|
1033 |
} |
|
1034 |
} |
|
1035 |
public void visitSelectInternal(JCFieldAccess tree) { |
|
1358 | 1036 |
if (tree.type.tsym.isStatic() && |
10 | 1037 |
tree.selected.type.isParameterized()) { |
1038 |
// The enclosing type is not a class, so we are |
|
1039 |
// looking at a static member type. However, the |
|
1040 |
// qualifying expression is parameterized. |
|
1041 |
log.error(tree.pos(), "cant.select.static.class.from.param.type"); |
|
1042 |
} else { |
|
1043 |
// otherwise validate the rest of the expression |
|
1358 | 1044 |
tree.selected.accept(this); |
10 | 1045 |
} |
1046 |
} |
|
1047 |
||
3780
368edd3d2082
6874249: Check has duplicate local variable and field for "source"
jjg
parents:
3765
diff
changeset
|
1048 |
@Override |
3149 | 1049 |
public void visitAnnotatedType(JCAnnotatedType tree) { |
1050 |
tree.underlyingType.accept(this); |
|
1051 |
} |
|
1052 |
||
10 | 1053 |
/** Default visitor method: do nothing. |
1054 |
*/ |
|
3780
368edd3d2082
6874249: Check has duplicate local variable and field for "source"
jjg
parents:
3765
diff
changeset
|
1055 |
@Override |
10 | 1056 |
public void visitTree(JCTree tree) { |
1057 |
} |
|
1358 | 1058 |
|
6351 | 1059 |
public void validateTree(JCTree tree, boolean checkRaw, boolean isOuter) { |
1060 |
try { |
|
1061 |
if (tree != null) { |
|
1062 |
this.isOuter = isOuter; |
|
1063 |
tree.accept(this); |
|
1064 |
if (checkRaw) |
|
1065 |
checkRaw(tree, env); |
|
1066 |
} |
|
1067 |
} catch (CompletionFailure ex) { |
|
1068 |
completionError(tree.pos(), ex); |
|
1069 |
} |
|
1070 |
} |
|
1071 |
||
1072 |
public void validateTrees(List<? extends JCTree> trees, boolean checkRaw, boolean isOuter) { |
|
1073 |
for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail) |
|
1074 |
validateTree(l.head, checkRaw, isOuter); |
|
1075 |
} |
|
1076 |
||
1077 |
void checkRaw(JCTree tree, Env<AttrContext> env) { |
|
1078 |
if (lint.isEnabled(Lint.LintCategory.RAW) && |
|
1079 |
tree.type.tag == CLASS && |
|
1080 |
!TreeInfo.isDiamond(tree) && |
|
1081 |
!env.enclClass.name.isEmpty() && //anonymous or intersection |
|
1082 |
tree.type.isRaw()) { |
|
1083 |
log.warning(Lint.LintCategory.RAW, |
|
1084 |
tree.pos(), "raw.class.use", tree.type, tree.type.tsym.type); |
|
1085 |
} |
|
1086 |
} |
|
10 | 1087 |
} |
1088 |
||
1089 |
/* ************************************************************************* |
|
1090 |
* Exception checking |
|
1091 |
**************************************************************************/ |
|
1092 |
||
1093 |
/* The following methods treat classes as sets that contain |
|
1094 |
* the class itself and all their subclasses |
|
1095 |
*/ |
|
1096 |
||
1097 |
/** Is given type a subtype of some of the types in given list? |
|
1098 |
*/ |
|
1099 |
boolean subset(Type t, List<Type> ts) { |
|
1100 |
for (List<Type> l = ts; l.nonEmpty(); l = l.tail) |
|
1101 |
if (types.isSubtype(t, l.head)) return true; |
|
1102 |
return false; |
|
1103 |
} |
|
1104 |
||
1105 |
/** Is given type a subtype or supertype of |
|
1106 |
* some of the types in given list? |
|
1107 |
*/ |
|
1108 |
boolean intersects(Type t, List<Type> ts) { |
|
1109 |
for (List<Type> l = ts; l.nonEmpty(); l = l.tail) |
|
1110 |
if (types.isSubtype(t, l.head) || types.isSubtype(l.head, t)) return true; |
|
1111 |
return false; |
|
1112 |
} |
|
1113 |
||
1114 |
/** Add type set to given type list, unless it is a subclass of some class |
|
1115 |
* in the list. |
|
1116 |
*/ |
|
1117 |
List<Type> incl(Type t, List<Type> ts) { |
|
1118 |
return subset(t, ts) ? ts : excl(t, ts).prepend(t); |
|
1119 |
} |
|
1120 |
||
1121 |
/** Remove type set from type set list. |
|
1122 |
*/ |
|
1123 |
List<Type> excl(Type t, List<Type> ts) { |
|
1124 |
if (ts.isEmpty()) { |
|
1125 |
return ts; |
|
1126 |
} else { |
|
1127 |
List<Type> ts1 = excl(t, ts.tail); |
|
1128 |
if (types.isSubtype(ts.head, t)) return ts1; |
|
1129 |
else if (ts1 == ts.tail) return ts; |
|
1130 |
else return ts1.prepend(ts.head); |
|
1131 |
} |
|
1132 |
} |
|
1133 |
||
1134 |
/** Form the union of two type set lists. |
|
1135 |
*/ |
|
1136 |
List<Type> union(List<Type> ts1, List<Type> ts2) { |
|
1137 |
List<Type> ts = ts1; |
|
1138 |
for (List<Type> l = ts2; l.nonEmpty(); l = l.tail) |
|
1139 |
ts = incl(l.head, ts); |
|
1140 |
return ts; |
|
1141 |
} |
|
1142 |
||
1143 |
/** Form the difference of two type lists. |
|
1144 |
*/ |
|
1145 |
List<Type> diff(List<Type> ts1, List<Type> ts2) { |
|
1146 |
List<Type> ts = ts1; |
|
1147 |
for (List<Type> l = ts2; l.nonEmpty(); l = l.tail) |
|
1148 |
ts = excl(l.head, ts); |
|
1149 |
return ts; |
|
1150 |
} |
|
1151 |
||
1152 |
/** Form the intersection of two type lists. |
|
1153 |
*/ |
|
1154 |
public List<Type> intersect(List<Type> ts1, List<Type> ts2) { |
|
1155 |
List<Type> ts = List.nil(); |
|
1156 |
for (List<Type> l = ts1; l.nonEmpty(); l = l.tail) |
|
1157 |
if (subset(l.head, ts2)) ts = incl(l.head, ts); |
|
1158 |
for (List<Type> l = ts2; l.nonEmpty(); l = l.tail) |
|
1159 |
if (subset(l.head, ts1)) ts = incl(l.head, ts); |
|
1160 |
return ts; |
|
1161 |
} |
|
1162 |
||
1163 |
/** Is exc an exception symbol that need not be declared? |
|
1164 |
*/ |
|
1165 |
boolean isUnchecked(ClassSymbol exc) { |
|
1166 |
return |
|
1167 |
exc.kind == ERR || |
|
1168 |
exc.isSubClass(syms.errorType.tsym, types) || |
|
1169 |
exc.isSubClass(syms.runtimeExceptionType.tsym, types); |
|
1170 |
} |
|
1171 |
||
1172 |
/** Is exc an exception type that need not be declared? |
|
1173 |
*/ |
|
1174 |
boolean isUnchecked(Type exc) { |
|
1175 |
return |
|
1176 |
(exc.tag == TYPEVAR) ? isUnchecked(types.supertype(exc)) : |
|
1177 |
(exc.tag == CLASS) ? isUnchecked((ClassSymbol)exc.tsym) : |
|
1178 |
exc.tag == BOT; |
|
1179 |
} |
|
1180 |
||
1181 |
/** Same, but handling completion failures. |
|
1182 |
*/ |
|
1183 |
boolean isUnchecked(DiagnosticPosition pos, Type exc) { |
|
1184 |
try { |
|
1185 |
return isUnchecked(exc); |
|
1186 |
} catch (CompletionFailure ex) { |
|
1187 |
completionError(pos, ex); |
|
1188 |
return true; |
|
1189 |
} |
|
1190 |
} |
|
1191 |
||
1192 |
/** Is exc handled by given exception list? |
|
1193 |
*/ |
|
1194 |
boolean isHandled(Type exc, List<Type> handled) { |
|
1195 |
return isUnchecked(exc) || subset(exc, handled); |
|
1196 |
} |
|
1197 |
||
1198 |
/** Return all exceptions in thrown list that are not in handled list. |
|
1199 |
* @param thrown The list of thrown exceptions. |
|
1200 |
* @param handled The list of handled exceptions. |
|
1201 |
*/ |
|
3560 | 1202 |
List<Type> unhandled(List<Type> thrown, List<Type> handled) { |
10 | 1203 |
List<Type> unhandled = List.nil(); |
1204 |
for (List<Type> l = thrown; l.nonEmpty(); l = l.tail) |
|
1205 |
if (!isHandled(l.head, handled)) unhandled = unhandled.prepend(l.head); |
|
1206 |
return unhandled; |
|
1207 |
} |
|
1208 |
||
1209 |
/* ************************************************************************* |
|
1210 |
* Overriding/Implementation checking |
|
1211 |
**************************************************************************/ |
|
1212 |
||
1213 |
/** The level of access protection given by a flag set, |
|
1214 |
* where PRIVATE is highest and PUBLIC is lowest. |
|
1215 |
*/ |
|
1216 |
static int protection(long flags) { |
|
1217 |
switch ((short)(flags & AccessFlags)) { |
|
1218 |
case PRIVATE: return 3; |
|
1219 |
case PROTECTED: return 1; |
|
1220 |
default: |
|
1221 |
case PUBLIC: return 0; |
|
1222 |
case 0: return 2; |
|
1223 |
} |
|
1224 |
} |
|
1225 |
||
1226 |
/** A customized "cannot override" error message. |
|
1227 |
* @param m The overriding method. |
|
1228 |
* @param other The overridden method. |
|
1229 |
* @return An internationalized string. |
|
1230 |
*/ |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
1231 |
Object cannotOverride(MethodSymbol m, MethodSymbol other) { |
10 | 1232 |
String key; |
1233 |
if ((other.owner.flags() & INTERFACE) == 0) |
|
1234 |
key = "cant.override"; |
|
1235 |
else if ((m.owner.flags() & INTERFACE) == 0) |
|
1236 |
key = "cant.implement"; |
|
1237 |
else |
|
1238 |
key = "clashes.with"; |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
1239 |
return diags.fragment(key, m, m.location(), other, other.location()); |
10 | 1240 |
} |
1241 |
||
1242 |
/** A customized "override" warning message. |
|
1243 |
* @param m The overriding method. |
|
1244 |
* @param other The overridden method. |
|
1245 |
* @return An internationalized string. |
|
1246 |
*/ |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
1247 |
Object uncheckedOverrides(MethodSymbol m, MethodSymbol other) { |
10 | 1248 |
String key; |
1249 |
if ((other.owner.flags() & INTERFACE) == 0) |
|
1250 |
key = "unchecked.override"; |
|
1251 |
else if ((m.owner.flags() & INTERFACE) == 0) |
|
1252 |
key = "unchecked.implement"; |
|
1253 |
else |
|
1254 |
key = "unchecked.clash.with"; |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
1255 |
return diags.fragment(key, m, m.location(), other, other.location()); |
10 | 1256 |
} |
1257 |
||
1258 |
/** A customized "override" warning message. |
|
1259 |
* @param m The overriding method. |
|
1260 |
* @param other The overridden method. |
|
1261 |
* @return An internationalized string. |
|
1262 |
*/ |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
1263 |
Object varargsOverrides(MethodSymbol m, MethodSymbol other) { |
10 | 1264 |
String key; |
1265 |
if ((other.owner.flags() & INTERFACE) == 0) |
|
1266 |
key = "varargs.override"; |
|
1267 |
else if ((m.owner.flags() & INTERFACE) == 0) |
|
1268 |
key = "varargs.implement"; |
|
1269 |
else |
|
1270 |
key = "varargs.clash.with"; |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
1271 |
return diags.fragment(key, m, m.location(), other, other.location()); |
10 | 1272 |
} |
1273 |
||
1274 |
/** Check that this method conforms with overridden method 'other'. |
|
1275 |
* where `origin' is the class where checking started. |
|
1276 |
* Complications: |
|
1277 |
* (1) Do not check overriding of synthetic methods |
|
1278 |
* (reason: they might be final). |
|
1279 |
* todo: check whether this is still necessary. |
|
1280 |
* (2) Admit the case where an interface proxy throws fewer exceptions |
|
1281 |
* than the method it implements. Augment the proxy methods with the |
|
1282 |
* undeclared exceptions in this case. |
|
1283 |
* (3) When generics are enabled, admit the case where an interface proxy |
|
1284 |
* has a result type |
|
1285 |
* extended by the result type of the method it implements. |
|
1286 |
* Change the proxies result type to the smaller type in this case. |
|
1287 |
* |
|
1288 |
* @param tree The tree from which positions |
|
1289 |
* are extracted for errors. |
|
1290 |
* @param m The overriding method. |
|
1291 |
* @param other The overridden method. |
|
1292 |
* @param origin The class of which the overriding method |
|
1293 |
* is a member. |
|
1294 |
*/ |
|
1295 |
void checkOverride(JCTree tree, |
|
1296 |
MethodSymbol m, |
|
1297 |
MethodSymbol other, |
|
1298 |
ClassSymbol origin) { |
|
1299 |
// Don't check overriding of synthetic methods or by bridge methods. |
|
1300 |
if ((m.flags() & (SYNTHETIC|BRIDGE)) != 0 || (other.flags() & SYNTHETIC) != 0) { |
|
1301 |
return; |
|
1302 |
} |
|
1303 |
||
1304 |
// Error if static method overrides instance method (JLS 8.4.6.2). |
|
1305 |
if ((m.flags() & STATIC) != 0 && |
|
1306 |
(other.flags() & STATIC) == 0) { |
|
1307 |
log.error(TreeInfo.diagnosticPositionFor(m, tree), "override.static", |
|
1308 |
cannotOverride(m, other)); |
|
1309 |
return; |
|
1310 |
} |
|
1311 |
||
1312 |
// Error if instance method overrides static or final |
|
1313 |
// method (JLS 8.4.6.1). |
|
1314 |
if ((other.flags() & FINAL) != 0 || |
|
1315 |
(m.flags() & STATIC) == 0 && |
|
1316 |
(other.flags() & STATIC) != 0) { |
|
1317 |
log.error(TreeInfo.diagnosticPositionFor(m, tree), "override.meth", |
|
1318 |
cannotOverride(m, other), |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
938
diff
changeset
|
1319 |
asFlagSet(other.flags() & (FINAL | STATIC))); |
10 | 1320 |
return; |
1321 |
} |
|
1322 |
||
1323 |
if ((m.owner.flags() & ANNOTATION) != 0) { |
|
1324 |
// handled in validateAnnotationMethod |
|
1325 |
return; |
|
1326 |
} |
|
1327 |
||
1328 |
// Error if overriding method has weaker access (JLS 8.4.6.3). |
|
1329 |
if ((origin.flags() & INTERFACE) == 0 && |
|
1330 |
protection(m.flags()) > protection(other.flags())) { |
|
1331 |
log.error(TreeInfo.diagnosticPositionFor(m, tree), "override.weaker.access", |
|
1332 |
cannotOverride(m, other), |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
938
diff
changeset
|
1333 |
other.flags() == 0 ? |
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
938
diff
changeset
|
1334 |
Flag.PACKAGE : |
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
938
diff
changeset
|
1335 |
asFlagSet(other.flags() & AccessFlags)); |
10 | 1336 |
return; |
1337 |
} |
|
1338 |
||
1339 |
Type mt = types.memberType(origin.type, m); |
|
1340 |
Type ot = types.memberType(origin.type, other); |
|
1341 |
// Error if overriding result type is different |
|
1342 |
// (or, in the case of generics mode, not a subtype) of |
|
1343 |
// overridden result type. We have to rename any type parameters |
|
1344 |
// before comparing types. |
|
1345 |
List<Type> mtvars = mt.getTypeArguments(); |
|
1346 |
List<Type> otvars = ot.getTypeArguments(); |
|
1347 |
Type mtres = mt.getReturnType(); |
|
1348 |
Type otres = types.subst(ot.getReturnType(), otvars, mtvars); |
|
1349 |
||
1350 |
overrideWarner.warned = false; |
|
1351 |
boolean resultTypesOK = |
|
1874 | 1352 |
types.returnTypeSubstitutable(mt, ot, otres, overrideWarner); |
10 | 1353 |
if (!resultTypesOK) { |
3780
368edd3d2082
6874249: Check has duplicate local variable and field for "source"
jjg
parents:
3765
diff
changeset
|
1354 |
if (!allowCovariantReturns && |
10 | 1355 |
m.owner != origin && |
1356 |
m.owner.isSubClass(other.owner, types)) { |
|
1357 |
// allow limited interoperability with covariant returns |
|
1358 |
} else { |
|
3560 | 1359 |
log.error(TreeInfo.diagnosticPositionFor(m, tree), |
1360 |
"override.incompatible.ret", |
|
1361 |
cannotOverride(m, other), |
|
10 | 1362 |
mtres, otres); |
1363 |
return; |
|
1364 |
} |
|
1365 |
} else if (overrideWarner.warned) { |
|
1366 |
warnUnchecked(TreeInfo.diagnosticPositionFor(m, tree), |
|
3560 | 1367 |
"override.unchecked.ret", |
1368 |
uncheckedOverrides(m, other), |
|
1369 |
mtres, otres); |
|
10 | 1370 |
} |
1371 |
||
1372 |
// Error if overriding method throws an exception not reported |
|
1373 |
// by overridden method. |
|
1374 |
List<Type> otthrown = types.subst(ot.getThrownTypes(), otvars, mtvars); |
|
3560 | 1375 |
List<Type> unhandledErased = unhandled(mt.getThrownTypes(), types.erasure(otthrown)); |
1376 |
List<Type> unhandledUnerased = unhandled(mt.getThrownTypes(), otthrown); |
|
1377 |
if (unhandledErased.nonEmpty()) { |
|
10 | 1378 |
log.error(TreeInfo.diagnosticPositionFor(m, tree), |
1379 |
"override.meth.doesnt.throw", |
|
1380 |
cannotOverride(m, other), |
|
3560 | 1381 |
unhandledUnerased.head); |
1382 |
return; |
|
1383 |
} |
|
1384 |
else if (unhandledUnerased.nonEmpty()) { |
|
1385 |
warnUnchecked(TreeInfo.diagnosticPositionFor(m, tree), |
|
1386 |
"override.unchecked.thrown", |
|
1387 |
cannotOverride(m, other), |
|
1388 |
unhandledUnerased.head); |
|
10 | 1389 |
return; |
1390 |
} |
|
1391 |
||
1392 |
// Optional warning if varargs don't agree |
|
1393 |
if ((((m.flags() ^ other.flags()) & Flags.VARARGS) != 0) |
|
1394 |
&& lint.isEnabled(Lint.LintCategory.OVERRIDES)) { |
|
1395 |
log.warning(TreeInfo.diagnosticPositionFor(m, tree), |
|
1396 |
((m.flags() & Flags.VARARGS) != 0) |
|
1397 |
? "override.varargs.missing" |
|
1398 |
: "override.varargs.extra", |
|
1399 |
varargsOverrides(m, other)); |
|
1400 |
} |
|
1401 |
||
1402 |
// Warn if instance method overrides bridge method (compiler spec ??) |
|
1403 |
if ((other.flags() & BRIDGE) != 0) { |
|
1404 |
log.warning(TreeInfo.diagnosticPositionFor(m, tree), "override.bridge", |
|
1405 |
uncheckedOverrides(m, other)); |
|
1406 |
} |
|
1407 |
||
1408 |
// Warn if a deprecated method overridden by a non-deprecated one. |
|
1409 |
if ((other.flags() & DEPRECATED) != 0 |
|
1410 |
&& (m.flags() & DEPRECATED) == 0 |
|
1411 |
&& m.outermostClass() != other.outermostClass() |
|
1412 |
&& !isDeprecatedOverrideIgnorable(other, origin)) { |
|
1413 |
warnDeprecated(TreeInfo.diagnosticPositionFor(m, tree), other); |
|
1414 |
} |
|
1415 |
} |
|
1416 |
// where |
|
1417 |
private boolean isDeprecatedOverrideIgnorable(MethodSymbol m, ClassSymbol origin) { |
|
1418 |
// If the method, m, is defined in an interface, then ignore the issue if the method |
|
1419 |
// is only inherited via a supertype and also implemented in the supertype, |
|
1420 |
// because in that case, we will rediscover the issue when examining the method |
|
1421 |
// in the supertype. |
|
1422 |
// If the method, m, is not defined in an interface, then the only time we need to |
|
1423 |
// address the issue is when the method is the supertype implemementation: any other |
|
1424 |
// case, we will have dealt with when examining the supertype classes |
|
1425 |
ClassSymbol mc = m.enclClass(); |
|
1426 |
Type st = types.supertype(origin.type); |
|
1427 |
if (st.tag != CLASS) |
|
1428 |
return true; |
|
1429 |
MethodSymbol stimpl = m.implementation((ClassSymbol)st.tsym, types, false); |
|
1430 |
||
1431 |
if (mc != null && ((mc.flags() & INTERFACE) != 0)) { |
|
1432 |
List<Type> intfs = types.interfaces(origin.type); |
|
1433 |
return (intfs.contains(mc.type) ? false : (stimpl != null)); |
|
1434 |
} |
|
1435 |
else |
|
1436 |
return (stimpl != m); |
|
1437 |
} |
|
1438 |
||
1439 |
||
1440 |
// used to check if there were any unchecked conversions |
|
1441 |
Warner overrideWarner = new Warner(); |
|
1442 |
||
1443 |
/** Check that a class does not inherit two concrete methods |
|
1444 |
* with the same signature. |
|
1445 |
* @param pos Position to be used for error reporting. |
|
1446 |
* @param site The class type to be checked. |
|
1447 |
*/ |
|
1448 |
public void checkCompatibleConcretes(DiagnosticPosition pos, Type site) { |
|
1449 |
Type sup = types.supertype(site); |
|
1450 |
if (sup.tag != CLASS) return; |
|
1451 |
||
1452 |
for (Type t1 = sup; |
|
1453 |
t1.tsym.type.isParameterized(); |
|
1454 |
t1 = types.supertype(t1)) { |
|
1455 |
for (Scope.Entry e1 = t1.tsym.members().elems; |
|
1456 |
e1 != null; |
|
1457 |
e1 = e1.sibling) { |
|
1458 |
Symbol s1 = e1.sym; |
|
1459 |
if (s1.kind != MTH || |
|
1460 |
(s1.flags() & (STATIC|SYNTHETIC|BRIDGE)) != 0 || |
|
1461 |
!s1.isInheritedIn(site.tsym, types) || |
|
1462 |
((MethodSymbol)s1).implementation(site.tsym, |
|
1463 |
types, |
|
1464 |
true) != s1) |
|
1465 |
continue; |
|
1466 |
Type st1 = types.memberType(t1, s1); |
|
1467 |
int s1ArgsLength = st1.getParameterTypes().length(); |
|
1468 |
if (st1 == s1.type) continue; |
|
1469 |
||
1470 |
for (Type t2 = sup; |
|
1471 |
t2.tag == CLASS; |
|
1472 |
t2 = types.supertype(t2)) { |
|
508
118e500e2e50
5009937: hiding versus generics versus binary compatibility
mcimadamore
parents:
169
diff
changeset
|
1473 |
for (Scope.Entry e2 = t2.tsym.members().lookup(s1.name); |
10 | 1474 |
e2.scope != null; |
1475 |
e2 = e2.next()) { |
|
1476 |
Symbol s2 = e2.sym; |
|
1477 |
if (s2 == s1 || |
|
1478 |
s2.kind != MTH || |
|
1479 |
(s2.flags() & (STATIC|SYNTHETIC|BRIDGE)) != 0 || |
|
1480 |
s2.type.getParameterTypes().length() != s1ArgsLength || |
|
1481 |
!s2.isInheritedIn(site.tsym, types) || |
|
1482 |
((MethodSymbol)s2).implementation(site.tsym, |
|
1483 |
types, |
|
1484 |
true) != s2) |
|
1485 |
continue; |
|
1486 |
Type st2 = types.memberType(t2, s2); |
|
1487 |
if (types.overrideEquivalent(st1, st2)) |
|
1488 |
log.error(pos, "concrete.inheritance.conflict", |
|
1489 |
s1, t1, s2, t2, sup); |
|
1490 |
} |
|
1491 |
} |
|
1492 |
} |
|
1493 |
} |
|
1494 |
} |
|
1495 |
||
1496 |
/** Check that classes (or interfaces) do not each define an abstract |
|
1497 |
* method with same name and arguments but incompatible return types. |
|
1498 |
* @param pos Position to be used for error reporting. |
|
1499 |
* @param t1 The first argument type. |
|
1500 |
* @param t2 The second argument type. |
|
1501 |
*/ |
|
1502 |
public boolean checkCompatibleAbstracts(DiagnosticPosition pos, |
|
1503 |
Type t1, |
|
1504 |
Type t2) { |
|
1505 |
return checkCompatibleAbstracts(pos, t1, t2, |
|
1506 |
types.makeCompoundType(t1, t2)); |
|
1507 |
} |
|
1508 |
||
1509 |
public boolean checkCompatibleAbstracts(DiagnosticPosition pos, |
|
1510 |
Type t1, |
|
1511 |
Type t2, |
|
1512 |
Type site) { |
|
1513 |
Symbol sym = firstIncompatibility(t1, t2, site); |
|
1514 |
if (sym != null) { |
|
1515 |
log.error(pos, "types.incompatible.diff.ret", |
|
1516 |
t1, t2, sym.name + |
|
1517 |
"(" + types.memberType(t2, sym).getParameterTypes() + ")"); |
|
1518 |
return false; |
|
1519 |
} |
|
1520 |
return true; |
|
1521 |
} |
|
1522 |
||
1523 |
/** Return the first method which is defined with same args |
|
1524 |
* but different return types in two given interfaces, or null if none |
|
1525 |
* exists. |
|
1526 |
* @param t1 The first type. |
|
1527 |
* @param t2 The second type. |
|
1528 |
* @param site The most derived type. |
|
1529 |
* @returns symbol from t2 that conflicts with one in t1. |
|
1530 |
*/ |
|
1531 |
private Symbol firstIncompatibility(Type t1, Type t2, Type site) { |
|
1532 |
Map<TypeSymbol,Type> interfaces1 = new HashMap<TypeSymbol,Type>(); |
|
1533 |
closure(t1, interfaces1); |
|
1534 |
Map<TypeSymbol,Type> interfaces2; |
|
1535 |
if (t1 == t2) |
|
1536 |
interfaces2 = interfaces1; |
|
1537 |
else |
|
1538 |
closure(t2, interfaces1, interfaces2 = new HashMap<TypeSymbol,Type>()); |
|
1539 |
||
1540 |
for (Type t3 : interfaces1.values()) { |
|
1541 |
for (Type t4 : interfaces2.values()) { |
|
1542 |
Symbol s = firstDirectIncompatibility(t3, t4, site); |
|
1543 |
if (s != null) return s; |
|
1544 |
} |
|
1545 |
} |
|
1546 |
return null; |
|
1547 |
} |
|
1548 |
||
1549 |
/** Compute all the supertypes of t, indexed by type symbol. */ |
|
1550 |
private void closure(Type t, Map<TypeSymbol,Type> typeMap) { |
|
1551 |
if (t.tag != CLASS) return; |
|
1552 |
if (typeMap.put(t.tsym, t) == null) { |
|
1553 |
closure(types.supertype(t), typeMap); |
|
1554 |
for (Type i : types.interfaces(t)) |
|
1555 |
closure(i, typeMap); |
|
1556 |
} |
|
1557 |
} |
|
1558 |
||
1559 |
/** Compute all the supertypes of t, indexed by type symbol (except thise in typesSkip). */ |
|
1560 |
private void closure(Type t, Map<TypeSymbol,Type> typesSkip, Map<TypeSymbol,Type> typeMap) { |
|
1561 |
if (t.tag != CLASS) return; |
|
1562 |
if (typesSkip.get(t.tsym) != null) return; |
|
1563 |
if (typeMap.put(t.tsym, t) == null) { |
|
1564 |
closure(types.supertype(t), typesSkip, typeMap); |
|
1565 |
for (Type i : types.interfaces(t)) |
|
1566 |
closure(i, typesSkip, typeMap); |
|
1567 |
} |
|
1568 |
} |
|
1569 |
||
1570 |
/** Return the first method in t2 that conflicts with a method from t1. */ |
|
1571 |
private Symbol firstDirectIncompatibility(Type t1, Type t2, Type site) { |
|
1572 |
for (Scope.Entry e1 = t1.tsym.members().elems; e1 != null; e1 = e1.sibling) { |
|
1573 |
Symbol s1 = e1.sym; |
|
1574 |
Type st1 = null; |
|
1575 |
if (s1.kind != MTH || !s1.isInheritedIn(site.tsym, types)) continue; |
|
1576 |
Symbol impl = ((MethodSymbol)s1).implementation(site.tsym, types, false); |
|
1577 |
if (impl != null && (impl.flags() & ABSTRACT) == 0) continue; |
|
1578 |
for (Scope.Entry e2 = t2.tsym.members().lookup(s1.name); e2.scope != null; e2 = e2.next()) { |
|
1579 |
Symbol s2 = e2.sym; |
|
1580 |
if (s1 == s2) continue; |
|
1581 |
if (s2.kind != MTH || !s2.isInheritedIn(site.tsym, types)) continue; |
|
1582 |
if (st1 == null) st1 = types.memberType(t1, s1); |
|
1583 |
Type st2 = types.memberType(t2, s2); |
|
1584 |
if (types.overrideEquivalent(st1, st2)) { |
|
1585 |
List<Type> tvars1 = st1.getTypeArguments(); |
|
1586 |
List<Type> tvars2 = st2.getTypeArguments(); |
|
1587 |
Type rt1 = st1.getReturnType(); |
|
1588 |
Type rt2 = types.subst(st2.getReturnType(), tvars2, tvars1); |
|
1589 |
boolean compat = |
|
1590 |
types.isSameType(rt1, rt2) || |
|
1591 |
rt1.tag >= CLASS && rt2.tag >= CLASS && |
|
1592 |
(types.covariantReturnType(rt1, rt2, Warner.noWarnings) || |
|
812
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1593 |
types.covariantReturnType(rt2, rt1, Warner.noWarnings)) || |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1594 |
checkCommonOverriderIn(s1,s2,site); |
10 | 1595 |
if (!compat) return s2; |
1596 |
} |
|
1597 |
} |
|
1598 |
} |
|
1599 |
return null; |
|
1600 |
} |
|
812
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1601 |
//WHERE |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1602 |
boolean checkCommonOverriderIn(Symbol s1, Symbol s2, Type site) { |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1603 |
Map<TypeSymbol,Type> supertypes = new HashMap<TypeSymbol,Type>(); |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1604 |
Type st1 = types.memberType(site, s1); |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1605 |
Type st2 = types.memberType(site, s2); |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1606 |
closure(site, supertypes); |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1607 |
for (Type t : supertypes.values()) { |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1608 |
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
|
1609 |
Symbol s3 = e.sym; |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1610 |
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
|
1611 |
Type st3 = types.memberType(site,s3); |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1612 |
if (types.overrideEquivalent(st3, st1) && types.overrideEquivalent(st3, st2)) { |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1613 |
if (s3.owner == site.tsym) { |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1614 |
return true; |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1615 |
} |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1616 |
List<Type> tvars1 = st1.getTypeArguments(); |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1617 |
List<Type> tvars2 = st2.getTypeArguments(); |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1618 |
List<Type> tvars3 = st3.getTypeArguments(); |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1619 |
Type rt1 = st1.getReturnType(); |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1620 |
Type rt2 = st2.getReturnType(); |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1621 |
Type rt13 = types.subst(st3.getReturnType(), tvars3, tvars1); |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1622 |
Type rt23 = types.subst(st3.getReturnType(), tvars3, tvars2); |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1623 |
boolean compat = |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1624 |
rt13.tag >= CLASS && rt23.tag >= CLASS && |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1625 |
(types.covariantReturnType(rt13, rt1, Warner.noWarnings) && |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1626 |
types.covariantReturnType(rt23, rt2, Warner.noWarnings)); |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1627 |
if (compat) |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1628 |
return true; |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1629 |
} |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1630 |
} |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1631 |
} |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1632 |
return false; |
d60171f70ade
6294779: Problem with interface inheritance and covariant return types
mcimadamore
parents:
661
diff
changeset
|
1633 |
} |
10 | 1634 |
|
1635 |
/** Check that a given method conforms with any method it overrides. |
|
1636 |
* @param tree The tree from which positions are extracted |
|
1637 |
* for errors. |
|
1638 |
* @param m The overriding method. |
|
1639 |
*/ |
|
1640 |
void checkOverride(JCTree tree, MethodSymbol m) { |
|
1641 |
ClassSymbol origin = (ClassSymbol)m.owner; |
|
1642 |
if ((origin.flags() & ENUM) != 0 && names.finalize.equals(m.name)) |
|
1643 |
if (m.overrides(syms.enumFinalFinalize, origin, types, false)) { |
|
1644 |
log.error(tree.pos(), "enum.no.finalize"); |
|
1645 |
return; |
|
1646 |
} |
|
1647 |
for (Type t = types.supertype(origin.type); t.tag == CLASS; |
|
1648 |
t = types.supertype(t)) { |
|
1649 |
TypeSymbol c = t.tsym; |
|
1650 |
Scope.Entry e = c.members().lookup(m.name); |
|
1651 |
while (e.scope != null) { |
|
1652 |
if (m.overrides(e.sym, origin, types, false)) |
|
1653 |
checkOverride(tree, m, (MethodSymbol)e.sym, origin); |
|
2509
c8a1744c87fd
6182950: methods clash algorithm should not depend on return type
mcimadamore
parents:
2223
diff
changeset
|
1654 |
else if (e.sym.kind == MTH && |
c8a1744c87fd
6182950: methods clash algorithm should not depend on return type
mcimadamore
parents:
2223
diff
changeset
|
1655 |
e.sym.isInheritedIn(origin, types) && |
c8a1744c87fd
6182950: methods clash algorithm should not depend on return type
mcimadamore
parents:
2223
diff
changeset
|
1656 |
(e.sym.flags() & SYNTHETIC) == 0 && |
c8a1744c87fd
6182950: methods clash algorithm should not depend on return type
mcimadamore
parents:
2223
diff
changeset
|
1657 |
!m.isConstructor()) { |
508
118e500e2e50
5009937: hiding versus generics versus binary compatibility
mcimadamore
parents:
169
diff
changeset
|
1658 |
Type er1 = m.erasure(types); |
118e500e2e50
5009937: hiding versus generics versus binary compatibility
mcimadamore
parents:
169
diff
changeset
|
1659 |
Type er2 = e.sym.erasure(types); |
2509
c8a1744c87fd
6182950: methods clash algorithm should not depend on return type
mcimadamore
parents:
2223
diff
changeset
|
1660 |
if (types.isSameTypes(er1.getParameterTypes(), |
c8a1744c87fd
6182950: methods clash algorithm should not depend on return type
mcimadamore
parents:
2223
diff
changeset
|
1661 |
er2.getParameterTypes())) { |
508
118e500e2e50
5009937: hiding versus generics versus binary compatibility
mcimadamore
parents:
169
diff
changeset
|
1662 |
log.error(TreeInfo.diagnosticPositionFor(m, tree), |
118e500e2e50
5009937: hiding versus generics versus binary compatibility
mcimadamore
parents:
169
diff
changeset
|
1663 |
"name.clash.same.erasure.no.override", |
118e500e2e50
5009937: hiding versus generics versus binary compatibility
mcimadamore
parents:
169
diff
changeset
|
1664 |
m, m.location(), |
118e500e2e50
5009937: hiding versus generics versus binary compatibility
mcimadamore
parents:
169
diff
changeset
|
1665 |
e.sym, e.sym.location()); |
118e500e2e50
5009937: hiding versus generics versus binary compatibility
mcimadamore
parents:
169
diff
changeset
|
1666 |
} |
118e500e2e50
5009937: hiding versus generics versus binary compatibility
mcimadamore
parents:
169
diff
changeset
|
1667 |
} |
10 | 1668 |
e = e.next(); |
1669 |
} |
|
1670 |
} |
|
1671 |
} |
|
1672 |
||
1673 |
/** Check that all abstract members of given class have definitions. |
|
1674 |
* @param pos Position to be used for error reporting. |
|
1675 |
* @param c The class. |
|
1676 |
*/ |
|
1677 |
void checkAllDefined(DiagnosticPosition pos, ClassSymbol c) { |
|
1678 |
try { |
|
1679 |
MethodSymbol undef = firstUndef(c, c); |
|
1680 |
if (undef != null) { |
|
1681 |
if ((c.flags() & ENUM) != 0 && |
|
1682 |
types.supertype(c.type).tsym == syms.enumSym && |
|
1683 |
(c.flags() & FINAL) == 0) { |
|
1684 |
// add the ABSTRACT flag to an enum |
|
1685 |
c.flags_field |= ABSTRACT; |
|
1686 |
} else { |
|
1687 |
MethodSymbol undef1 = |
|
1688 |
new MethodSymbol(undef.flags(), undef.name, |
|
1689 |
types.memberType(c.type, undef), undef.owner); |
|
1690 |
log.error(pos, "does.not.override.abstract", |
|
1691 |
c, undef1, undef1.location()); |
|
1692 |
} |
|
1693 |
} |
|
1694 |
} catch (CompletionFailure ex) { |
|
1695 |
completionError(pos, ex); |
|
1696 |
} |
|
1697 |
} |
|
1698 |
//where |
|
1699 |
/** Return first abstract member of class `c' that is not defined |
|
1700 |
* in `impl', null if there is none. |
|
1701 |
*/ |
|
1702 |
private MethodSymbol firstUndef(ClassSymbol impl, ClassSymbol c) { |
|
1703 |
MethodSymbol undef = null; |
|
1704 |
// Do not bother to search in classes that are not abstract, |
|
1705 |
// since they cannot have abstract members. |
|
1706 |
if (c == impl || (c.flags() & (ABSTRACT | INTERFACE)) != 0) { |
|
1707 |
Scope s = c.members(); |
|
1708 |
for (Scope.Entry e = s.elems; |
|
1709 |
undef == null && e != null; |
|
1710 |
e = e.sibling) { |
|
1711 |
if (e.sym.kind == MTH && |
|
1712 |
(e.sym.flags() & (ABSTRACT|IPROXY)) == ABSTRACT) { |
|
1713 |
MethodSymbol absmeth = (MethodSymbol)e.sym; |
|
1714 |
MethodSymbol implmeth = absmeth.implementation(impl, types, true); |
|
1715 |
if (implmeth == null || implmeth == absmeth) |
|
1716 |
undef = absmeth; |
|
1717 |
} |
|
1718 |
} |
|
1719 |
if (undef == null) { |
|
1720 |
Type st = types.supertype(c.type); |
|
1721 |
if (st.tag == CLASS) |
|
1722 |
undef = firstUndef(impl, (ClassSymbol)st.tsym); |
|
1723 |
} |
|
1724 |
for (List<Type> l = types.interfaces(c.type); |
|
1725 |
undef == null && l.nonEmpty(); |
|
1726 |
l = l.tail) { |
|
1727 |
undef = firstUndef(impl, (ClassSymbol)l.head.tsym); |
|
1728 |
} |
|
1729 |
} |
|
1730 |
return undef; |
|
1731 |
} |
|
1732 |
||
6711
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1733 |
void checkNonCyclicDecl(JCClassDecl tree) { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1734 |
CycleChecker cc = new CycleChecker(); |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1735 |
cc.scan(tree); |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1736 |
if (!cc.errorFound && !cc.partialCheck) { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1737 |
tree.sym.flags_field |= ACYCLIC; |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1738 |
} |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1739 |
} |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1740 |
|
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1741 |
class CycleChecker extends TreeScanner { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1742 |
|
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1743 |
List<Symbol> seenClasses = List.nil(); |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1744 |
boolean errorFound = false; |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1745 |
boolean partialCheck = false; |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1746 |
|
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1747 |
private void checkSymbol(DiagnosticPosition pos, Symbol sym) { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1748 |
if (sym != null && sym.kind == TYP) { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1749 |
Env<AttrContext> classEnv = enter.getEnv((TypeSymbol)sym); |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1750 |
if (classEnv != null) { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1751 |
DiagnosticSource prevSource = log.currentSource(); |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1752 |
try { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1753 |
log.useSource(classEnv.toplevel.sourcefile); |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1754 |
scan(classEnv.tree); |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1755 |
} |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1756 |
finally { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1757 |
log.useSource(prevSource.getFile()); |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1758 |
} |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1759 |
} else if (sym.kind == TYP) { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1760 |
checkClass(pos, sym, List.<JCTree>nil()); |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1761 |
} |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1762 |
} else { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1763 |
//not completed yet |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1764 |
partialCheck = true; |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1765 |
} |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1766 |
} |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1767 |
|
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1768 |
@Override |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1769 |
public void visitSelect(JCFieldAccess tree) { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1770 |
super.visitSelect(tree); |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1771 |
checkSymbol(tree.pos(), tree.sym); |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1772 |
} |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1773 |
|
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1774 |
@Override |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1775 |
public void visitIdent(JCIdent tree) { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1776 |
checkSymbol(tree.pos(), tree.sym); |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1777 |
} |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1778 |
|
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1779 |
@Override |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1780 |
public void visitTypeApply(JCTypeApply tree) { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1781 |
scan(tree.clazz); |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1782 |
} |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1783 |
|
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1784 |
@Override |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1785 |
public void visitTypeArray(JCArrayTypeTree tree) { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1786 |
scan(tree.elemtype); |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1787 |
} |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1788 |
|
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1789 |
@Override |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1790 |
public void visitClassDef(JCClassDecl tree) { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1791 |
List<JCTree> supertypes = List.nil(); |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1792 |
if (tree.getExtendsClause() != null) { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1793 |
supertypes = supertypes.prepend(tree.getExtendsClause()); |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1794 |
} |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1795 |
if (tree.getImplementsClause() != null) { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1796 |
for (JCTree intf : tree.getImplementsClause()) { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1797 |
supertypes = supertypes.prepend(intf); |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1798 |
} |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1799 |
} |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1800 |
checkClass(tree.pos(), tree.sym, supertypes); |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1801 |
} |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1802 |
|
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1803 |
void checkClass(DiagnosticPosition pos, Symbol c, List<JCTree> supertypes) { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1804 |
if ((c.flags_field & ACYCLIC) != 0) |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1805 |
return; |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1806 |
if (seenClasses.contains(c)) { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1807 |
errorFound = true; |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1808 |
noteCyclic(pos, (ClassSymbol)c); |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1809 |
} else if (!c.type.isErroneous()) { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1810 |
try { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1811 |
seenClasses = seenClasses.prepend(c); |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1812 |
if (c.type.tag == CLASS) { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1813 |
if (supertypes.nonEmpty()) { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1814 |
scan(supertypes); |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1815 |
} |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1816 |
else { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1817 |
ClassType ct = (ClassType)c.type; |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1818 |
if (ct.supertype_field == null || |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1819 |
ct.interfaces_field == null) { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1820 |
//not completed yet |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1821 |
partialCheck = true; |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1822 |
return; |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1823 |
} |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1824 |
checkSymbol(pos, ct.supertype_field.tsym); |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1825 |
for (Type intf : ct.interfaces_field) { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1826 |
checkSymbol(pos, intf.tsym); |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1827 |
} |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1828 |
} |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1829 |
if (c.owner.kind == TYP) { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1830 |
checkSymbol(pos, c.owner); |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1831 |
} |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1832 |
} |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1833 |
} finally { |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1834 |
seenClasses = seenClasses.tail; |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1835 |
} |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1836 |
} |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1837 |
} |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1838 |
} |
fc9732adc790
6863465: javac doesn't detect circular subclass dependencies via qualified names
mcimadamore
parents:
6352
diff
changeset
|
1839 |
|
10 | 1840 |
/** Check for cyclic references. Issue an error if the |
1841 |
* symbol of the type referred to has a LOCKED flag set. |
|
1842 |
* |
|
1843 |
* @param pos Position to be used for error reporting. |
|
1844 |
* @param t The type referred to. |
|
1845 |
*/ |
|
1846 |
void checkNonCyclic(DiagnosticPosition pos, Type t) { |
|
1847 |
checkNonCyclicInternal(pos, t); |
|
1848 |
} |
|
1849 |
||
1850 |
||
1851 |
void checkNonCyclic(DiagnosticPosition pos, TypeVar t) { |
|
2219
03b1b17c4b2e
6804733: javac generates spourious diagnostics for ill-formed type-variable bounds
mcimadamore
parents:
1874
diff
changeset
|
1852 |
checkNonCyclic1(pos, t, List.<TypeVar>nil()); |
10 | 1853 |
} |
1854 |
||
2219
03b1b17c4b2e
6804733: javac generates spourious diagnostics for ill-formed type-variable bounds
mcimadamore
parents:
1874
diff
changeset
|
1855 |
private void checkNonCyclic1(DiagnosticPosition pos, Type t, List<TypeVar> seen) { |
10 | 1856 |
final TypeVar tv; |
661
9b2f1fe5c183
6677785: REGRESSION: StackOverFlowError with Cyclic Class level Type Parameters when used in constructors
mcimadamore
parents:
508
diff
changeset
|
1857 |
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
|
1858 |
return; |
10 | 1859 |
if (seen.contains(t)) { |
1860 |
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
|
1861 |
tv.bound = types.createErrorType(t); |
10 | 1862 |
log.error(pos, "cyclic.inheritance", t); |
1863 |
} else if (t.tag == TYPEVAR) { |
|
1864 |
tv = (TypeVar)t; |
|
2219
03b1b17c4b2e
6804733: javac generates spourious diagnostics for ill-formed type-variable bounds
mcimadamore
parents:
1874
diff
changeset
|
1865 |
seen = seen.prepend(tv); |
10 | 1866 |
for (Type b : types.getBounds(tv)) |
1867 |
checkNonCyclic1(pos, b, seen); |
|
1868 |
} |
|
1869 |
} |
|
1870 |
||
1871 |
/** Check for cyclic references. Issue an error if the |
|
1872 |
* symbol of the type referred to has a LOCKED flag set. |
|
1873 |
* |
|
1874 |
* @param pos Position to be used for error reporting. |
|
1875 |
* @param t The type referred to. |
|
1876 |
* @returns True if the check completed on all attributed classes |
|
1877 |
*/ |
|
1878 |
private boolean checkNonCyclicInternal(DiagnosticPosition pos, Type t) { |
|
1879 |
boolean complete = true; // was the check complete? |
|
1880 |
//- System.err.println("checkNonCyclicInternal("+t+");");//DEBUG |
|
1881 |
Symbol c = t.tsym; |
|
1882 |
if ((c.flags_field & ACYCLIC) != 0) return true; |
|
1883 |
||
1884 |
if ((c.flags_field & LOCKED) != 0) { |
|
1885 |
noteCyclic(pos, (ClassSymbol)c); |
|
1886 |
} else if (!c.type.isErroneous()) { |
|
1887 |
try { |
|
1888 |
c.flags_field |= LOCKED; |
|
1889 |
if (c.type.tag == CLASS) { |
|
1890 |
ClassType clazz = (ClassType)c.type; |
|
1891 |
if (clazz.interfaces_field != null) |
|
1892 |
for (List<Type> l=clazz.interfaces_field; l.nonEmpty(); l=l.tail) |
|
1893 |
complete &= checkNonCyclicInternal(pos, l.head); |
|
1894 |
if (clazz.supertype_field != null) { |
|
1895 |
Type st = clazz.supertype_field; |
|
1896 |
if (st != null && st.tag == CLASS) |
|
1897 |
complete &= checkNonCyclicInternal(pos, st); |
|
1898 |
} |
|
1899 |
if (c.owner.kind == TYP) |
|
1900 |
complete &= checkNonCyclicInternal(pos, c.owner.type); |
|
1901 |
} |
|
1902 |
} finally { |
|
1903 |
c.flags_field &= ~LOCKED; |
|
1904 |
} |
|
1905 |
} |
|
1906 |
if (complete) |
|
1907 |
complete = ((c.flags_field & UNATTRIBUTED) == 0) && c.completer == null; |
|
1908 |
if (complete) c.flags_field |= ACYCLIC; |
|
1909 |
return complete; |
|
1910 |
} |
|
1911 |
||
1912 |
/** Note that we found an inheritance cycle. */ |
|
1913 |
private void noteCyclic(DiagnosticPosition pos, ClassSymbol c) { |
|
1914 |
log.error(pos, "cyclic.inheritance", c); |
|
1915 |
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
|
1916 |
l.head = types.createErrorType((ClassSymbol)l.head.tsym, Type.noType); |
10 | 1917 |
Type st = types.supertype(c.type); |
1918 |
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
|
1919 |
((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
|
1920 |
c.type = types.createErrorType(c, c.type); |
10 | 1921 |
c.flags_field |= ACYCLIC; |
1922 |
} |
|
1923 |
||
1924 |
/** Check that all methods which implement some |
|
1925 |
* method conform to the method they implement. |
|
1926 |
* @param tree The class definition whose members are checked. |
|
1927 |
*/ |
|
1928 |
void checkImplementations(JCClassDecl tree) { |
|
1929 |
checkImplementations(tree, tree.sym); |
|
1930 |
} |
|
1931 |
//where |
|
1932 |
/** Check that all methods which implement some |
|
1933 |
* method in `ic' conform to the method they implement. |
|
1934 |
*/ |
|
1935 |
void checkImplementations(JCClassDecl tree, ClassSymbol ic) { |
|
1936 |
ClassSymbol origin = tree.sym; |
|
1937 |
for (List<Type> l = types.closure(ic.type); l.nonEmpty(); l = l.tail) { |
|
1938 |
ClassSymbol lc = (ClassSymbol)l.head.tsym; |
|
1939 |
if ((allowGenerics || origin != lc) && (lc.flags() & ABSTRACT) != 0) { |
|
1940 |
for (Scope.Entry e=lc.members().elems; e != null; e=e.sibling) { |
|
1941 |
if (e.sym.kind == MTH && |
|
1942 |
(e.sym.flags() & (STATIC|ABSTRACT)) == ABSTRACT) { |
|
1943 |
MethodSymbol absmeth = (MethodSymbol)e.sym; |
|
1944 |
MethodSymbol implmeth = absmeth.implementation(origin, types, false); |
|
1945 |
if (implmeth != null && implmeth != absmeth && |
|
1946 |
(implmeth.owner.flags() & INTERFACE) == |
|
1947 |
(origin.flags() & INTERFACE)) { |
|
1948 |
// don't check if implmeth is in a class, yet |
|
1949 |
// origin is an interface. This case arises only |
|
1950 |
// if implmeth is declared in Object. The reason is |
|
1951 |
// that interfaces really don't inherit from |
|
1952 |
// Object it's just that the compiler represents |
|
1953 |
// things that way. |
|
1954 |
checkOverride(tree, implmeth, absmeth, origin); |
|
1955 |
} |
|
1956 |
} |
|
1957 |
} |
|
1958 |
} |
|
1959 |
} |
|
1960 |
} |
|
1961 |
||
1962 |
/** Check that all abstract methods implemented by a class are |
|
1963 |
* mutually compatible. |
|
1964 |
* @param pos Position to be used for error reporting. |
|
1965 |
* @param c The class whose interfaces are checked. |
|
1966 |
*/ |
|
1967 |
void checkCompatibleSupertypes(DiagnosticPosition pos, Type c) { |
|
1968 |
List<Type> supertypes = types.interfaces(c); |
|
1969 |
Type supertype = types.supertype(c); |
|
1970 |
if (supertype.tag == CLASS && |
|
1971 |
(supertype.tsym.flags() & ABSTRACT) != 0) |
|
1972 |
supertypes = supertypes.prepend(supertype); |
|
1973 |
for (List<Type> l = supertypes; l.nonEmpty(); l = l.tail) { |
|
1974 |
if (allowGenerics && !l.head.getTypeArguments().isEmpty() && |
|
1975 |
!checkCompatibleAbstracts(pos, l.head, l.head, c)) |
|
1976 |
return; |
|
1977 |
for (List<Type> m = supertypes; m != l; m = m.tail) |
|
1978 |
if (!checkCompatibleAbstracts(pos, l.head, m.head, c)) |
|
1979 |
return; |
|
1980 |
} |
|
1981 |
checkCompatibleConcretes(pos, c); |
|
1982 |
} |
|
1983 |
||
3557
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
1984 |
void checkConflicts(DiagnosticPosition pos, Symbol sym, TypeSymbol c) { |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
1985 |
for (Type ct = c.type; ct != Type.noType ; ct = types.supertype(ct)) { |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
1986 |
for (Scope.Entry e = ct.tsym.members().lookup(sym.name); e.scope == ct.tsym.members(); e = e.next()) { |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
1987 |
// VM allows methods and variables with differing types |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
1988 |
if (sym.kind == e.sym.kind && |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
1989 |
types.isSameType(types.erasure(sym.type), types.erasure(e.sym.type)) && |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
1990 |
sym != e.sym && |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
1991 |
(sym.flags() & Flags.SYNTHETIC) != (e.sym.flags() & Flags.SYNTHETIC) && |
6147
0074061d0efd
6964669: javac reports error on miranda methods
mcimadamore
parents:
6032
diff
changeset
|
1992 |
(sym.flags() & IPROXY) == 0 && (e.sym.flags() & IPROXY) == 0 && |
3557
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
1993 |
(sym.flags() & BRIDGE) == 0 && (e.sym.flags() & BRIDGE) == 0) { |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
1994 |
syntheticError(pos, (e.sym.flags() & SYNTHETIC) == 0 ? e.sym : sym); |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
1995 |
return; |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
1996 |
} |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
1997 |
} |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
1998 |
} |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
1999 |
} |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
2000 |
|
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
2001 |
/** Report a conflict between a user symbol and a synthetic symbol. |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
2002 |
*/ |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
2003 |
private void syntheticError(DiagnosticPosition pos, Symbol sym) { |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
2004 |
if (!sym.type.isErroneous()) { |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
2005 |
if (warnOnSyntheticConflicts) { |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
2006 |
log.warning(pos, "synthetic.name.conflict", sym, sym.location()); |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
2007 |
} |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
2008 |
else { |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
2009 |
log.error(pos, "synthetic.name.conflict", sym, sym.location()); |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
2010 |
} |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
2011 |
} |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
2012 |
} |
a803afefa115
6521805: Regression: JDK5/JDK6 javac allows write access to outer class reference
mcimadamore
parents:
3149
diff
changeset
|
2013 |
|
10 | 2014 |
/** Check that class c does not implement directly or indirectly |
2015 |
* the same parameterized interface with two different argument lists. |
|
2016 |
* @param pos Position to be used for error reporting. |
|
2017 |
* @param type The type whose interfaces are checked. |
|
2018 |
*/ |
|
2019 |
void checkClassBounds(DiagnosticPosition pos, Type type) { |
|
2020 |
checkClassBounds(pos, new HashMap<TypeSymbol,Type>(), type); |
|
2021 |
} |
|
2022 |
//where |
|
2023 |
/** Enter all interfaces of type `type' into the hash table `seensofar' |
|
2024 |
* with their class symbol as key and their type as value. Make |
|
2025 |
* sure no class is entered with two different types. |
|
2026 |
*/ |
|
2027 |
void checkClassBounds(DiagnosticPosition pos, |
|
2028 |
Map<TypeSymbol,Type> seensofar, |
|
2029 |
Type type) { |
|
2030 |
if (type.isErroneous()) return; |
|
2031 |
for (List<Type> l = types.interfaces(type); l.nonEmpty(); l = l.tail) { |
|
2032 |
Type it = l.head; |
|
2033 |
Type oldit = seensofar.put(it.tsym, it); |
|
2034 |
if (oldit != null) { |
|
2035 |
List<Type> oldparams = oldit.allparams(); |
|
2036 |
List<Type> newparams = it.allparams(); |
|
2037 |
if (!types.containsTypeEquivalent(oldparams, newparams)) |
|
2038 |
log.error(pos, "cant.inherit.diff.arg", |
|
2039 |
it.tsym, Type.toString(oldparams), |
|
2040 |
Type.toString(newparams)); |
|
2041 |
} |
|
2042 |
checkClassBounds(pos, seensofar, it); |
|
2043 |
} |
|
2044 |
Type st = types.supertype(type); |
|
2045 |
if (st != null) checkClassBounds(pos, seensofar, st); |
|
2046 |
} |
|
2047 |
||
2048 |
/** Enter interface into into set. |
|
2049 |
* If it existed already, issue a "repeated interface" error. |
|
2050 |
*/ |
|
2051 |
void checkNotRepeated(DiagnosticPosition pos, Type it, Set<Type> its) { |
|
2052 |
if (its.contains(it)) |
|
2053 |
log.error(pos, "repeated.interface"); |
|
2054 |
else { |
|
2055 |
its.add(it); |
|
2056 |
} |
|
2057 |
} |
|
2058 |
||
2059 |
/* ************************************************************************* |
|
2060 |
* Check annotations |
|
2061 |
**************************************************************************/ |
|
2062 |
||
6342
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6151
diff
changeset
|
2063 |
/** |
6347
947437d52cc1
6976649: javac does not enforce required annotation elements in arrays
mcimadamore
parents:
6345
diff
changeset
|
2064 |
* Recursively validate annotations values |
6342
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6151
diff
changeset
|
2065 |
*/ |
6347
947437d52cc1
6976649: javac does not enforce required annotation elements in arrays
mcimadamore
parents:
6345
diff
changeset
|
2066 |
void validateAnnotationTree(JCTree tree) { |
947437d52cc1
6976649: javac does not enforce required annotation elements in arrays
mcimadamore
parents:
6345
diff
changeset
|
2067 |
class AnnotationValidator extends TreeScanner { |
6342
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6151
diff
changeset
|
2068 |
@Override |
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6151
diff
changeset
|
2069 |
public void visitAnnotation(JCAnnotation tree) { |
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6151
diff
changeset
|
2070 |
super.visitAnnotation(tree); |
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6151
diff
changeset
|
2071 |
validateAnnotation(tree); |
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6151
diff
changeset
|
2072 |
} |
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6151
diff
changeset
|
2073 |
} |
6347
947437d52cc1
6976649: javac does not enforce required annotation elements in arrays
mcimadamore
parents:
6345
diff
changeset
|
2074 |
tree.accept(new AnnotationValidator()); |
6342
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6151
diff
changeset
|
2075 |
} |
228b73431edb
6881115: javac permits nested anno w/o mandatory attrs => IncompleteAnnotationException
mcimadamore
parents:
6151
diff
changeset
|
2076 |
|
10 | 2077 |
/** Annotation types are restricted to primitives, String, an |
2078 |
* enum, an annotation, Class, Class<?>, Class<? extends |
|
2079 |
* Anything>, arrays of the preceding. |
|
2080 |
*/ |
|
2081 |
void validateAnnotationType(JCTree restype) { |
|
2082 |
// restype may be null if an error occurred, so don't bother validating it |
|
2083 |
if (restype != null) { |
|
2084 |
validateAnnotationType(restype.pos(), restype.type); |
|
2085 |
} |
|
2086 |
} |
|
2087 |
||
2088 |
void validateAnnotationType(DiagnosticPosition pos, Type type) { |
|
2089 |
if (type.isPrimitive()) return; |
|
2090 |
if (types.isSameType(type, syms.stringType)) return; |
|
2091 |
if ((type.tsym.flags() & Flags.ENUM) != 0) return; |
|
2092 |
if ((type.tsym.flags() & Flags.ANNOTATION) != 0) return; |
|
2093 |
if (types.lowerBound(type).tsym == syms.classType.tsym) return; |
|
2094 |
if (types.isArray(type) && !types.isArray(types.elemtype(type))) { |
|
2095 |
validateAnnotationType(pos, types.elemtype(type)); |
|
2096 |
return; |
|
2097 |
} |
|
2098 |
log.error(pos, "invalid.annotation.member.type"); |
|
2099 |
} |
|
2100 |
||
2101 |
/** |
|
2102 |
* "It is also a compile-time error if any method declared in an |
|
2103 |
* annotation type has a signature that is override-equivalent to |
|
2104 |
* that of any public or protected method declared in class Object |
|
2105 |
* or in the interface annotation.Annotation." |
|
2106 |
* |
|
2107 |
* @jls3 9.6 Annotation Types |
|
2108 |
*/ |
|
2109 |
void validateAnnotationMethod(DiagnosticPosition pos, MethodSymbol m) { |
|
2110 |
for (Type sup = syms.annotationType; sup.tag == CLASS; sup = types.supertype(sup)) { |
|
2111 |
Scope s = sup.tsym.members(); |
|
2112 |
for (Scope.Entry e = s.lookup(m.name); e.scope != null; e = e.next()) { |
|
2113 |
if (e.sym.kind == MTH && |
|
2114 |
(e.sym.flags() & (PUBLIC | PROTECTED)) != 0 && |
|
2115 |
types.overrideEquivalent(m.type, e.sym.type)) |
|
2116 |
log.error(pos, "intf.annotation.member.clash", e.sym, sup); |
|
2117 |
} |
|
2118 |
} |
|
2119 |
} |
|
2120 |
||
2121 |
/** Check the annotations of a symbol. |
|
2122 |
*/ |
|
2123 |
public void validateAnnotations(List<JCAnnotation> annotations, Symbol s) { |
|
2124 |
if (skipAnnotations) return; |
|
2125 |
for (JCAnnotation a : annotations) |
|
2126 |
validateAnnotation(a, s); |
|
2127 |
} |
|
2128 |
||
3149 | 2129 |
/** Check the type annotations |
2130 |
*/ |
|
2131 |
public void validateTypeAnnotations(List<JCTypeAnnotation> annotations, boolean isTypeParameter) { |
|
2132 |
if (skipAnnotations) return; |
|
2133 |
for (JCTypeAnnotation a : annotations) |
|
2134 |
validateTypeAnnotation(a, isTypeParameter); |
|
2135 |
} |
|
2136 |
||
10 | 2137 |
/** Check an annotation of a symbol. |
2138 |
*/ |
|
2139 |
public void validateAnnotation(JCAnnotation a, Symbol s) { |
|
6347
947437d52cc1
6976649: javac does not enforce required annotation elements in arrays
mcimadamore
parents:
6345
diff
changeset
|
2140 |
validateAnnotationTree(a); |
10 | 2141 |
|
2142 |
if (!annotationApplicable(a, s)) |
|
2143 |
log.error(a.pos(), "annotation.type.not.applicable"); |
|
2144 |
||
2145 |
if (a.annotationType.type.tsym == syms.overrideType.tsym) { |
|
2146 |
if (!isOverrider(s)) |
|
2147 |
log.error(a.pos(), "method.does.not.override.superclass"); |
|
2148 |
} |
|
2149 |
} |
|
2150 |
||
3149 | 2151 |
public void validateTypeAnnotation(JCTypeAnnotation a, boolean isTypeParameter) { |
2152 |
if (a.type == null) |
|
2153 |
throw new AssertionError("annotation tree hasn't been attributed yet: " + a); |
|
6347
947437d52cc1
6976649: javac does not enforce required annotation elements in arrays
mcimadamore
parents:
6345
diff
changeset
|
2154 |
validateAnnotationTree(a); |
3149 | 2155 |
|
2156 |
if (!isTypeAnnotation(a, isTypeParameter)) |
|
2157 |
log.error(a.pos(), "annotation.type.not.applicable"); |
|
2158 |
} |
|
2159 |
||
10 | 2160 |
/** Is s a method symbol that overrides a method in a superclass? */ |
2161 |
boolean isOverrider(Symbol s) { |
|
2162 |
if (s.kind != MTH || s.isStatic()) |
|
2163 |
return false; |
|
2164 |
MethodSymbol m = (MethodSymbol)s; |
|
2165 |
TypeSymbol owner = (TypeSymbol)m.owner; |
|
2166 |
for (Type sup : types.closure(owner.type)) { |
|
2167 |
if (sup == owner.type) |
|
2168 |
continue; // skip "this" |
|
2169 |
Scope scope = sup.tsym.members(); |
|
2170 |
for (Scope.Entry e = scope.lookup(m.name); e.scope != null; e = e.next()) { |
|
2171 |
if (!e.sym.isStatic() && m.overrides(e.sym, owner, types, true)) |
|
2172 |
return true; |
|
2173 |
} |
|
2174 |
} |
|
2175 |
return false; |
|
2176 |
} |
|
2177 |
||
3149 | 2178 |
/** Is the annotation applicable to type annotations */ |
2179 |
boolean isTypeAnnotation(JCTypeAnnotation a, boolean isTypeParameter) { |
|
2180 |
Attribute.Compound atTarget = |
|
2181 |
a.annotationType.type.tsym.attribute(syms.annotationTargetType.tsym); |
|
2182 |
if (atTarget == null) return true; |
|
2183 |
Attribute atValue = atTarget.member(names.value); |
|
2184 |
if (!(atValue instanceof Attribute.Array)) return true; // error recovery |
|
2185 |
Attribute.Array arr = (Attribute.Array) atValue; |
|
2186 |
for (Attribute app : arr.values) { |
|
2187 |
if (!(app instanceof Attribute.Enum)) return true; // recovery |
|
2188 |
Attribute.Enum e = (Attribute.Enum) app; |
|
2189 |
if (!isTypeParameter && e.value.name == names.TYPE_USE) |
|
2190 |
return true; |
|
2191 |
else if (isTypeParameter && e.value.name == names.TYPE_PARAMETER) |
|
2192 |
return true; |
|
2193 |
} |
|
2194 |
return false; |
|
2195 |
} |
|
2196 |
||
10 | 2197 |
/** Is the annotation applicable to the symbol? */ |
2198 |
boolean annotationApplicable(JCAnnotation a, Symbol s) { |
|
2199 |
Attribute.Compound atTarget = |
|
2200 |
a.annotationType.type.tsym.attribute(syms.annotationTargetType.tsym); |
|
2201 |
if (atTarget == null) return true; |
|
2202 |
Attribute atValue = atTarget.member(names.value); |
|
2203 |
if (!(atValue instanceof Attribute.Array)) return true; // error recovery |
|
2204 |
Attribute.Array arr = (Attribute.Array) atValue; |
|
2205 |
for (Attribute app : arr.values) { |
|
2206 |
if (!(app instanceof Attribute.Enum)) return true; // recovery |
|
2207 |
Attribute.Enum e = (Attribute.Enum) app; |
|
2208 |
if (e.value.name == names.TYPE) |
|
2209 |
{ if (s.kind == TYP) return true; } |
|
2210 |
else if (e.value.name == names.FIELD) |
|
2211 |
{ if (s.kind == VAR && s.owner.kind != MTH) return true; } |
|
2212 |
else if (e.value.name == names.METHOD) |
|
2213 |
{ if (s.kind == MTH && !s.isConstructor()) return true; } |
|
2214 |
else if (e.value.name == names.PARAMETER) |
|
2215 |
{ if (s.kind == VAR && |
|
2216 |
s.owner.kind == MTH && |
|
2217 |
(s.flags() & PARAMETER) != 0) |
|
2218 |
return true; |
|
2219 |
} |
|
2220 |
else if (e.value.name == names.CONSTRUCTOR) |
|
2221 |
{ if (s.kind == MTH && s.isConstructor()) return true; } |
|
2222 |
else if (e.value.name == names.LOCAL_VARIABLE) |
|
2223 |
{ if (s.kind == VAR && s.owner.kind == MTH && |
|
2224 |
(s.flags() & PARAMETER) == 0) |
|
2225 |
return true; |
|
2226 |
} |
|
2227 |
else if (e.value.name == names.ANNOTATION_TYPE) |
|
2228 |
{ if (s.kind == TYP && (s.flags() & ANNOTATION) != 0) |
|
2229 |
return true; |
|
2230 |
} |
|
2231 |
else if (e.value.name == names.PACKAGE) |
|
2232 |
{ if (s.kind == PCK) return true; } |
|
3149 | 2233 |
else if (e.value.name == names.TYPE_USE) |
2234 |
{ if (s.kind == TYP || |
|
2235 |
s.kind == VAR || |
|
2236 |
(s.kind == MTH && !s.isConstructor() && |
|
2237 |
s.type.getReturnType().tag != VOID)) |
|
2238 |
return true; |
|
2239 |
} |
|
10 | 2240 |
else |
2241 |
return true; // recovery |
|
2242 |
} |
|
2243 |
return false; |
|
2244 |
} |
|
2245 |
||
2246 |
/** Check an annotation value. |
|
2247 |
*/ |
|
2248 |
public void validateAnnotation(JCAnnotation a) { |
|
2249 |
if (a.type.isErroneous()) return; |
|
2250 |
||
6345
7d98c298aafd
6975231: Regression test for 6881115 is failing with compiler output not matching expected output
mcimadamore
parents:
6342
diff
changeset
|
2251 |
// collect an inventory of the members (sorted alphabetically) |
7d98c298aafd
6975231: Regression test for 6881115 is failing with compiler output not matching expected output
mcimadamore
parents:
6342
diff
changeset
|
2252 |
Set<MethodSymbol> members = new TreeSet<MethodSymbol>(new Comparator<Symbol>() { |
7d98c298aafd
6975231: Regression test for 6881115 is failing with compiler output not matching expected output
mcimadamore
parents:
6342
diff
changeset
|
2253 |
public int compare(Symbol t, Symbol t1) { |
7d98c298aafd
6975231: Regression test for 6881115 is failing with compiler output not matching expected output
mcimadamore
parents:
6342
diff
changeset
|
2254 |
return t.name.compareTo(t1.name); |
7d98c298aafd
6975231: Regression test for 6881115 is failing with compiler output not matching expected output
mcimadamore
parents:
6342
diff
changeset
|
2255 |
} |
7d98c298aafd
6975231: Regression test for 6881115 is failing with compiler output not matching expected output
mcimadamore
parents:
6342
diff
changeset
|
2256 |
}); |
10 | 2257 |
for (Scope.Entry e = a.annotationType.type.tsym.members().elems; |
2258 |
e != null; |
|
2259 |
e = e.sibling) |
|
2260 |
if (e.sym.kind == MTH) |
|
2261 |
members.add((MethodSymbol) e.sym); |
|
2262 |
||
2263 |
// count them off as they're annotated |
|
2264 |
for (JCTree arg : a.args) { |
|
2265 |
if (arg.getTag() != JCTree.ASSIGN) continue; // recovery |
|
2266 |
JCAssign assign = (JCAssign) arg; |
|
2267 |
Symbol m = TreeInfo.symbol(assign.lhs); |
|
2268 |
if (m == null || m.type.isErroneous()) continue; |
|
2269 |
if (!members.remove(m)) |
|
4867
7f4efc9b2f9f
6919944: incorrect position given for duplicate annotation value error
jjg
parents:
3780
diff
changeset
|
2270 |
log.error(assign.lhs.pos(), "duplicate.annotation.member.value", |
10 | 2271 |
m.name, a.type); |
2272 |
} |
|
2273 |
||
2274 |
// all the remaining ones better have default values |
|
6345
7d98c298aafd
6975231: Regression test for 6881115 is failing with compiler output not matching expected output
mcimadamore
parents:
6342
diff
changeset
|
2275 |
ListBuffer<Name> missingDefaults = ListBuffer.lb(); |
7d98c298aafd
6975231: Regression test for 6881115 is failing with compiler output not matching expected output
mcimadamore
parents:
6342
diff
changeset
|
2276 |
for (MethodSymbol m : members) { |
7d98c298aafd
6975231: Regression test for 6881115 is failing with compiler output not matching expected output
mcimadamore
parents:
6342
diff
changeset
|
2277 |
if (m.defaultValue == null && !m.type.isErroneous()) { |
7d98c298aafd
6975231: Regression test for 6881115 is failing with compiler output not matching expected output
mcimadamore
parents:
6342
diff
changeset
|
2278 |
missingDefaults.append(m.name); |
7d98c298aafd
6975231: Regression test for 6881115 is failing with compiler output not matching expected output
mcimadamore
parents:
6342
diff
changeset
|
2279 |
} |
7d98c298aafd
6975231: Regression test for 6881115 is failing with compiler output not matching expected output
mcimadamore
parents:
6342
diff
changeset
|
2280 |
} |
7d98c298aafd
6975231: Regression test for 6881115 is failing with compiler output not matching expected output
mcimadamore
parents:
6342
diff
changeset
|
2281 |
if (missingDefaults.nonEmpty()) { |
7d98c298aafd
6975231: Regression test for 6881115 is failing with compiler output not matching expected output
mcimadamore
parents:
6342
diff
changeset
|
2282 |
String key = (missingDefaults.size() > 1) |
7d98c298aafd
6975231: Regression test for 6881115 is failing with compiler output not matching expected output
mcimadamore
parents:
6342
diff
changeset
|
2283 |
? "annotation.missing.default.value.1" |
7d98c298aafd
6975231: Regression test for 6881115 is failing with compiler output not matching expected output
mcimadamore
parents:
6342
diff
changeset
|
2284 |
: "annotation.missing.default.value"; |
7d98c298aafd
6975231: Regression test for 6881115 is failing with compiler output not matching expected output
mcimadamore
parents:
6342
diff
changeset
|
2285 |
log.error(a.pos(), key, a.type, missingDefaults); |
7d98c298aafd
6975231: Regression test for 6881115 is failing with compiler output not matching expected output
mcimadamore
parents:
6342
diff
changeset
|
2286 |
} |
10 | 2287 |
|
2288 |
// special case: java.lang.annotation.Target must not have |
|
2289 |
// repeated values in its value member |
|
2290 |
if (a.annotationType.type.tsym != syms.annotationTargetType.tsym || |
|
2291 |
a.args.tail == null) |
|
2292 |
return; |
|
2293 |
||
2294 |
if (a.args.head.getTag() != JCTree.ASSIGN) return; // error recovery |
|
2295 |
JCAssign assign = (JCAssign) a.args.head; |
|
2296 |
Symbol m = TreeInfo.symbol(assign.lhs); |
|
2297 |
if (m.name != names.value) return; |
|
2298 |
JCTree rhs = assign.rhs; |
|
2299 |
if (rhs.getTag() != JCTree.NEWARRAY) return; |
|
2300 |
JCNewArray na = (JCNewArray) rhs; |
|
2301 |
Set<Symbol> targets = new HashSet<Symbol>(); |
|
2302 |
for (JCTree elem : na.elems) { |
|
2303 |
if (!targets.add(TreeInfo.symbol(elem))) { |
|
2304 |
log.error(elem.pos(), "repeated.annotation.target"); |
|
2305 |
} |
|
2306 |
} |
|
2307 |
} |
|
2308 |
||
2309 |
void checkDeprecatedAnnotation(DiagnosticPosition pos, Symbol s) { |
|
2310 |
if (allowAnnotations && |
|
2311 |
lint.isEnabled(Lint.LintCategory.DEP_ANN) && |
|
2312 |
(s.flags() & DEPRECATED) != 0 && |
|
2313 |
!syms.deprecatedType.isErroneous() && |
|
2314 |
s.attribute(syms.deprecatedType.tsym) == null) { |
|
6151
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
6148
diff
changeset
|
2315 |
log.warning(Lint.LintCategory.DEP_ANN, |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
6148
diff
changeset
|
2316 |
pos, "missing.deprecated.annotation"); |
10 | 2317 |
} |
2318 |
} |
|
2319 |
||
2320 |
/* ************************************************************************* |
|
2321 |
* Check for recursive annotation elements. |
|
2322 |
**************************************************************************/ |
|
2323 |
||
2324 |
/** Check for cycles in the graph of annotation elements. |
|
2325 |
*/ |
|
2326 |
void checkNonCyclicElements(JCClassDecl tree) { |
|
2327 |
if ((tree.sym.flags_field & ANNOTATION) == 0) return; |
|
2328 |
assert (tree.sym.flags_field & LOCKED) == 0; |
|
2329 |
try { |
|
2330 |
tree.sym.flags_field |= LOCKED; |
|
2331 |
for (JCTree def : tree.defs) { |
|
2332 |
if (def.getTag() != JCTree.METHODDEF) continue; |
|
2333 |
JCMethodDecl meth = (JCMethodDecl)def; |
|
2334 |
checkAnnotationResType(meth.pos(), meth.restype.type); |
|
2335 |
} |
|
2336 |
} finally { |
|
2337 |
tree.sym.flags_field &= ~LOCKED; |
|
2338 |
tree.sym.flags_field |= ACYCLIC_ANN; |
|
2339 |
} |
|
2340 |
} |
|
2341 |
||
2342 |
void checkNonCyclicElementsInternal(DiagnosticPosition pos, TypeSymbol tsym) { |
|
2343 |
if ((tsym.flags_field & ACYCLIC_ANN) != 0) |
|
2344 |
return; |
|
2345 |
if ((tsym.flags_field & LOCKED) != 0) { |
|
2346 |
log.error(pos, "cyclic.annotation.element"); |
|
2347 |
return; |
|
2348 |
} |
|
2349 |
try { |
|
2350 |
tsym.flags_field |= LOCKED; |
|
2351 |
for (Scope.Entry e = tsym.members().elems; e != null; e = e.sibling) { |
|
2352 |
Symbol s = e.sym; |
|
2353 |
if (s.kind != Kinds.MTH) |
|
2354 |
continue; |
|
2355 |
checkAnnotationResType(pos, ((MethodSymbol)s).type.getReturnType()); |
|
2356 |
} |
|
2357 |
} finally { |
|
2358 |
tsym.flags_field &= ~LOCKED; |
|
2359 |
tsym.flags_field |= ACYCLIC_ANN; |
|
2360 |
} |
|
2361 |
} |
|
2362 |
||
2363 |
void checkAnnotationResType(DiagnosticPosition pos, Type type) { |
|
2364 |
switch (type.tag) { |
|
2365 |
case TypeTags.CLASS: |
|
2366 |
if ((type.tsym.flags() & ANNOTATION) != 0) |
|
2367 |
checkNonCyclicElementsInternal(pos, type.tsym); |
|
2368 |
break; |
|
2369 |
case TypeTags.ARRAY: |
|
2370 |
checkAnnotationResType(pos, types.elemtype(type)); |
|
2371 |
break; |
|
2372 |
default: |
|
2373 |
break; // int etc |
|
2374 |
} |
|
2375 |
} |
|
2376 |
||
2377 |
/* ************************************************************************* |
|
2378 |
* Check for cycles in the constructor call graph. |
|
2379 |
**************************************************************************/ |
|
2380 |
||
2381 |
/** Check for cycles in the graph of constructors calling other |
|
2382 |
* constructors. |
|
2383 |
*/ |
|
2384 |
void checkCyclicConstructors(JCClassDecl tree) { |
|
2385 |
Map<Symbol,Symbol> callMap = new HashMap<Symbol, Symbol>(); |
|
2386 |
||
2387 |
// enter each constructor this-call into the map |
|
2388 |
for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) { |
|
2389 |
JCMethodInvocation app = TreeInfo.firstConstructorCall(l.head); |
|
2390 |
if (app == null) continue; |
|
2391 |
JCMethodDecl meth = (JCMethodDecl) l.head; |
|
2392 |
if (TreeInfo.name(app.meth) == names._this) { |
|
2393 |
callMap.put(meth.sym, TreeInfo.symbol(app.meth)); |
|
2394 |
} else { |
|
2395 |
meth.sym.flags_field |= ACYCLIC; |
|
2396 |
} |
|
2397 |
} |
|
2398 |
||
2399 |
// Check for cycles in the map |
|
2400 |
Symbol[] ctors = new Symbol[0]; |
|
2401 |
ctors = callMap.keySet().toArray(ctors); |
|
2402 |
for (Symbol caller : ctors) { |
|
2403 |
checkCyclicConstructor(tree, caller, callMap); |
|
2404 |
} |
|
2405 |
} |
|
2406 |
||
2407 |
/** Look in the map to see if the given constructor is part of a |
|
2408 |
* call cycle. |
|
2409 |
*/ |
|
2410 |
private void checkCyclicConstructor(JCClassDecl tree, Symbol ctor, |
|
2411 |
Map<Symbol,Symbol> callMap) { |
|
2412 |
if (ctor != null && (ctor.flags_field & ACYCLIC) == 0) { |
|
2413 |
if ((ctor.flags_field & LOCKED) != 0) { |
|
2414 |
log.error(TreeInfo.diagnosticPositionFor(ctor, tree), |
|
2415 |
"recursive.ctor.invocation"); |
|
2416 |
} else { |
|
2417 |
ctor.flags_field |= LOCKED; |
|
2418 |
checkCyclicConstructor(tree, callMap.remove(ctor), callMap); |
|
2419 |
ctor.flags_field &= ~LOCKED; |
|
2420 |
} |
|
2421 |
ctor.flags_field |= ACYCLIC; |
|
2422 |
} |
|
2423 |
} |
|
2424 |
||
2425 |
/* ************************************************************************* |
|
2426 |
* Miscellaneous |
|
2427 |
**************************************************************************/ |
|
2428 |
||
2429 |
/** |
|
2430 |
* Return the opcode of the operator but emit an error if it is an |
|
2431 |
* error. |
|
2432 |
* @param pos position for error reporting. |
|
2433 |
* @param operator an operator |
|
2434 |
* @param tag a tree tag |
|
2435 |
* @param left type of left hand side |
|
2436 |
* @param right type of right hand side |
|
2437 |
*/ |
|
2438 |
int checkOperator(DiagnosticPosition pos, |
|
2439 |
OperatorSymbol operator, |
|
2440 |
int tag, |
|
2441 |
Type left, |
|
2442 |
Type right) { |
|
2443 |
if (operator.opcode == ByteCodes.error) { |
|
2444 |
log.error(pos, |
|
2445 |
"operator.cant.be.applied", |
|
2446 |
treeinfo.operatorName(tag), |
|
939
38e24969c7e9
6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents:
938
diff
changeset
|
2447 |
List.of(left, right)); |
10 | 2448 |
} |
2449 |
return operator.opcode; |
|
2450 |
} |
|
2451 |
||
2452 |
||
2453 |
/** |
|
2454 |
* Check for division by integer constant zero |
|
2455 |
* @param pos Position for error reporting. |
|
2456 |
* @param operator The operator for the expression |
|
2457 |
* @param operand The right hand operand for the expression |
|
2458 |
*/ |
|
2459 |
void checkDivZero(DiagnosticPosition pos, Symbol operator, Type operand) { |
|
2460 |
if (operand.constValue() != null |
|
2461 |
&& lint.isEnabled(Lint.LintCategory.DIVZERO) |
|
2462 |
&& operand.tag <= LONG |
|
2463 |
&& ((Number) (operand.constValue())).longValue() == 0) { |
|
2464 |
int opc = ((OperatorSymbol)operator).opcode; |
|
2465 |
if (opc == ByteCodes.idiv || opc == ByteCodes.imod |
|
2466 |
|| opc == ByteCodes.ldiv || opc == ByteCodes.lmod) { |
|
6151
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
6148
diff
changeset
|
2467 |
log.warning(Lint.LintCategory.DIVZERO, pos, "div.zero"); |
10 | 2468 |
} |
2469 |
} |
|
2470 |
} |
|
2471 |
||
2472 |
/** |
|
2473 |
* Check for empty statements after if |
|
2474 |
*/ |
|
2475 |
void checkEmptyIf(JCIf tree) { |
|
2476 |
if (tree.thenpart.getTag() == JCTree.SKIP && tree.elsepart == null && lint.isEnabled(Lint.LintCategory.EMPTY)) |
|
6151
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
6148
diff
changeset
|
2477 |
log.warning(Lint.LintCategory.EMPTY, tree.thenpart.pos(), "empty.if"); |
10 | 2478 |
} |
2479 |
||
2480 |
/** Check that symbol is unique in given scope. |
|
2481 |
* @param pos Position for error reporting. |
|
2482 |
* @param sym The symbol. |
|
2483 |
* @param s The scope. |
|
2484 |
*/ |
|
2485 |
boolean checkUnique(DiagnosticPosition pos, Symbol sym, Scope s) { |
|
2486 |
if (sym.type.isErroneous()) |
|
2487 |
return true; |
|
2488 |
if (sym.owner.name == names.any) return false; |
|
2489 |
for (Scope.Entry e = s.lookup(sym.name); e.scope == s; e = e.next()) { |
|
2490 |
if (sym != e.sym && |
|
2491 |
sym.kind == e.sym.kind && |
|
2492 |
sym.name != names.error && |
|
2509
c8a1744c87fd
6182950: methods clash algorithm should not depend on return type
mcimadamore
parents:
2223
diff
changeset
|
2493 |
(sym.kind != MTH || types.hasSameArgs(types.erasure(sym.type), types.erasure(e.sym.type)))) { |
10 | 2494 |
if ((sym.flags() & VARARGS) != (e.sym.flags() & VARARGS)) |
2495 |
varargsDuplicateError(pos, sym, e.sym); |
|
2509
c8a1744c87fd
6182950: methods clash algorithm should not depend on return type
mcimadamore
parents:
2223
diff
changeset
|
2496 |
else if (sym.kind == MTH && !types.overrideEquivalent(sym.type, e.sym.type)) |
c8a1744c87fd
6182950: methods clash algorithm should not depend on return type
mcimadamore
parents:
2223
diff
changeset
|
2497 |
duplicateErasureError(pos, sym, e.sym); |
10 | 2498 |
else |
2499 |
duplicateError(pos, e.sym); |
|
2500 |
return false; |
|
2501 |
} |
|
2502 |
} |
|
2503 |
return true; |
|
2504 |
} |
|
2509
c8a1744c87fd
6182950: methods clash algorithm should not depend on return type
mcimadamore
parents:
2223
diff
changeset
|
2505 |
//where |
c8a1744c87fd
6182950: methods clash algorithm should not depend on return type
mcimadamore
parents:
2223
diff
changeset
|
2506 |
/** Report duplicate declaration error. |
c8a1744c87fd
6182950: methods clash algorithm should not depend on return type
mcimadamore
parents:
2223
diff
changeset
|
2507 |
*/ |
c8a1744c87fd
6182950: methods clash algorithm should not depend on return type
mcimadamore
parents:
2223
diff
changeset
|
2508 |
void duplicateErasureError(DiagnosticPosition pos, Symbol sym1, Symbol sym2) { |
c8a1744c87fd
6182950: methods clash algorithm should not depend on return type
mcimadamore
parents:
2223
diff
changeset
|
2509 |
if (!sym1.type.isErroneous() && !sym2.type.isErroneous()) { |
c8a1744c87fd
6182950: methods clash algorithm should not depend on return type
mcimadamore
parents:
2223
diff
changeset
|
2510 |
log.error(pos, "name.clash.same.erasure", sym1, sym2); |
c8a1744c87fd
6182950: methods clash algorithm should not depend on return type
mcimadamore
parents:
2223
diff
changeset
|
2511 |
} |
c8a1744c87fd
6182950: methods clash algorithm should not depend on return type
mcimadamore
parents:
2223
diff
changeset
|
2512 |
} |
10 | 2513 |
|
2514 |
/** Check that single-type import is not already imported or top-level defined, |
|
2515 |
* but make an exception for two single-type imports which denote the same type. |
|
2516 |
* @param pos Position for error reporting. |
|
2517 |
* @param sym The symbol. |
|
2518 |
* @param s The scope |
|
2519 |
*/ |
|
2520 |
boolean checkUniqueImport(DiagnosticPosition pos, Symbol sym, Scope s) { |
|
2521 |
return checkUniqueImport(pos, sym, s, false); |
|
2522 |
} |
|
2523 |
||
2524 |
/** Check that static single-type import is not already imported or top-level defined, |
|
2525 |
* but make an exception for two single-type imports which denote the same type. |
|
2526 |
* @param pos Position for error reporting. |
|
2527 |
* @param sym The symbol. |
|
2528 |
* @param s The scope |
|
2529 |
* @param staticImport Whether or not this was a static import |
|
2530 |
*/ |
|
2531 |
boolean checkUniqueStaticImport(DiagnosticPosition pos, Symbol sym, Scope s) { |
|
2532 |
return checkUniqueImport(pos, sym, s, true); |
|
2533 |
} |
|
2534 |
||
2535 |
/** Check that single-type import is not already imported or top-level defined, |
|
2536 |
* but make an exception for two single-type imports which denote the same type. |
|
2537 |
* @param pos Position for error reporting. |
|
2538 |
* @param sym The symbol. |
|
2539 |
* @param s The scope. |
|
2540 |
* @param staticImport Whether or not this was a static import |
|
2541 |
*/ |
|
2542 |
private boolean checkUniqueImport(DiagnosticPosition pos, Symbol sym, Scope s, boolean staticImport) { |
|
2543 |
for (Scope.Entry e = s.lookup(sym.name); e.scope != null; e = e.next()) { |
|
2544 |
// is encountered class entered via a class declaration? |
|
2545 |
boolean isClassDecl = e.scope == s; |
|
2546 |
if ((isClassDecl || sym != e.sym) && |
|
2547 |
sym.kind == e.sym.kind && |
|
2548 |
sym.name != names.error) { |
|
2549 |
if (!e.sym.type.isErroneous()) { |
|
2550 |
String what = e.sym.toString(); |
|
2551 |
if (!isClassDecl) { |
|
2552 |
if (staticImport) |
|
2553 |
log.error(pos, "already.defined.static.single.import", what); |
|
2554 |
else |
|
2555 |
log.error(pos, "already.defined.single.import", what); |
|
2556 |
} |
|
2557 |
else if (sym != e.sym) |
|
2558 |
log.error(pos, "already.defined.this.unit", what); |
|
2559 |
} |
|
2560 |
return false; |
|
2561 |
} |
|
2562 |
} |
|
2563 |
return true; |
|
2564 |
} |
|
2565 |
||
2566 |
/** Check that a qualified name is in canonical form (for import decls). |
|
2567 |
*/ |
|
2568 |
public void checkCanonical(JCTree tree) { |
|
2569 |
if (!isCanonical(tree)) |
|
2570 |
log.error(tree.pos(), "import.requires.canonical", |
|
2571 |
TreeInfo.symbol(tree)); |
|
2572 |
} |
|
2573 |
// where |
|
2574 |
private boolean isCanonical(JCTree tree) { |
|
2575 |
while (tree.getTag() == JCTree.SELECT) { |
|
2576 |
JCFieldAccess s = (JCFieldAccess) tree; |
|
2577 |
if (s.sym.owner != TreeInfo.symbol(s.selected)) |
|
2578 |
return false; |
|
2579 |
tree = s.selected; |
|
2580 |
} |
|
2581 |
return true; |
|
2582 |
} |
|
2583 |
||
2584 |
private class ConversionWarner extends Warner { |
|
2585 |
final String key; |
|
2586 |
final Type found; |
|
2587 |
final Type expected; |
|
2588 |
public ConversionWarner(DiagnosticPosition pos, String key, Type found, Type expected) { |
|
2589 |
super(pos); |
|
2590 |
this.key = key; |
|
2591 |
this.found = found; |
|
2592 |
this.expected = expected; |
|
2593 |
} |
|
2594 |
||
3780
368edd3d2082
6874249: Check has duplicate local variable and field for "source"
jjg
parents:
3765
diff
changeset
|
2595 |
@Override |
10 | 2596 |
public void warnUnchecked() { |
2597 |
boolean warned = this.warned; |
|
2598 |
super.warnUnchecked(); |
|
2599 |
if (warned) return; // suppress redundant diagnostics |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
939
diff
changeset
|
2600 |
Object problem = diags.fragment(key); |
10 | 2601 |
Check.this.warnUnchecked(pos(), "prob.found.req", problem, found, expected); |
2602 |
} |
|
2603 |
} |
|
2604 |
||
2605 |
public Warner castWarner(DiagnosticPosition pos, Type found, Type expected) { |
|
2606 |
return new ConversionWarner(pos, "unchecked.cast.to.type", found, expected); |
|
2607 |
} |
|
2608 |
||
2609 |
public Warner convertWarner(DiagnosticPosition pos, Type found, Type expected) { |
|
2610 |
return new ConversionWarner(pos, "unchecked.assign", found, expected); |
|
2611 |
} |
|
2612 |
} |