Mon, 16 Oct 2017 16:07:48 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.tools.javac.comp; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.util.*; |
aoqi@0 | 29 | |
aoqi@0 | 30 | import javax.tools.JavaFileManager; |
aoqi@0 | 31 | |
aoqi@0 | 32 | import com.sun.tools.javac.code.*; |
aoqi@0 | 33 | import com.sun.tools.javac.code.Attribute.Compound; |
aoqi@0 | 34 | import com.sun.tools.javac.jvm.*; |
aoqi@0 | 35 | import com.sun.tools.javac.tree.*; |
aoqi@0 | 36 | import com.sun.tools.javac.util.*; |
aoqi@0 | 37 | import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
aoqi@0 | 38 | import com.sun.tools.javac.util.List; |
aoqi@0 | 39 | |
aoqi@0 | 40 | import com.sun.tools.javac.code.Lint; |
aoqi@0 | 41 | import com.sun.tools.javac.code.Lint.LintCategory; |
aoqi@0 | 42 | import com.sun.tools.javac.code.Type.*; |
aoqi@0 | 43 | import com.sun.tools.javac.code.Symbol.*; |
aoqi@0 | 44 | import com.sun.tools.javac.comp.DeferredAttr.DeferredAttrContext; |
aoqi@0 | 45 | import com.sun.tools.javac.comp.Infer.InferenceContext; |
aoqi@0 | 46 | import com.sun.tools.javac.comp.Infer.FreeTypeListener; |
aoqi@0 | 47 | import com.sun.tools.javac.tree.JCTree.*; |
aoqi@0 | 48 | import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*; |
aoqi@0 | 49 | |
aoqi@0 | 50 | import static com.sun.tools.javac.code.Flags.*; |
aoqi@0 | 51 | import static com.sun.tools.javac.code.Flags.ANNOTATION; |
aoqi@0 | 52 | import static com.sun.tools.javac.code.Flags.SYNCHRONIZED; |
aoqi@0 | 53 | import static com.sun.tools.javac.code.Kinds.*; |
aoqi@0 | 54 | import static com.sun.tools.javac.code.TypeTag.*; |
aoqi@0 | 55 | import static com.sun.tools.javac.code.TypeTag.WILDCARD; |
aoqi@0 | 56 | |
aoqi@0 | 57 | import static com.sun.tools.javac.tree.JCTree.Tag.*; |
aoqi@0 | 58 | |
aoqi@0 | 59 | /** Type checking helper class for the attribution phase. |
aoqi@0 | 60 | * |
aoqi@0 | 61 | * <p><b>This is NOT part of any supported API. |
aoqi@0 | 62 | * If you write code that depends on this, you do so at your own risk. |
aoqi@0 | 63 | * This code and its internal interfaces are subject to change or |
aoqi@0 | 64 | * deletion without notice.</b> |
aoqi@0 | 65 | */ |
aoqi@0 | 66 | public class Check { |
aoqi@0 | 67 | protected static final Context.Key<Check> checkKey = |
aoqi@0 | 68 | new Context.Key<Check>(); |
aoqi@0 | 69 | |
aoqi@0 | 70 | private final Names names; |
aoqi@0 | 71 | private final Log log; |
aoqi@0 | 72 | private final Resolve rs; |
aoqi@0 | 73 | private final Symtab syms; |
aoqi@0 | 74 | private final Enter enter; |
aoqi@0 | 75 | private final DeferredAttr deferredAttr; |
aoqi@0 | 76 | private final Infer infer; |
aoqi@0 | 77 | private final Types types; |
aoqi@0 | 78 | private final JCDiagnostic.Factory diags; |
aoqi@0 | 79 | private boolean warnOnSyntheticConflicts; |
aoqi@0 | 80 | private boolean suppressAbortOnBadClassFile; |
aoqi@0 | 81 | private boolean enableSunApiLintControl; |
aoqi@0 | 82 | private final TreeInfo treeinfo; |
aoqi@0 | 83 | private final JavaFileManager fileManager; |
aoqi@0 | 84 | private final Profile profile; |
aoqi@0 | 85 | private final boolean warnOnAccessToSensitiveMembers; |
aoqi@0 | 86 | |
aoqi@0 | 87 | // The set of lint options currently in effect. It is initialized |
aoqi@0 | 88 | // from the context, and then is set/reset as needed by Attr as it |
aoqi@0 | 89 | // visits all the various parts of the trees during attribution. |
aoqi@0 | 90 | private Lint lint; |
aoqi@0 | 91 | |
aoqi@0 | 92 | // The method being analyzed in Attr - it is set/reset as needed by |
aoqi@0 | 93 | // Attr as it visits new method declarations. |
aoqi@0 | 94 | private MethodSymbol method; |
aoqi@0 | 95 | |
aoqi@0 | 96 | public static Check instance(Context context) { |
aoqi@0 | 97 | Check instance = context.get(checkKey); |
aoqi@0 | 98 | if (instance == null) |
aoqi@0 | 99 | instance = new Check(context); |
aoqi@0 | 100 | return instance; |
aoqi@0 | 101 | } |
aoqi@0 | 102 | |
aoqi@0 | 103 | protected Check(Context context) { |
aoqi@0 | 104 | context.put(checkKey, this); |
aoqi@0 | 105 | |
aoqi@0 | 106 | names = Names.instance(context); |
aoqi@0 | 107 | dfltTargetMeta = new Name[] { names.PACKAGE, names.TYPE, |
aoqi@0 | 108 | names.FIELD, names.METHOD, names.CONSTRUCTOR, |
aoqi@0 | 109 | names.ANNOTATION_TYPE, names.LOCAL_VARIABLE, names.PARAMETER}; |
aoqi@0 | 110 | log = Log.instance(context); |
aoqi@0 | 111 | rs = Resolve.instance(context); |
aoqi@0 | 112 | syms = Symtab.instance(context); |
aoqi@0 | 113 | enter = Enter.instance(context); |
aoqi@0 | 114 | deferredAttr = DeferredAttr.instance(context); |
aoqi@0 | 115 | infer = Infer.instance(context); |
aoqi@0 | 116 | types = Types.instance(context); |
aoqi@0 | 117 | diags = JCDiagnostic.Factory.instance(context); |
aoqi@0 | 118 | Options options = Options.instance(context); |
aoqi@0 | 119 | lint = Lint.instance(context); |
aoqi@0 | 120 | treeinfo = TreeInfo.instance(context); |
aoqi@0 | 121 | fileManager = context.get(JavaFileManager.class); |
aoqi@0 | 122 | |
aoqi@0 | 123 | Source source = Source.instance(context); |
aoqi@0 | 124 | allowGenerics = source.allowGenerics(); |
aoqi@0 | 125 | allowVarargs = source.allowVarargs(); |
aoqi@0 | 126 | allowAnnotations = source.allowAnnotations(); |
aoqi@0 | 127 | allowCovariantReturns = source.allowCovariantReturns(); |
aoqi@0 | 128 | allowSimplifiedVarargs = source.allowSimplifiedVarargs(); |
aoqi@0 | 129 | allowDefaultMethods = source.allowDefaultMethods(); |
aoqi@0 | 130 | allowStrictMethodClashCheck = source.allowStrictMethodClashCheck(); |
aoqi@0 | 131 | complexInference = options.isSet("complexinference"); |
aoqi@0 | 132 | warnOnSyntheticConflicts = options.isSet("warnOnSyntheticConflicts"); |
aoqi@0 | 133 | suppressAbortOnBadClassFile = options.isSet("suppressAbortOnBadClassFile"); |
aoqi@0 | 134 | enableSunApiLintControl = options.isSet("enableSunApiLintControl"); |
aoqi@0 | 135 | warnOnAccessToSensitiveMembers = options.isSet("warnOnAccessToSensitiveMembers"); |
aoqi@0 | 136 | |
aoqi@0 | 137 | Target target = Target.instance(context); |
aoqi@0 | 138 | syntheticNameChar = target.syntheticNameChar(); |
aoqi@0 | 139 | |
aoqi@0 | 140 | profile = Profile.instance(context); |
aoqi@0 | 141 | |
aoqi@0 | 142 | boolean verboseDeprecated = lint.isEnabled(LintCategory.DEPRECATION); |
aoqi@0 | 143 | boolean verboseUnchecked = lint.isEnabled(LintCategory.UNCHECKED); |
aoqi@0 | 144 | boolean verboseSunApi = lint.isEnabled(LintCategory.SUNAPI); |
aoqi@0 | 145 | boolean enforceMandatoryWarnings = source.enforceMandatoryWarnings(); |
aoqi@0 | 146 | |
aoqi@0 | 147 | deprecationHandler = new MandatoryWarningHandler(log, verboseDeprecated, |
aoqi@0 | 148 | enforceMandatoryWarnings, "deprecated", LintCategory.DEPRECATION); |
aoqi@0 | 149 | uncheckedHandler = new MandatoryWarningHandler(log, verboseUnchecked, |
aoqi@0 | 150 | enforceMandatoryWarnings, "unchecked", LintCategory.UNCHECKED); |
aoqi@0 | 151 | sunApiHandler = new MandatoryWarningHandler(log, verboseSunApi, |
aoqi@0 | 152 | enforceMandatoryWarnings, "sunapi", null); |
aoqi@0 | 153 | |
aoqi@0 | 154 | deferredLintHandler = DeferredLintHandler.instance(context); |
aoqi@0 | 155 | } |
aoqi@0 | 156 | |
aoqi@0 | 157 | /** Switch: generics enabled? |
aoqi@0 | 158 | */ |
aoqi@0 | 159 | boolean allowGenerics; |
aoqi@0 | 160 | |
aoqi@0 | 161 | /** Switch: varargs enabled? |
aoqi@0 | 162 | */ |
aoqi@0 | 163 | boolean allowVarargs; |
aoqi@0 | 164 | |
aoqi@0 | 165 | /** Switch: annotations enabled? |
aoqi@0 | 166 | */ |
aoqi@0 | 167 | boolean allowAnnotations; |
aoqi@0 | 168 | |
aoqi@0 | 169 | /** Switch: covariant returns enabled? |
aoqi@0 | 170 | */ |
aoqi@0 | 171 | boolean allowCovariantReturns; |
aoqi@0 | 172 | |
aoqi@0 | 173 | /** Switch: simplified varargs enabled? |
aoqi@0 | 174 | */ |
aoqi@0 | 175 | boolean allowSimplifiedVarargs; |
aoqi@0 | 176 | |
aoqi@0 | 177 | /** Switch: default methods enabled? |
aoqi@0 | 178 | */ |
aoqi@0 | 179 | boolean allowDefaultMethods; |
aoqi@0 | 180 | |
aoqi@0 | 181 | /** Switch: should unrelated return types trigger a method clash? |
aoqi@0 | 182 | */ |
aoqi@0 | 183 | boolean allowStrictMethodClashCheck; |
aoqi@0 | 184 | |
aoqi@0 | 185 | /** Switch: -complexinference option set? |
aoqi@0 | 186 | */ |
aoqi@0 | 187 | boolean complexInference; |
aoqi@0 | 188 | |
aoqi@0 | 189 | /** Character for synthetic names |
aoqi@0 | 190 | */ |
aoqi@0 | 191 | char syntheticNameChar; |
aoqi@0 | 192 | |
aoqi@0 | 193 | /** A table mapping flat names of all compiled classes in this run to their |
aoqi@0 | 194 | * symbols; maintained from outside. |
aoqi@0 | 195 | */ |
aoqi@0 | 196 | public Map<Name,ClassSymbol> compiled = new HashMap<Name, ClassSymbol>(); |
aoqi@0 | 197 | |
aoqi@0 | 198 | /** A handler for messages about deprecated usage. |
aoqi@0 | 199 | */ |
aoqi@0 | 200 | private MandatoryWarningHandler deprecationHandler; |
aoqi@0 | 201 | |
aoqi@0 | 202 | /** A handler for messages about unchecked or unsafe usage. |
aoqi@0 | 203 | */ |
aoqi@0 | 204 | private MandatoryWarningHandler uncheckedHandler; |
aoqi@0 | 205 | |
aoqi@0 | 206 | /** A handler for messages about using proprietary API. |
aoqi@0 | 207 | */ |
aoqi@0 | 208 | private MandatoryWarningHandler sunApiHandler; |
aoqi@0 | 209 | |
aoqi@0 | 210 | /** A handler for deferred lint warnings. |
aoqi@0 | 211 | */ |
aoqi@0 | 212 | private DeferredLintHandler deferredLintHandler; |
aoqi@0 | 213 | |
aoqi@0 | 214 | /* ************************************************************************* |
aoqi@0 | 215 | * Errors and Warnings |
aoqi@0 | 216 | **************************************************************************/ |
aoqi@0 | 217 | |
aoqi@0 | 218 | Lint setLint(Lint newLint) { |
aoqi@0 | 219 | Lint prev = lint; |
aoqi@0 | 220 | lint = newLint; |
aoqi@0 | 221 | return prev; |
aoqi@0 | 222 | } |
aoqi@0 | 223 | |
aoqi@0 | 224 | MethodSymbol setMethod(MethodSymbol newMethod) { |
aoqi@0 | 225 | MethodSymbol prev = method; |
aoqi@0 | 226 | method = newMethod; |
aoqi@0 | 227 | return prev; |
aoqi@0 | 228 | } |
aoqi@0 | 229 | |
aoqi@0 | 230 | /** Warn about deprecated symbol. |
aoqi@0 | 231 | * @param pos Position to be used for error reporting. |
aoqi@0 | 232 | * @param sym The deprecated symbol. |
aoqi@0 | 233 | */ |
aoqi@0 | 234 | void warnDeprecated(DiagnosticPosition pos, Symbol sym) { |
aoqi@0 | 235 | if (!lint.isSuppressed(LintCategory.DEPRECATION)) |
aoqi@0 | 236 | deprecationHandler.report(pos, "has.been.deprecated", sym, sym.location()); |
aoqi@0 | 237 | } |
aoqi@0 | 238 | |
aoqi@0 | 239 | /** Warn about unchecked operation. |
aoqi@0 | 240 | * @param pos Position to be used for error reporting. |
aoqi@0 | 241 | * @param msg A string describing the problem. |
aoqi@0 | 242 | */ |
aoqi@0 | 243 | public void warnUnchecked(DiagnosticPosition pos, String msg, Object... args) { |
aoqi@0 | 244 | if (!lint.isSuppressed(LintCategory.UNCHECKED)) |
aoqi@0 | 245 | uncheckedHandler.report(pos, msg, args); |
aoqi@0 | 246 | } |
aoqi@0 | 247 | |
aoqi@0 | 248 | /** Warn about unsafe vararg method decl. |
aoqi@0 | 249 | * @param pos Position to be used for error reporting. |
aoqi@0 | 250 | */ |
aoqi@0 | 251 | void warnUnsafeVararg(DiagnosticPosition pos, String key, Object... args) { |
aoqi@0 | 252 | if (lint.isEnabled(LintCategory.VARARGS) && allowSimplifiedVarargs) |
aoqi@0 | 253 | log.warning(LintCategory.VARARGS, pos, key, args); |
aoqi@0 | 254 | } |
aoqi@0 | 255 | |
aoqi@0 | 256 | /** Warn about using proprietary API. |
aoqi@0 | 257 | * @param pos Position to be used for error reporting. |
aoqi@0 | 258 | * @param msg A string describing the problem. |
aoqi@0 | 259 | */ |
aoqi@0 | 260 | public void warnSunApi(DiagnosticPosition pos, String msg, Object... args) { |
aoqi@0 | 261 | if (!lint.isSuppressed(LintCategory.SUNAPI)) |
aoqi@0 | 262 | sunApiHandler.report(pos, msg, args); |
aoqi@0 | 263 | } |
aoqi@0 | 264 | |
aoqi@0 | 265 | public void warnStatic(DiagnosticPosition pos, String msg, Object... args) { |
aoqi@0 | 266 | if (lint.isEnabled(LintCategory.STATIC)) |
aoqi@0 | 267 | log.warning(LintCategory.STATIC, pos, msg, args); |
aoqi@0 | 268 | } |
aoqi@0 | 269 | |
aoqi@0 | 270 | /** |
aoqi@0 | 271 | * Report any deferred diagnostics. |
aoqi@0 | 272 | */ |
aoqi@0 | 273 | public void reportDeferredDiagnostics() { |
aoqi@0 | 274 | deprecationHandler.reportDeferredDiagnostic(); |
aoqi@0 | 275 | uncheckedHandler.reportDeferredDiagnostic(); |
aoqi@0 | 276 | sunApiHandler.reportDeferredDiagnostic(); |
aoqi@0 | 277 | } |
aoqi@0 | 278 | |
aoqi@0 | 279 | |
aoqi@0 | 280 | /** Report a failure to complete a class. |
aoqi@0 | 281 | * @param pos Position to be used for error reporting. |
aoqi@0 | 282 | * @param ex The failure to report. |
aoqi@0 | 283 | */ |
aoqi@0 | 284 | public Type completionError(DiagnosticPosition pos, CompletionFailure ex) { |
aoqi@0 | 285 | log.error(JCDiagnostic.DiagnosticFlag.NON_DEFERRABLE, pos, "cant.access", ex.sym, ex.getDetailValue()); |
aoqi@0 | 286 | if (ex instanceof ClassReader.BadClassFile |
aoqi@0 | 287 | && !suppressAbortOnBadClassFile) throw new Abort(); |
aoqi@0 | 288 | else return syms.errType; |
aoqi@0 | 289 | } |
aoqi@0 | 290 | |
aoqi@0 | 291 | /** Report an error that wrong type tag was found. |
aoqi@0 | 292 | * @param pos Position to be used for error reporting. |
aoqi@0 | 293 | * @param required An internationalized string describing the type tag |
aoqi@0 | 294 | * required. |
aoqi@0 | 295 | * @param found The type that was found. |
aoqi@0 | 296 | */ |
aoqi@0 | 297 | Type typeTagError(DiagnosticPosition pos, Object required, Object found) { |
aoqi@0 | 298 | // this error used to be raised by the parser, |
aoqi@0 | 299 | // but has been delayed to this point: |
aoqi@0 | 300 | if (found instanceof Type && ((Type)found).hasTag(VOID)) { |
aoqi@0 | 301 | log.error(pos, "illegal.start.of.type"); |
aoqi@0 | 302 | return syms.errType; |
aoqi@0 | 303 | } |
aoqi@0 | 304 | log.error(pos, "type.found.req", found, required); |
aoqi@0 | 305 | return types.createErrorType(found instanceof Type ? (Type)found : syms.errType); |
aoqi@0 | 306 | } |
aoqi@0 | 307 | |
aoqi@0 | 308 | /** Report an error that symbol cannot be referenced before super |
aoqi@0 | 309 | * has been called. |
aoqi@0 | 310 | * @param pos Position to be used for error reporting. |
aoqi@0 | 311 | * @param sym The referenced symbol. |
aoqi@0 | 312 | */ |
aoqi@0 | 313 | void earlyRefError(DiagnosticPosition pos, Symbol sym) { |
aoqi@0 | 314 | log.error(pos, "cant.ref.before.ctor.called", sym); |
aoqi@0 | 315 | } |
aoqi@0 | 316 | |
aoqi@0 | 317 | /** Report duplicate declaration error. |
aoqi@0 | 318 | */ |
aoqi@0 | 319 | void duplicateError(DiagnosticPosition pos, Symbol sym) { |
aoqi@0 | 320 | if (!sym.type.isErroneous()) { |
aoqi@0 | 321 | Symbol location = sym.location(); |
aoqi@0 | 322 | if (location.kind == MTH && |
aoqi@0 | 323 | ((MethodSymbol)location).isStaticOrInstanceInit()) { |
aoqi@0 | 324 | log.error(pos, "already.defined.in.clinit", kindName(sym), sym, |
aoqi@0 | 325 | kindName(sym.location()), kindName(sym.location().enclClass()), |
aoqi@0 | 326 | sym.location().enclClass()); |
aoqi@0 | 327 | } else { |
aoqi@0 | 328 | log.error(pos, "already.defined", kindName(sym), sym, |
aoqi@0 | 329 | kindName(sym.location()), sym.location()); |
aoqi@0 | 330 | } |
aoqi@0 | 331 | } |
aoqi@0 | 332 | } |
aoqi@0 | 333 | |
aoqi@0 | 334 | /** Report array/varargs duplicate declaration |
aoqi@0 | 335 | */ |
aoqi@0 | 336 | void varargsDuplicateError(DiagnosticPosition pos, Symbol sym1, Symbol sym2) { |
aoqi@0 | 337 | if (!sym1.type.isErroneous() && !sym2.type.isErroneous()) { |
aoqi@0 | 338 | log.error(pos, "array.and.varargs", sym1, sym2, sym2.location()); |
aoqi@0 | 339 | } |
aoqi@0 | 340 | } |
aoqi@0 | 341 | |
aoqi@0 | 342 | /* ************************************************************************ |
aoqi@0 | 343 | * duplicate declaration checking |
aoqi@0 | 344 | *************************************************************************/ |
aoqi@0 | 345 | |
aoqi@0 | 346 | /** Check that variable does not hide variable with same name in |
aoqi@0 | 347 | * immediately enclosing local scope. |
aoqi@0 | 348 | * @param pos Position for error reporting. |
aoqi@0 | 349 | * @param v The symbol. |
aoqi@0 | 350 | * @param s The scope. |
aoqi@0 | 351 | */ |
aoqi@0 | 352 | void checkTransparentVar(DiagnosticPosition pos, VarSymbol v, Scope s) { |
aoqi@0 | 353 | if (s.next != null) { |
aoqi@0 | 354 | for (Scope.Entry e = s.next.lookup(v.name); |
aoqi@0 | 355 | e.scope != null && e.sym.owner == v.owner; |
aoqi@0 | 356 | e = e.next()) { |
aoqi@0 | 357 | if (e.sym.kind == VAR && |
aoqi@0 | 358 | (e.sym.owner.kind & (VAR | MTH)) != 0 && |
aoqi@0 | 359 | v.name != names.error) { |
aoqi@0 | 360 | duplicateError(pos, e.sym); |
aoqi@0 | 361 | return; |
aoqi@0 | 362 | } |
aoqi@0 | 363 | } |
aoqi@0 | 364 | } |
aoqi@0 | 365 | } |
aoqi@0 | 366 | |
aoqi@0 | 367 | /** Check that a class or interface does not hide a class or |
aoqi@0 | 368 | * interface with same name in immediately enclosing local scope. |
aoqi@0 | 369 | * @param pos Position for error reporting. |
aoqi@0 | 370 | * @param c The symbol. |
aoqi@0 | 371 | * @param s The scope. |
aoqi@0 | 372 | */ |
aoqi@0 | 373 | void checkTransparentClass(DiagnosticPosition pos, ClassSymbol c, Scope s) { |
aoqi@0 | 374 | if (s.next != null) { |
aoqi@0 | 375 | for (Scope.Entry e = s.next.lookup(c.name); |
aoqi@0 | 376 | e.scope != null && e.sym.owner == c.owner; |
aoqi@0 | 377 | e = e.next()) { |
aoqi@0 | 378 | if (e.sym.kind == TYP && !e.sym.type.hasTag(TYPEVAR) && |
aoqi@0 | 379 | (e.sym.owner.kind & (VAR | MTH)) != 0 && |
aoqi@0 | 380 | c.name != names.error) { |
aoqi@0 | 381 | duplicateError(pos, e.sym); |
aoqi@0 | 382 | return; |
aoqi@0 | 383 | } |
aoqi@0 | 384 | } |
aoqi@0 | 385 | } |
aoqi@0 | 386 | } |
aoqi@0 | 387 | |
aoqi@0 | 388 | /** Check that class does not have the same name as one of |
aoqi@0 | 389 | * its enclosing classes, or as a class defined in its enclosing scope. |
aoqi@0 | 390 | * return true if class is unique in its enclosing scope. |
aoqi@0 | 391 | * @param pos Position for error reporting. |
aoqi@0 | 392 | * @param name The class name. |
aoqi@0 | 393 | * @param s The enclosing scope. |
aoqi@0 | 394 | */ |
aoqi@0 | 395 | boolean checkUniqueClassName(DiagnosticPosition pos, Name name, Scope s) { |
aoqi@0 | 396 | for (Scope.Entry e = s.lookup(name); e.scope == s; e = e.next()) { |
aoqi@0 | 397 | if (e.sym.kind == TYP && e.sym.name != names.error) { |
aoqi@0 | 398 | duplicateError(pos, e.sym); |
aoqi@0 | 399 | return false; |
aoqi@0 | 400 | } |
aoqi@0 | 401 | } |
aoqi@0 | 402 | for (Symbol sym = s.owner; sym != null; sym = sym.owner) { |
aoqi@0 | 403 | if (sym.kind == TYP && sym.name == name && sym.name != names.error) { |
aoqi@0 | 404 | duplicateError(pos, sym); |
aoqi@0 | 405 | return true; |
aoqi@0 | 406 | } |
aoqi@0 | 407 | } |
aoqi@0 | 408 | return true; |
aoqi@0 | 409 | } |
aoqi@0 | 410 | |
aoqi@0 | 411 | /* ************************************************************************* |
aoqi@0 | 412 | * Class name generation |
aoqi@0 | 413 | **************************************************************************/ |
aoqi@0 | 414 | |
aoqi@0 | 415 | /** Return name of local class. |
aoqi@0 | 416 | * This is of the form {@code <enclClass> $ n <classname> } |
aoqi@0 | 417 | * where |
aoqi@0 | 418 | * enclClass is the flat name of the enclosing class, |
aoqi@0 | 419 | * classname is the simple name of the local class |
aoqi@0 | 420 | */ |
aoqi@0 | 421 | Name localClassName(ClassSymbol c) { |
aoqi@0 | 422 | for (int i=1; ; i++) { |
aoqi@0 | 423 | Name flatname = names. |
aoqi@0 | 424 | fromString("" + c.owner.enclClass().flatname + |
aoqi@0 | 425 | syntheticNameChar + i + |
aoqi@0 | 426 | c.name); |
aoqi@0 | 427 | if (compiled.get(flatname) == null) return flatname; |
aoqi@0 | 428 | } |
aoqi@0 | 429 | } |
aoqi@0 | 430 | |
aoqi@0 | 431 | /* ************************************************************************* |
aoqi@0 | 432 | * Type Checking |
aoqi@0 | 433 | **************************************************************************/ |
aoqi@0 | 434 | |
aoqi@0 | 435 | /** |
aoqi@0 | 436 | * A check context is an object that can be used to perform compatibility |
aoqi@0 | 437 | * checks - depending on the check context, meaning of 'compatibility' might |
aoqi@0 | 438 | * vary significantly. |
aoqi@0 | 439 | */ |
aoqi@0 | 440 | public interface CheckContext { |
aoqi@0 | 441 | /** |
aoqi@0 | 442 | * Is type 'found' compatible with type 'req' in given context |
aoqi@0 | 443 | */ |
aoqi@0 | 444 | boolean compatible(Type found, Type req, Warner warn); |
aoqi@0 | 445 | /** |
aoqi@0 | 446 | * Report a check error |
aoqi@0 | 447 | */ |
aoqi@0 | 448 | void report(DiagnosticPosition pos, JCDiagnostic details); |
aoqi@0 | 449 | /** |
aoqi@0 | 450 | * Obtain a warner for this check context |
aoqi@0 | 451 | */ |
aoqi@0 | 452 | public Warner checkWarner(DiagnosticPosition pos, Type found, Type req); |
aoqi@0 | 453 | |
aoqi@0 | 454 | public Infer.InferenceContext inferenceContext(); |
aoqi@0 | 455 | |
aoqi@0 | 456 | public DeferredAttr.DeferredAttrContext deferredAttrContext(); |
aoqi@0 | 457 | } |
aoqi@0 | 458 | |
aoqi@0 | 459 | /** |
aoqi@0 | 460 | * This class represent a check context that is nested within another check |
aoqi@0 | 461 | * context - useful to check sub-expressions. The default behavior simply |
aoqi@0 | 462 | * redirects all method calls to the enclosing check context leveraging |
aoqi@0 | 463 | * the forwarding pattern. |
aoqi@0 | 464 | */ |
aoqi@0 | 465 | static class NestedCheckContext implements CheckContext { |
aoqi@0 | 466 | CheckContext enclosingContext; |
aoqi@0 | 467 | |
aoqi@0 | 468 | NestedCheckContext(CheckContext enclosingContext) { |
aoqi@0 | 469 | this.enclosingContext = enclosingContext; |
aoqi@0 | 470 | } |
aoqi@0 | 471 | |
aoqi@0 | 472 | public boolean compatible(Type found, Type req, Warner warn) { |
aoqi@0 | 473 | return enclosingContext.compatible(found, req, warn); |
aoqi@0 | 474 | } |
aoqi@0 | 475 | |
aoqi@0 | 476 | public void report(DiagnosticPosition pos, JCDiagnostic details) { |
aoqi@0 | 477 | enclosingContext.report(pos, details); |
aoqi@0 | 478 | } |
aoqi@0 | 479 | |
aoqi@0 | 480 | public Warner checkWarner(DiagnosticPosition pos, Type found, Type req) { |
aoqi@0 | 481 | return enclosingContext.checkWarner(pos, found, req); |
aoqi@0 | 482 | } |
aoqi@0 | 483 | |
aoqi@0 | 484 | public Infer.InferenceContext inferenceContext() { |
aoqi@0 | 485 | return enclosingContext.inferenceContext(); |
aoqi@0 | 486 | } |
aoqi@0 | 487 | |
aoqi@0 | 488 | public DeferredAttrContext deferredAttrContext() { |
aoqi@0 | 489 | return enclosingContext.deferredAttrContext(); |
aoqi@0 | 490 | } |
aoqi@0 | 491 | } |
aoqi@0 | 492 | |
aoqi@0 | 493 | /** |
aoqi@0 | 494 | * Check context to be used when evaluating assignment/return statements |
aoqi@0 | 495 | */ |
aoqi@0 | 496 | CheckContext basicHandler = new CheckContext() { |
aoqi@0 | 497 | public void report(DiagnosticPosition pos, JCDiagnostic details) { |
aoqi@0 | 498 | log.error(pos, "prob.found.req", details); |
aoqi@0 | 499 | } |
aoqi@0 | 500 | public boolean compatible(Type found, Type req, Warner warn) { |
aoqi@0 | 501 | return types.isAssignable(found, req, warn); |
aoqi@0 | 502 | } |
aoqi@0 | 503 | |
aoqi@0 | 504 | public Warner checkWarner(DiagnosticPosition pos, Type found, Type req) { |
aoqi@0 | 505 | return convertWarner(pos, found, req); |
aoqi@0 | 506 | } |
aoqi@0 | 507 | |
aoqi@0 | 508 | public InferenceContext inferenceContext() { |
aoqi@0 | 509 | return infer.emptyContext; |
aoqi@0 | 510 | } |
aoqi@0 | 511 | |
aoqi@0 | 512 | public DeferredAttrContext deferredAttrContext() { |
aoqi@0 | 513 | return deferredAttr.emptyDeferredAttrContext; |
aoqi@0 | 514 | } |
aoqi@0 | 515 | |
aoqi@0 | 516 | @Override |
aoqi@0 | 517 | public String toString() { |
aoqi@0 | 518 | return "CheckContext: basicHandler"; |
aoqi@0 | 519 | } |
aoqi@0 | 520 | }; |
aoqi@0 | 521 | |
aoqi@0 | 522 | /** Check that a given type is assignable to a given proto-type. |
aoqi@0 | 523 | * If it is, return the type, otherwise return errType. |
aoqi@0 | 524 | * @param pos Position to be used for error reporting. |
aoqi@0 | 525 | * @param found The type that was found. |
aoqi@0 | 526 | * @param req The type that was required. |
aoqi@0 | 527 | */ |
aoqi@0 | 528 | Type checkType(DiagnosticPosition pos, Type found, Type req) { |
aoqi@0 | 529 | return checkType(pos, found, req, basicHandler); |
aoqi@0 | 530 | } |
aoqi@0 | 531 | |
aoqi@0 | 532 | Type checkType(final DiagnosticPosition pos, final Type found, final Type req, final CheckContext checkContext) { |
aoqi@0 | 533 | final Infer.InferenceContext inferenceContext = checkContext.inferenceContext(); |
vromero@2543 | 534 | if (inferenceContext.free(req) || inferenceContext.free(found)) { |
vromero@2543 | 535 | inferenceContext.addFreeTypeListener(List.of(req, found), new FreeTypeListener() { |
aoqi@0 | 536 | @Override |
aoqi@0 | 537 | public void typesInferred(InferenceContext inferenceContext) { |
aoqi@0 | 538 | checkType(pos, inferenceContext.asInstType(found), inferenceContext.asInstType(req), checkContext); |
aoqi@0 | 539 | } |
aoqi@0 | 540 | }); |
aoqi@0 | 541 | } |
aoqi@0 | 542 | if (req.hasTag(ERROR)) |
aoqi@0 | 543 | return req; |
aoqi@0 | 544 | if (req.hasTag(NONE)) |
aoqi@0 | 545 | return found; |
aoqi@0 | 546 | if (checkContext.compatible(found, req, checkContext.checkWarner(pos, found, req))) { |
aoqi@0 | 547 | return found; |
aoqi@0 | 548 | } else { |
aoqi@0 | 549 | if (found.isNumeric() && req.isNumeric()) { |
aoqi@0 | 550 | checkContext.report(pos, diags.fragment("possible.loss.of.precision", found, req)); |
aoqi@0 | 551 | return types.createErrorType(found); |
aoqi@0 | 552 | } |
aoqi@0 | 553 | checkContext.report(pos, diags.fragment("inconvertible.types", found, req)); |
aoqi@0 | 554 | return types.createErrorType(found); |
aoqi@0 | 555 | } |
aoqi@0 | 556 | } |
aoqi@0 | 557 | |
aoqi@0 | 558 | /** Check that a given type can be cast to a given target type. |
aoqi@0 | 559 | * Return the result of the cast. |
aoqi@0 | 560 | * @param pos Position to be used for error reporting. |
aoqi@0 | 561 | * @param found The type that is being cast. |
aoqi@0 | 562 | * @param req The target type of the cast. |
aoqi@0 | 563 | */ |
aoqi@0 | 564 | Type checkCastable(DiagnosticPosition pos, Type found, Type req) { |
aoqi@0 | 565 | return checkCastable(pos, found, req, basicHandler); |
aoqi@0 | 566 | } |
aoqi@0 | 567 | Type checkCastable(DiagnosticPosition pos, Type found, Type req, CheckContext checkContext) { |
aoqi@0 | 568 | if (types.isCastable(found, req, castWarner(pos, found, req))) { |
aoqi@0 | 569 | return req; |
aoqi@0 | 570 | } else { |
aoqi@0 | 571 | checkContext.report(pos, diags.fragment("inconvertible.types", found, req)); |
aoqi@0 | 572 | return types.createErrorType(found); |
aoqi@0 | 573 | } |
aoqi@0 | 574 | } |
aoqi@0 | 575 | |
aoqi@0 | 576 | /** Check for redundant casts (i.e. where source type is a subtype of target type) |
aoqi@0 | 577 | * The problem should only be reported for non-292 cast |
aoqi@0 | 578 | */ |
aoqi@0 | 579 | public void checkRedundantCast(Env<AttrContext> env, final JCTypeCast tree) { |
aoqi@0 | 580 | if (!tree.type.isErroneous() |
aoqi@0 | 581 | && types.isSameType(tree.expr.type, tree.clazz.type) |
aoqi@0 | 582 | && !(ignoreAnnotatedCasts && TreeInfo.containsTypeAnnotation(tree.clazz)) |
aoqi@0 | 583 | && !is292targetTypeCast(tree)) { |
aoqi@0 | 584 | deferredLintHandler.report(new DeferredLintHandler.LintLogger() { |
aoqi@0 | 585 | @Override |
aoqi@0 | 586 | public void report() { |
aoqi@0 | 587 | if (lint.isEnabled(Lint.LintCategory.CAST)) |
aoqi@0 | 588 | log.warning(Lint.LintCategory.CAST, |
aoqi@0 | 589 | tree.pos(), "redundant.cast", tree.expr.type); |
aoqi@0 | 590 | } |
aoqi@0 | 591 | }); |
aoqi@0 | 592 | } |
aoqi@0 | 593 | } |
aoqi@0 | 594 | //where |
aoqi@0 | 595 | private boolean is292targetTypeCast(JCTypeCast tree) { |
aoqi@0 | 596 | boolean is292targetTypeCast = false; |
aoqi@0 | 597 | JCExpression expr = TreeInfo.skipParens(tree.expr); |
aoqi@0 | 598 | if (expr.hasTag(APPLY)) { |
aoqi@0 | 599 | JCMethodInvocation apply = (JCMethodInvocation)expr; |
aoqi@0 | 600 | Symbol sym = TreeInfo.symbol(apply.meth); |
aoqi@0 | 601 | is292targetTypeCast = sym != null && |
aoqi@0 | 602 | sym.kind == MTH && |
aoqi@0 | 603 | (sym.flags() & HYPOTHETICAL) != 0; |
aoqi@0 | 604 | } |
aoqi@0 | 605 | return is292targetTypeCast; |
aoqi@0 | 606 | } |
aoqi@0 | 607 | |
aoqi@0 | 608 | private static final boolean ignoreAnnotatedCasts = true; |
aoqi@0 | 609 | |
aoqi@0 | 610 | /** Check that a type is within some bounds. |
aoqi@0 | 611 | * |
aoqi@0 | 612 | * Used in TypeApply to verify that, e.g., X in {@code V<X>} is a valid |
aoqi@0 | 613 | * type argument. |
aoqi@0 | 614 | * @param a The type that should be bounded by bs. |
aoqi@0 | 615 | * @param bound The bound. |
aoqi@0 | 616 | */ |
aoqi@0 | 617 | private boolean checkExtends(Type a, Type bound) { |
aoqi@0 | 618 | if (a.isUnbound()) { |
aoqi@0 | 619 | return true; |
aoqi@0 | 620 | } else if (!a.hasTag(WILDCARD)) { |
aoqi@0 | 621 | a = types.cvarUpperBound(a); |
aoqi@0 | 622 | return types.isSubtype(a, bound); |
aoqi@0 | 623 | } else if (a.isExtendsBound()) { |
aoqi@0 | 624 | return types.isCastable(bound, types.wildUpperBound(a), types.noWarnings); |
aoqi@0 | 625 | } else if (a.isSuperBound()) { |
aoqi@0 | 626 | return !types.notSoftSubtype(types.wildLowerBound(a), bound); |
aoqi@0 | 627 | } |
aoqi@0 | 628 | return true; |
aoqi@0 | 629 | } |
aoqi@0 | 630 | |
aoqi@0 | 631 | /** Check that type is different from 'void'. |
aoqi@0 | 632 | * @param pos Position to be used for error reporting. |
aoqi@0 | 633 | * @param t The type to be checked. |
aoqi@0 | 634 | */ |
aoqi@0 | 635 | Type checkNonVoid(DiagnosticPosition pos, Type t) { |
aoqi@0 | 636 | if (t.hasTag(VOID)) { |
aoqi@0 | 637 | log.error(pos, "void.not.allowed.here"); |
aoqi@0 | 638 | return types.createErrorType(t); |
aoqi@0 | 639 | } else { |
aoqi@0 | 640 | return t; |
aoqi@0 | 641 | } |
aoqi@0 | 642 | } |
aoqi@0 | 643 | |
aoqi@0 | 644 | Type checkClassOrArrayType(DiagnosticPosition pos, Type t) { |
aoqi@0 | 645 | if (!t.hasTag(CLASS) && !t.hasTag(ARRAY) && !t.hasTag(ERROR)) { |
aoqi@0 | 646 | return typeTagError(pos, |
aoqi@0 | 647 | diags.fragment("type.req.class.array"), |
aoqi@0 | 648 | asTypeParam(t)); |
aoqi@0 | 649 | } else { |
aoqi@0 | 650 | return t; |
aoqi@0 | 651 | } |
aoqi@0 | 652 | } |
aoqi@0 | 653 | |
aoqi@0 | 654 | /** Check that type is a class or interface type. |
aoqi@0 | 655 | * @param pos Position to be used for error reporting. |
aoqi@0 | 656 | * @param t The type to be checked. |
aoqi@0 | 657 | */ |
aoqi@0 | 658 | Type checkClassType(DiagnosticPosition pos, Type t) { |
aoqi@0 | 659 | if (!t.hasTag(CLASS) && !t.hasTag(ERROR)) { |
aoqi@0 | 660 | return typeTagError(pos, |
aoqi@0 | 661 | diags.fragment("type.req.class"), |
aoqi@0 | 662 | asTypeParam(t)); |
aoqi@0 | 663 | } else { |
aoqi@0 | 664 | return t; |
aoqi@0 | 665 | } |
aoqi@0 | 666 | } |
aoqi@0 | 667 | //where |
aoqi@0 | 668 | private Object asTypeParam(Type t) { |
aoqi@0 | 669 | return (t.hasTag(TYPEVAR)) |
aoqi@0 | 670 | ? diags.fragment("type.parameter", t) |
aoqi@0 | 671 | : t; |
aoqi@0 | 672 | } |
aoqi@0 | 673 | |
aoqi@0 | 674 | /** Check that type is a valid qualifier for a constructor reference expression |
aoqi@0 | 675 | */ |
aoqi@0 | 676 | Type checkConstructorRefType(DiagnosticPosition pos, Type t) { |
aoqi@0 | 677 | t = checkClassOrArrayType(pos, t); |
aoqi@0 | 678 | if (t.hasTag(CLASS)) { |
aoqi@0 | 679 | if ((t.tsym.flags() & (ABSTRACT | INTERFACE)) != 0) { |
aoqi@0 | 680 | log.error(pos, "abstract.cant.be.instantiated", t.tsym); |
aoqi@0 | 681 | t = types.createErrorType(t); |
aoqi@0 | 682 | } else if ((t.tsym.flags() & ENUM) != 0) { |
aoqi@0 | 683 | log.error(pos, "enum.cant.be.instantiated"); |
aoqi@0 | 684 | t = types.createErrorType(t); |
aoqi@0 | 685 | } else { |
aoqi@0 | 686 | t = checkClassType(pos, t, true); |
aoqi@0 | 687 | } |
aoqi@0 | 688 | } else if (t.hasTag(ARRAY)) { |
aoqi@0 | 689 | if (!types.isReifiable(((ArrayType)t).elemtype)) { |
aoqi@0 | 690 | log.error(pos, "generic.array.creation"); |
aoqi@0 | 691 | t = types.createErrorType(t); |
aoqi@0 | 692 | } |
aoqi@0 | 693 | } |
aoqi@0 | 694 | return t; |
aoqi@0 | 695 | } |
aoqi@0 | 696 | |
aoqi@0 | 697 | /** Check that type is a class or interface type. |
aoqi@0 | 698 | * @param pos Position to be used for error reporting. |
aoqi@0 | 699 | * @param t The type to be checked. |
aoqi@0 | 700 | * @param noBounds True if type bounds are illegal here. |
aoqi@0 | 701 | */ |
aoqi@0 | 702 | Type checkClassType(DiagnosticPosition pos, Type t, boolean noBounds) { |
aoqi@0 | 703 | t = checkClassType(pos, t); |
aoqi@0 | 704 | if (noBounds && t.isParameterized()) { |
aoqi@0 | 705 | List<Type> args = t.getTypeArguments(); |
aoqi@0 | 706 | while (args.nonEmpty()) { |
aoqi@0 | 707 | if (args.head.hasTag(WILDCARD)) |
aoqi@0 | 708 | return typeTagError(pos, |
aoqi@0 | 709 | diags.fragment("type.req.exact"), |
aoqi@0 | 710 | args.head); |
aoqi@0 | 711 | args = args.tail; |
aoqi@0 | 712 | } |
aoqi@0 | 713 | } |
aoqi@0 | 714 | return t; |
aoqi@0 | 715 | } |
aoqi@0 | 716 | |
aoqi@0 | 717 | /** Check that type is a reference type, i.e. a class, interface or array type |
aoqi@0 | 718 | * or a type variable. |
aoqi@0 | 719 | * @param pos Position to be used for error reporting. |
aoqi@0 | 720 | * @param t The type to be checked. |
aoqi@0 | 721 | */ |
aoqi@0 | 722 | Type checkRefType(DiagnosticPosition pos, Type t) { |
aoqi@0 | 723 | if (t.isReference()) |
aoqi@0 | 724 | return t; |
aoqi@0 | 725 | else |
aoqi@0 | 726 | return typeTagError(pos, |
aoqi@0 | 727 | diags.fragment("type.req.ref"), |
aoqi@0 | 728 | t); |
aoqi@0 | 729 | } |
aoqi@0 | 730 | |
aoqi@0 | 731 | /** Check that each type is a reference type, i.e. a class, interface or array type |
aoqi@0 | 732 | * or a type variable. |
aoqi@0 | 733 | * @param trees Original trees, used for error reporting. |
aoqi@0 | 734 | * @param types The types to be checked. |
aoqi@0 | 735 | */ |
aoqi@0 | 736 | List<Type> checkRefTypes(List<JCExpression> trees, List<Type> types) { |
aoqi@0 | 737 | List<JCExpression> tl = trees; |
aoqi@0 | 738 | for (List<Type> l = types; l.nonEmpty(); l = l.tail) { |
aoqi@0 | 739 | l.head = checkRefType(tl.head.pos(), l.head); |
aoqi@0 | 740 | tl = tl.tail; |
aoqi@0 | 741 | } |
aoqi@0 | 742 | return types; |
aoqi@0 | 743 | } |
aoqi@0 | 744 | |
aoqi@0 | 745 | /** Check that type is a null or reference type. |
aoqi@0 | 746 | * @param pos Position to be used for error reporting. |
aoqi@0 | 747 | * @param t The type to be checked. |
aoqi@0 | 748 | */ |
aoqi@0 | 749 | Type checkNullOrRefType(DiagnosticPosition pos, Type t) { |
aoqi@0 | 750 | if (t.isReference() || t.hasTag(BOT)) |
aoqi@0 | 751 | return t; |
aoqi@0 | 752 | else |
aoqi@0 | 753 | return typeTagError(pos, |
aoqi@0 | 754 | diags.fragment("type.req.ref"), |
aoqi@0 | 755 | t); |
aoqi@0 | 756 | } |
aoqi@0 | 757 | |
aoqi@0 | 758 | /** Check that flag set does not contain elements of two conflicting sets. s |
aoqi@0 | 759 | * Return true if it doesn't. |
aoqi@0 | 760 | * @param pos Position to be used for error reporting. |
aoqi@0 | 761 | * @param flags The set of flags to be checked. |
aoqi@0 | 762 | * @param set1 Conflicting flags set #1. |
aoqi@0 | 763 | * @param set2 Conflicting flags set #2. |
aoqi@0 | 764 | */ |
aoqi@0 | 765 | boolean checkDisjoint(DiagnosticPosition pos, long flags, long set1, long set2) { |
aoqi@0 | 766 | if ((flags & set1) != 0 && (flags & set2) != 0) { |
aoqi@0 | 767 | log.error(pos, |
aoqi@0 | 768 | "illegal.combination.of.modifiers", |
aoqi@0 | 769 | asFlagSet(TreeInfo.firstFlag(flags & set1)), |
aoqi@0 | 770 | asFlagSet(TreeInfo.firstFlag(flags & set2))); |
aoqi@0 | 771 | return false; |
aoqi@0 | 772 | } else |
aoqi@0 | 773 | return true; |
aoqi@0 | 774 | } |
aoqi@0 | 775 | |
aoqi@0 | 776 | /** Check that usage of diamond operator is correct (i.e. diamond should not |
aoqi@0 | 777 | * be used with non-generic classes or in anonymous class creation expressions) |
aoqi@0 | 778 | */ |
aoqi@0 | 779 | Type checkDiamond(JCNewClass tree, Type t) { |
aoqi@0 | 780 | if (!TreeInfo.isDiamond(tree) || |
aoqi@0 | 781 | t.isErroneous()) { |
aoqi@0 | 782 | return checkClassType(tree.clazz.pos(), t, true); |
aoqi@0 | 783 | } else if (tree.def != null) { |
aoqi@0 | 784 | log.error(tree.clazz.pos(), |
aoqi@0 | 785 | "cant.apply.diamond.1", |
aoqi@0 | 786 | t, diags.fragment("diamond.and.anon.class", t)); |
aoqi@0 | 787 | return types.createErrorType(t); |
aoqi@0 | 788 | } else if (t.tsym.type.getTypeArguments().isEmpty()) { |
aoqi@0 | 789 | log.error(tree.clazz.pos(), |
aoqi@0 | 790 | "cant.apply.diamond.1", |
aoqi@0 | 791 | t, diags.fragment("diamond.non.generic", t)); |
aoqi@0 | 792 | return types.createErrorType(t); |
aoqi@0 | 793 | } else if (tree.typeargs != null && |
aoqi@0 | 794 | tree.typeargs.nonEmpty()) { |
aoqi@0 | 795 | log.error(tree.clazz.pos(), |
aoqi@0 | 796 | "cant.apply.diamond.1", |
aoqi@0 | 797 | t, diags.fragment("diamond.and.explicit.params", t)); |
aoqi@0 | 798 | return types.createErrorType(t); |
aoqi@0 | 799 | } else { |
aoqi@0 | 800 | return t; |
aoqi@0 | 801 | } |
aoqi@0 | 802 | } |
aoqi@0 | 803 | |
aoqi@0 | 804 | void checkVarargsMethodDecl(Env<AttrContext> env, JCMethodDecl tree) { |
aoqi@0 | 805 | MethodSymbol m = tree.sym; |
aoqi@0 | 806 | if (!allowSimplifiedVarargs) return; |
aoqi@0 | 807 | boolean hasTrustMeAnno = m.attribute(syms.trustMeType.tsym) != null; |
aoqi@0 | 808 | Type varargElemType = null; |
aoqi@0 | 809 | if (m.isVarArgs()) { |
aoqi@0 | 810 | varargElemType = types.elemtype(tree.params.last().type); |
aoqi@0 | 811 | } |
aoqi@0 | 812 | if (hasTrustMeAnno && !isTrustMeAllowedOnMethod(m)) { |
aoqi@0 | 813 | if (varargElemType != null) { |
aoqi@0 | 814 | log.error(tree, |
aoqi@0 | 815 | "varargs.invalid.trustme.anno", |
aoqi@0 | 816 | syms.trustMeType.tsym, |
aoqi@0 | 817 | diags.fragment("varargs.trustme.on.virtual.varargs", m)); |
aoqi@0 | 818 | } else { |
aoqi@0 | 819 | log.error(tree, |
aoqi@0 | 820 | "varargs.invalid.trustme.anno", |
aoqi@0 | 821 | syms.trustMeType.tsym, |
aoqi@0 | 822 | diags.fragment("varargs.trustme.on.non.varargs.meth", m)); |
aoqi@0 | 823 | } |
aoqi@0 | 824 | } else if (hasTrustMeAnno && varargElemType != null && |
aoqi@0 | 825 | types.isReifiable(varargElemType)) { |
aoqi@0 | 826 | warnUnsafeVararg(tree, |
aoqi@0 | 827 | "varargs.redundant.trustme.anno", |
aoqi@0 | 828 | syms.trustMeType.tsym, |
aoqi@0 | 829 | diags.fragment("varargs.trustme.on.reifiable.varargs", varargElemType)); |
aoqi@0 | 830 | } |
aoqi@0 | 831 | else if (!hasTrustMeAnno && varargElemType != null && |
aoqi@0 | 832 | !types.isReifiable(varargElemType)) { |
aoqi@0 | 833 | warnUnchecked(tree.params.head.pos(), "unchecked.varargs.non.reifiable.type", varargElemType); |
aoqi@0 | 834 | } |
aoqi@0 | 835 | } |
aoqi@0 | 836 | //where |
aoqi@0 | 837 | private boolean isTrustMeAllowedOnMethod(Symbol s) { |
aoqi@0 | 838 | return (s.flags() & VARARGS) != 0 && |
aoqi@0 | 839 | (s.isConstructor() || |
aoqi@0 | 840 | (s.flags() & (STATIC | FINAL)) != 0); |
aoqi@0 | 841 | } |
aoqi@0 | 842 | |
aoqi@0 | 843 | Type checkMethod(final Type mtype, |
aoqi@0 | 844 | final Symbol sym, |
aoqi@0 | 845 | final Env<AttrContext> env, |
aoqi@0 | 846 | final List<JCExpression> argtrees, |
aoqi@0 | 847 | final List<Type> argtypes, |
aoqi@0 | 848 | final boolean useVarargs, |
aoqi@0 | 849 | InferenceContext inferenceContext) { |
aoqi@0 | 850 | // System.out.println("call : " + env.tree); |
aoqi@0 | 851 | // System.out.println("method : " + owntype); |
aoqi@0 | 852 | // System.out.println("actuals: " + argtypes); |
aoqi@0 | 853 | if (inferenceContext.free(mtype)) { |
aoqi@0 | 854 | inferenceContext.addFreeTypeListener(List.of(mtype), new FreeTypeListener() { |
aoqi@0 | 855 | public void typesInferred(InferenceContext inferenceContext) { |
aoqi@0 | 856 | checkMethod(inferenceContext.asInstType(mtype), sym, env, argtrees, argtypes, useVarargs, inferenceContext); |
aoqi@0 | 857 | } |
aoqi@0 | 858 | }); |
aoqi@0 | 859 | return mtype; |
aoqi@0 | 860 | } |
aoqi@0 | 861 | Type owntype = mtype; |
aoqi@0 | 862 | List<Type> formals = owntype.getParameterTypes(); |
aoqi@0 | 863 | List<Type> nonInferred = sym.type.getParameterTypes(); |
aoqi@0 | 864 | if (nonInferred.length() != formals.length()) nonInferred = formals; |
aoqi@0 | 865 | Type last = useVarargs ? formals.last() : null; |
aoqi@0 | 866 | if (sym.name == names.init && sym.owner == syms.enumSym) { |
aoqi@0 | 867 | formals = formals.tail.tail; |
aoqi@0 | 868 | nonInferred = nonInferred.tail.tail; |
aoqi@0 | 869 | } |
aoqi@0 | 870 | List<JCExpression> args = argtrees; |
aoqi@0 | 871 | if (args != null) { |
aoqi@0 | 872 | //this is null when type-checking a method reference |
aoqi@0 | 873 | while (formals.head != last) { |
aoqi@0 | 874 | JCTree arg = args.head; |
aoqi@0 | 875 | Warner warn = convertWarner(arg.pos(), arg.type, nonInferred.head); |
aoqi@0 | 876 | assertConvertible(arg, arg.type, formals.head, warn); |
aoqi@0 | 877 | args = args.tail; |
aoqi@0 | 878 | formals = formals.tail; |
aoqi@0 | 879 | nonInferred = nonInferred.tail; |
aoqi@0 | 880 | } |
aoqi@0 | 881 | if (useVarargs) { |
aoqi@0 | 882 | Type varArg = types.elemtype(last); |
aoqi@0 | 883 | while (args.tail != null) { |
aoqi@0 | 884 | JCTree arg = args.head; |
aoqi@0 | 885 | Warner warn = convertWarner(arg.pos(), arg.type, varArg); |
aoqi@0 | 886 | assertConvertible(arg, arg.type, varArg, warn); |
aoqi@0 | 887 | args = args.tail; |
aoqi@0 | 888 | } |
aoqi@0 | 889 | } else if ((sym.flags() & (VARARGS | SIGNATURE_POLYMORPHIC)) == VARARGS && |
aoqi@0 | 890 | allowVarargs) { |
aoqi@0 | 891 | // non-varargs call to varargs method |
aoqi@0 | 892 | Type varParam = owntype.getParameterTypes().last(); |
aoqi@0 | 893 | Type lastArg = argtypes.last(); |
aoqi@0 | 894 | if (types.isSubtypeUnchecked(lastArg, types.elemtype(varParam)) && |
aoqi@0 | 895 | !types.isSameType(types.erasure(varParam), types.erasure(lastArg))) |
aoqi@0 | 896 | log.warning(argtrees.last().pos(), "inexact.non-varargs.call", |
aoqi@0 | 897 | types.elemtype(varParam), varParam); |
aoqi@0 | 898 | } |
aoqi@0 | 899 | } |
aoqi@0 | 900 | if (useVarargs) { |
aoqi@0 | 901 | Type argtype = owntype.getParameterTypes().last(); |
aoqi@0 | 902 | if (!types.isReifiable(argtype) && |
aoqi@0 | 903 | (!allowSimplifiedVarargs || |
aoqi@0 | 904 | sym.attribute(syms.trustMeType.tsym) == null || |
aoqi@0 | 905 | !isTrustMeAllowedOnMethod(sym))) { |
aoqi@0 | 906 | warnUnchecked(env.tree.pos(), |
aoqi@0 | 907 | "unchecked.generic.array.creation", |
aoqi@0 | 908 | argtype); |
aoqi@0 | 909 | } |
aoqi@0 | 910 | if ((sym.baseSymbol().flags() & SIGNATURE_POLYMORPHIC) == 0) { |
aoqi@0 | 911 | TreeInfo.setVarargsElement(env.tree, types.elemtype(argtype)); |
aoqi@0 | 912 | } |
aoqi@0 | 913 | } |
aoqi@0 | 914 | PolyKind pkind = (sym.type.hasTag(FORALL) && |
aoqi@0 | 915 | sym.type.getReturnType().containsAny(((ForAll)sym.type).tvars)) ? |
aoqi@0 | 916 | PolyKind.POLY : PolyKind.STANDALONE; |
aoqi@0 | 917 | TreeInfo.setPolyKind(env.tree, pkind); |
aoqi@0 | 918 | return owntype; |
aoqi@0 | 919 | } |
aoqi@0 | 920 | //where |
aoqi@0 | 921 | private void assertConvertible(JCTree tree, Type actual, Type formal, Warner warn) { |
aoqi@0 | 922 | if (types.isConvertible(actual, formal, warn)) |
aoqi@0 | 923 | return; |
aoqi@0 | 924 | |
aoqi@0 | 925 | if (formal.isCompound() |
aoqi@0 | 926 | && types.isSubtype(actual, types.supertype(formal)) |
aoqi@0 | 927 | && types.isSubtypeUnchecked(actual, types.interfaces(formal), warn)) |
aoqi@0 | 928 | return; |
aoqi@0 | 929 | } |
aoqi@0 | 930 | |
aoqi@0 | 931 | /** |
aoqi@0 | 932 | * Check that type 't' is a valid instantiation of a generic class |
aoqi@0 | 933 | * (see JLS 4.5) |
aoqi@0 | 934 | * |
aoqi@0 | 935 | * @param t class type to be checked |
aoqi@0 | 936 | * @return true if 't' is well-formed |
aoqi@0 | 937 | */ |
aoqi@0 | 938 | public boolean checkValidGenericType(Type t) { |
aoqi@0 | 939 | return firstIncompatibleTypeArg(t) == null; |
aoqi@0 | 940 | } |
aoqi@0 | 941 | //WHERE |
aoqi@0 | 942 | private Type firstIncompatibleTypeArg(Type type) { |
aoqi@0 | 943 | List<Type> formals = type.tsym.type.allparams(); |
aoqi@0 | 944 | List<Type> actuals = type.allparams(); |
aoqi@0 | 945 | List<Type> args = type.getTypeArguments(); |
aoqi@0 | 946 | List<Type> forms = type.tsym.type.getTypeArguments(); |
aoqi@0 | 947 | ListBuffer<Type> bounds_buf = new ListBuffer<Type>(); |
aoqi@0 | 948 | |
aoqi@0 | 949 | // For matching pairs of actual argument types `a' and |
aoqi@0 | 950 | // formal type parameters with declared bound `b' ... |
aoqi@0 | 951 | while (args.nonEmpty() && forms.nonEmpty()) { |
aoqi@0 | 952 | // exact type arguments needs to know their |
aoqi@0 | 953 | // bounds (for upper and lower bound |
aoqi@0 | 954 | // calculations). So we create new bounds where |
aoqi@0 | 955 | // type-parameters are replaced with actuals argument types. |
aoqi@0 | 956 | bounds_buf.append(types.subst(forms.head.getUpperBound(), formals, actuals)); |
aoqi@0 | 957 | args = args.tail; |
aoqi@0 | 958 | forms = forms.tail; |
aoqi@0 | 959 | } |
aoqi@0 | 960 | |
aoqi@0 | 961 | args = type.getTypeArguments(); |
aoqi@0 | 962 | List<Type> tvars_cap = types.substBounds(formals, |
aoqi@0 | 963 | formals, |
aoqi@0 | 964 | types.capture(type).allparams()); |
aoqi@0 | 965 | while (args.nonEmpty() && tvars_cap.nonEmpty()) { |
aoqi@0 | 966 | // Let the actual arguments know their bound |
aoqi@0 | 967 | args.head.withTypeVar((TypeVar)tvars_cap.head); |
aoqi@0 | 968 | args = args.tail; |
aoqi@0 | 969 | tvars_cap = tvars_cap.tail; |
aoqi@0 | 970 | } |
aoqi@0 | 971 | |
aoqi@0 | 972 | args = type.getTypeArguments(); |
aoqi@0 | 973 | List<Type> bounds = bounds_buf.toList(); |
aoqi@0 | 974 | |
aoqi@0 | 975 | while (args.nonEmpty() && bounds.nonEmpty()) { |
aoqi@0 | 976 | Type actual = args.head; |
aoqi@0 | 977 | if (!isTypeArgErroneous(actual) && |
aoqi@0 | 978 | !bounds.head.isErroneous() && |
aoqi@0 | 979 | !checkExtends(actual, bounds.head)) { |
aoqi@0 | 980 | return args.head; |
aoqi@0 | 981 | } |
aoqi@0 | 982 | args = args.tail; |
aoqi@0 | 983 | bounds = bounds.tail; |
aoqi@0 | 984 | } |
aoqi@0 | 985 | |
aoqi@0 | 986 | args = type.getTypeArguments(); |
aoqi@0 | 987 | bounds = bounds_buf.toList(); |
aoqi@0 | 988 | |
aoqi@0 | 989 | for (Type arg : types.capture(type).getTypeArguments()) { |
aoqi@0 | 990 | if (arg.hasTag(TYPEVAR) && |
aoqi@0 | 991 | arg.getUpperBound().isErroneous() && |
aoqi@0 | 992 | !bounds.head.isErroneous() && |
aoqi@0 | 993 | !isTypeArgErroneous(args.head)) { |
aoqi@0 | 994 | return args.head; |
aoqi@0 | 995 | } |
aoqi@0 | 996 | bounds = bounds.tail; |
aoqi@0 | 997 | args = args.tail; |
aoqi@0 | 998 | } |
aoqi@0 | 999 | |
aoqi@0 | 1000 | return null; |
aoqi@0 | 1001 | } |
aoqi@0 | 1002 | //where |
aoqi@0 | 1003 | boolean isTypeArgErroneous(Type t) { |
aoqi@0 | 1004 | return isTypeArgErroneous.visit(t); |
aoqi@0 | 1005 | } |
aoqi@0 | 1006 | |
aoqi@0 | 1007 | Types.UnaryVisitor<Boolean> isTypeArgErroneous = new Types.UnaryVisitor<Boolean>() { |
aoqi@0 | 1008 | public Boolean visitType(Type t, Void s) { |
aoqi@0 | 1009 | return t.isErroneous(); |
aoqi@0 | 1010 | } |
aoqi@0 | 1011 | @Override |
aoqi@0 | 1012 | public Boolean visitTypeVar(TypeVar t, Void s) { |
aoqi@0 | 1013 | return visit(t.getUpperBound()); |
aoqi@0 | 1014 | } |
aoqi@0 | 1015 | @Override |
aoqi@0 | 1016 | public Boolean visitCapturedType(CapturedType t, Void s) { |
aoqi@0 | 1017 | return visit(t.getUpperBound()) || |
aoqi@0 | 1018 | visit(t.getLowerBound()); |
aoqi@0 | 1019 | } |
aoqi@0 | 1020 | @Override |
aoqi@0 | 1021 | public Boolean visitWildcardType(WildcardType t, Void s) { |
aoqi@0 | 1022 | return visit(t.type); |
aoqi@0 | 1023 | } |
aoqi@0 | 1024 | }; |
aoqi@0 | 1025 | |
aoqi@0 | 1026 | /** Check that given modifiers are legal for given symbol and |
aoqi@0 | 1027 | * return modifiers together with any implicit modifiers for that symbol. |
aoqi@0 | 1028 | * Warning: we can't use flags() here since this method |
aoqi@0 | 1029 | * is called during class enter, when flags() would cause a premature |
aoqi@0 | 1030 | * completion. |
aoqi@0 | 1031 | * @param pos Position to be used for error reporting. |
aoqi@0 | 1032 | * @param flags The set of modifiers given in a definition. |
aoqi@0 | 1033 | * @param sym The defined symbol. |
aoqi@0 | 1034 | */ |
aoqi@0 | 1035 | long checkFlags(DiagnosticPosition pos, long flags, Symbol sym, JCTree tree) { |
aoqi@0 | 1036 | long mask; |
aoqi@0 | 1037 | long implicit = 0; |
aoqi@0 | 1038 | |
aoqi@0 | 1039 | switch (sym.kind) { |
aoqi@0 | 1040 | case VAR: |
aoqi@0 | 1041 | if (TreeInfo.isReceiverParam(tree)) |
aoqi@0 | 1042 | mask = ReceiverParamFlags; |
aoqi@0 | 1043 | else if (sym.owner.kind != TYP) |
aoqi@0 | 1044 | mask = LocalVarFlags; |
aoqi@0 | 1045 | else if ((sym.owner.flags_field & INTERFACE) != 0) |
aoqi@0 | 1046 | mask = implicit = InterfaceVarFlags; |
aoqi@0 | 1047 | else |
aoqi@0 | 1048 | mask = VarFlags; |
aoqi@0 | 1049 | break; |
aoqi@0 | 1050 | case MTH: |
aoqi@0 | 1051 | if (sym.name == names.init) { |
aoqi@0 | 1052 | if ((sym.owner.flags_field & ENUM) != 0) { |
aoqi@0 | 1053 | // enum constructors cannot be declared public or |
aoqi@0 | 1054 | // protected and must be implicitly or explicitly |
aoqi@0 | 1055 | // private |
aoqi@0 | 1056 | implicit = PRIVATE; |
aoqi@0 | 1057 | mask = PRIVATE; |
aoqi@0 | 1058 | } else |
aoqi@0 | 1059 | mask = ConstructorFlags; |
aoqi@0 | 1060 | } else if ((sym.owner.flags_field & INTERFACE) != 0) { |
aoqi@0 | 1061 | if ((sym.owner.flags_field & ANNOTATION) != 0) { |
aoqi@0 | 1062 | mask = AnnotationTypeElementMask; |
aoqi@0 | 1063 | implicit = PUBLIC | ABSTRACT; |
aoqi@0 | 1064 | } else if ((flags & (DEFAULT | STATIC)) != 0) { |
aoqi@0 | 1065 | mask = InterfaceMethodMask; |
aoqi@0 | 1066 | implicit = PUBLIC; |
aoqi@0 | 1067 | if ((flags & DEFAULT) != 0) { |
aoqi@0 | 1068 | implicit |= ABSTRACT; |
aoqi@0 | 1069 | } |
aoqi@0 | 1070 | } else { |
aoqi@0 | 1071 | mask = implicit = InterfaceMethodFlags; |
aoqi@0 | 1072 | } |
aoqi@0 | 1073 | } else { |
aoqi@0 | 1074 | mask = MethodFlags; |
aoqi@0 | 1075 | } |
aoqi@0 | 1076 | // Imply STRICTFP if owner has STRICTFP set. |
aoqi@0 | 1077 | if (((flags|implicit) & Flags.ABSTRACT) == 0 || |
aoqi@0 | 1078 | ((flags) & Flags.DEFAULT) != 0) |
aoqi@0 | 1079 | implicit |= sym.owner.flags_field & STRICTFP; |
aoqi@0 | 1080 | break; |
aoqi@0 | 1081 | case TYP: |
aoqi@0 | 1082 | if (sym.isLocal()) { |
aoqi@0 | 1083 | mask = LocalClassFlags; |
aoqi@0 | 1084 | if (sym.name.isEmpty()) { // Anonymous class |
aoqi@0 | 1085 | // Anonymous classes in static methods are themselves static; |
aoqi@0 | 1086 | // that's why we admit STATIC here. |
aoqi@0 | 1087 | mask |= STATIC; |
aoqi@0 | 1088 | // JLS: Anonymous classes are final. |
aoqi@0 | 1089 | implicit |= FINAL; |
aoqi@0 | 1090 | } |
aoqi@0 | 1091 | if ((sym.owner.flags_field & STATIC) == 0 && |
aoqi@0 | 1092 | (flags & ENUM) != 0) |
aoqi@0 | 1093 | log.error(pos, "enums.must.be.static"); |
aoqi@0 | 1094 | } else if (sym.owner.kind == TYP) { |
aoqi@0 | 1095 | mask = MemberClassFlags; |
aoqi@0 | 1096 | if (sym.owner.owner.kind == PCK || |
aoqi@0 | 1097 | (sym.owner.flags_field & STATIC) != 0) |
aoqi@0 | 1098 | mask |= STATIC; |
aoqi@0 | 1099 | else if ((flags & ENUM) != 0) |
aoqi@0 | 1100 | log.error(pos, "enums.must.be.static"); |
aoqi@0 | 1101 | // Nested interfaces and enums are always STATIC (Spec ???) |
aoqi@0 | 1102 | if ((flags & (INTERFACE | ENUM)) != 0 ) implicit = STATIC; |
aoqi@0 | 1103 | } else { |
aoqi@0 | 1104 | mask = ClassFlags; |
aoqi@0 | 1105 | } |
aoqi@0 | 1106 | // Interfaces are always ABSTRACT |
aoqi@0 | 1107 | if ((flags & INTERFACE) != 0) implicit |= ABSTRACT; |
aoqi@0 | 1108 | |
aoqi@0 | 1109 | if ((flags & ENUM) != 0) { |
aoqi@0 | 1110 | // enums can't be declared abstract or final |
aoqi@0 | 1111 | mask &= ~(ABSTRACT | FINAL); |
aoqi@0 | 1112 | implicit |= implicitEnumFinalFlag(tree); |
aoqi@0 | 1113 | } |
aoqi@0 | 1114 | // Imply STRICTFP if owner has STRICTFP set. |
aoqi@0 | 1115 | implicit |= sym.owner.flags_field & STRICTFP; |
aoqi@0 | 1116 | break; |
aoqi@0 | 1117 | default: |
aoqi@0 | 1118 | throw new AssertionError(); |
aoqi@0 | 1119 | } |
aoqi@0 | 1120 | long illegal = flags & ExtendedStandardFlags & ~mask; |
aoqi@0 | 1121 | if (illegal != 0) { |
aoqi@0 | 1122 | if ((illegal & INTERFACE) != 0) { |
aoqi@0 | 1123 | log.error(pos, "intf.not.allowed.here"); |
aoqi@0 | 1124 | mask |= INTERFACE; |
aoqi@0 | 1125 | } |
aoqi@0 | 1126 | else { |
aoqi@0 | 1127 | log.error(pos, |
aoqi@0 | 1128 | "mod.not.allowed.here", asFlagSet(illegal)); |
aoqi@0 | 1129 | } |
aoqi@0 | 1130 | } |
aoqi@0 | 1131 | else if ((sym.kind == TYP || |
aoqi@0 | 1132 | // ISSUE: Disallowing abstract&private is no longer appropriate |
aoqi@0 | 1133 | // in the presence of inner classes. Should it be deleted here? |
aoqi@0 | 1134 | checkDisjoint(pos, flags, |
aoqi@0 | 1135 | ABSTRACT, |
aoqi@0 | 1136 | PRIVATE | STATIC | DEFAULT)) |
aoqi@0 | 1137 | && |
aoqi@0 | 1138 | checkDisjoint(pos, flags, |
aoqi@0 | 1139 | STATIC, |
aoqi@0 | 1140 | DEFAULT) |
aoqi@0 | 1141 | && |
aoqi@0 | 1142 | checkDisjoint(pos, flags, |
aoqi@0 | 1143 | ABSTRACT | INTERFACE, |
aoqi@0 | 1144 | FINAL | NATIVE | SYNCHRONIZED) |
aoqi@0 | 1145 | && |
aoqi@0 | 1146 | checkDisjoint(pos, flags, |
aoqi@0 | 1147 | PUBLIC, |
aoqi@0 | 1148 | PRIVATE | PROTECTED) |
aoqi@0 | 1149 | && |
aoqi@0 | 1150 | checkDisjoint(pos, flags, |
aoqi@0 | 1151 | PRIVATE, |
aoqi@0 | 1152 | PUBLIC | PROTECTED) |
aoqi@0 | 1153 | && |
aoqi@0 | 1154 | checkDisjoint(pos, flags, |
aoqi@0 | 1155 | FINAL, |
aoqi@0 | 1156 | VOLATILE) |
aoqi@0 | 1157 | && |
aoqi@0 | 1158 | (sym.kind == TYP || |
aoqi@0 | 1159 | checkDisjoint(pos, flags, |
aoqi@0 | 1160 | ABSTRACT | NATIVE, |
aoqi@0 | 1161 | STRICTFP))) { |
aoqi@0 | 1162 | // skip |
aoqi@0 | 1163 | } |
aoqi@0 | 1164 | return flags & (mask | ~ExtendedStandardFlags) | implicit; |
aoqi@0 | 1165 | } |
aoqi@0 | 1166 | |
aoqi@0 | 1167 | |
aoqi@0 | 1168 | /** Determine if this enum should be implicitly final. |
aoqi@0 | 1169 | * |
aoqi@0 | 1170 | * If the enum has no specialized enum contants, it is final. |
aoqi@0 | 1171 | * |
aoqi@0 | 1172 | * If the enum does have specialized enum contants, it is |
aoqi@0 | 1173 | * <i>not</i> final. |
aoqi@0 | 1174 | */ |
aoqi@0 | 1175 | private long implicitEnumFinalFlag(JCTree tree) { |
aoqi@0 | 1176 | if (!tree.hasTag(CLASSDEF)) return 0; |
aoqi@0 | 1177 | class SpecialTreeVisitor extends JCTree.Visitor { |
aoqi@0 | 1178 | boolean specialized; |
aoqi@0 | 1179 | SpecialTreeVisitor() { |
aoqi@0 | 1180 | this.specialized = false; |
aoqi@0 | 1181 | }; |
aoqi@0 | 1182 | |
aoqi@0 | 1183 | @Override |
aoqi@0 | 1184 | public void visitTree(JCTree tree) { /* no-op */ } |
aoqi@0 | 1185 | |
aoqi@0 | 1186 | @Override |
aoqi@0 | 1187 | public void visitVarDef(JCVariableDecl tree) { |
aoqi@0 | 1188 | if ((tree.mods.flags & ENUM) != 0) { |
aoqi@0 | 1189 | if (tree.init instanceof JCNewClass && |
aoqi@0 | 1190 | ((JCNewClass) tree.init).def != null) { |
aoqi@0 | 1191 | specialized = true; |
aoqi@0 | 1192 | } |
aoqi@0 | 1193 | } |
aoqi@0 | 1194 | } |
aoqi@0 | 1195 | } |
aoqi@0 | 1196 | |
aoqi@0 | 1197 | SpecialTreeVisitor sts = new SpecialTreeVisitor(); |
aoqi@0 | 1198 | JCClassDecl cdef = (JCClassDecl) tree; |
aoqi@0 | 1199 | for (JCTree defs: cdef.defs) { |
aoqi@0 | 1200 | defs.accept(sts); |
aoqi@0 | 1201 | if (sts.specialized) return 0; |
aoqi@0 | 1202 | } |
aoqi@0 | 1203 | return FINAL; |
aoqi@0 | 1204 | } |
aoqi@0 | 1205 | |
aoqi@0 | 1206 | /* ************************************************************************* |
aoqi@0 | 1207 | * Type Validation |
aoqi@0 | 1208 | **************************************************************************/ |
aoqi@0 | 1209 | |
aoqi@0 | 1210 | /** Validate a type expression. That is, |
aoqi@0 | 1211 | * check that all type arguments of a parametric type are within |
aoqi@0 | 1212 | * their bounds. This must be done in a second phase after type attribution |
aoqi@0 | 1213 | * since a class might have a subclass as type parameter bound. E.g: |
aoqi@0 | 1214 | * |
aoqi@0 | 1215 | * <pre>{@code |
aoqi@0 | 1216 | * class B<A extends C> { ... } |
aoqi@0 | 1217 | * class C extends B<C> { ... } |
aoqi@0 | 1218 | * }</pre> |
aoqi@0 | 1219 | * |
aoqi@0 | 1220 | * and we can't make sure that the bound is already attributed because |
aoqi@0 | 1221 | * of possible cycles. |
aoqi@0 | 1222 | * |
aoqi@0 | 1223 | * Visitor method: Validate a type expression, if it is not null, catching |
aoqi@0 | 1224 | * and reporting any completion failures. |
aoqi@0 | 1225 | */ |
aoqi@0 | 1226 | void validate(JCTree tree, Env<AttrContext> env) { |
aoqi@0 | 1227 | validate(tree, env, true); |
aoqi@0 | 1228 | } |
aoqi@0 | 1229 | void validate(JCTree tree, Env<AttrContext> env, boolean checkRaw) { |
aoqi@0 | 1230 | new Validator(env).validateTree(tree, checkRaw, true); |
aoqi@0 | 1231 | } |
aoqi@0 | 1232 | |
aoqi@0 | 1233 | /** Visitor method: Validate a list of type expressions. |
aoqi@0 | 1234 | */ |
aoqi@0 | 1235 | void validate(List<? extends JCTree> trees, Env<AttrContext> env) { |
aoqi@0 | 1236 | for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail) |
aoqi@0 | 1237 | validate(l.head, env); |
aoqi@0 | 1238 | } |
aoqi@0 | 1239 | |
aoqi@0 | 1240 | /** A visitor class for type validation. |
aoqi@0 | 1241 | */ |
aoqi@0 | 1242 | class Validator extends JCTree.Visitor { |
aoqi@0 | 1243 | |
aoqi@0 | 1244 | boolean checkRaw; |
aoqi@0 | 1245 | boolean isOuter; |
aoqi@0 | 1246 | Env<AttrContext> env; |
aoqi@0 | 1247 | |
aoqi@0 | 1248 | Validator(Env<AttrContext> env) { |
aoqi@0 | 1249 | this.env = env; |
aoqi@0 | 1250 | } |
aoqi@0 | 1251 | |
aoqi@0 | 1252 | @Override |
aoqi@0 | 1253 | public void visitTypeArray(JCArrayTypeTree tree) { |
aoqi@0 | 1254 | validateTree(tree.elemtype, checkRaw, isOuter); |
aoqi@0 | 1255 | } |
aoqi@0 | 1256 | |
aoqi@0 | 1257 | @Override |
aoqi@0 | 1258 | public void visitTypeApply(JCTypeApply tree) { |
aoqi@0 | 1259 | if (tree.type.hasTag(CLASS)) { |
aoqi@0 | 1260 | List<JCExpression> args = tree.arguments; |
aoqi@0 | 1261 | List<Type> forms = tree.type.tsym.type.getTypeArguments(); |
aoqi@0 | 1262 | |
aoqi@0 | 1263 | Type incompatibleArg = firstIncompatibleTypeArg(tree.type); |
aoqi@0 | 1264 | if (incompatibleArg != null) { |
aoqi@0 | 1265 | for (JCTree arg : tree.arguments) { |
aoqi@0 | 1266 | if (arg.type == incompatibleArg) { |
aoqi@0 | 1267 | log.error(arg, "not.within.bounds", incompatibleArg, forms.head); |
aoqi@0 | 1268 | } |
aoqi@0 | 1269 | forms = forms.tail; |
aoqi@0 | 1270 | } |
aoqi@0 | 1271 | } |
aoqi@0 | 1272 | |
aoqi@0 | 1273 | forms = tree.type.tsym.type.getTypeArguments(); |
aoqi@0 | 1274 | |
aoqi@0 | 1275 | boolean is_java_lang_Class = tree.type.tsym.flatName() == names.java_lang_Class; |
aoqi@0 | 1276 | |
aoqi@0 | 1277 | // For matching pairs of actual argument types `a' and |
aoqi@0 | 1278 | // formal type parameters with declared bound `b' ... |
aoqi@0 | 1279 | while (args.nonEmpty() && forms.nonEmpty()) { |
aoqi@0 | 1280 | validateTree(args.head, |
aoqi@0 | 1281 | !(isOuter && is_java_lang_Class), |
aoqi@0 | 1282 | false); |
aoqi@0 | 1283 | args = args.tail; |
aoqi@0 | 1284 | forms = forms.tail; |
aoqi@0 | 1285 | } |
aoqi@0 | 1286 | |
aoqi@0 | 1287 | // Check that this type is either fully parameterized, or |
aoqi@0 | 1288 | // not parameterized at all. |
aoqi@0 | 1289 | if (tree.type.getEnclosingType().isRaw()) |
aoqi@0 | 1290 | log.error(tree.pos(), "improperly.formed.type.inner.raw.param"); |
aoqi@0 | 1291 | if (tree.clazz.hasTag(SELECT)) |
aoqi@0 | 1292 | visitSelectInternal((JCFieldAccess)tree.clazz); |
aoqi@0 | 1293 | } |
aoqi@0 | 1294 | } |
aoqi@0 | 1295 | |
aoqi@0 | 1296 | @Override |
aoqi@0 | 1297 | public void visitTypeParameter(JCTypeParameter tree) { |
aoqi@0 | 1298 | validateTrees(tree.bounds, true, isOuter); |
aoqi@0 | 1299 | checkClassBounds(tree.pos(), tree.type); |
aoqi@0 | 1300 | } |
aoqi@0 | 1301 | |
aoqi@0 | 1302 | @Override |
aoqi@0 | 1303 | public void visitWildcard(JCWildcard tree) { |
aoqi@0 | 1304 | if (tree.inner != null) |
aoqi@0 | 1305 | validateTree(tree.inner, true, isOuter); |
aoqi@0 | 1306 | } |
aoqi@0 | 1307 | |
aoqi@0 | 1308 | @Override |
aoqi@0 | 1309 | public void visitSelect(JCFieldAccess tree) { |
aoqi@0 | 1310 | if (tree.type.hasTag(CLASS)) { |
aoqi@0 | 1311 | visitSelectInternal(tree); |
aoqi@0 | 1312 | |
aoqi@0 | 1313 | // Check that this type is either fully parameterized, or |
aoqi@0 | 1314 | // not parameterized at all. |
aoqi@0 | 1315 | if (tree.selected.type.isParameterized() && tree.type.tsym.type.getTypeArguments().nonEmpty()) |
aoqi@0 | 1316 | log.error(tree.pos(), "improperly.formed.type.param.missing"); |
aoqi@0 | 1317 | } |
aoqi@0 | 1318 | } |
aoqi@0 | 1319 | |
aoqi@0 | 1320 | public void visitSelectInternal(JCFieldAccess tree) { |
aoqi@0 | 1321 | if (tree.type.tsym.isStatic() && |
aoqi@0 | 1322 | tree.selected.type.isParameterized()) { |
aoqi@0 | 1323 | // The enclosing type is not a class, so we are |
aoqi@0 | 1324 | // looking at a static member type. However, the |
aoqi@0 | 1325 | // qualifying expression is parameterized. |
aoqi@0 | 1326 | log.error(tree.pos(), "cant.select.static.class.from.param.type"); |
aoqi@0 | 1327 | } else { |
aoqi@0 | 1328 | // otherwise validate the rest of the expression |
aoqi@0 | 1329 | tree.selected.accept(this); |
aoqi@0 | 1330 | } |
aoqi@0 | 1331 | } |
aoqi@0 | 1332 | |
aoqi@0 | 1333 | @Override |
aoqi@0 | 1334 | public void visitAnnotatedType(JCAnnotatedType tree) { |
aoqi@0 | 1335 | tree.underlyingType.accept(this); |
aoqi@0 | 1336 | } |
aoqi@0 | 1337 | |
aoqi@0 | 1338 | @Override |
aoqi@0 | 1339 | public void visitTypeIdent(JCPrimitiveTypeTree that) { |
aoqi@0 | 1340 | if (that.type.hasTag(TypeTag.VOID)) { |
aoqi@0 | 1341 | log.error(that.pos(), "void.not.allowed.here"); |
aoqi@0 | 1342 | } |
aoqi@0 | 1343 | super.visitTypeIdent(that); |
aoqi@0 | 1344 | } |
aoqi@0 | 1345 | |
aoqi@0 | 1346 | /** Default visitor method: do nothing. |
aoqi@0 | 1347 | */ |
aoqi@0 | 1348 | @Override |
aoqi@0 | 1349 | public void visitTree(JCTree tree) { |
aoqi@0 | 1350 | } |
aoqi@0 | 1351 | |
aoqi@0 | 1352 | public void validateTree(JCTree tree, boolean checkRaw, boolean isOuter) { |
aoqi@0 | 1353 | if (tree != null) { |
aoqi@0 | 1354 | boolean prevCheckRaw = this.checkRaw; |
aoqi@0 | 1355 | this.checkRaw = checkRaw; |
aoqi@0 | 1356 | this.isOuter = isOuter; |
aoqi@0 | 1357 | |
aoqi@0 | 1358 | try { |
aoqi@0 | 1359 | tree.accept(this); |
aoqi@0 | 1360 | if (checkRaw) |
aoqi@0 | 1361 | checkRaw(tree, env); |
aoqi@0 | 1362 | } catch (CompletionFailure ex) { |
aoqi@0 | 1363 | completionError(tree.pos(), ex); |
aoqi@0 | 1364 | } finally { |
aoqi@0 | 1365 | this.checkRaw = prevCheckRaw; |
aoqi@0 | 1366 | } |
aoqi@0 | 1367 | } |
aoqi@0 | 1368 | } |
aoqi@0 | 1369 | |
aoqi@0 | 1370 | public void validateTrees(List<? extends JCTree> trees, boolean checkRaw, boolean isOuter) { |
aoqi@0 | 1371 | for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail) |
aoqi@0 | 1372 | validateTree(l.head, checkRaw, isOuter); |
aoqi@0 | 1373 | } |
aoqi@0 | 1374 | } |
aoqi@0 | 1375 | |
aoqi@0 | 1376 | void checkRaw(JCTree tree, Env<AttrContext> env) { |
aoqi@0 | 1377 | if (lint.isEnabled(LintCategory.RAW) && |
aoqi@0 | 1378 | tree.type.hasTag(CLASS) && |
aoqi@0 | 1379 | !TreeInfo.isDiamond(tree) && |
aoqi@0 | 1380 | !withinAnonConstr(env) && |
aoqi@0 | 1381 | tree.type.isRaw()) { |
aoqi@0 | 1382 | log.warning(LintCategory.RAW, |
aoqi@0 | 1383 | tree.pos(), "raw.class.use", tree.type, tree.type.tsym.type); |
aoqi@0 | 1384 | } |
aoqi@0 | 1385 | } |
aoqi@0 | 1386 | //where |
aoqi@0 | 1387 | private boolean withinAnonConstr(Env<AttrContext> env) { |
aoqi@0 | 1388 | return env.enclClass.name.isEmpty() && |
aoqi@0 | 1389 | env.enclMethod != null && env.enclMethod.name == names.init; |
aoqi@0 | 1390 | } |
aoqi@0 | 1391 | |
aoqi@0 | 1392 | /* ************************************************************************* |
aoqi@0 | 1393 | * Exception checking |
aoqi@0 | 1394 | **************************************************************************/ |
aoqi@0 | 1395 | |
aoqi@0 | 1396 | /* The following methods treat classes as sets that contain |
aoqi@0 | 1397 | * the class itself and all their subclasses |
aoqi@0 | 1398 | */ |
aoqi@0 | 1399 | |
aoqi@0 | 1400 | /** Is given type a subtype of some of the types in given list? |
aoqi@0 | 1401 | */ |
aoqi@0 | 1402 | boolean subset(Type t, List<Type> ts) { |
aoqi@0 | 1403 | for (List<Type> l = ts; l.nonEmpty(); l = l.tail) |
aoqi@0 | 1404 | if (types.isSubtype(t, l.head)) return true; |
aoqi@0 | 1405 | return false; |
aoqi@0 | 1406 | } |
aoqi@0 | 1407 | |
aoqi@0 | 1408 | /** Is given type a subtype or supertype of |
aoqi@0 | 1409 | * some of the types in given list? |
aoqi@0 | 1410 | */ |
aoqi@0 | 1411 | boolean intersects(Type t, List<Type> ts) { |
aoqi@0 | 1412 | for (List<Type> l = ts; l.nonEmpty(); l = l.tail) |
aoqi@0 | 1413 | if (types.isSubtype(t, l.head) || types.isSubtype(l.head, t)) return true; |
aoqi@0 | 1414 | return false; |
aoqi@0 | 1415 | } |
aoqi@0 | 1416 | |
aoqi@0 | 1417 | /** Add type set to given type list, unless it is a subclass of some class |
aoqi@0 | 1418 | * in the list. |
aoqi@0 | 1419 | */ |
aoqi@0 | 1420 | List<Type> incl(Type t, List<Type> ts) { |
aoqi@0 | 1421 | return subset(t, ts) ? ts : excl(t, ts).prepend(t); |
aoqi@0 | 1422 | } |
aoqi@0 | 1423 | |
aoqi@0 | 1424 | /** Remove type set from type set list. |
aoqi@0 | 1425 | */ |
aoqi@0 | 1426 | List<Type> excl(Type t, List<Type> ts) { |
aoqi@0 | 1427 | if (ts.isEmpty()) { |
aoqi@0 | 1428 | return ts; |
aoqi@0 | 1429 | } else { |
aoqi@0 | 1430 | List<Type> ts1 = excl(t, ts.tail); |
aoqi@0 | 1431 | if (types.isSubtype(ts.head, t)) return ts1; |
aoqi@0 | 1432 | else if (ts1 == ts.tail) return ts; |
aoqi@0 | 1433 | else return ts1.prepend(ts.head); |
aoqi@0 | 1434 | } |
aoqi@0 | 1435 | } |
aoqi@0 | 1436 | |
aoqi@0 | 1437 | /** Form the union of two type set lists. |
aoqi@0 | 1438 | */ |
aoqi@0 | 1439 | List<Type> union(List<Type> ts1, List<Type> ts2) { |
aoqi@0 | 1440 | List<Type> ts = ts1; |
aoqi@0 | 1441 | for (List<Type> l = ts2; l.nonEmpty(); l = l.tail) |
aoqi@0 | 1442 | ts = incl(l.head, ts); |
aoqi@0 | 1443 | return ts; |
aoqi@0 | 1444 | } |
aoqi@0 | 1445 | |
aoqi@0 | 1446 | /** Form the difference of two type lists. |
aoqi@0 | 1447 | */ |
aoqi@0 | 1448 | List<Type> diff(List<Type> ts1, List<Type> ts2) { |
aoqi@0 | 1449 | List<Type> ts = ts1; |
aoqi@0 | 1450 | for (List<Type> l = ts2; l.nonEmpty(); l = l.tail) |
aoqi@0 | 1451 | ts = excl(l.head, ts); |
aoqi@0 | 1452 | return ts; |
aoqi@0 | 1453 | } |
aoqi@0 | 1454 | |
aoqi@0 | 1455 | /** Form the intersection of two type lists. |
aoqi@0 | 1456 | */ |
aoqi@0 | 1457 | public List<Type> intersect(List<Type> ts1, List<Type> ts2) { |
aoqi@0 | 1458 | List<Type> ts = List.nil(); |
aoqi@0 | 1459 | for (List<Type> l = ts1; l.nonEmpty(); l = l.tail) |
aoqi@0 | 1460 | if (subset(l.head, ts2)) ts = incl(l.head, ts); |
aoqi@0 | 1461 | for (List<Type> l = ts2; l.nonEmpty(); l = l.tail) |
aoqi@0 | 1462 | if (subset(l.head, ts1)) ts = incl(l.head, ts); |
aoqi@0 | 1463 | return ts; |
aoqi@0 | 1464 | } |
aoqi@0 | 1465 | |
aoqi@0 | 1466 | /** Is exc an exception symbol that need not be declared? |
aoqi@0 | 1467 | */ |
aoqi@0 | 1468 | boolean isUnchecked(ClassSymbol exc) { |
aoqi@0 | 1469 | return |
aoqi@0 | 1470 | exc.kind == ERR || |
aoqi@0 | 1471 | exc.isSubClass(syms.errorType.tsym, types) || |
aoqi@0 | 1472 | exc.isSubClass(syms.runtimeExceptionType.tsym, types); |
aoqi@0 | 1473 | } |
aoqi@0 | 1474 | |
aoqi@0 | 1475 | /** Is exc an exception type that need not be declared? |
aoqi@0 | 1476 | */ |
aoqi@0 | 1477 | boolean isUnchecked(Type exc) { |
aoqi@0 | 1478 | return |
aoqi@0 | 1479 | (exc.hasTag(TYPEVAR)) ? isUnchecked(types.supertype(exc)) : |
aoqi@0 | 1480 | (exc.hasTag(CLASS)) ? isUnchecked((ClassSymbol)exc.tsym) : |
aoqi@0 | 1481 | exc.hasTag(BOT); |
aoqi@0 | 1482 | } |
aoqi@0 | 1483 | |
aoqi@0 | 1484 | /** Same, but handling completion failures. |
aoqi@0 | 1485 | */ |
aoqi@0 | 1486 | boolean isUnchecked(DiagnosticPosition pos, Type exc) { |
aoqi@0 | 1487 | try { |
aoqi@0 | 1488 | return isUnchecked(exc); |
aoqi@0 | 1489 | } catch (CompletionFailure ex) { |
aoqi@0 | 1490 | completionError(pos, ex); |
aoqi@0 | 1491 | return true; |
aoqi@0 | 1492 | } |
aoqi@0 | 1493 | } |
aoqi@0 | 1494 | |
aoqi@0 | 1495 | /** Is exc handled by given exception list? |
aoqi@0 | 1496 | */ |
aoqi@0 | 1497 | boolean isHandled(Type exc, List<Type> handled) { |
aoqi@0 | 1498 | return isUnchecked(exc) || subset(exc, handled); |
aoqi@0 | 1499 | } |
aoqi@0 | 1500 | |
aoqi@0 | 1501 | /** Return all exceptions in thrown list that are not in handled list. |
aoqi@0 | 1502 | * @param thrown The list of thrown exceptions. |
aoqi@0 | 1503 | * @param handled The list of handled exceptions. |
aoqi@0 | 1504 | */ |
aoqi@0 | 1505 | List<Type> unhandled(List<Type> thrown, List<Type> handled) { |
aoqi@0 | 1506 | List<Type> unhandled = List.nil(); |
aoqi@0 | 1507 | for (List<Type> l = thrown; l.nonEmpty(); l = l.tail) |
aoqi@0 | 1508 | if (!isHandled(l.head, handled)) unhandled = unhandled.prepend(l.head); |
aoqi@0 | 1509 | return unhandled; |
aoqi@0 | 1510 | } |
aoqi@0 | 1511 | |
aoqi@0 | 1512 | /* ************************************************************************* |
aoqi@0 | 1513 | * Overriding/Implementation checking |
aoqi@0 | 1514 | **************************************************************************/ |
aoqi@0 | 1515 | |
aoqi@0 | 1516 | /** The level of access protection given by a flag set, |
aoqi@0 | 1517 | * where PRIVATE is highest and PUBLIC is lowest. |
aoqi@0 | 1518 | */ |
aoqi@0 | 1519 | static int protection(long flags) { |
aoqi@0 | 1520 | switch ((short)(flags & AccessFlags)) { |
aoqi@0 | 1521 | case PRIVATE: return 3; |
aoqi@0 | 1522 | case PROTECTED: return 1; |
aoqi@0 | 1523 | default: |
aoqi@0 | 1524 | case PUBLIC: return 0; |
aoqi@0 | 1525 | case 0: return 2; |
aoqi@0 | 1526 | } |
aoqi@0 | 1527 | } |
aoqi@0 | 1528 | |
aoqi@0 | 1529 | /** A customized "cannot override" error message. |
aoqi@0 | 1530 | * @param m The overriding method. |
aoqi@0 | 1531 | * @param other The overridden method. |
aoqi@0 | 1532 | * @return An internationalized string. |
aoqi@0 | 1533 | */ |
aoqi@0 | 1534 | Object cannotOverride(MethodSymbol m, MethodSymbol other) { |
aoqi@0 | 1535 | String key; |
aoqi@0 | 1536 | if ((other.owner.flags() & INTERFACE) == 0) |
aoqi@0 | 1537 | key = "cant.override"; |
aoqi@0 | 1538 | else if ((m.owner.flags() & INTERFACE) == 0) |
aoqi@0 | 1539 | key = "cant.implement"; |
aoqi@0 | 1540 | else |
aoqi@0 | 1541 | key = "clashes.with"; |
aoqi@0 | 1542 | return diags.fragment(key, m, m.location(), other, other.location()); |
aoqi@0 | 1543 | } |
aoqi@0 | 1544 | |
aoqi@0 | 1545 | /** A customized "override" warning message. |
aoqi@0 | 1546 | * @param m The overriding method. |
aoqi@0 | 1547 | * @param other The overridden method. |
aoqi@0 | 1548 | * @return An internationalized string. |
aoqi@0 | 1549 | */ |
aoqi@0 | 1550 | Object uncheckedOverrides(MethodSymbol m, MethodSymbol other) { |
aoqi@0 | 1551 | String key; |
aoqi@0 | 1552 | if ((other.owner.flags() & INTERFACE) == 0) |
aoqi@0 | 1553 | key = "unchecked.override"; |
aoqi@0 | 1554 | else if ((m.owner.flags() & INTERFACE) == 0) |
aoqi@0 | 1555 | key = "unchecked.implement"; |
aoqi@0 | 1556 | else |
aoqi@0 | 1557 | key = "unchecked.clash.with"; |
aoqi@0 | 1558 | return diags.fragment(key, m, m.location(), other, other.location()); |
aoqi@0 | 1559 | } |
aoqi@0 | 1560 | |
aoqi@0 | 1561 | /** A customized "override" warning message. |
aoqi@0 | 1562 | * @param m The overriding method. |
aoqi@0 | 1563 | * @param other The overridden method. |
aoqi@0 | 1564 | * @return An internationalized string. |
aoqi@0 | 1565 | */ |
aoqi@0 | 1566 | Object varargsOverrides(MethodSymbol m, MethodSymbol other) { |
aoqi@0 | 1567 | String key; |
aoqi@0 | 1568 | if ((other.owner.flags() & INTERFACE) == 0) |
aoqi@0 | 1569 | key = "varargs.override"; |
aoqi@0 | 1570 | else if ((m.owner.flags() & INTERFACE) == 0) |
aoqi@0 | 1571 | key = "varargs.implement"; |
aoqi@0 | 1572 | else |
aoqi@0 | 1573 | key = "varargs.clash.with"; |
aoqi@0 | 1574 | return diags.fragment(key, m, m.location(), other, other.location()); |
aoqi@0 | 1575 | } |
aoqi@0 | 1576 | |
aoqi@0 | 1577 | /** Check that this method conforms with overridden method 'other'. |
aoqi@0 | 1578 | * where `origin' is the class where checking started. |
aoqi@0 | 1579 | * Complications: |
aoqi@0 | 1580 | * (1) Do not check overriding of synthetic methods |
aoqi@0 | 1581 | * (reason: they might be final). |
aoqi@0 | 1582 | * todo: check whether this is still necessary. |
aoqi@0 | 1583 | * (2) Admit the case where an interface proxy throws fewer exceptions |
aoqi@0 | 1584 | * than the method it implements. Augment the proxy methods with the |
aoqi@0 | 1585 | * undeclared exceptions in this case. |
aoqi@0 | 1586 | * (3) When generics are enabled, admit the case where an interface proxy |
aoqi@0 | 1587 | * has a result type |
aoqi@0 | 1588 | * extended by the result type of the method it implements. |
aoqi@0 | 1589 | * Change the proxies result type to the smaller type in this case. |
aoqi@0 | 1590 | * |
aoqi@0 | 1591 | * @param tree The tree from which positions |
aoqi@0 | 1592 | * are extracted for errors. |
aoqi@0 | 1593 | * @param m The overriding method. |
aoqi@0 | 1594 | * @param other The overridden method. |
aoqi@0 | 1595 | * @param origin The class of which the overriding method |
aoqi@0 | 1596 | * is a member. |
aoqi@0 | 1597 | */ |
aoqi@0 | 1598 | void checkOverride(JCTree tree, |
aoqi@0 | 1599 | MethodSymbol m, |
aoqi@0 | 1600 | MethodSymbol other, |
aoqi@0 | 1601 | ClassSymbol origin) { |
aoqi@0 | 1602 | // Don't check overriding of synthetic methods or by bridge methods. |
aoqi@0 | 1603 | if ((m.flags() & (SYNTHETIC|BRIDGE)) != 0 || (other.flags() & SYNTHETIC) != 0) { |
aoqi@0 | 1604 | return; |
aoqi@0 | 1605 | } |
aoqi@0 | 1606 | |
aoqi@0 | 1607 | // Error if static method overrides instance method (JLS 8.4.6.2). |
aoqi@0 | 1608 | if ((m.flags() & STATIC) != 0 && |
aoqi@0 | 1609 | (other.flags() & STATIC) == 0) { |
aoqi@0 | 1610 | log.error(TreeInfo.diagnosticPositionFor(m, tree), "override.static", |
aoqi@0 | 1611 | cannotOverride(m, other)); |
aoqi@0 | 1612 | m.flags_field |= BAD_OVERRIDE; |
aoqi@0 | 1613 | return; |
aoqi@0 | 1614 | } |
aoqi@0 | 1615 | |
aoqi@0 | 1616 | // Error if instance method overrides static or final |
aoqi@0 | 1617 | // method (JLS 8.4.6.1). |
aoqi@0 | 1618 | if ((other.flags() & FINAL) != 0 || |
aoqi@0 | 1619 | (m.flags() & STATIC) == 0 && |
aoqi@0 | 1620 | (other.flags() & STATIC) != 0) { |
aoqi@0 | 1621 | log.error(TreeInfo.diagnosticPositionFor(m, tree), "override.meth", |
aoqi@0 | 1622 | cannotOverride(m, other), |
aoqi@0 | 1623 | asFlagSet(other.flags() & (FINAL | STATIC))); |
aoqi@0 | 1624 | m.flags_field |= BAD_OVERRIDE; |
aoqi@0 | 1625 | return; |
aoqi@0 | 1626 | } |
aoqi@0 | 1627 | |
aoqi@0 | 1628 | if ((m.owner.flags() & ANNOTATION) != 0) { |
aoqi@0 | 1629 | // handled in validateAnnotationMethod |
aoqi@0 | 1630 | return; |
aoqi@0 | 1631 | } |
aoqi@0 | 1632 | |
aoqi@0 | 1633 | // Error if overriding method has weaker access (JLS 8.4.6.3). |
aoqi@0 | 1634 | if ((origin.flags() & INTERFACE) == 0 && |
aoqi@0 | 1635 | protection(m.flags()) > protection(other.flags())) { |
aoqi@0 | 1636 | log.error(TreeInfo.diagnosticPositionFor(m, tree), "override.weaker.access", |
aoqi@0 | 1637 | cannotOverride(m, other), |
aoqi@0 | 1638 | other.flags() == 0 ? |
aoqi@0 | 1639 | "package" : |
aoqi@0 | 1640 | asFlagSet(other.flags() & AccessFlags)); |
aoqi@0 | 1641 | m.flags_field |= BAD_OVERRIDE; |
aoqi@0 | 1642 | return; |
aoqi@0 | 1643 | } |
aoqi@0 | 1644 | |
aoqi@0 | 1645 | Type mt = types.memberType(origin.type, m); |
aoqi@0 | 1646 | Type ot = types.memberType(origin.type, other); |
aoqi@0 | 1647 | // Error if overriding result type is different |
aoqi@0 | 1648 | // (or, in the case of generics mode, not a subtype) of |
aoqi@0 | 1649 | // overridden result type. We have to rename any type parameters |
aoqi@0 | 1650 | // before comparing types. |
aoqi@0 | 1651 | List<Type> mtvars = mt.getTypeArguments(); |
aoqi@0 | 1652 | List<Type> otvars = ot.getTypeArguments(); |
aoqi@0 | 1653 | Type mtres = mt.getReturnType(); |
aoqi@0 | 1654 | Type otres = types.subst(ot.getReturnType(), otvars, mtvars); |
aoqi@0 | 1655 | |
aoqi@0 | 1656 | overrideWarner.clear(); |
aoqi@0 | 1657 | boolean resultTypesOK = |
aoqi@0 | 1658 | types.returnTypeSubstitutable(mt, ot, otres, overrideWarner); |
aoqi@0 | 1659 | if (!resultTypesOK) { |
aoqi@0 | 1660 | if (!allowCovariantReturns && |
aoqi@0 | 1661 | m.owner != origin && |
aoqi@0 | 1662 | m.owner.isSubClass(other.owner, types)) { |
aoqi@0 | 1663 | // allow limited interoperability with covariant returns |
aoqi@0 | 1664 | } else { |
aoqi@0 | 1665 | log.error(TreeInfo.diagnosticPositionFor(m, tree), |
aoqi@0 | 1666 | "override.incompatible.ret", |
aoqi@0 | 1667 | cannotOverride(m, other), |
aoqi@0 | 1668 | mtres, otres); |
aoqi@0 | 1669 | m.flags_field |= BAD_OVERRIDE; |
aoqi@0 | 1670 | return; |
aoqi@0 | 1671 | } |
aoqi@0 | 1672 | } else if (overrideWarner.hasNonSilentLint(LintCategory.UNCHECKED)) { |
aoqi@0 | 1673 | warnUnchecked(TreeInfo.diagnosticPositionFor(m, tree), |
aoqi@0 | 1674 | "override.unchecked.ret", |
aoqi@0 | 1675 | uncheckedOverrides(m, other), |
aoqi@0 | 1676 | mtres, otres); |
aoqi@0 | 1677 | } |
aoqi@0 | 1678 | |
aoqi@0 | 1679 | // Error if overriding method throws an exception not reported |
aoqi@0 | 1680 | // by overridden method. |
aoqi@0 | 1681 | List<Type> otthrown = types.subst(ot.getThrownTypes(), otvars, mtvars); |
aoqi@0 | 1682 | List<Type> unhandledErased = unhandled(mt.getThrownTypes(), types.erasure(otthrown)); |
aoqi@0 | 1683 | List<Type> unhandledUnerased = unhandled(mt.getThrownTypes(), otthrown); |
aoqi@0 | 1684 | if (unhandledErased.nonEmpty()) { |
aoqi@0 | 1685 | log.error(TreeInfo.diagnosticPositionFor(m, tree), |
aoqi@0 | 1686 | "override.meth.doesnt.throw", |
aoqi@0 | 1687 | cannotOverride(m, other), |
aoqi@0 | 1688 | unhandledUnerased.head); |
aoqi@0 | 1689 | m.flags_field |= BAD_OVERRIDE; |
aoqi@0 | 1690 | return; |
aoqi@0 | 1691 | } |
aoqi@0 | 1692 | else if (unhandledUnerased.nonEmpty()) { |
aoqi@0 | 1693 | warnUnchecked(TreeInfo.diagnosticPositionFor(m, tree), |
aoqi@0 | 1694 | "override.unchecked.thrown", |
aoqi@0 | 1695 | cannotOverride(m, other), |
aoqi@0 | 1696 | unhandledUnerased.head); |
aoqi@0 | 1697 | return; |
aoqi@0 | 1698 | } |
aoqi@0 | 1699 | |
aoqi@0 | 1700 | // Optional warning if varargs don't agree |
aoqi@0 | 1701 | if ((((m.flags() ^ other.flags()) & Flags.VARARGS) != 0) |
aoqi@0 | 1702 | && lint.isEnabled(LintCategory.OVERRIDES)) { |
aoqi@0 | 1703 | log.warning(TreeInfo.diagnosticPositionFor(m, tree), |
aoqi@0 | 1704 | ((m.flags() & Flags.VARARGS) != 0) |
aoqi@0 | 1705 | ? "override.varargs.missing" |
aoqi@0 | 1706 | : "override.varargs.extra", |
aoqi@0 | 1707 | varargsOverrides(m, other)); |
aoqi@0 | 1708 | } |
aoqi@0 | 1709 | |
aoqi@0 | 1710 | // Warn if instance method overrides bridge method (compiler spec ??) |
aoqi@0 | 1711 | if ((other.flags() & BRIDGE) != 0) { |
aoqi@0 | 1712 | log.warning(TreeInfo.diagnosticPositionFor(m, tree), "override.bridge", |
aoqi@0 | 1713 | uncheckedOverrides(m, other)); |
aoqi@0 | 1714 | } |
aoqi@0 | 1715 | |
aoqi@0 | 1716 | // Warn if a deprecated method overridden by a non-deprecated one. |
aoqi@0 | 1717 | if (!isDeprecatedOverrideIgnorable(other, origin)) { |
jlahoda@2553 | 1718 | Lint prevLint = setLint(lint.augment(m)); |
jlahoda@2553 | 1719 | try { |
jlahoda@2553 | 1720 | checkDeprecated(TreeInfo.diagnosticPositionFor(m, tree), m, other); |
jlahoda@2553 | 1721 | } finally { |
jlahoda@2553 | 1722 | setLint(prevLint); |
jlahoda@2553 | 1723 | } |
aoqi@0 | 1724 | } |
aoqi@0 | 1725 | } |
aoqi@0 | 1726 | // where |
aoqi@0 | 1727 | private boolean isDeprecatedOverrideIgnorable(MethodSymbol m, ClassSymbol origin) { |
aoqi@0 | 1728 | // If the method, m, is defined in an interface, then ignore the issue if the method |
aoqi@0 | 1729 | // is only inherited via a supertype and also implemented in the supertype, |
aoqi@0 | 1730 | // because in that case, we will rediscover the issue when examining the method |
aoqi@0 | 1731 | // in the supertype. |
aoqi@0 | 1732 | // If the method, m, is not defined in an interface, then the only time we need to |
aoqi@0 | 1733 | // address the issue is when the method is the supertype implemementation: any other |
aoqi@0 | 1734 | // case, we will have dealt with when examining the supertype classes |
aoqi@0 | 1735 | ClassSymbol mc = m.enclClass(); |
aoqi@0 | 1736 | Type st = types.supertype(origin.type); |
aoqi@0 | 1737 | if (!st.hasTag(CLASS)) |
aoqi@0 | 1738 | return true; |
aoqi@0 | 1739 | MethodSymbol stimpl = m.implementation((ClassSymbol)st.tsym, types, false); |
aoqi@0 | 1740 | |
aoqi@0 | 1741 | if (mc != null && ((mc.flags() & INTERFACE) != 0)) { |
aoqi@0 | 1742 | List<Type> intfs = types.interfaces(origin.type); |
aoqi@0 | 1743 | return (intfs.contains(mc.type) ? false : (stimpl != null)); |
aoqi@0 | 1744 | } |
aoqi@0 | 1745 | else |
aoqi@0 | 1746 | return (stimpl != m); |
aoqi@0 | 1747 | } |
aoqi@0 | 1748 | |
aoqi@0 | 1749 | |
aoqi@0 | 1750 | // used to check if there were any unchecked conversions |
aoqi@0 | 1751 | Warner overrideWarner = new Warner(); |
aoqi@0 | 1752 | |
aoqi@0 | 1753 | /** Check that a class does not inherit two concrete methods |
aoqi@0 | 1754 | * with the same signature. |
aoqi@0 | 1755 | * @param pos Position to be used for error reporting. |
aoqi@0 | 1756 | * @param site The class type to be checked. |
aoqi@0 | 1757 | */ |
aoqi@0 | 1758 | public void checkCompatibleConcretes(DiagnosticPosition pos, Type site) { |
aoqi@0 | 1759 | Type sup = types.supertype(site); |
aoqi@0 | 1760 | if (!sup.hasTag(CLASS)) return; |
aoqi@0 | 1761 | |
aoqi@0 | 1762 | for (Type t1 = sup; |
aoqi@0 | 1763 | t1.hasTag(CLASS) && t1.tsym.type.isParameterized(); |
aoqi@0 | 1764 | t1 = types.supertype(t1)) { |
aoqi@0 | 1765 | for (Scope.Entry e1 = t1.tsym.members().elems; |
aoqi@0 | 1766 | e1 != null; |
aoqi@0 | 1767 | e1 = e1.sibling) { |
aoqi@0 | 1768 | Symbol s1 = e1.sym; |
aoqi@0 | 1769 | if (s1.kind != MTH || |
aoqi@0 | 1770 | (s1.flags() & (STATIC|SYNTHETIC|BRIDGE)) != 0 || |
aoqi@0 | 1771 | !s1.isInheritedIn(site.tsym, types) || |
aoqi@0 | 1772 | ((MethodSymbol)s1).implementation(site.tsym, |
aoqi@0 | 1773 | types, |
aoqi@0 | 1774 | true) != s1) |
aoqi@0 | 1775 | continue; |
aoqi@0 | 1776 | Type st1 = types.memberType(t1, s1); |
aoqi@0 | 1777 | int s1ArgsLength = st1.getParameterTypes().length(); |
aoqi@0 | 1778 | if (st1 == s1.type) continue; |
aoqi@0 | 1779 | |
aoqi@0 | 1780 | for (Type t2 = sup; |
aoqi@0 | 1781 | t2.hasTag(CLASS); |
aoqi@0 | 1782 | t2 = types.supertype(t2)) { |
aoqi@0 | 1783 | for (Scope.Entry e2 = t2.tsym.members().lookup(s1.name); |
aoqi@0 | 1784 | e2.scope != null; |
aoqi@0 | 1785 | e2 = e2.next()) { |
aoqi@0 | 1786 | Symbol s2 = e2.sym; |
aoqi@0 | 1787 | if (s2 == s1 || |
aoqi@0 | 1788 | s2.kind != MTH || |
aoqi@0 | 1789 | (s2.flags() & (STATIC|SYNTHETIC|BRIDGE)) != 0 || |
aoqi@0 | 1790 | s2.type.getParameterTypes().length() != s1ArgsLength || |
aoqi@0 | 1791 | !s2.isInheritedIn(site.tsym, types) || |
aoqi@0 | 1792 | ((MethodSymbol)s2).implementation(site.tsym, |
aoqi@0 | 1793 | types, |
aoqi@0 | 1794 | true) != s2) |
aoqi@0 | 1795 | continue; |
aoqi@0 | 1796 | Type st2 = types.memberType(t2, s2); |
aoqi@0 | 1797 | if (types.overrideEquivalent(st1, st2)) |
aoqi@0 | 1798 | log.error(pos, "concrete.inheritance.conflict", |
aoqi@0 | 1799 | s1, t1, s2, t2, sup); |
aoqi@0 | 1800 | } |
aoqi@0 | 1801 | } |
aoqi@0 | 1802 | } |
aoqi@0 | 1803 | } |
aoqi@0 | 1804 | } |
aoqi@0 | 1805 | |
aoqi@0 | 1806 | /** Check that classes (or interfaces) do not each define an abstract |
aoqi@0 | 1807 | * method with same name and arguments but incompatible return types. |
aoqi@0 | 1808 | * @param pos Position to be used for error reporting. |
aoqi@0 | 1809 | * @param t1 The first argument type. |
aoqi@0 | 1810 | * @param t2 The second argument type. |
aoqi@0 | 1811 | */ |
aoqi@0 | 1812 | public boolean checkCompatibleAbstracts(DiagnosticPosition pos, |
aoqi@0 | 1813 | Type t1, |
aoqi@0 | 1814 | Type t2) { |
aoqi@0 | 1815 | return checkCompatibleAbstracts(pos, t1, t2, |
aoqi@0 | 1816 | types.makeCompoundType(t1, t2)); |
aoqi@0 | 1817 | } |
aoqi@0 | 1818 | |
aoqi@0 | 1819 | public boolean checkCompatibleAbstracts(DiagnosticPosition pos, |
aoqi@0 | 1820 | Type t1, |
aoqi@0 | 1821 | Type t2, |
aoqi@0 | 1822 | Type site) { |
aoqi@0 | 1823 | if ((site.tsym.flags() & COMPOUND) != 0) { |
aoqi@0 | 1824 | // special case for intersections: need to eliminate wildcards in supertypes |
aoqi@0 | 1825 | t1 = types.capture(t1); |
aoqi@0 | 1826 | t2 = types.capture(t2); |
aoqi@0 | 1827 | } |
aoqi@0 | 1828 | return firstIncompatibility(pos, t1, t2, site) == null; |
aoqi@0 | 1829 | } |
aoqi@0 | 1830 | |
aoqi@0 | 1831 | /** Return the first method which is defined with same args |
aoqi@0 | 1832 | * but different return types in two given interfaces, or null if none |
aoqi@0 | 1833 | * exists. |
aoqi@0 | 1834 | * @param t1 The first type. |
aoqi@0 | 1835 | * @param t2 The second type. |
aoqi@0 | 1836 | * @param site The most derived type. |
aoqi@0 | 1837 | * @returns symbol from t2 that conflicts with one in t1. |
aoqi@0 | 1838 | */ |
aoqi@0 | 1839 | private Symbol firstIncompatibility(DiagnosticPosition pos, Type t1, Type t2, Type site) { |
aoqi@0 | 1840 | Map<TypeSymbol,Type> interfaces1 = new HashMap<TypeSymbol,Type>(); |
aoqi@0 | 1841 | closure(t1, interfaces1); |
aoqi@0 | 1842 | Map<TypeSymbol,Type> interfaces2; |
aoqi@0 | 1843 | if (t1 == t2) |
aoqi@0 | 1844 | interfaces2 = interfaces1; |
aoqi@0 | 1845 | else |
aoqi@0 | 1846 | closure(t2, interfaces1, interfaces2 = new HashMap<TypeSymbol,Type>()); |
aoqi@0 | 1847 | |
aoqi@0 | 1848 | for (Type t3 : interfaces1.values()) { |
aoqi@0 | 1849 | for (Type t4 : interfaces2.values()) { |
aoqi@0 | 1850 | Symbol s = firstDirectIncompatibility(pos, t3, t4, site); |
aoqi@0 | 1851 | if (s != null) return s; |
aoqi@0 | 1852 | } |
aoqi@0 | 1853 | } |
aoqi@0 | 1854 | return null; |
aoqi@0 | 1855 | } |
aoqi@0 | 1856 | |
aoqi@0 | 1857 | /** Compute all the supertypes of t, indexed by type symbol. */ |
aoqi@0 | 1858 | private void closure(Type t, Map<TypeSymbol,Type> typeMap) { |
aoqi@0 | 1859 | if (!t.hasTag(CLASS)) return; |
aoqi@0 | 1860 | if (typeMap.put(t.tsym, t) == null) { |
aoqi@0 | 1861 | closure(types.supertype(t), typeMap); |
aoqi@0 | 1862 | for (Type i : types.interfaces(t)) |
aoqi@0 | 1863 | closure(i, typeMap); |
aoqi@0 | 1864 | } |
aoqi@0 | 1865 | } |
aoqi@0 | 1866 | |
aoqi@0 | 1867 | /** Compute all the supertypes of t, indexed by type symbol (except thise in typesSkip). */ |
aoqi@0 | 1868 | private void closure(Type t, Map<TypeSymbol,Type> typesSkip, Map<TypeSymbol,Type> typeMap) { |
aoqi@0 | 1869 | if (!t.hasTag(CLASS)) return; |
aoqi@0 | 1870 | if (typesSkip.get(t.tsym) != null) return; |
aoqi@0 | 1871 | if (typeMap.put(t.tsym, t) == null) { |
aoqi@0 | 1872 | closure(types.supertype(t), typesSkip, typeMap); |
aoqi@0 | 1873 | for (Type i : types.interfaces(t)) |
aoqi@0 | 1874 | closure(i, typesSkip, typeMap); |
aoqi@0 | 1875 | } |
aoqi@0 | 1876 | } |
aoqi@0 | 1877 | |
aoqi@0 | 1878 | /** Return the first method in t2 that conflicts with a method from t1. */ |
aoqi@0 | 1879 | private Symbol firstDirectIncompatibility(DiagnosticPosition pos, Type t1, Type t2, Type site) { |
aoqi@0 | 1880 | for (Scope.Entry e1 = t1.tsym.members().elems; e1 != null; e1 = e1.sibling) { |
aoqi@0 | 1881 | Symbol s1 = e1.sym; |
aoqi@0 | 1882 | Type st1 = null; |
aoqi@0 | 1883 | if (s1.kind != MTH || !s1.isInheritedIn(site.tsym, types) || |
aoqi@0 | 1884 | (s1.flags() & SYNTHETIC) != 0) continue; |
aoqi@0 | 1885 | Symbol impl = ((MethodSymbol)s1).implementation(site.tsym, types, false); |
aoqi@0 | 1886 | if (impl != null && (impl.flags() & ABSTRACT) == 0) continue; |
aoqi@0 | 1887 | for (Scope.Entry e2 = t2.tsym.members().lookup(s1.name); e2.scope != null; e2 = e2.next()) { |
aoqi@0 | 1888 | Symbol s2 = e2.sym; |
aoqi@0 | 1889 | if (s1 == s2) continue; |
aoqi@0 | 1890 | if (s2.kind != MTH || !s2.isInheritedIn(site.tsym, types) || |
aoqi@0 | 1891 | (s2.flags() & SYNTHETIC) != 0) continue; |
aoqi@0 | 1892 | if (st1 == null) st1 = types.memberType(t1, s1); |
aoqi@0 | 1893 | Type st2 = types.memberType(t2, s2); |
aoqi@0 | 1894 | if (types.overrideEquivalent(st1, st2)) { |
aoqi@0 | 1895 | List<Type> tvars1 = st1.getTypeArguments(); |
aoqi@0 | 1896 | List<Type> tvars2 = st2.getTypeArguments(); |
aoqi@0 | 1897 | Type rt1 = st1.getReturnType(); |
aoqi@0 | 1898 | Type rt2 = types.subst(st2.getReturnType(), tvars2, tvars1); |
aoqi@0 | 1899 | boolean compat = |
aoqi@0 | 1900 | types.isSameType(rt1, rt2) || |
aoqi@0 | 1901 | !rt1.isPrimitiveOrVoid() && |
aoqi@0 | 1902 | !rt2.isPrimitiveOrVoid() && |
aoqi@0 | 1903 | (types.covariantReturnType(rt1, rt2, types.noWarnings) || |
aoqi@0 | 1904 | types.covariantReturnType(rt2, rt1, types.noWarnings)) || |
aoqi@0 | 1905 | checkCommonOverriderIn(s1,s2,site); |
aoqi@0 | 1906 | if (!compat) { |
aoqi@0 | 1907 | log.error(pos, "types.incompatible.diff.ret", |
aoqi@0 | 1908 | t1, t2, s2.name + |
aoqi@0 | 1909 | "(" + types.memberType(t2, s2).getParameterTypes() + ")"); |
aoqi@0 | 1910 | return s2; |
aoqi@0 | 1911 | } |
aoqi@0 | 1912 | } else if (checkNameClash((ClassSymbol)site.tsym, s1, s2) && |
aoqi@0 | 1913 | !checkCommonOverriderIn(s1, s2, site)) { |
aoqi@0 | 1914 | log.error(pos, |
aoqi@0 | 1915 | "name.clash.same.erasure.no.override", |
aoqi@0 | 1916 | s1, s1.location(), |
aoqi@0 | 1917 | s2, s2.location()); |
aoqi@0 | 1918 | return s2; |
aoqi@0 | 1919 | } |
aoqi@0 | 1920 | } |
aoqi@0 | 1921 | } |
aoqi@0 | 1922 | return null; |
aoqi@0 | 1923 | } |
aoqi@0 | 1924 | //WHERE |
aoqi@0 | 1925 | boolean checkCommonOverriderIn(Symbol s1, Symbol s2, Type site) { |
aoqi@0 | 1926 | Map<TypeSymbol,Type> supertypes = new HashMap<TypeSymbol,Type>(); |
aoqi@0 | 1927 | Type st1 = types.memberType(site, s1); |
aoqi@0 | 1928 | Type st2 = types.memberType(site, s2); |
aoqi@0 | 1929 | closure(site, supertypes); |
aoqi@0 | 1930 | for (Type t : supertypes.values()) { |
aoqi@0 | 1931 | for (Scope.Entry e = t.tsym.members().lookup(s1.name); e.scope != null; e = e.next()) { |
aoqi@0 | 1932 | Symbol s3 = e.sym; |
aoqi@0 | 1933 | if (s3 == s1 || s3 == s2 || s3.kind != MTH || (s3.flags() & (BRIDGE|SYNTHETIC)) != 0) continue; |
aoqi@0 | 1934 | Type st3 = types.memberType(site,s3); |
aoqi@0 | 1935 | if (types.overrideEquivalent(st3, st1) && |
aoqi@0 | 1936 | types.overrideEquivalent(st3, st2) && |
aoqi@0 | 1937 | types.returnTypeSubstitutable(st3, st1) && |
aoqi@0 | 1938 | types.returnTypeSubstitutable(st3, st2)) { |
aoqi@0 | 1939 | return true; |
aoqi@0 | 1940 | } |
aoqi@0 | 1941 | } |
aoqi@0 | 1942 | } |
aoqi@0 | 1943 | return false; |
aoqi@0 | 1944 | } |
aoqi@0 | 1945 | |
aoqi@0 | 1946 | /** Check that a given method conforms with any method it overrides. |
aoqi@0 | 1947 | * @param tree The tree from which positions are extracted |
aoqi@0 | 1948 | * for errors. |
aoqi@0 | 1949 | * @param m The overriding method. |
aoqi@0 | 1950 | */ |
aoqi@0 | 1951 | void checkOverride(JCMethodDecl tree, MethodSymbol m) { |
aoqi@0 | 1952 | ClassSymbol origin = (ClassSymbol)m.owner; |
aoqi@0 | 1953 | if ((origin.flags() & ENUM) != 0 && names.finalize.equals(m.name)) |
aoqi@0 | 1954 | if (m.overrides(syms.enumFinalFinalize, origin, types, false)) { |
aoqi@0 | 1955 | log.error(tree.pos(), "enum.no.finalize"); |
aoqi@0 | 1956 | return; |
aoqi@0 | 1957 | } |
aoqi@0 | 1958 | for (Type t = origin.type; t.hasTag(CLASS); |
aoqi@0 | 1959 | t = types.supertype(t)) { |
aoqi@0 | 1960 | if (t != origin.type) { |
aoqi@0 | 1961 | checkOverride(tree, t, origin, m); |
aoqi@0 | 1962 | } |
aoqi@0 | 1963 | for (Type t2 : types.interfaces(t)) { |
aoqi@0 | 1964 | checkOverride(tree, t2, origin, m); |
aoqi@0 | 1965 | } |
aoqi@0 | 1966 | } |
aoqi@0 | 1967 | |
aoqi@0 | 1968 | if (m.attribute(syms.overrideType.tsym) != null && !isOverrider(m)) { |
aoqi@0 | 1969 | DiagnosticPosition pos = tree.pos(); |
aoqi@0 | 1970 | for (JCAnnotation a : tree.getModifiers().annotations) { |
aoqi@0 | 1971 | if (a.annotationType.type.tsym == syms.overrideType.tsym) { |
aoqi@0 | 1972 | pos = a.pos(); |
aoqi@0 | 1973 | break; |
aoqi@0 | 1974 | } |
aoqi@0 | 1975 | } |
aoqi@0 | 1976 | log.error(pos, "method.does.not.override.superclass"); |
aoqi@0 | 1977 | } |
aoqi@0 | 1978 | } |
aoqi@0 | 1979 | |
aoqi@0 | 1980 | void checkOverride(JCTree tree, Type site, ClassSymbol origin, MethodSymbol m) { |
aoqi@0 | 1981 | TypeSymbol c = site.tsym; |
aoqi@0 | 1982 | Scope.Entry e = c.members().lookup(m.name); |
aoqi@0 | 1983 | while (e.scope != null) { |
aoqi@0 | 1984 | if (m.overrides(e.sym, origin, types, false)) { |
aoqi@0 | 1985 | if ((e.sym.flags() & ABSTRACT) == 0) { |
aoqi@0 | 1986 | checkOverride(tree, m, (MethodSymbol)e.sym, origin); |
aoqi@0 | 1987 | } |
aoqi@0 | 1988 | } |
aoqi@0 | 1989 | e = e.next(); |
aoqi@0 | 1990 | } |
aoqi@0 | 1991 | } |
aoqi@0 | 1992 | |
aoqi@0 | 1993 | private Filter<Symbol> equalsHasCodeFilter = new Filter<Symbol>() { |
aoqi@0 | 1994 | public boolean accepts(Symbol s) { |
aoqi@0 | 1995 | return MethodSymbol.implementation_filter.accepts(s) && |
aoqi@0 | 1996 | (s.flags() & BAD_OVERRIDE) == 0; |
aoqi@0 | 1997 | |
aoqi@0 | 1998 | } |
aoqi@0 | 1999 | }; |
aoqi@0 | 2000 | |
aoqi@0 | 2001 | public void checkClassOverrideEqualsAndHashIfNeeded(DiagnosticPosition pos, |
aoqi@0 | 2002 | ClassSymbol someClass) { |
aoqi@0 | 2003 | /* At present, annotations cannot possibly have a method that is override |
aoqi@0 | 2004 | * equivalent with Object.equals(Object) but in any case the condition is |
aoqi@0 | 2005 | * fine for completeness. |
aoqi@0 | 2006 | */ |
aoqi@0 | 2007 | if (someClass == (ClassSymbol)syms.objectType.tsym || |
aoqi@0 | 2008 | someClass.isInterface() || someClass.isEnum() || |
aoqi@0 | 2009 | (someClass.flags() & ANNOTATION) != 0 || |
aoqi@0 | 2010 | (someClass.flags() & ABSTRACT) != 0) return; |
aoqi@0 | 2011 | //anonymous inner classes implementing interfaces need especial treatment |
aoqi@0 | 2012 | if (someClass.isAnonymous()) { |
aoqi@0 | 2013 | List<Type> interfaces = types.interfaces(someClass.type); |
aoqi@0 | 2014 | if (interfaces != null && !interfaces.isEmpty() && |
aoqi@0 | 2015 | interfaces.head.tsym == syms.comparatorType.tsym) return; |
aoqi@0 | 2016 | } |
aoqi@0 | 2017 | checkClassOverrideEqualsAndHash(pos, someClass); |
aoqi@0 | 2018 | } |
aoqi@0 | 2019 | |
aoqi@0 | 2020 | private void checkClassOverrideEqualsAndHash(DiagnosticPosition pos, |
aoqi@0 | 2021 | ClassSymbol someClass) { |
aoqi@0 | 2022 | if (lint.isEnabled(LintCategory.OVERRIDES)) { |
aoqi@0 | 2023 | MethodSymbol equalsAtObject = (MethodSymbol)syms.objectType |
aoqi@0 | 2024 | .tsym.members().lookup(names.equals).sym; |
aoqi@0 | 2025 | MethodSymbol hashCodeAtObject = (MethodSymbol)syms.objectType |
aoqi@0 | 2026 | .tsym.members().lookup(names.hashCode).sym; |
aoqi@0 | 2027 | boolean overridesEquals = types.implementation(equalsAtObject, |
aoqi@0 | 2028 | someClass, false, equalsHasCodeFilter).owner == someClass; |
aoqi@0 | 2029 | boolean overridesHashCode = types.implementation(hashCodeAtObject, |
aoqi@0 | 2030 | someClass, false, equalsHasCodeFilter) != hashCodeAtObject; |
aoqi@0 | 2031 | |
aoqi@0 | 2032 | if (overridesEquals && !overridesHashCode) { |
aoqi@0 | 2033 | log.warning(LintCategory.OVERRIDES, pos, |
aoqi@0 | 2034 | "override.equals.but.not.hashcode", someClass); |
aoqi@0 | 2035 | } |
aoqi@0 | 2036 | } |
aoqi@0 | 2037 | } |
aoqi@0 | 2038 | |
aoqi@0 | 2039 | private boolean checkNameClash(ClassSymbol origin, Symbol s1, Symbol s2) { |
aoqi@0 | 2040 | ClashFilter cf = new ClashFilter(origin.type); |
aoqi@0 | 2041 | return (cf.accepts(s1) && |
aoqi@0 | 2042 | cf.accepts(s2) && |
aoqi@0 | 2043 | types.hasSameArgs(s1.erasure(types), s2.erasure(types))); |
aoqi@0 | 2044 | } |
aoqi@0 | 2045 | |
aoqi@0 | 2046 | |
aoqi@0 | 2047 | /** Check that all abstract members of given class have definitions. |
aoqi@0 | 2048 | * @param pos Position to be used for error reporting. |
aoqi@0 | 2049 | * @param c The class. |
aoqi@0 | 2050 | */ |
aoqi@0 | 2051 | void checkAllDefined(DiagnosticPosition pos, ClassSymbol c) { |
jlahoda@2717 | 2052 | MethodSymbol undef = types.firstUnimplementedAbstract(c); |
jlahoda@2717 | 2053 | if (undef != null) { |
jlahoda@2717 | 2054 | MethodSymbol undef1 = |
jlahoda@2717 | 2055 | new MethodSymbol(undef.flags(), undef.name, |
jlahoda@2717 | 2056 | types.memberType(c.type, undef), undef.owner); |
jlahoda@2717 | 2057 | log.error(pos, "does.not.override.abstract", |
jlahoda@2717 | 2058 | c, undef1, undef1.location()); |
aoqi@0 | 2059 | } |
aoqi@0 | 2060 | } |
aoqi@0 | 2061 | |
aoqi@0 | 2062 | void checkNonCyclicDecl(JCClassDecl tree) { |
aoqi@0 | 2063 | CycleChecker cc = new CycleChecker(); |
aoqi@0 | 2064 | cc.scan(tree); |
aoqi@0 | 2065 | if (!cc.errorFound && !cc.partialCheck) { |
aoqi@0 | 2066 | tree.sym.flags_field |= ACYCLIC; |
aoqi@0 | 2067 | } |
aoqi@0 | 2068 | } |
aoqi@0 | 2069 | |
aoqi@0 | 2070 | class CycleChecker extends TreeScanner { |
aoqi@0 | 2071 | |
aoqi@0 | 2072 | List<Symbol> seenClasses = List.nil(); |
aoqi@0 | 2073 | boolean errorFound = false; |
aoqi@0 | 2074 | boolean partialCheck = false; |
aoqi@0 | 2075 | |
aoqi@0 | 2076 | private void checkSymbol(DiagnosticPosition pos, Symbol sym) { |
aoqi@0 | 2077 | if (sym != null && sym.kind == TYP) { |
aoqi@0 | 2078 | Env<AttrContext> classEnv = enter.getEnv((TypeSymbol)sym); |
aoqi@0 | 2079 | if (classEnv != null) { |
aoqi@0 | 2080 | DiagnosticSource prevSource = log.currentSource(); |
aoqi@0 | 2081 | try { |
aoqi@0 | 2082 | log.useSource(classEnv.toplevel.sourcefile); |
aoqi@0 | 2083 | scan(classEnv.tree); |
aoqi@0 | 2084 | } |
aoqi@0 | 2085 | finally { |
aoqi@0 | 2086 | log.useSource(prevSource.getFile()); |
aoqi@0 | 2087 | } |
aoqi@0 | 2088 | } else if (sym.kind == TYP) { |
aoqi@0 | 2089 | checkClass(pos, sym, List.<JCTree>nil()); |
aoqi@0 | 2090 | } |
aoqi@0 | 2091 | } else { |
aoqi@0 | 2092 | //not completed yet |
aoqi@0 | 2093 | partialCheck = true; |
aoqi@0 | 2094 | } |
aoqi@0 | 2095 | } |
aoqi@0 | 2096 | |
aoqi@0 | 2097 | @Override |
aoqi@0 | 2098 | public void visitSelect(JCFieldAccess tree) { |
aoqi@0 | 2099 | super.visitSelect(tree); |
aoqi@0 | 2100 | checkSymbol(tree.pos(), tree.sym); |
aoqi@0 | 2101 | } |
aoqi@0 | 2102 | |
aoqi@0 | 2103 | @Override |
aoqi@0 | 2104 | public void visitIdent(JCIdent tree) { |
aoqi@0 | 2105 | checkSymbol(tree.pos(), tree.sym); |
aoqi@0 | 2106 | } |
aoqi@0 | 2107 | |
aoqi@0 | 2108 | @Override |
aoqi@0 | 2109 | public void visitTypeApply(JCTypeApply tree) { |
aoqi@0 | 2110 | scan(tree.clazz); |
aoqi@0 | 2111 | } |
aoqi@0 | 2112 | |
aoqi@0 | 2113 | @Override |
aoqi@0 | 2114 | public void visitTypeArray(JCArrayTypeTree tree) { |
aoqi@0 | 2115 | scan(tree.elemtype); |
aoqi@0 | 2116 | } |
aoqi@0 | 2117 | |
aoqi@0 | 2118 | @Override |
aoqi@0 | 2119 | public void visitClassDef(JCClassDecl tree) { |
aoqi@0 | 2120 | List<JCTree> supertypes = List.nil(); |
aoqi@0 | 2121 | if (tree.getExtendsClause() != null) { |
aoqi@0 | 2122 | supertypes = supertypes.prepend(tree.getExtendsClause()); |
aoqi@0 | 2123 | } |
aoqi@0 | 2124 | if (tree.getImplementsClause() != null) { |
aoqi@0 | 2125 | for (JCTree intf : tree.getImplementsClause()) { |
aoqi@0 | 2126 | supertypes = supertypes.prepend(intf); |
aoqi@0 | 2127 | } |
aoqi@0 | 2128 | } |
aoqi@0 | 2129 | checkClass(tree.pos(), tree.sym, supertypes); |
aoqi@0 | 2130 | } |
aoqi@0 | 2131 | |
aoqi@0 | 2132 | void checkClass(DiagnosticPosition pos, Symbol c, List<JCTree> supertypes) { |
aoqi@0 | 2133 | if ((c.flags_field & ACYCLIC) != 0) |
aoqi@0 | 2134 | return; |
aoqi@0 | 2135 | if (seenClasses.contains(c)) { |
aoqi@0 | 2136 | errorFound = true; |
aoqi@0 | 2137 | noteCyclic(pos, (ClassSymbol)c); |
aoqi@0 | 2138 | } else if (!c.type.isErroneous()) { |
aoqi@0 | 2139 | try { |
aoqi@0 | 2140 | seenClasses = seenClasses.prepend(c); |
aoqi@0 | 2141 | if (c.type.hasTag(CLASS)) { |
aoqi@0 | 2142 | if (supertypes.nonEmpty()) { |
aoqi@0 | 2143 | scan(supertypes); |
aoqi@0 | 2144 | } |
aoqi@0 | 2145 | else { |
aoqi@0 | 2146 | ClassType ct = (ClassType)c.type; |
aoqi@0 | 2147 | if (ct.supertype_field == null || |
aoqi@0 | 2148 | ct.interfaces_field == null) { |
aoqi@0 | 2149 | //not completed yet |
aoqi@0 | 2150 | partialCheck = true; |
aoqi@0 | 2151 | return; |
aoqi@0 | 2152 | } |
aoqi@0 | 2153 | checkSymbol(pos, ct.supertype_field.tsym); |
aoqi@0 | 2154 | for (Type intf : ct.interfaces_field) { |
aoqi@0 | 2155 | checkSymbol(pos, intf.tsym); |
aoqi@0 | 2156 | } |
aoqi@0 | 2157 | } |
aoqi@0 | 2158 | if (c.owner.kind == TYP) { |
aoqi@0 | 2159 | checkSymbol(pos, c.owner); |
aoqi@0 | 2160 | } |
aoqi@0 | 2161 | } |
aoqi@0 | 2162 | } finally { |
aoqi@0 | 2163 | seenClasses = seenClasses.tail; |
aoqi@0 | 2164 | } |
aoqi@0 | 2165 | } |
aoqi@0 | 2166 | } |
aoqi@0 | 2167 | } |
aoqi@0 | 2168 | |
aoqi@0 | 2169 | /** Check for cyclic references. Issue an error if the |
aoqi@0 | 2170 | * symbol of the type referred to has a LOCKED flag set. |
aoqi@0 | 2171 | * |
aoqi@0 | 2172 | * @param pos Position to be used for error reporting. |
aoqi@0 | 2173 | * @param t The type referred to. |
aoqi@0 | 2174 | */ |
aoqi@0 | 2175 | void checkNonCyclic(DiagnosticPosition pos, Type t) { |
aoqi@0 | 2176 | checkNonCyclicInternal(pos, t); |
aoqi@0 | 2177 | } |
aoqi@0 | 2178 | |
aoqi@0 | 2179 | |
aoqi@0 | 2180 | void checkNonCyclic(DiagnosticPosition pos, TypeVar t) { |
aoqi@0 | 2181 | checkNonCyclic1(pos, t, List.<TypeVar>nil()); |
aoqi@0 | 2182 | } |
aoqi@0 | 2183 | |
aoqi@0 | 2184 | private void checkNonCyclic1(DiagnosticPosition pos, Type t, List<TypeVar> seen) { |
aoqi@0 | 2185 | final TypeVar tv; |
aoqi@0 | 2186 | if (t.hasTag(TYPEVAR) && (t.tsym.flags() & UNATTRIBUTED) != 0) |
aoqi@0 | 2187 | return; |
aoqi@0 | 2188 | if (seen.contains(t)) { |
aoqi@0 | 2189 | tv = (TypeVar)t.unannotatedType(); |
aoqi@0 | 2190 | tv.bound = types.createErrorType(t); |
aoqi@0 | 2191 | log.error(pos, "cyclic.inheritance", t); |
aoqi@0 | 2192 | } else if (t.hasTag(TYPEVAR)) { |
aoqi@0 | 2193 | tv = (TypeVar)t.unannotatedType(); |
aoqi@0 | 2194 | seen = seen.prepend(tv); |
aoqi@0 | 2195 | for (Type b : types.getBounds(tv)) |
aoqi@0 | 2196 | checkNonCyclic1(pos, b, seen); |
aoqi@0 | 2197 | } |
aoqi@0 | 2198 | } |
aoqi@0 | 2199 | |
aoqi@0 | 2200 | /** Check for cyclic references. Issue an error if the |
aoqi@0 | 2201 | * symbol of the type referred to has a LOCKED flag set. |
aoqi@0 | 2202 | * |
aoqi@0 | 2203 | * @param pos Position to be used for error reporting. |
aoqi@0 | 2204 | * @param t The type referred to. |
aoqi@0 | 2205 | * @returns True if the check completed on all attributed classes |
aoqi@0 | 2206 | */ |
aoqi@0 | 2207 | private boolean checkNonCyclicInternal(DiagnosticPosition pos, Type t) { |
aoqi@0 | 2208 | boolean complete = true; // was the check complete? |
aoqi@0 | 2209 | //- System.err.println("checkNonCyclicInternal("+t+");");//DEBUG |
aoqi@0 | 2210 | Symbol c = t.tsym; |
aoqi@0 | 2211 | if ((c.flags_field & ACYCLIC) != 0) return true; |
aoqi@0 | 2212 | |
aoqi@0 | 2213 | if ((c.flags_field & LOCKED) != 0) { |
aoqi@0 | 2214 | noteCyclic(pos, (ClassSymbol)c); |
aoqi@0 | 2215 | } else if (!c.type.isErroneous()) { |
aoqi@0 | 2216 | try { |
aoqi@0 | 2217 | c.flags_field |= LOCKED; |
aoqi@0 | 2218 | if (c.type.hasTag(CLASS)) { |
aoqi@0 | 2219 | ClassType clazz = (ClassType)c.type; |
aoqi@0 | 2220 | if (clazz.interfaces_field != null) |
aoqi@0 | 2221 | for (List<Type> l=clazz.interfaces_field; l.nonEmpty(); l=l.tail) |
aoqi@0 | 2222 | complete &= checkNonCyclicInternal(pos, l.head); |
aoqi@0 | 2223 | if (clazz.supertype_field != null) { |
aoqi@0 | 2224 | Type st = clazz.supertype_field; |
aoqi@0 | 2225 | if (st != null && st.hasTag(CLASS)) |
aoqi@0 | 2226 | complete &= checkNonCyclicInternal(pos, st); |
aoqi@0 | 2227 | } |
aoqi@0 | 2228 | if (c.owner.kind == TYP) |
aoqi@0 | 2229 | complete &= checkNonCyclicInternal(pos, c.owner.type); |
aoqi@0 | 2230 | } |
aoqi@0 | 2231 | } finally { |
aoqi@0 | 2232 | c.flags_field &= ~LOCKED; |
aoqi@0 | 2233 | } |
aoqi@0 | 2234 | } |
aoqi@0 | 2235 | if (complete) |
aoqi@0 | 2236 | complete = ((c.flags_field & UNATTRIBUTED) == 0) && c.completer == null; |
aoqi@0 | 2237 | if (complete) c.flags_field |= ACYCLIC; |
aoqi@0 | 2238 | return complete; |
aoqi@0 | 2239 | } |
aoqi@0 | 2240 | |
aoqi@0 | 2241 | /** Note that we found an inheritance cycle. */ |
aoqi@0 | 2242 | private void noteCyclic(DiagnosticPosition pos, ClassSymbol c) { |
aoqi@0 | 2243 | log.error(pos, "cyclic.inheritance", c); |
aoqi@0 | 2244 | for (List<Type> l=types.interfaces(c.type); l.nonEmpty(); l=l.tail) |
aoqi@0 | 2245 | l.head = types.createErrorType((ClassSymbol)l.head.tsym, Type.noType); |
aoqi@0 | 2246 | Type st = types.supertype(c.type); |
aoqi@0 | 2247 | if (st.hasTag(CLASS)) |
aoqi@0 | 2248 | ((ClassType)c.type).supertype_field = types.createErrorType((ClassSymbol)st.tsym, Type.noType); |
aoqi@0 | 2249 | c.type = types.createErrorType(c, c.type); |
aoqi@0 | 2250 | c.flags_field |= ACYCLIC; |
aoqi@0 | 2251 | } |
aoqi@0 | 2252 | |
aoqi@0 | 2253 | /** Check that all methods which implement some |
aoqi@0 | 2254 | * method conform to the method they implement. |
aoqi@0 | 2255 | * @param tree The class definition whose members are checked. |
aoqi@0 | 2256 | */ |
aoqi@0 | 2257 | void checkImplementations(JCClassDecl tree) { |
aoqi@0 | 2258 | checkImplementations(tree, tree.sym, tree.sym); |
aoqi@0 | 2259 | } |
aoqi@0 | 2260 | //where |
aoqi@0 | 2261 | /** Check that all methods which implement some |
aoqi@0 | 2262 | * method in `ic' conform to the method they implement. |
aoqi@0 | 2263 | */ |
aoqi@0 | 2264 | void checkImplementations(JCTree tree, ClassSymbol origin, ClassSymbol ic) { |
aoqi@0 | 2265 | for (List<Type> l = types.closure(ic.type); l.nonEmpty(); l = l.tail) { |
aoqi@0 | 2266 | ClassSymbol lc = (ClassSymbol)l.head.tsym; |
aoqi@0 | 2267 | if ((allowGenerics || origin != lc) && (lc.flags() & ABSTRACT) != 0) { |
aoqi@0 | 2268 | for (Scope.Entry e=lc.members().elems; e != null; e=e.sibling) { |
aoqi@0 | 2269 | if (e.sym.kind == MTH && |
aoqi@0 | 2270 | (e.sym.flags() & (STATIC|ABSTRACT)) == ABSTRACT) { |
aoqi@0 | 2271 | MethodSymbol absmeth = (MethodSymbol)e.sym; |
aoqi@0 | 2272 | MethodSymbol implmeth = absmeth.implementation(origin, types, false); |
aoqi@0 | 2273 | if (implmeth != null && implmeth != absmeth && |
aoqi@0 | 2274 | (implmeth.owner.flags() & INTERFACE) == |
aoqi@0 | 2275 | (origin.flags() & INTERFACE)) { |
aoqi@0 | 2276 | // don't check if implmeth is in a class, yet |
aoqi@0 | 2277 | // origin is an interface. This case arises only |
aoqi@0 | 2278 | // if implmeth is declared in Object. The reason is |
aoqi@0 | 2279 | // that interfaces really don't inherit from |
aoqi@0 | 2280 | // Object it's just that the compiler represents |
aoqi@0 | 2281 | // things that way. |
aoqi@0 | 2282 | checkOverride(tree, implmeth, absmeth, origin); |
aoqi@0 | 2283 | } |
aoqi@0 | 2284 | } |
aoqi@0 | 2285 | } |
aoqi@0 | 2286 | } |
aoqi@0 | 2287 | } |
aoqi@0 | 2288 | } |
aoqi@0 | 2289 | |
aoqi@0 | 2290 | /** Check that all abstract methods implemented by a class are |
aoqi@0 | 2291 | * mutually compatible. |
aoqi@0 | 2292 | * @param pos Position to be used for error reporting. |
aoqi@0 | 2293 | * @param c The class whose interfaces are checked. |
aoqi@0 | 2294 | */ |
aoqi@0 | 2295 | void checkCompatibleSupertypes(DiagnosticPosition pos, Type c) { |
aoqi@0 | 2296 | List<Type> supertypes = types.interfaces(c); |
aoqi@0 | 2297 | Type supertype = types.supertype(c); |
aoqi@0 | 2298 | if (supertype.hasTag(CLASS) && |
aoqi@0 | 2299 | (supertype.tsym.flags() & ABSTRACT) != 0) |
aoqi@0 | 2300 | supertypes = supertypes.prepend(supertype); |
aoqi@0 | 2301 | for (List<Type> l = supertypes; l.nonEmpty(); l = l.tail) { |
aoqi@0 | 2302 | if (allowGenerics && !l.head.getTypeArguments().isEmpty() && |
aoqi@0 | 2303 | !checkCompatibleAbstracts(pos, l.head, l.head, c)) |
aoqi@0 | 2304 | return; |
aoqi@0 | 2305 | for (List<Type> m = supertypes; m != l; m = m.tail) |
aoqi@0 | 2306 | if (!checkCompatibleAbstracts(pos, l.head, m.head, c)) |
aoqi@0 | 2307 | return; |
aoqi@0 | 2308 | } |
aoqi@0 | 2309 | checkCompatibleConcretes(pos, c); |
aoqi@0 | 2310 | } |
aoqi@0 | 2311 | |
aoqi@0 | 2312 | void checkConflicts(DiagnosticPosition pos, Symbol sym, TypeSymbol c) { |
aoqi@0 | 2313 | for (Type ct = c.type; ct != Type.noType ; ct = types.supertype(ct)) { |
aoqi@0 | 2314 | for (Scope.Entry e = ct.tsym.members().lookup(sym.name); e.scope == ct.tsym.members(); e = e.next()) { |
aoqi@0 | 2315 | // VM allows methods and variables with differing types |
aoqi@0 | 2316 | if (sym.kind == e.sym.kind && |
aoqi@0 | 2317 | types.isSameType(types.erasure(sym.type), types.erasure(e.sym.type)) && |
aoqi@0 | 2318 | sym != e.sym && |
aoqi@0 | 2319 | (sym.flags() & Flags.SYNTHETIC) != (e.sym.flags() & Flags.SYNTHETIC) && |
aoqi@0 | 2320 | (sym.flags() & IPROXY) == 0 && (e.sym.flags() & IPROXY) == 0 && |
aoqi@0 | 2321 | (sym.flags() & BRIDGE) == 0 && (e.sym.flags() & BRIDGE) == 0) { |
aoqi@0 | 2322 | syntheticError(pos, (e.sym.flags() & SYNTHETIC) == 0 ? e.sym : sym); |
aoqi@0 | 2323 | return; |
aoqi@0 | 2324 | } |
aoqi@0 | 2325 | } |
aoqi@0 | 2326 | } |
aoqi@0 | 2327 | } |
aoqi@0 | 2328 | |
aoqi@0 | 2329 | /** Check that all non-override equivalent methods accessible from 'site' |
aoqi@0 | 2330 | * are mutually compatible (JLS 8.4.8/9.4.1). |
aoqi@0 | 2331 | * |
aoqi@0 | 2332 | * @param pos Position to be used for error reporting. |
aoqi@0 | 2333 | * @param site The class whose methods are checked. |
aoqi@0 | 2334 | * @param sym The method symbol to be checked. |
aoqi@0 | 2335 | */ |
aoqi@0 | 2336 | void checkOverrideClashes(DiagnosticPosition pos, Type site, MethodSymbol sym) { |
aoqi@0 | 2337 | ClashFilter cf = new ClashFilter(site); |
aoqi@0 | 2338 | //for each method m1 that is overridden (directly or indirectly) |
aoqi@0 | 2339 | //by method 'sym' in 'site'... |
aoqi@0 | 2340 | |
aoqi@0 | 2341 | List<MethodSymbol> potentiallyAmbiguousList = List.nil(); |
aoqi@0 | 2342 | boolean overridesAny = false; |
aoqi@0 | 2343 | for (Symbol m1 : types.membersClosure(site, false).getElementsByName(sym.name, cf)) { |
aoqi@0 | 2344 | if (!sym.overrides(m1, site.tsym, types, false)) { |
aoqi@0 | 2345 | if (m1 == sym) { |
aoqi@0 | 2346 | continue; |
aoqi@0 | 2347 | } |
aoqi@0 | 2348 | |
aoqi@0 | 2349 | if (!overridesAny) { |
aoqi@0 | 2350 | potentiallyAmbiguousList = potentiallyAmbiguousList.prepend((MethodSymbol)m1); |
aoqi@0 | 2351 | } |
aoqi@0 | 2352 | continue; |
aoqi@0 | 2353 | } |
aoqi@0 | 2354 | |
aoqi@0 | 2355 | if (m1 != sym) { |
aoqi@0 | 2356 | overridesAny = true; |
aoqi@0 | 2357 | potentiallyAmbiguousList = List.nil(); |
aoqi@0 | 2358 | } |
aoqi@0 | 2359 | |
aoqi@0 | 2360 | //...check each method m2 that is a member of 'site' |
aoqi@0 | 2361 | for (Symbol m2 : types.membersClosure(site, false).getElementsByName(sym.name, cf)) { |
aoqi@0 | 2362 | if (m2 == m1) continue; |
aoqi@0 | 2363 | //if (i) the signature of 'sym' is not a subsignature of m1 (seen as |
aoqi@0 | 2364 | //a member of 'site') and (ii) m1 has the same erasure as m2, issue an error |
aoqi@0 | 2365 | if (!types.isSubSignature(sym.type, types.memberType(site, m2), allowStrictMethodClashCheck) && |
aoqi@0 | 2366 | types.hasSameArgs(m2.erasure(types), m1.erasure(types))) { |
aoqi@0 | 2367 | sym.flags_field |= CLASH; |
aoqi@0 | 2368 | String key = m1 == sym ? |
aoqi@0 | 2369 | "name.clash.same.erasure.no.override" : |
aoqi@0 | 2370 | "name.clash.same.erasure.no.override.1"; |
aoqi@0 | 2371 | log.error(pos, |
aoqi@0 | 2372 | key, |
aoqi@0 | 2373 | sym, sym.location(), |
aoqi@0 | 2374 | m2, m2.location(), |
aoqi@0 | 2375 | m1, m1.location()); |
aoqi@0 | 2376 | return; |
aoqi@0 | 2377 | } |
aoqi@0 | 2378 | } |
aoqi@0 | 2379 | } |
aoqi@0 | 2380 | |
aoqi@0 | 2381 | if (!overridesAny) { |
aoqi@0 | 2382 | for (MethodSymbol m: potentiallyAmbiguousList) { |
aoqi@0 | 2383 | checkPotentiallyAmbiguousOverloads(pos, site, sym, m); |
aoqi@0 | 2384 | } |
aoqi@0 | 2385 | } |
aoqi@0 | 2386 | } |
aoqi@0 | 2387 | |
aoqi@0 | 2388 | /** Check that all static methods accessible from 'site' are |
aoqi@0 | 2389 | * mutually compatible (JLS 8.4.8). |
aoqi@0 | 2390 | * |
aoqi@0 | 2391 | * @param pos Position to be used for error reporting. |
aoqi@0 | 2392 | * @param site The class whose methods are checked. |
aoqi@0 | 2393 | * @param sym The method symbol to be checked. |
aoqi@0 | 2394 | */ |
aoqi@0 | 2395 | void checkHideClashes(DiagnosticPosition pos, Type site, MethodSymbol sym) { |
aoqi@0 | 2396 | ClashFilter cf = new ClashFilter(site); |
aoqi@0 | 2397 | //for each method m1 that is a member of 'site'... |
aoqi@0 | 2398 | for (Symbol s : types.membersClosure(site, true).getElementsByName(sym.name, cf)) { |
aoqi@0 | 2399 | //if (i) the signature of 'sym' is not a subsignature of m1 (seen as |
aoqi@0 | 2400 | //a member of 'site') and (ii) 'sym' has the same erasure as m1, issue an error |
aoqi@0 | 2401 | if (!types.isSubSignature(sym.type, types.memberType(site, s), allowStrictMethodClashCheck)) { |
aoqi@0 | 2402 | if (types.hasSameArgs(s.erasure(types), sym.erasure(types))) { |
aoqi@0 | 2403 | log.error(pos, |
aoqi@0 | 2404 | "name.clash.same.erasure.no.hide", |
aoqi@0 | 2405 | sym, sym.location(), |
aoqi@0 | 2406 | s, s.location()); |
aoqi@0 | 2407 | return; |
aoqi@0 | 2408 | } else { |
aoqi@0 | 2409 | checkPotentiallyAmbiguousOverloads(pos, site, sym, (MethodSymbol)s); |
aoqi@0 | 2410 | } |
aoqi@0 | 2411 | } |
aoqi@0 | 2412 | } |
aoqi@0 | 2413 | } |
aoqi@0 | 2414 | |
aoqi@0 | 2415 | //where |
aoqi@0 | 2416 | private class ClashFilter implements Filter<Symbol> { |
aoqi@0 | 2417 | |
aoqi@0 | 2418 | Type site; |
aoqi@0 | 2419 | |
aoqi@0 | 2420 | ClashFilter(Type site) { |
aoqi@0 | 2421 | this.site = site; |
aoqi@0 | 2422 | } |
aoqi@0 | 2423 | |
aoqi@0 | 2424 | boolean shouldSkip(Symbol s) { |
aoqi@0 | 2425 | return (s.flags() & CLASH) != 0 && |
aoqi@0 | 2426 | s.owner == site.tsym; |
aoqi@0 | 2427 | } |
aoqi@0 | 2428 | |
aoqi@0 | 2429 | public boolean accepts(Symbol s) { |
aoqi@0 | 2430 | return s.kind == MTH && |
aoqi@0 | 2431 | (s.flags() & SYNTHETIC) == 0 && |
aoqi@0 | 2432 | !shouldSkip(s) && |
aoqi@0 | 2433 | s.isInheritedIn(site.tsym, types) && |
aoqi@0 | 2434 | !s.isConstructor(); |
aoqi@0 | 2435 | } |
aoqi@0 | 2436 | } |
aoqi@0 | 2437 | |
aoqi@0 | 2438 | void checkDefaultMethodClashes(DiagnosticPosition pos, Type site) { |
aoqi@0 | 2439 | DefaultMethodClashFilter dcf = new DefaultMethodClashFilter(site); |
aoqi@0 | 2440 | for (Symbol m : types.membersClosure(site, false).getElements(dcf)) { |
aoqi@0 | 2441 | Assert.check(m.kind == MTH); |
aoqi@0 | 2442 | List<MethodSymbol> prov = types.interfaceCandidates(site, (MethodSymbol)m); |
aoqi@0 | 2443 | if (prov.size() > 1) { |
aoqi@0 | 2444 | ListBuffer<Symbol> abstracts = new ListBuffer<>(); |
aoqi@0 | 2445 | ListBuffer<Symbol> defaults = new ListBuffer<>(); |
aoqi@0 | 2446 | for (MethodSymbol provSym : prov) { |
aoqi@0 | 2447 | if ((provSym.flags() & DEFAULT) != 0) { |
aoqi@0 | 2448 | defaults = defaults.append(provSym); |
aoqi@0 | 2449 | } else if ((provSym.flags() & ABSTRACT) != 0) { |
aoqi@0 | 2450 | abstracts = abstracts.append(provSym); |
aoqi@0 | 2451 | } |
aoqi@0 | 2452 | if (defaults.nonEmpty() && defaults.size() + abstracts.size() >= 2) { |
aoqi@0 | 2453 | //strong semantics - issue an error if two sibling interfaces |
aoqi@0 | 2454 | //have two override-equivalent defaults - or if one is abstract |
aoqi@0 | 2455 | //and the other is default |
aoqi@0 | 2456 | String errKey; |
aoqi@0 | 2457 | Symbol s1 = defaults.first(); |
aoqi@0 | 2458 | Symbol s2; |
aoqi@0 | 2459 | if (defaults.size() > 1) { |
aoqi@0 | 2460 | errKey = "types.incompatible.unrelated.defaults"; |
aoqi@0 | 2461 | s2 = defaults.toList().tail.head; |
aoqi@0 | 2462 | } else { |
aoqi@0 | 2463 | errKey = "types.incompatible.abstract.default"; |
aoqi@0 | 2464 | s2 = abstracts.first(); |
aoqi@0 | 2465 | } |
aoqi@0 | 2466 | log.error(pos, errKey, |
aoqi@0 | 2467 | Kinds.kindName(site.tsym), site, |
aoqi@0 | 2468 | m.name, types.memberType(site, m).getParameterTypes(), |
aoqi@0 | 2469 | s1.location(), s2.location()); |
aoqi@0 | 2470 | break; |
aoqi@0 | 2471 | } |
aoqi@0 | 2472 | } |
aoqi@0 | 2473 | } |
aoqi@0 | 2474 | } |
aoqi@0 | 2475 | } |
aoqi@0 | 2476 | |
aoqi@0 | 2477 | //where |
aoqi@0 | 2478 | private class DefaultMethodClashFilter implements Filter<Symbol> { |
aoqi@0 | 2479 | |
aoqi@0 | 2480 | Type site; |
aoqi@0 | 2481 | |
aoqi@0 | 2482 | DefaultMethodClashFilter(Type site) { |
aoqi@0 | 2483 | this.site = site; |
aoqi@0 | 2484 | } |
aoqi@0 | 2485 | |
aoqi@0 | 2486 | public boolean accepts(Symbol s) { |
aoqi@0 | 2487 | return s.kind == MTH && |
aoqi@0 | 2488 | (s.flags() & DEFAULT) != 0 && |
aoqi@0 | 2489 | s.isInheritedIn(site.tsym, types) && |
aoqi@0 | 2490 | !s.isConstructor(); |
aoqi@0 | 2491 | } |
aoqi@0 | 2492 | } |
aoqi@0 | 2493 | |
aoqi@0 | 2494 | /** |
aoqi@0 | 2495 | * Report warnings for potentially ambiguous method declarations. Two declarations |
aoqi@0 | 2496 | * are potentially ambiguous if they feature two unrelated functional interface |
aoqi@0 | 2497 | * in same argument position (in which case, a call site passing an implicit |
aoqi@0 | 2498 | * lambda would be ambiguous). |
aoqi@0 | 2499 | */ |
aoqi@0 | 2500 | void checkPotentiallyAmbiguousOverloads(DiagnosticPosition pos, Type site, |
aoqi@0 | 2501 | MethodSymbol msym1, MethodSymbol msym2) { |
aoqi@0 | 2502 | if (msym1 != msym2 && |
aoqi@0 | 2503 | allowDefaultMethods && |
aoqi@0 | 2504 | lint.isEnabled(LintCategory.OVERLOADS) && |
aoqi@0 | 2505 | (msym1.flags() & POTENTIALLY_AMBIGUOUS) == 0 && |
aoqi@0 | 2506 | (msym2.flags() & POTENTIALLY_AMBIGUOUS) == 0) { |
aoqi@0 | 2507 | Type mt1 = types.memberType(site, msym1); |
aoqi@0 | 2508 | Type mt2 = types.memberType(site, msym2); |
aoqi@0 | 2509 | //if both generic methods, adjust type variables |
aoqi@0 | 2510 | if (mt1.hasTag(FORALL) && mt2.hasTag(FORALL) && |
aoqi@0 | 2511 | types.hasSameBounds((ForAll)mt1, (ForAll)mt2)) { |
aoqi@0 | 2512 | mt2 = types.subst(mt2, ((ForAll)mt2).tvars, ((ForAll)mt1).tvars); |
aoqi@0 | 2513 | } |
aoqi@0 | 2514 | //expand varargs methods if needed |
aoqi@0 | 2515 | int maxLength = Math.max(mt1.getParameterTypes().length(), mt2.getParameterTypes().length()); |
aoqi@0 | 2516 | List<Type> args1 = rs.adjustArgs(mt1.getParameterTypes(), msym1, maxLength, true); |
aoqi@0 | 2517 | List<Type> args2 = rs.adjustArgs(mt2.getParameterTypes(), msym2, maxLength, true); |
aoqi@0 | 2518 | //if arities don't match, exit |
aoqi@0 | 2519 | if (args1.length() != args2.length()) return; |
aoqi@0 | 2520 | boolean potentiallyAmbiguous = false; |
aoqi@0 | 2521 | while (args1.nonEmpty() && args2.nonEmpty()) { |
aoqi@0 | 2522 | Type s = args1.head; |
aoqi@0 | 2523 | Type t = args2.head; |
aoqi@0 | 2524 | if (!types.isSubtype(t, s) && !types.isSubtype(s, t)) { |
aoqi@0 | 2525 | if (types.isFunctionalInterface(s) && types.isFunctionalInterface(t) && |
aoqi@0 | 2526 | types.findDescriptorType(s).getParameterTypes().length() > 0 && |
aoqi@0 | 2527 | types.findDescriptorType(s).getParameterTypes().length() == |
aoqi@0 | 2528 | types.findDescriptorType(t).getParameterTypes().length()) { |
aoqi@0 | 2529 | potentiallyAmbiguous = true; |
aoqi@0 | 2530 | } else { |
aoqi@0 | 2531 | break; |
aoqi@0 | 2532 | } |
aoqi@0 | 2533 | } |
aoqi@0 | 2534 | args1 = args1.tail; |
aoqi@0 | 2535 | args2 = args2.tail; |
aoqi@0 | 2536 | } |
aoqi@0 | 2537 | if (potentiallyAmbiguous) { |
aoqi@0 | 2538 | //we found two incompatible functional interfaces with same arity |
aoqi@0 | 2539 | //this means a call site passing an implicit lambda would be ambigiuous |
aoqi@0 | 2540 | msym1.flags_field |= POTENTIALLY_AMBIGUOUS; |
aoqi@0 | 2541 | msym2.flags_field |= POTENTIALLY_AMBIGUOUS; |
aoqi@0 | 2542 | log.warning(LintCategory.OVERLOADS, pos, "potentially.ambiguous.overload", |
aoqi@0 | 2543 | msym1, msym1.location(), |
aoqi@0 | 2544 | msym2, msym2.location()); |
aoqi@0 | 2545 | return; |
aoqi@0 | 2546 | } |
aoqi@0 | 2547 | } |
aoqi@0 | 2548 | } |
aoqi@0 | 2549 | |
aoqi@0 | 2550 | void checkElemAccessFromSerializableLambda(final JCTree tree) { |
aoqi@0 | 2551 | if (warnOnAccessToSensitiveMembers) { |
aoqi@0 | 2552 | Symbol sym = TreeInfo.symbol(tree); |
aoqi@0 | 2553 | if ((sym.kind & (VAR | MTH)) == 0) { |
aoqi@0 | 2554 | return; |
aoqi@0 | 2555 | } |
aoqi@0 | 2556 | |
aoqi@0 | 2557 | if (sym.kind == VAR) { |
aoqi@0 | 2558 | if ((sym.flags() & PARAMETER) != 0 || |
aoqi@0 | 2559 | sym.isLocal() || |
aoqi@0 | 2560 | sym.name == names._this || |
aoqi@0 | 2561 | sym.name == names._super) { |
aoqi@0 | 2562 | return; |
aoqi@0 | 2563 | } |
aoqi@0 | 2564 | } |
aoqi@0 | 2565 | |
aoqi@0 | 2566 | if (!types.isSubtype(sym.owner.type, syms.serializableType) && |
aoqi@0 | 2567 | isEffectivelyNonPublic(sym)) { |
aoqi@0 | 2568 | log.warning(tree.pos(), |
aoqi@0 | 2569 | "access.to.sensitive.member.from.serializable.element", sym); |
aoqi@0 | 2570 | } |
aoqi@0 | 2571 | } |
aoqi@0 | 2572 | } |
aoqi@0 | 2573 | |
aoqi@0 | 2574 | private boolean isEffectivelyNonPublic(Symbol sym) { |
aoqi@0 | 2575 | if (sym.packge() == syms.rootPackage) { |
aoqi@0 | 2576 | return false; |
aoqi@0 | 2577 | } |
aoqi@0 | 2578 | |
aoqi@0 | 2579 | while (sym.kind != Kinds.PCK) { |
aoqi@0 | 2580 | if ((sym.flags() & PUBLIC) == 0) { |
aoqi@0 | 2581 | return true; |
aoqi@0 | 2582 | } |
aoqi@0 | 2583 | sym = sym.owner; |
aoqi@0 | 2584 | } |
aoqi@0 | 2585 | return false; |
aoqi@0 | 2586 | } |
aoqi@0 | 2587 | |
aoqi@0 | 2588 | /** Report a conflict between a user symbol and a synthetic symbol. |
aoqi@0 | 2589 | */ |
aoqi@0 | 2590 | private void syntheticError(DiagnosticPosition pos, Symbol sym) { |
aoqi@0 | 2591 | if (!sym.type.isErroneous()) { |
aoqi@0 | 2592 | if (warnOnSyntheticConflicts) { |
aoqi@0 | 2593 | log.warning(pos, "synthetic.name.conflict", sym, sym.location()); |
aoqi@0 | 2594 | } |
aoqi@0 | 2595 | else { |
aoqi@0 | 2596 | log.error(pos, "synthetic.name.conflict", sym, sym.location()); |
aoqi@0 | 2597 | } |
aoqi@0 | 2598 | } |
aoqi@0 | 2599 | } |
aoqi@0 | 2600 | |
aoqi@0 | 2601 | /** Check that class c does not implement directly or indirectly |
aoqi@0 | 2602 | * the same parameterized interface with two different argument lists. |
aoqi@0 | 2603 | * @param pos Position to be used for error reporting. |
aoqi@0 | 2604 | * @param type The type whose interfaces are checked. |
aoqi@0 | 2605 | */ |
aoqi@0 | 2606 | void checkClassBounds(DiagnosticPosition pos, Type type) { |
aoqi@0 | 2607 | checkClassBounds(pos, new HashMap<TypeSymbol,Type>(), type); |
aoqi@0 | 2608 | } |
aoqi@0 | 2609 | //where |
aoqi@0 | 2610 | /** Enter all interfaces of type `type' into the hash table `seensofar' |
aoqi@0 | 2611 | * with their class symbol as key and their type as value. Make |
aoqi@0 | 2612 | * sure no class is entered with two different types. |
aoqi@0 | 2613 | */ |
aoqi@0 | 2614 | void checkClassBounds(DiagnosticPosition pos, |
aoqi@0 | 2615 | Map<TypeSymbol,Type> seensofar, |
aoqi@0 | 2616 | Type type) { |
aoqi@0 | 2617 | if (type.isErroneous()) return; |
aoqi@0 | 2618 | for (List<Type> l = types.interfaces(type); l.nonEmpty(); l = l.tail) { |
aoqi@0 | 2619 | Type it = l.head; |
aoqi@0 | 2620 | Type oldit = seensofar.put(it.tsym, it); |
aoqi@0 | 2621 | if (oldit != null) { |
aoqi@0 | 2622 | List<Type> oldparams = oldit.allparams(); |
aoqi@0 | 2623 | List<Type> newparams = it.allparams(); |
aoqi@0 | 2624 | if (!types.containsTypeEquivalent(oldparams, newparams)) |
aoqi@0 | 2625 | log.error(pos, "cant.inherit.diff.arg", |
aoqi@0 | 2626 | it.tsym, Type.toString(oldparams), |
aoqi@0 | 2627 | Type.toString(newparams)); |
aoqi@0 | 2628 | } |
aoqi@0 | 2629 | checkClassBounds(pos, seensofar, it); |
aoqi@0 | 2630 | } |
aoqi@0 | 2631 | Type st = types.supertype(type); |
aoqi@0 | 2632 | if (st != Type.noType) checkClassBounds(pos, seensofar, st); |
aoqi@0 | 2633 | } |
aoqi@0 | 2634 | |
aoqi@0 | 2635 | /** Enter interface into into set. |
aoqi@0 | 2636 | * If it existed already, issue a "repeated interface" error. |
aoqi@0 | 2637 | */ |
aoqi@0 | 2638 | void checkNotRepeated(DiagnosticPosition pos, Type it, Set<Type> its) { |
aoqi@0 | 2639 | if (its.contains(it)) |
aoqi@0 | 2640 | log.error(pos, "repeated.interface"); |
aoqi@0 | 2641 | else { |
aoqi@0 | 2642 | its.add(it); |
aoqi@0 | 2643 | } |
aoqi@0 | 2644 | } |
aoqi@0 | 2645 | |
aoqi@0 | 2646 | /* ************************************************************************* |
aoqi@0 | 2647 | * Check annotations |
aoqi@0 | 2648 | **************************************************************************/ |
aoqi@0 | 2649 | |
aoqi@0 | 2650 | /** |
aoqi@0 | 2651 | * Recursively validate annotations values |
aoqi@0 | 2652 | */ |
aoqi@0 | 2653 | void validateAnnotationTree(JCTree tree) { |
aoqi@0 | 2654 | class AnnotationValidator extends TreeScanner { |
aoqi@0 | 2655 | @Override |
aoqi@0 | 2656 | public void visitAnnotation(JCAnnotation tree) { |
aoqi@0 | 2657 | if (!tree.type.isErroneous()) { |
aoqi@0 | 2658 | super.visitAnnotation(tree); |
aoqi@0 | 2659 | validateAnnotation(tree); |
aoqi@0 | 2660 | } |
aoqi@0 | 2661 | } |
aoqi@0 | 2662 | } |
aoqi@0 | 2663 | tree.accept(new AnnotationValidator()); |
aoqi@0 | 2664 | } |
aoqi@0 | 2665 | |
aoqi@0 | 2666 | /** |
aoqi@0 | 2667 | * {@literal |
aoqi@0 | 2668 | * Annotation types are restricted to primitives, String, an |
aoqi@0 | 2669 | * enum, an annotation, Class, Class<?>, Class<? extends |
aoqi@0 | 2670 | * Anything>, arrays of the preceding. |
aoqi@0 | 2671 | * } |
aoqi@0 | 2672 | */ |
aoqi@0 | 2673 | void validateAnnotationType(JCTree restype) { |
aoqi@0 | 2674 | // restype may be null if an error occurred, so don't bother validating it |
aoqi@0 | 2675 | if (restype != null) { |
aoqi@0 | 2676 | validateAnnotationType(restype.pos(), restype.type); |
aoqi@0 | 2677 | } |
aoqi@0 | 2678 | } |
aoqi@0 | 2679 | |
aoqi@0 | 2680 | void validateAnnotationType(DiagnosticPosition pos, Type type) { |
aoqi@0 | 2681 | if (type.isPrimitive()) return; |
aoqi@0 | 2682 | if (types.isSameType(type, syms.stringType)) return; |
aoqi@0 | 2683 | if ((type.tsym.flags() & Flags.ENUM) != 0) return; |
aoqi@0 | 2684 | if ((type.tsym.flags() & Flags.ANNOTATION) != 0) return; |
aoqi@0 | 2685 | if (types.cvarLowerBound(type).tsym == syms.classType.tsym) return; |
aoqi@0 | 2686 | if (types.isArray(type) && !types.isArray(types.elemtype(type))) { |
aoqi@0 | 2687 | validateAnnotationType(pos, types.elemtype(type)); |
aoqi@0 | 2688 | return; |
aoqi@0 | 2689 | } |
aoqi@0 | 2690 | log.error(pos, "invalid.annotation.member.type"); |
aoqi@0 | 2691 | } |
aoqi@0 | 2692 | |
aoqi@0 | 2693 | /** |
aoqi@0 | 2694 | * "It is also a compile-time error if any method declared in an |
aoqi@0 | 2695 | * annotation type has a signature that is override-equivalent to |
aoqi@0 | 2696 | * that of any public or protected method declared in class Object |
aoqi@0 | 2697 | * or in the interface annotation.Annotation." |
aoqi@0 | 2698 | * |
aoqi@0 | 2699 | * @jls 9.6 Annotation Types |
aoqi@0 | 2700 | */ |
aoqi@0 | 2701 | void validateAnnotationMethod(DiagnosticPosition pos, MethodSymbol m) { |
aoqi@0 | 2702 | for (Type sup = syms.annotationType; sup.hasTag(CLASS); sup = types.supertype(sup)) { |
aoqi@0 | 2703 | Scope s = sup.tsym.members(); |
aoqi@0 | 2704 | for (Scope.Entry e = s.lookup(m.name); e.scope != null; e = e.next()) { |
aoqi@0 | 2705 | if (e.sym.kind == MTH && |
aoqi@0 | 2706 | (e.sym.flags() & (PUBLIC | PROTECTED)) != 0 && |
aoqi@0 | 2707 | types.overrideEquivalent(m.type, e.sym.type)) |
aoqi@0 | 2708 | log.error(pos, "intf.annotation.member.clash", e.sym, sup); |
aoqi@0 | 2709 | } |
aoqi@0 | 2710 | } |
aoqi@0 | 2711 | } |
aoqi@0 | 2712 | |
aoqi@0 | 2713 | /** Check the annotations of a symbol. |
aoqi@0 | 2714 | */ |
aoqi@0 | 2715 | public void validateAnnotations(List<JCAnnotation> annotations, Symbol s) { |
aoqi@0 | 2716 | for (JCAnnotation a : annotations) |
aoqi@0 | 2717 | validateAnnotation(a, s); |
aoqi@0 | 2718 | } |
aoqi@0 | 2719 | |
aoqi@0 | 2720 | /** Check the type annotations. |
aoqi@0 | 2721 | */ |
aoqi@0 | 2722 | public void validateTypeAnnotations(List<JCAnnotation> annotations, boolean isTypeParameter) { |
aoqi@0 | 2723 | for (JCAnnotation a : annotations) |
aoqi@0 | 2724 | validateTypeAnnotation(a, isTypeParameter); |
aoqi@0 | 2725 | } |
aoqi@0 | 2726 | |
aoqi@0 | 2727 | /** Check an annotation of a symbol. |
aoqi@0 | 2728 | */ |
aoqi@0 | 2729 | private void validateAnnotation(JCAnnotation a, Symbol s) { |
aoqi@0 | 2730 | validateAnnotationTree(a); |
aoqi@0 | 2731 | |
aoqi@0 | 2732 | if (!annotationApplicable(a, s)) |
aoqi@0 | 2733 | log.error(a.pos(), "annotation.type.not.applicable"); |
aoqi@0 | 2734 | |
aoqi@0 | 2735 | if (a.annotationType.type.tsym == syms.functionalInterfaceType.tsym) { |
aoqi@0 | 2736 | if (s.kind != TYP) { |
aoqi@0 | 2737 | log.error(a.pos(), "bad.functional.intf.anno"); |
aoqi@0 | 2738 | } else if (!s.isInterface() || (s.flags() & ANNOTATION) != 0) { |
aoqi@0 | 2739 | log.error(a.pos(), "bad.functional.intf.anno.1", diags.fragment("not.a.functional.intf", s)); |
aoqi@0 | 2740 | } |
aoqi@0 | 2741 | } |
aoqi@0 | 2742 | } |
aoqi@0 | 2743 | |
aoqi@0 | 2744 | public void validateTypeAnnotation(JCAnnotation a, boolean isTypeParameter) { |
aoqi@0 | 2745 | Assert.checkNonNull(a.type, "annotation tree hasn't been attributed yet: " + a); |
aoqi@0 | 2746 | validateAnnotationTree(a); |
aoqi@0 | 2747 | |
aoqi@0 | 2748 | if (a.hasTag(TYPE_ANNOTATION) && |
aoqi@0 | 2749 | !a.annotationType.type.isErroneous() && |
aoqi@0 | 2750 | !isTypeAnnotation(a, isTypeParameter)) { |
aoqi@0 | 2751 | log.error(a.pos(), "annotation.type.not.applicable"); |
aoqi@0 | 2752 | } |
aoqi@0 | 2753 | } |
aoqi@0 | 2754 | |
aoqi@0 | 2755 | /** |
aoqi@0 | 2756 | * Validate the proposed container 'repeatable' on the |
aoqi@0 | 2757 | * annotation type symbol 's'. Report errors at position |
aoqi@0 | 2758 | * 'pos'. |
aoqi@0 | 2759 | * |
aoqi@0 | 2760 | * @param s The (annotation)type declaration annotated with a @Repeatable |
aoqi@0 | 2761 | * @param repeatable the @Repeatable on 's' |
aoqi@0 | 2762 | * @param pos where to report errors |
aoqi@0 | 2763 | */ |
aoqi@0 | 2764 | public void validateRepeatable(TypeSymbol s, Attribute.Compound repeatable, DiagnosticPosition pos) { |
aoqi@0 | 2765 | Assert.check(types.isSameType(repeatable.type, syms.repeatableType)); |
aoqi@0 | 2766 | |
aoqi@0 | 2767 | Type t = null; |
aoqi@0 | 2768 | List<Pair<MethodSymbol,Attribute>> l = repeatable.values; |
aoqi@0 | 2769 | if (!l.isEmpty()) { |
aoqi@0 | 2770 | Assert.check(l.head.fst.name == names.value); |
aoqi@0 | 2771 | t = ((Attribute.Class)l.head.snd).getValue(); |
aoqi@0 | 2772 | } |
aoqi@0 | 2773 | |
aoqi@0 | 2774 | if (t == null) { |
aoqi@0 | 2775 | // errors should already have been reported during Annotate |
aoqi@0 | 2776 | return; |
aoqi@0 | 2777 | } |
aoqi@0 | 2778 | |
aoqi@0 | 2779 | validateValue(t.tsym, s, pos); |
aoqi@0 | 2780 | validateRetention(t.tsym, s, pos); |
aoqi@0 | 2781 | validateDocumented(t.tsym, s, pos); |
aoqi@0 | 2782 | validateInherited(t.tsym, s, pos); |
aoqi@0 | 2783 | validateTarget(t.tsym, s, pos); |
aoqi@0 | 2784 | validateDefault(t.tsym, pos); |
aoqi@0 | 2785 | } |
aoqi@0 | 2786 | |
aoqi@0 | 2787 | private void validateValue(TypeSymbol container, TypeSymbol contained, DiagnosticPosition pos) { |
aoqi@0 | 2788 | Scope.Entry e = container.members().lookup(names.value); |
aoqi@0 | 2789 | if (e.scope != null && e.sym.kind == MTH) { |
aoqi@0 | 2790 | MethodSymbol m = (MethodSymbol) e.sym; |
aoqi@0 | 2791 | Type ret = m.getReturnType(); |
aoqi@0 | 2792 | if (!(ret.hasTag(ARRAY) && types.isSameType(((ArrayType)ret).elemtype, contained.type))) { |
aoqi@0 | 2793 | log.error(pos, "invalid.repeatable.annotation.value.return", |
aoqi@0 | 2794 | container, ret, types.makeArrayType(contained.type)); |
aoqi@0 | 2795 | } |
aoqi@0 | 2796 | } else { |
aoqi@0 | 2797 | log.error(pos, "invalid.repeatable.annotation.no.value", container); |
aoqi@0 | 2798 | } |
aoqi@0 | 2799 | } |
aoqi@0 | 2800 | |
aoqi@0 | 2801 | private void validateRetention(Symbol container, Symbol contained, DiagnosticPosition pos) { |
aoqi@0 | 2802 | Attribute.RetentionPolicy containerRetention = types.getRetention(container); |
aoqi@0 | 2803 | Attribute.RetentionPolicy containedRetention = types.getRetention(contained); |
aoqi@0 | 2804 | |
aoqi@0 | 2805 | boolean error = false; |
aoqi@0 | 2806 | switch (containedRetention) { |
aoqi@0 | 2807 | case RUNTIME: |
aoqi@0 | 2808 | if (containerRetention != Attribute.RetentionPolicy.RUNTIME) { |
aoqi@0 | 2809 | error = true; |
aoqi@0 | 2810 | } |
aoqi@0 | 2811 | break; |
aoqi@0 | 2812 | case CLASS: |
aoqi@0 | 2813 | if (containerRetention == Attribute.RetentionPolicy.SOURCE) { |
aoqi@0 | 2814 | error = true; |
aoqi@0 | 2815 | } |
aoqi@0 | 2816 | } |
aoqi@0 | 2817 | if (error ) { |
aoqi@0 | 2818 | log.error(pos, "invalid.repeatable.annotation.retention", |
aoqi@0 | 2819 | container, containerRetention, |
aoqi@0 | 2820 | contained, containedRetention); |
aoqi@0 | 2821 | } |
aoqi@0 | 2822 | } |
aoqi@0 | 2823 | |
aoqi@0 | 2824 | private void validateDocumented(Symbol container, Symbol contained, DiagnosticPosition pos) { |
aoqi@0 | 2825 | if (contained.attribute(syms.documentedType.tsym) != null) { |
aoqi@0 | 2826 | if (container.attribute(syms.documentedType.tsym) == null) { |
aoqi@0 | 2827 | log.error(pos, "invalid.repeatable.annotation.not.documented", container, contained); |
aoqi@0 | 2828 | } |
aoqi@0 | 2829 | } |
aoqi@0 | 2830 | } |
aoqi@0 | 2831 | |
aoqi@0 | 2832 | private void validateInherited(Symbol container, Symbol contained, DiagnosticPosition pos) { |
aoqi@0 | 2833 | if (contained.attribute(syms.inheritedType.tsym) != null) { |
aoqi@0 | 2834 | if (container.attribute(syms.inheritedType.tsym) == null) { |
aoqi@0 | 2835 | log.error(pos, "invalid.repeatable.annotation.not.inherited", container, contained); |
aoqi@0 | 2836 | } |
aoqi@0 | 2837 | } |
aoqi@0 | 2838 | } |
aoqi@0 | 2839 | |
aoqi@0 | 2840 | private void validateTarget(Symbol container, Symbol contained, DiagnosticPosition pos) { |
aoqi@0 | 2841 | // The set of targets the container is applicable to must be a subset |
aoqi@0 | 2842 | // (with respect to annotation target semantics) of the set of targets |
aoqi@0 | 2843 | // the contained is applicable to. The target sets may be implicit or |
aoqi@0 | 2844 | // explicit. |
aoqi@0 | 2845 | |
aoqi@0 | 2846 | Set<Name> containerTargets; |
aoqi@0 | 2847 | Attribute.Array containerTarget = getAttributeTargetAttribute(container); |
aoqi@0 | 2848 | if (containerTarget == null) { |
aoqi@0 | 2849 | containerTargets = getDefaultTargetSet(); |
aoqi@0 | 2850 | } else { |
aoqi@0 | 2851 | containerTargets = new HashSet<Name>(); |
aoqi@0 | 2852 | for (Attribute app : containerTarget.values) { |
aoqi@0 | 2853 | if (!(app instanceof Attribute.Enum)) { |
aoqi@0 | 2854 | continue; // recovery |
aoqi@0 | 2855 | } |
aoqi@0 | 2856 | Attribute.Enum e = (Attribute.Enum)app; |
aoqi@0 | 2857 | containerTargets.add(e.value.name); |
aoqi@0 | 2858 | } |
aoqi@0 | 2859 | } |
aoqi@0 | 2860 | |
aoqi@0 | 2861 | Set<Name> containedTargets; |
aoqi@0 | 2862 | Attribute.Array containedTarget = getAttributeTargetAttribute(contained); |
aoqi@0 | 2863 | if (containedTarget == null) { |
aoqi@0 | 2864 | containedTargets = getDefaultTargetSet(); |
aoqi@0 | 2865 | } else { |
aoqi@0 | 2866 | containedTargets = new HashSet<Name>(); |
aoqi@0 | 2867 | for (Attribute app : containedTarget.values) { |
aoqi@0 | 2868 | if (!(app instanceof Attribute.Enum)) { |
aoqi@0 | 2869 | continue; // recovery |
aoqi@0 | 2870 | } |
aoqi@0 | 2871 | Attribute.Enum e = (Attribute.Enum)app; |
aoqi@0 | 2872 | containedTargets.add(e.value.name); |
aoqi@0 | 2873 | } |
aoqi@0 | 2874 | } |
aoqi@0 | 2875 | |
aoqi@0 | 2876 | if (!isTargetSubsetOf(containerTargets, containedTargets)) { |
aoqi@0 | 2877 | log.error(pos, "invalid.repeatable.annotation.incompatible.target", container, contained); |
aoqi@0 | 2878 | } |
aoqi@0 | 2879 | } |
aoqi@0 | 2880 | |
aoqi@0 | 2881 | /* get a set of names for the default target */ |
aoqi@0 | 2882 | private Set<Name> getDefaultTargetSet() { |
aoqi@0 | 2883 | if (defaultTargets == null) { |
aoqi@0 | 2884 | Set<Name> targets = new HashSet<Name>(); |
aoqi@0 | 2885 | targets.add(names.ANNOTATION_TYPE); |
aoqi@0 | 2886 | targets.add(names.CONSTRUCTOR); |
aoqi@0 | 2887 | targets.add(names.FIELD); |
aoqi@0 | 2888 | targets.add(names.LOCAL_VARIABLE); |
aoqi@0 | 2889 | targets.add(names.METHOD); |
aoqi@0 | 2890 | targets.add(names.PACKAGE); |
aoqi@0 | 2891 | targets.add(names.PARAMETER); |
aoqi@0 | 2892 | targets.add(names.TYPE); |
aoqi@0 | 2893 | |
aoqi@0 | 2894 | defaultTargets = java.util.Collections.unmodifiableSet(targets); |
aoqi@0 | 2895 | } |
aoqi@0 | 2896 | |
aoqi@0 | 2897 | return defaultTargets; |
aoqi@0 | 2898 | } |
aoqi@0 | 2899 | private Set<Name> defaultTargets; |
aoqi@0 | 2900 | |
aoqi@0 | 2901 | |
aoqi@0 | 2902 | /** Checks that s is a subset of t, with respect to ElementType |
aoqi@0 | 2903 | * semantics, specifically {ANNOTATION_TYPE} is a subset of {TYPE}, |
aoqi@0 | 2904 | * and {TYPE_USE} covers the set {ANNOTATION_TYPE, TYPE, TYPE_USE, |
aoqi@0 | 2905 | * TYPE_PARAMETER}. |
aoqi@0 | 2906 | */ |
aoqi@0 | 2907 | private boolean isTargetSubsetOf(Set<Name> s, Set<Name> t) { |
aoqi@0 | 2908 | // Check that all elements in s are present in t |
aoqi@0 | 2909 | for (Name n2 : s) { |
aoqi@0 | 2910 | boolean currentElementOk = false; |
aoqi@0 | 2911 | for (Name n1 : t) { |
aoqi@0 | 2912 | if (n1 == n2) { |
aoqi@0 | 2913 | currentElementOk = true; |
aoqi@0 | 2914 | break; |
aoqi@0 | 2915 | } else if (n1 == names.TYPE && n2 == names.ANNOTATION_TYPE) { |
aoqi@0 | 2916 | currentElementOk = true; |
aoqi@0 | 2917 | break; |
aoqi@0 | 2918 | } else if (n1 == names.TYPE_USE && |
aoqi@0 | 2919 | (n2 == names.TYPE || |
aoqi@0 | 2920 | n2 == names.ANNOTATION_TYPE || |
aoqi@0 | 2921 | n2 == names.TYPE_PARAMETER)) { |
aoqi@0 | 2922 | currentElementOk = true; |
aoqi@0 | 2923 | break; |
aoqi@0 | 2924 | } |
aoqi@0 | 2925 | } |
aoqi@0 | 2926 | if (!currentElementOk) |
aoqi@0 | 2927 | return false; |
aoqi@0 | 2928 | } |
aoqi@0 | 2929 | return true; |
aoqi@0 | 2930 | } |
aoqi@0 | 2931 | |
aoqi@0 | 2932 | private void validateDefault(Symbol container, DiagnosticPosition pos) { |
aoqi@0 | 2933 | // validate that all other elements of containing type has defaults |
aoqi@0 | 2934 | Scope scope = container.members(); |
aoqi@0 | 2935 | for(Symbol elm : scope.getElements()) { |
aoqi@0 | 2936 | if (elm.name != names.value && |
aoqi@0 | 2937 | elm.kind == Kinds.MTH && |
aoqi@0 | 2938 | ((MethodSymbol)elm).defaultValue == null) { |
aoqi@0 | 2939 | log.error(pos, |
aoqi@0 | 2940 | "invalid.repeatable.annotation.elem.nondefault", |
aoqi@0 | 2941 | container, |
aoqi@0 | 2942 | elm); |
aoqi@0 | 2943 | } |
aoqi@0 | 2944 | } |
aoqi@0 | 2945 | } |
aoqi@0 | 2946 | |
aoqi@0 | 2947 | /** Is s a method symbol that overrides a method in a superclass? */ |
aoqi@0 | 2948 | boolean isOverrider(Symbol s) { |
aoqi@0 | 2949 | if (s.kind != MTH || s.isStatic()) |
aoqi@0 | 2950 | return false; |
aoqi@0 | 2951 | MethodSymbol m = (MethodSymbol)s; |
aoqi@0 | 2952 | TypeSymbol owner = (TypeSymbol)m.owner; |
aoqi@0 | 2953 | for (Type sup : types.closure(owner.type)) { |
aoqi@0 | 2954 | if (sup == owner.type) |
aoqi@0 | 2955 | continue; // skip "this" |
aoqi@0 | 2956 | Scope scope = sup.tsym.members(); |
aoqi@0 | 2957 | for (Scope.Entry e = scope.lookup(m.name); e.scope != null; e = e.next()) { |
aoqi@0 | 2958 | if (!e.sym.isStatic() && m.overrides(e.sym, owner, types, true)) |
aoqi@0 | 2959 | return true; |
aoqi@0 | 2960 | } |
aoqi@0 | 2961 | } |
aoqi@0 | 2962 | return false; |
aoqi@0 | 2963 | } |
aoqi@0 | 2964 | |
aoqi@0 | 2965 | /** Is the annotation applicable to types? */ |
aoqi@0 | 2966 | protected boolean isTypeAnnotation(JCAnnotation a, boolean isTypeParameter) { |
aoqi@0 | 2967 | Attribute.Compound atTarget = |
aoqi@0 | 2968 | a.annotationType.type.tsym.attribute(syms.annotationTargetType.tsym); |
aoqi@0 | 2969 | if (atTarget == null) { |
aoqi@0 | 2970 | // An annotation without @Target is not a type annotation. |
aoqi@0 | 2971 | return false; |
aoqi@0 | 2972 | } |
aoqi@0 | 2973 | |
aoqi@0 | 2974 | Attribute atValue = atTarget.member(names.value); |
aoqi@0 | 2975 | if (!(atValue instanceof Attribute.Array)) { |
aoqi@0 | 2976 | return false; // error recovery |
aoqi@0 | 2977 | } |
aoqi@0 | 2978 | |
aoqi@0 | 2979 | Attribute.Array arr = (Attribute.Array) atValue; |
aoqi@0 | 2980 | for (Attribute app : arr.values) { |
aoqi@0 | 2981 | if (!(app instanceof Attribute.Enum)) { |
aoqi@0 | 2982 | return false; // recovery |
aoqi@0 | 2983 | } |
aoqi@0 | 2984 | Attribute.Enum e = (Attribute.Enum) app; |
aoqi@0 | 2985 | |
aoqi@0 | 2986 | if (e.value.name == names.TYPE_USE) |
aoqi@0 | 2987 | return true; |
aoqi@0 | 2988 | else if (isTypeParameter && e.value.name == names.TYPE_PARAMETER) |
aoqi@0 | 2989 | return true; |
aoqi@0 | 2990 | } |
aoqi@0 | 2991 | return false; |
aoqi@0 | 2992 | } |
aoqi@0 | 2993 | |
aoqi@0 | 2994 | /** Is the annotation applicable to the symbol? */ |
aoqi@0 | 2995 | boolean annotationApplicable(JCAnnotation a, Symbol s) { |
aoqi@0 | 2996 | Attribute.Array arr = getAttributeTargetAttribute(a.annotationType.type.tsym); |
aoqi@0 | 2997 | Name[] targets; |
aoqi@0 | 2998 | |
aoqi@0 | 2999 | if (arr == null) { |
aoqi@0 | 3000 | targets = defaultTargetMetaInfo(a, s); |
aoqi@0 | 3001 | } else { |
aoqi@0 | 3002 | // TODO: can we optimize this? |
aoqi@0 | 3003 | targets = new Name[arr.values.length]; |
aoqi@0 | 3004 | for (int i=0; i<arr.values.length; ++i) { |
aoqi@0 | 3005 | Attribute app = arr.values[i]; |
aoqi@0 | 3006 | if (!(app instanceof Attribute.Enum)) { |
aoqi@0 | 3007 | return true; // recovery |
aoqi@0 | 3008 | } |
aoqi@0 | 3009 | Attribute.Enum e = (Attribute.Enum) app; |
aoqi@0 | 3010 | targets[i] = e.value.name; |
aoqi@0 | 3011 | } |
aoqi@0 | 3012 | } |
aoqi@0 | 3013 | for (Name target : targets) { |
aoqi@0 | 3014 | if (target == names.TYPE) |
aoqi@0 | 3015 | { if (s.kind == TYP) return true; } |
aoqi@0 | 3016 | else if (target == names.FIELD) |
aoqi@0 | 3017 | { if (s.kind == VAR && s.owner.kind != MTH) return true; } |
aoqi@0 | 3018 | else if (target == names.METHOD) |
aoqi@0 | 3019 | { if (s.kind == MTH && !s.isConstructor()) return true; } |
aoqi@0 | 3020 | else if (target == names.PARAMETER) |
aoqi@0 | 3021 | { if (s.kind == VAR && |
aoqi@0 | 3022 | s.owner.kind == MTH && |
aoqi@0 | 3023 | (s.flags() & PARAMETER) != 0) |
aoqi@0 | 3024 | return true; |
aoqi@0 | 3025 | } |
aoqi@0 | 3026 | else if (target == names.CONSTRUCTOR) |
aoqi@0 | 3027 | { if (s.kind == MTH && s.isConstructor()) return true; } |
aoqi@0 | 3028 | else if (target == names.LOCAL_VARIABLE) |
aoqi@0 | 3029 | { if (s.kind == VAR && s.owner.kind == MTH && |
aoqi@0 | 3030 | (s.flags() & PARAMETER) == 0) |
aoqi@0 | 3031 | return true; |
aoqi@0 | 3032 | } |
aoqi@0 | 3033 | else if (target == names.ANNOTATION_TYPE) |
aoqi@0 | 3034 | { if (s.kind == TYP && (s.flags() & ANNOTATION) != 0) |
aoqi@0 | 3035 | return true; |
aoqi@0 | 3036 | } |
aoqi@0 | 3037 | else if (target == names.PACKAGE) |
aoqi@0 | 3038 | { if (s.kind == PCK) return true; } |
aoqi@0 | 3039 | else if (target == names.TYPE_USE) |
aoqi@0 | 3040 | { if (s.kind == TYP || |
aoqi@0 | 3041 | s.kind == VAR || |
aoqi@0 | 3042 | (s.kind == MTH && !s.isConstructor() && |
aoqi@0 | 3043 | !s.type.getReturnType().hasTag(VOID)) || |
aoqi@0 | 3044 | (s.kind == MTH && s.isConstructor())) |
aoqi@0 | 3045 | return true; |
aoqi@0 | 3046 | } |
aoqi@0 | 3047 | else if (target == names.TYPE_PARAMETER) |
aoqi@0 | 3048 | { if (s.kind == TYP && s.type.hasTag(TYPEVAR)) |
aoqi@0 | 3049 | return true; |
aoqi@0 | 3050 | } |
aoqi@0 | 3051 | else |
aoqi@0 | 3052 | return true; // recovery |
aoqi@0 | 3053 | } |
aoqi@0 | 3054 | return false; |
aoqi@0 | 3055 | } |
aoqi@0 | 3056 | |
aoqi@0 | 3057 | |
aoqi@0 | 3058 | Attribute.Array getAttributeTargetAttribute(Symbol s) { |
aoqi@0 | 3059 | Attribute.Compound atTarget = |
aoqi@0 | 3060 | s.attribute(syms.annotationTargetType.tsym); |
aoqi@0 | 3061 | if (atTarget == null) return null; // ok, is applicable |
aoqi@0 | 3062 | Attribute atValue = atTarget.member(names.value); |
aoqi@0 | 3063 | if (!(atValue instanceof Attribute.Array)) return null; // error recovery |
aoqi@0 | 3064 | return (Attribute.Array) atValue; |
aoqi@0 | 3065 | } |
aoqi@0 | 3066 | |
aoqi@0 | 3067 | private final Name[] dfltTargetMeta; |
aoqi@0 | 3068 | private Name[] defaultTargetMetaInfo(JCAnnotation a, Symbol s) { |
aoqi@0 | 3069 | return dfltTargetMeta; |
aoqi@0 | 3070 | } |
aoqi@0 | 3071 | |
aoqi@0 | 3072 | /** Check an annotation value. |
aoqi@0 | 3073 | * |
aoqi@0 | 3074 | * @param a The annotation tree to check |
aoqi@0 | 3075 | * @return true if this annotation tree is valid, otherwise false |
aoqi@0 | 3076 | */ |
aoqi@0 | 3077 | public boolean validateAnnotationDeferErrors(JCAnnotation a) { |
aoqi@0 | 3078 | boolean res = false; |
aoqi@0 | 3079 | final Log.DiagnosticHandler diagHandler = new Log.DiscardDiagnosticHandler(log); |
aoqi@0 | 3080 | try { |
aoqi@0 | 3081 | res = validateAnnotation(a); |
aoqi@0 | 3082 | } finally { |
aoqi@0 | 3083 | log.popDiagnosticHandler(diagHandler); |
aoqi@0 | 3084 | } |
aoqi@0 | 3085 | return res; |
aoqi@0 | 3086 | } |
aoqi@0 | 3087 | |
aoqi@0 | 3088 | private boolean validateAnnotation(JCAnnotation a) { |
aoqi@0 | 3089 | boolean isValid = true; |
aoqi@0 | 3090 | // collect an inventory of the annotation elements |
aoqi@0 | 3091 | Set<MethodSymbol> members = new LinkedHashSet<MethodSymbol>(); |
aoqi@0 | 3092 | for (Scope.Entry e = a.annotationType.type.tsym.members().elems; |
aoqi@0 | 3093 | e != null; |
aoqi@0 | 3094 | e = e.sibling) |
aoqi@0 | 3095 | if (e.sym.kind == MTH && e.sym.name != names.clinit && |
aoqi@0 | 3096 | (e.sym.flags() & SYNTHETIC) == 0) |
aoqi@0 | 3097 | members.add((MethodSymbol) e.sym); |
aoqi@0 | 3098 | |
aoqi@0 | 3099 | // remove the ones that are assigned values |
aoqi@0 | 3100 | for (JCTree arg : a.args) { |
aoqi@0 | 3101 | if (!arg.hasTag(ASSIGN)) continue; // recovery |
aoqi@0 | 3102 | JCAssign assign = (JCAssign) arg; |
aoqi@0 | 3103 | Symbol m = TreeInfo.symbol(assign.lhs); |
aoqi@0 | 3104 | if (m == null || m.type.isErroneous()) continue; |
aoqi@0 | 3105 | if (!members.remove(m)) { |
aoqi@0 | 3106 | isValid = false; |
aoqi@0 | 3107 | log.error(assign.lhs.pos(), "duplicate.annotation.member.value", |
aoqi@0 | 3108 | m.name, a.type); |
aoqi@0 | 3109 | } |
aoqi@0 | 3110 | } |
aoqi@0 | 3111 | |
aoqi@0 | 3112 | // all the remaining ones better have default values |
aoqi@0 | 3113 | List<Name> missingDefaults = List.nil(); |
aoqi@0 | 3114 | for (MethodSymbol m : members) { |
aoqi@0 | 3115 | if (m.defaultValue == null && !m.type.isErroneous()) { |
aoqi@0 | 3116 | missingDefaults = missingDefaults.append(m.name); |
aoqi@0 | 3117 | } |
aoqi@0 | 3118 | } |
aoqi@0 | 3119 | missingDefaults = missingDefaults.reverse(); |
aoqi@0 | 3120 | if (missingDefaults.nonEmpty()) { |
aoqi@0 | 3121 | isValid = false; |
aoqi@0 | 3122 | String key = (missingDefaults.size() > 1) |
aoqi@0 | 3123 | ? "annotation.missing.default.value.1" |
aoqi@0 | 3124 | : "annotation.missing.default.value"; |
aoqi@0 | 3125 | log.error(a.pos(), key, a.type, missingDefaults); |
aoqi@0 | 3126 | } |
aoqi@0 | 3127 | |
aoqi@0 | 3128 | // special case: java.lang.annotation.Target must not have |
aoqi@0 | 3129 | // repeated values in its value member |
aoqi@0 | 3130 | if (a.annotationType.type.tsym != syms.annotationTargetType.tsym || |
aoqi@0 | 3131 | a.args.tail == null) |
aoqi@0 | 3132 | return isValid; |
aoqi@0 | 3133 | |
aoqi@0 | 3134 | if (!a.args.head.hasTag(ASSIGN)) return false; // error recovery |
aoqi@0 | 3135 | JCAssign assign = (JCAssign) a.args.head; |
aoqi@0 | 3136 | Symbol m = TreeInfo.symbol(assign.lhs); |
aoqi@0 | 3137 | if (m.name != names.value) return false; |
aoqi@0 | 3138 | JCTree rhs = assign.rhs; |
aoqi@0 | 3139 | if (!rhs.hasTag(NEWARRAY)) return false; |
aoqi@0 | 3140 | JCNewArray na = (JCNewArray) rhs; |
aoqi@0 | 3141 | Set<Symbol> targets = new HashSet<Symbol>(); |
aoqi@0 | 3142 | for (JCTree elem : na.elems) { |
aoqi@0 | 3143 | if (!targets.add(TreeInfo.symbol(elem))) { |
aoqi@0 | 3144 | isValid = false; |
aoqi@0 | 3145 | log.error(elem.pos(), "repeated.annotation.target"); |
aoqi@0 | 3146 | } |
aoqi@0 | 3147 | } |
aoqi@0 | 3148 | return isValid; |
aoqi@0 | 3149 | } |
aoqi@0 | 3150 | |
aoqi@0 | 3151 | void checkDeprecatedAnnotation(DiagnosticPosition pos, Symbol s) { |
aoqi@0 | 3152 | if (allowAnnotations && |
aoqi@0 | 3153 | lint.isEnabled(LintCategory.DEP_ANN) && |
aoqi@0 | 3154 | (s.flags() & DEPRECATED) != 0 && |
aoqi@0 | 3155 | !syms.deprecatedType.isErroneous() && |
aoqi@0 | 3156 | s.attribute(syms.deprecatedType.tsym) == null) { |
aoqi@0 | 3157 | log.warning(LintCategory.DEP_ANN, |
aoqi@0 | 3158 | pos, "missing.deprecated.annotation"); |
aoqi@0 | 3159 | } |
aoqi@0 | 3160 | } |
aoqi@0 | 3161 | |
aoqi@0 | 3162 | void checkDeprecated(final DiagnosticPosition pos, final Symbol other, final Symbol s) { |
aoqi@0 | 3163 | if ((s.flags() & DEPRECATED) != 0 && |
aoqi@0 | 3164 | (other.flags() & DEPRECATED) == 0 && |
aoqi@0 | 3165 | s.outermostClass() != other.outermostClass()) { |
aoqi@0 | 3166 | deferredLintHandler.report(new DeferredLintHandler.LintLogger() { |
aoqi@0 | 3167 | @Override |
aoqi@0 | 3168 | public void report() { |
aoqi@0 | 3169 | warnDeprecated(pos, s); |
aoqi@0 | 3170 | } |
aoqi@0 | 3171 | }); |
aoqi@0 | 3172 | } |
aoqi@0 | 3173 | } |
aoqi@0 | 3174 | |
aoqi@0 | 3175 | void checkSunAPI(final DiagnosticPosition pos, final Symbol s) { |
aoqi@0 | 3176 | if ((s.flags() & PROPRIETARY) != 0) { |
aoqi@0 | 3177 | deferredLintHandler.report(new DeferredLintHandler.LintLogger() { |
aoqi@0 | 3178 | public void report() { |
aoqi@0 | 3179 | if (enableSunApiLintControl) |
aoqi@0 | 3180 | warnSunApi(pos, "sun.proprietary", s); |
aoqi@0 | 3181 | else |
aoqi@0 | 3182 | log.mandatoryWarning(pos, "sun.proprietary", s); |
aoqi@0 | 3183 | } |
aoqi@0 | 3184 | }); |
aoqi@0 | 3185 | } |
aoqi@0 | 3186 | } |
aoqi@0 | 3187 | |
aoqi@0 | 3188 | void checkProfile(final DiagnosticPosition pos, final Symbol s) { |
aoqi@0 | 3189 | if (profile != Profile.DEFAULT && (s.flags() & NOT_IN_PROFILE) != 0) { |
aoqi@0 | 3190 | log.error(pos, "not.in.profile", s, profile); |
aoqi@0 | 3191 | } |
aoqi@0 | 3192 | } |
aoqi@0 | 3193 | |
aoqi@0 | 3194 | /* ************************************************************************* |
aoqi@0 | 3195 | * Check for recursive annotation elements. |
aoqi@0 | 3196 | **************************************************************************/ |
aoqi@0 | 3197 | |
aoqi@0 | 3198 | /** Check for cycles in the graph of annotation elements. |
aoqi@0 | 3199 | */ |
aoqi@0 | 3200 | void checkNonCyclicElements(JCClassDecl tree) { |
aoqi@0 | 3201 | if ((tree.sym.flags_field & ANNOTATION) == 0) return; |
aoqi@0 | 3202 | Assert.check((tree.sym.flags_field & LOCKED) == 0); |
aoqi@0 | 3203 | try { |
aoqi@0 | 3204 | tree.sym.flags_field |= LOCKED; |
aoqi@0 | 3205 | for (JCTree def : tree.defs) { |
aoqi@0 | 3206 | if (!def.hasTag(METHODDEF)) continue; |
aoqi@0 | 3207 | JCMethodDecl meth = (JCMethodDecl)def; |
aoqi@0 | 3208 | checkAnnotationResType(meth.pos(), meth.restype.type); |
aoqi@0 | 3209 | } |
aoqi@0 | 3210 | } finally { |
aoqi@0 | 3211 | tree.sym.flags_field &= ~LOCKED; |
aoqi@0 | 3212 | tree.sym.flags_field |= ACYCLIC_ANN; |
aoqi@0 | 3213 | } |
aoqi@0 | 3214 | } |
aoqi@0 | 3215 | |
aoqi@0 | 3216 | void checkNonCyclicElementsInternal(DiagnosticPosition pos, TypeSymbol tsym) { |
aoqi@0 | 3217 | if ((tsym.flags_field & ACYCLIC_ANN) != 0) |
aoqi@0 | 3218 | return; |
aoqi@0 | 3219 | if ((tsym.flags_field & LOCKED) != 0) { |
aoqi@0 | 3220 | log.error(pos, "cyclic.annotation.element"); |
aoqi@0 | 3221 | return; |
aoqi@0 | 3222 | } |
aoqi@0 | 3223 | try { |
aoqi@0 | 3224 | tsym.flags_field |= LOCKED; |
aoqi@0 | 3225 | for (Scope.Entry e = tsym.members().elems; e != null; e = e.sibling) { |
aoqi@0 | 3226 | Symbol s = e.sym; |
aoqi@0 | 3227 | if (s.kind != Kinds.MTH) |
aoqi@0 | 3228 | continue; |
aoqi@0 | 3229 | checkAnnotationResType(pos, ((MethodSymbol)s).type.getReturnType()); |
aoqi@0 | 3230 | } |
aoqi@0 | 3231 | } finally { |
aoqi@0 | 3232 | tsym.flags_field &= ~LOCKED; |
aoqi@0 | 3233 | tsym.flags_field |= ACYCLIC_ANN; |
aoqi@0 | 3234 | } |
aoqi@0 | 3235 | } |
aoqi@0 | 3236 | |
aoqi@0 | 3237 | void checkAnnotationResType(DiagnosticPosition pos, Type type) { |
aoqi@0 | 3238 | switch (type.getTag()) { |
aoqi@0 | 3239 | case CLASS: |
aoqi@0 | 3240 | if ((type.tsym.flags() & ANNOTATION) != 0) |
aoqi@0 | 3241 | checkNonCyclicElementsInternal(pos, type.tsym); |
aoqi@0 | 3242 | break; |
aoqi@0 | 3243 | case ARRAY: |
aoqi@0 | 3244 | checkAnnotationResType(pos, types.elemtype(type)); |
aoqi@0 | 3245 | break; |
aoqi@0 | 3246 | default: |
aoqi@0 | 3247 | break; // int etc |
aoqi@0 | 3248 | } |
aoqi@0 | 3249 | } |
aoqi@0 | 3250 | |
aoqi@0 | 3251 | /* ************************************************************************* |
aoqi@0 | 3252 | * Check for cycles in the constructor call graph. |
aoqi@0 | 3253 | **************************************************************************/ |
aoqi@0 | 3254 | |
aoqi@0 | 3255 | /** Check for cycles in the graph of constructors calling other |
aoqi@0 | 3256 | * constructors. |
aoqi@0 | 3257 | */ |
aoqi@0 | 3258 | void checkCyclicConstructors(JCClassDecl tree) { |
aoqi@0 | 3259 | Map<Symbol,Symbol> callMap = new HashMap<Symbol, Symbol>(); |
aoqi@0 | 3260 | |
aoqi@0 | 3261 | // enter each constructor this-call into the map |
aoqi@0 | 3262 | for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) { |
aoqi@0 | 3263 | JCMethodInvocation app = TreeInfo.firstConstructorCall(l.head); |
aoqi@0 | 3264 | if (app == null) continue; |
aoqi@0 | 3265 | JCMethodDecl meth = (JCMethodDecl) l.head; |
aoqi@0 | 3266 | if (TreeInfo.name(app.meth) == names._this) { |
aoqi@0 | 3267 | callMap.put(meth.sym, TreeInfo.symbol(app.meth)); |
aoqi@0 | 3268 | } else { |
aoqi@0 | 3269 | meth.sym.flags_field |= ACYCLIC; |
aoqi@0 | 3270 | } |
aoqi@0 | 3271 | } |
aoqi@0 | 3272 | |
aoqi@0 | 3273 | // Check for cycles in the map |
aoqi@0 | 3274 | Symbol[] ctors = new Symbol[0]; |
aoqi@0 | 3275 | ctors = callMap.keySet().toArray(ctors); |
aoqi@0 | 3276 | for (Symbol caller : ctors) { |
aoqi@0 | 3277 | checkCyclicConstructor(tree, caller, callMap); |
aoqi@0 | 3278 | } |
aoqi@0 | 3279 | } |
aoqi@0 | 3280 | |
aoqi@0 | 3281 | /** Look in the map to see if the given constructor is part of a |
aoqi@0 | 3282 | * call cycle. |
aoqi@0 | 3283 | */ |
aoqi@0 | 3284 | private void checkCyclicConstructor(JCClassDecl tree, Symbol ctor, |
aoqi@0 | 3285 | Map<Symbol,Symbol> callMap) { |
aoqi@0 | 3286 | if (ctor != null && (ctor.flags_field & ACYCLIC) == 0) { |
aoqi@0 | 3287 | if ((ctor.flags_field & LOCKED) != 0) { |
aoqi@0 | 3288 | log.error(TreeInfo.diagnosticPositionFor(ctor, tree), |
aoqi@0 | 3289 | "recursive.ctor.invocation"); |
aoqi@0 | 3290 | } else { |
aoqi@0 | 3291 | ctor.flags_field |= LOCKED; |
aoqi@0 | 3292 | checkCyclicConstructor(tree, callMap.remove(ctor), callMap); |
aoqi@0 | 3293 | ctor.flags_field &= ~LOCKED; |
aoqi@0 | 3294 | } |
aoqi@0 | 3295 | ctor.flags_field |= ACYCLIC; |
aoqi@0 | 3296 | } |
aoqi@0 | 3297 | } |
aoqi@0 | 3298 | |
aoqi@0 | 3299 | /* ************************************************************************* |
aoqi@0 | 3300 | * Miscellaneous |
aoqi@0 | 3301 | **************************************************************************/ |
aoqi@0 | 3302 | |
aoqi@0 | 3303 | /** |
aoqi@0 | 3304 | * Return the opcode of the operator but emit an error if it is an |
aoqi@0 | 3305 | * error. |
aoqi@0 | 3306 | * @param pos position for error reporting. |
aoqi@0 | 3307 | * @param operator an operator |
aoqi@0 | 3308 | * @param tag a tree tag |
aoqi@0 | 3309 | * @param left type of left hand side |
aoqi@0 | 3310 | * @param right type of right hand side |
aoqi@0 | 3311 | */ |
aoqi@0 | 3312 | int checkOperator(DiagnosticPosition pos, |
aoqi@0 | 3313 | OperatorSymbol operator, |
aoqi@0 | 3314 | JCTree.Tag tag, |
aoqi@0 | 3315 | Type left, |
aoqi@0 | 3316 | Type right) { |
aoqi@0 | 3317 | if (operator.opcode == ByteCodes.error) { |
aoqi@0 | 3318 | log.error(pos, |
aoqi@0 | 3319 | "operator.cant.be.applied.1", |
aoqi@0 | 3320 | treeinfo.operatorName(tag), |
aoqi@0 | 3321 | left, right); |
aoqi@0 | 3322 | } |
aoqi@0 | 3323 | return operator.opcode; |
aoqi@0 | 3324 | } |
aoqi@0 | 3325 | |
aoqi@0 | 3326 | |
aoqi@0 | 3327 | /** |
aoqi@0 | 3328 | * Check for division by integer constant zero |
aoqi@0 | 3329 | * @param pos Position for error reporting. |
aoqi@0 | 3330 | * @param operator The operator for the expression |
aoqi@0 | 3331 | * @param operand The right hand operand for the expression |
aoqi@0 | 3332 | */ |
aoqi@0 | 3333 | void checkDivZero(DiagnosticPosition pos, Symbol operator, Type operand) { |
aoqi@0 | 3334 | if (operand.constValue() != null |
aoqi@0 | 3335 | && lint.isEnabled(LintCategory.DIVZERO) |
aoqi@0 | 3336 | && operand.getTag().isSubRangeOf(LONG) |
aoqi@0 | 3337 | && ((Number) (operand.constValue())).longValue() == 0) { |
aoqi@0 | 3338 | int opc = ((OperatorSymbol)operator).opcode; |
aoqi@0 | 3339 | if (opc == ByteCodes.idiv || opc == ByteCodes.imod |
aoqi@0 | 3340 | || opc == ByteCodes.ldiv || opc == ByteCodes.lmod) { |
aoqi@0 | 3341 | log.warning(LintCategory.DIVZERO, pos, "div.zero"); |
aoqi@0 | 3342 | } |
aoqi@0 | 3343 | } |
aoqi@0 | 3344 | } |
aoqi@0 | 3345 | |
aoqi@0 | 3346 | /** |
aoqi@0 | 3347 | * Check for empty statements after if |
aoqi@0 | 3348 | */ |
aoqi@0 | 3349 | void checkEmptyIf(JCIf tree) { |
aoqi@0 | 3350 | if (tree.thenpart.hasTag(SKIP) && tree.elsepart == null && |
aoqi@0 | 3351 | lint.isEnabled(LintCategory.EMPTY)) |
aoqi@0 | 3352 | log.warning(LintCategory.EMPTY, tree.thenpart.pos(), "empty.if"); |
aoqi@0 | 3353 | } |
aoqi@0 | 3354 | |
aoqi@0 | 3355 | /** Check that symbol is unique in given scope. |
aoqi@0 | 3356 | * @param pos Position for error reporting. |
aoqi@0 | 3357 | * @param sym The symbol. |
aoqi@0 | 3358 | * @param s The scope. |
aoqi@0 | 3359 | */ |
aoqi@0 | 3360 | boolean checkUnique(DiagnosticPosition pos, Symbol sym, Scope s) { |
aoqi@0 | 3361 | if (sym.type.isErroneous()) |
aoqi@0 | 3362 | return true; |
aoqi@0 | 3363 | if (sym.owner.name == names.any) return false; |
aoqi@0 | 3364 | for (Scope.Entry e = s.lookup(sym.name); e.scope == s; e = e.next()) { |
aoqi@0 | 3365 | if (sym != e.sym && |
aoqi@0 | 3366 | (e.sym.flags() & CLASH) == 0 && |
aoqi@0 | 3367 | sym.kind == e.sym.kind && |
aoqi@0 | 3368 | sym.name != names.error && |
aoqi@0 | 3369 | (sym.kind != MTH || |
aoqi@0 | 3370 | types.hasSameArgs(sym.type, e.sym.type) || |
aoqi@0 | 3371 | types.hasSameArgs(types.erasure(sym.type), types.erasure(e.sym.type)))) { |
aoqi@0 | 3372 | if ((sym.flags() & VARARGS) != (e.sym.flags() & VARARGS)) { |
aoqi@0 | 3373 | varargsDuplicateError(pos, sym, e.sym); |
aoqi@0 | 3374 | return true; |
aoqi@0 | 3375 | } else if (sym.kind == MTH && !types.hasSameArgs(sym.type, e.sym.type, false)) { |
aoqi@0 | 3376 | duplicateErasureError(pos, sym, e.sym); |
aoqi@0 | 3377 | sym.flags_field |= CLASH; |
aoqi@0 | 3378 | return true; |
aoqi@0 | 3379 | } else { |
aoqi@0 | 3380 | duplicateError(pos, e.sym); |
aoqi@0 | 3381 | return false; |
aoqi@0 | 3382 | } |
aoqi@0 | 3383 | } |
aoqi@0 | 3384 | } |
aoqi@0 | 3385 | return true; |
aoqi@0 | 3386 | } |
aoqi@0 | 3387 | |
aoqi@0 | 3388 | /** Report duplicate declaration error. |
aoqi@0 | 3389 | */ |
aoqi@0 | 3390 | void duplicateErasureError(DiagnosticPosition pos, Symbol sym1, Symbol sym2) { |
aoqi@0 | 3391 | if (!sym1.type.isErroneous() && !sym2.type.isErroneous()) { |
aoqi@0 | 3392 | log.error(pos, "name.clash.same.erasure", sym1, sym2); |
aoqi@0 | 3393 | } |
aoqi@0 | 3394 | } |
aoqi@0 | 3395 | |
aoqi@0 | 3396 | /** Check that single-type import is not already imported or top-level defined, |
aoqi@0 | 3397 | * but make an exception for two single-type imports which denote the same type. |
aoqi@0 | 3398 | * @param pos Position for error reporting. |
aoqi@0 | 3399 | * @param sym The symbol. |
aoqi@0 | 3400 | * @param s The scope |
aoqi@0 | 3401 | */ |
aoqi@0 | 3402 | boolean checkUniqueImport(DiagnosticPosition pos, Symbol sym, Scope s) { |
aoqi@0 | 3403 | return checkUniqueImport(pos, sym, s, false); |
aoqi@0 | 3404 | } |
aoqi@0 | 3405 | |
aoqi@0 | 3406 | /** Check that static single-type import is not already imported or top-level defined, |
aoqi@0 | 3407 | * but make an exception for two single-type imports which denote the same type. |
aoqi@0 | 3408 | * @param pos Position for error reporting. |
aoqi@0 | 3409 | * @param sym The symbol. |
aoqi@0 | 3410 | * @param s The scope |
aoqi@0 | 3411 | */ |
aoqi@0 | 3412 | boolean checkUniqueStaticImport(DiagnosticPosition pos, Symbol sym, Scope s) { |
aoqi@0 | 3413 | return checkUniqueImport(pos, sym, s, true); |
aoqi@0 | 3414 | } |
aoqi@0 | 3415 | |
aoqi@0 | 3416 | /** Check that single-type import is not already imported or top-level defined, |
aoqi@0 | 3417 | * but make an exception for two single-type imports which denote the same type. |
aoqi@0 | 3418 | * @param pos Position for error reporting. |
aoqi@0 | 3419 | * @param sym The symbol. |
aoqi@0 | 3420 | * @param s The scope. |
aoqi@0 | 3421 | * @param staticImport Whether or not this was a static import |
aoqi@0 | 3422 | */ |
aoqi@0 | 3423 | private boolean checkUniqueImport(DiagnosticPosition pos, Symbol sym, Scope s, boolean staticImport) { |
aoqi@0 | 3424 | for (Scope.Entry e = s.lookup(sym.name); e.scope != null; e = e.next()) { |
aoqi@0 | 3425 | // is encountered class entered via a class declaration? |
aoqi@0 | 3426 | boolean isClassDecl = e.scope == s; |
aoqi@0 | 3427 | if ((isClassDecl || sym != e.sym) && |
aoqi@0 | 3428 | sym.kind == e.sym.kind && |
aoqi@0 | 3429 | sym.name != names.error && |
aoqi@0 | 3430 | (!staticImport || !e.isStaticallyImported())) { |
aoqi@0 | 3431 | if (!e.sym.type.isErroneous()) { |
aoqi@0 | 3432 | if (!isClassDecl) { |
aoqi@0 | 3433 | if (staticImport) |
aoqi@0 | 3434 | log.error(pos, "already.defined.static.single.import", e.sym); |
aoqi@0 | 3435 | else |
aoqi@0 | 3436 | log.error(pos, "already.defined.single.import", e.sym); |
aoqi@0 | 3437 | } |
aoqi@0 | 3438 | else if (sym != e.sym) |
aoqi@0 | 3439 | log.error(pos, "already.defined.this.unit", e.sym); |
aoqi@0 | 3440 | } |
aoqi@0 | 3441 | return false; |
aoqi@0 | 3442 | } |
aoqi@0 | 3443 | } |
aoqi@0 | 3444 | return true; |
aoqi@0 | 3445 | } |
aoqi@0 | 3446 | |
aoqi@0 | 3447 | /** Check that a qualified name is in canonical form (for import decls). |
aoqi@0 | 3448 | */ |
aoqi@0 | 3449 | public void checkCanonical(JCTree tree) { |
aoqi@0 | 3450 | if (!isCanonical(tree)) |
aoqi@0 | 3451 | log.error(tree.pos(), "import.requires.canonical", |
aoqi@0 | 3452 | TreeInfo.symbol(tree)); |
aoqi@0 | 3453 | } |
aoqi@0 | 3454 | // where |
aoqi@0 | 3455 | private boolean isCanonical(JCTree tree) { |
aoqi@0 | 3456 | while (tree.hasTag(SELECT)) { |
aoqi@0 | 3457 | JCFieldAccess s = (JCFieldAccess) tree; |
aoqi@0 | 3458 | if (s.sym.owner != TreeInfo.symbol(s.selected)) |
aoqi@0 | 3459 | return false; |
aoqi@0 | 3460 | tree = s.selected; |
aoqi@0 | 3461 | } |
aoqi@0 | 3462 | return true; |
aoqi@0 | 3463 | } |
aoqi@0 | 3464 | |
aoqi@0 | 3465 | /** Check that an auxiliary class is not accessed from any other file than its own. |
aoqi@0 | 3466 | */ |
aoqi@0 | 3467 | void checkForBadAuxiliaryClassAccess(DiagnosticPosition pos, Env<AttrContext> env, ClassSymbol c) { |
aoqi@0 | 3468 | if (lint.isEnabled(Lint.LintCategory.AUXILIARYCLASS) && |
aoqi@0 | 3469 | (c.flags() & AUXILIARY) != 0 && |
aoqi@0 | 3470 | rs.isAccessible(env, c) && |
aoqi@0 | 3471 | !fileManager.isSameFile(c.sourcefile, env.toplevel.sourcefile)) |
aoqi@0 | 3472 | { |
aoqi@0 | 3473 | log.warning(pos, "auxiliary.class.accessed.from.outside.of.its.source.file", |
aoqi@0 | 3474 | c, c.sourcefile); |
aoqi@0 | 3475 | } |
aoqi@0 | 3476 | } |
aoqi@0 | 3477 | |
aoqi@0 | 3478 | private class ConversionWarner extends Warner { |
aoqi@0 | 3479 | final String uncheckedKey; |
aoqi@0 | 3480 | final Type found; |
aoqi@0 | 3481 | final Type expected; |
aoqi@0 | 3482 | public ConversionWarner(DiagnosticPosition pos, String uncheckedKey, Type found, Type expected) { |
aoqi@0 | 3483 | super(pos); |
aoqi@0 | 3484 | this.uncheckedKey = uncheckedKey; |
aoqi@0 | 3485 | this.found = found; |
aoqi@0 | 3486 | this.expected = expected; |
aoqi@0 | 3487 | } |
aoqi@0 | 3488 | |
aoqi@0 | 3489 | @Override |
aoqi@0 | 3490 | public void warn(LintCategory lint) { |
aoqi@0 | 3491 | boolean warned = this.warned; |
aoqi@0 | 3492 | super.warn(lint); |
aoqi@0 | 3493 | if (warned) return; // suppress redundant diagnostics |
aoqi@0 | 3494 | switch (lint) { |
aoqi@0 | 3495 | case UNCHECKED: |
aoqi@0 | 3496 | Check.this.warnUnchecked(pos(), "prob.found.req", diags.fragment(uncheckedKey), found, expected); |
aoqi@0 | 3497 | break; |
aoqi@0 | 3498 | case VARARGS: |
aoqi@0 | 3499 | if (method != null && |
aoqi@0 | 3500 | method.attribute(syms.trustMeType.tsym) != null && |
aoqi@0 | 3501 | isTrustMeAllowedOnMethod(method) && |
aoqi@0 | 3502 | !types.isReifiable(method.type.getParameterTypes().last())) { |
aoqi@0 | 3503 | Check.this.warnUnsafeVararg(pos(), "varargs.unsafe.use.varargs.param", method.params.last()); |
aoqi@0 | 3504 | } |
aoqi@0 | 3505 | break; |
aoqi@0 | 3506 | default: |
aoqi@0 | 3507 | throw new AssertionError("Unexpected lint: " + lint); |
aoqi@0 | 3508 | } |
aoqi@0 | 3509 | } |
aoqi@0 | 3510 | } |
aoqi@0 | 3511 | |
aoqi@0 | 3512 | public Warner castWarner(DiagnosticPosition pos, Type found, Type expected) { |
aoqi@0 | 3513 | return new ConversionWarner(pos, "unchecked.cast.to.type", found, expected); |
aoqi@0 | 3514 | } |
aoqi@0 | 3515 | |
aoqi@0 | 3516 | public Warner convertWarner(DiagnosticPosition pos, Type found, Type expected) { |
aoqi@0 | 3517 | return new ConversionWarner(pos, "unchecked.assign", found, expected); |
aoqi@0 | 3518 | } |
aoqi@0 | 3519 | |
aoqi@0 | 3520 | public void checkFunctionalInterface(JCClassDecl tree, ClassSymbol cs) { |
aoqi@0 | 3521 | Compound functionalType = cs.attribute(syms.functionalInterfaceType.tsym); |
aoqi@0 | 3522 | |
aoqi@0 | 3523 | if (functionalType != null) { |
aoqi@0 | 3524 | try { |
aoqi@0 | 3525 | types.findDescriptorSymbol((TypeSymbol)cs); |
aoqi@0 | 3526 | } catch (Types.FunctionDescriptorLookupError ex) { |
aoqi@0 | 3527 | DiagnosticPosition pos = tree.pos(); |
aoqi@0 | 3528 | for (JCAnnotation a : tree.getModifiers().annotations) { |
aoqi@0 | 3529 | if (a.annotationType.type.tsym == syms.functionalInterfaceType.tsym) { |
aoqi@0 | 3530 | pos = a.pos(); |
aoqi@0 | 3531 | break; |
aoqi@0 | 3532 | } |
aoqi@0 | 3533 | } |
aoqi@0 | 3534 | log.error(pos, "bad.functional.intf.anno.1", ex.getDiagnostic()); |
aoqi@0 | 3535 | } |
aoqi@0 | 3536 | } |
aoqi@0 | 3537 | } |
aoqi@0 | 3538 | } |