Wed, 15 Jul 2009 10:25:01 +0100
6846972: cannot access member of raw type when erasure change overriding into overloading
Summary: fix of 6400189 caused a nasty problem in method resolution
Reviewed-by: jjg
1 /*
2 * Copyright 1999-2008 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
26 package com.sun.tools.javac.comp;
28 import com.sun.tools.javac.util.*;
29 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
30 import com.sun.tools.javac.code.*;
31 import com.sun.tools.javac.jvm.*;
32 import com.sun.tools.javac.tree.*;
33 import com.sun.tools.javac.api.Formattable.LocalizedString;
34 import static com.sun.tools.javac.comp.Resolve.MethodResolutionPhase.*;
36 import com.sun.tools.javac.code.Type.*;
37 import com.sun.tools.javac.code.Symbol.*;
38 import com.sun.tools.javac.tree.JCTree.*;
40 import static com.sun.tools.javac.code.Flags.*;
41 import static com.sun.tools.javac.code.Kinds.*;
42 import static com.sun.tools.javac.code.TypeTags.*;
43 import javax.lang.model.element.ElementVisitor;
45 import java.util.Map;
46 import java.util.HashMap;
48 /** Helper class for name resolution, used mostly by the attribution phase.
49 *
50 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
51 * you write code that depends on this, you do so at your own risk.
52 * This code and its internal interfaces are subject to change or
53 * deletion without notice.</b>
54 */
55 public class Resolve {
56 protected static final Context.Key<Resolve> resolveKey =
57 new Context.Key<Resolve>();
59 Names names;
60 Log log;
61 Symtab syms;
62 Check chk;
63 Infer infer;
64 ClassReader reader;
65 TreeInfo treeinfo;
66 Types types;
67 JCDiagnostic.Factory diags;
68 public final boolean boxingEnabled; // = source.allowBoxing();
69 public final boolean varargsEnabled; // = source.allowVarargs();
70 public final boolean allowInvokedynamic; // = options.get("invokedynamic");
71 private final boolean debugResolve;
73 public static Resolve instance(Context context) {
74 Resolve instance = context.get(resolveKey);
75 if (instance == null)
76 instance = new Resolve(context);
77 return instance;
78 }
80 protected Resolve(Context context) {
81 context.put(resolveKey, this);
82 syms = Symtab.instance(context);
84 varNotFound = new
85 SymbolNotFoundError(ABSENT_VAR);
86 wrongMethod = new
87 InapplicableSymbolError(syms.errSymbol);
88 wrongMethods = new
89 InapplicableSymbolsError(syms.errSymbol);
90 methodNotFound = new
91 SymbolNotFoundError(ABSENT_MTH);
92 typeNotFound = new
93 SymbolNotFoundError(ABSENT_TYP);
95 names = Names.instance(context);
96 log = Log.instance(context);
97 chk = Check.instance(context);
98 infer = Infer.instance(context);
99 reader = ClassReader.instance(context);
100 treeinfo = TreeInfo.instance(context);
101 types = Types.instance(context);
102 diags = JCDiagnostic.Factory.instance(context);
103 Source source = Source.instance(context);
104 boxingEnabled = source.allowBoxing();
105 varargsEnabled = source.allowVarargs();
106 Options options = Options.instance(context);
107 debugResolve = options.get("debugresolve") != null;
108 allowInvokedynamic = options.get("invokedynamic") != null;
109 }
111 /** error symbols, which are returned when resolution fails
112 */
113 final SymbolNotFoundError varNotFound;
114 final InapplicableSymbolError wrongMethod;
115 final InapplicableSymbolsError wrongMethods;
116 final SymbolNotFoundError methodNotFound;
117 final SymbolNotFoundError typeNotFound;
119 /* ************************************************************************
120 * Identifier resolution
121 *************************************************************************/
123 /** An environment is "static" if its static level is greater than
124 * the one of its outer environment
125 */
126 static boolean isStatic(Env<AttrContext> env) {
127 return env.info.staticLevel > env.outer.info.staticLevel;
128 }
130 /** An environment is an "initializer" if it is a constructor or
131 * an instance initializer.
132 */
133 static boolean isInitializer(Env<AttrContext> env) {
134 Symbol owner = env.info.scope.owner;
135 return owner.isConstructor() ||
136 owner.owner.kind == TYP &&
137 (owner.kind == VAR ||
138 owner.kind == MTH && (owner.flags() & BLOCK) != 0) &&
139 (owner.flags() & STATIC) == 0;
140 }
142 /** Is class accessible in given evironment?
143 * @param env The current environment.
144 * @param c The class whose accessibility is checked.
145 */
146 public boolean isAccessible(Env<AttrContext> env, TypeSymbol c) {
147 switch ((short)(c.flags() & AccessFlags)) {
148 case PRIVATE:
149 return
150 env.enclClass.sym.outermostClass() ==
151 c.owner.outermostClass();
152 case 0:
153 return
154 env.toplevel.packge == c.owner // fast special case
155 ||
156 env.toplevel.packge == c.packge()
157 ||
158 // Hack: this case is added since synthesized default constructors
159 // of anonymous classes should be allowed to access
160 // classes which would be inaccessible otherwise.
161 env.enclMethod != null &&
162 (env.enclMethod.mods.flags & ANONCONSTR) != 0;
163 default: // error recovery
164 case PUBLIC:
165 return true;
166 case PROTECTED:
167 return
168 env.toplevel.packge == c.owner // fast special case
169 ||
170 env.toplevel.packge == c.packge()
171 ||
172 isInnerSubClass(env.enclClass.sym, c.owner);
173 }
174 }
175 //where
176 /** Is given class a subclass of given base class, or an inner class
177 * of a subclass?
178 * Return null if no such class exists.
179 * @param c The class which is the subclass or is contained in it.
180 * @param base The base class
181 */
182 private boolean isInnerSubClass(ClassSymbol c, Symbol base) {
183 while (c != null && !c.isSubClass(base, types)) {
184 c = c.owner.enclClass();
185 }
186 return c != null;
187 }
189 boolean isAccessible(Env<AttrContext> env, Type t) {
190 return (t.tag == ARRAY)
191 ? isAccessible(env, types.elemtype(t))
192 : isAccessible(env, t.tsym);
193 }
195 /** Is symbol accessible as a member of given type in given evironment?
196 * @param env The current environment.
197 * @param site The type of which the tested symbol is regarded
198 * as a member.
199 * @param sym The symbol.
200 */
201 public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym) {
202 if (sym.name == names.init && sym.owner != site.tsym) return false;
203 ClassSymbol sub;
204 switch ((short)(sym.flags() & AccessFlags)) {
205 case PRIVATE:
206 return
207 (env.enclClass.sym == sym.owner // fast special case
208 ||
209 env.enclClass.sym.outermostClass() ==
210 sym.owner.outermostClass())
211 &&
212 sym.isInheritedIn(site.tsym, types);
213 case 0:
214 return
215 (env.toplevel.packge == sym.owner.owner // fast special case
216 ||
217 env.toplevel.packge == sym.packge())
218 &&
219 isAccessible(env, site)
220 &&
221 sym.isInheritedIn(site.tsym, types)
222 &&
223 notOverriddenIn(site, sym);
224 case PROTECTED:
225 return
226 (env.toplevel.packge == sym.owner.owner // fast special case
227 ||
228 env.toplevel.packge == sym.packge()
229 ||
230 isProtectedAccessible(sym, env.enclClass.sym, site)
231 ||
232 // OK to select instance method or field from 'super' or type name
233 // (but type names should be disallowed elsewhere!)
234 env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
235 &&
236 isAccessible(env, site)
237 &&
238 notOverriddenIn(site, sym);
239 default: // this case includes erroneous combinations as well
240 return isAccessible(env, site) && notOverriddenIn(site, sym);
241 }
242 }
243 //where
244 /* `sym' is accessible only if not overridden by
245 * another symbol which is a member of `site'
246 * (because, if it is overridden, `sym' is not strictly
247 * speaking a member of `site'.)
248 */
249 private boolean notOverriddenIn(Type site, Symbol sym) {
250 if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
251 return true;
252 else {
253 Symbol s2 = ((MethodSymbol)sym).implementation(site.tsym, types, true);
254 return (s2 == null || s2 == sym ||
255 !types.isSubSignature(types.memberType(site, s2), types.memberType(site, sym)));
256 }
257 }
258 //where
259 /** Is given protected symbol accessible if it is selected from given site
260 * and the selection takes place in given class?
261 * @param sym The symbol with protected access
262 * @param c The class where the access takes place
263 * @site The type of the qualifier
264 */
265 private
266 boolean isProtectedAccessible(Symbol sym, ClassSymbol c, Type site) {
267 while (c != null &&
268 !(c.isSubClass(sym.owner, types) &&
269 (c.flags() & INTERFACE) == 0 &&
270 // In JLS 2e 6.6.2.1, the subclass restriction applies
271 // only to instance fields and methods -- types are excluded
272 // regardless of whether they are declared 'static' or not.
273 ((sym.flags() & STATIC) != 0 || sym.kind == TYP || site.tsym.isSubClass(c, types))))
274 c = c.owner.enclClass();
275 return c != null;
276 }
278 /** Try to instantiate the type of a method so that it fits
279 * given type arguments and argument types. If succesful, return
280 * the method's instantiated type, else return null.
281 * The instantiation will take into account an additional leading
282 * formal parameter if the method is an instance method seen as a member
283 * of un underdetermined site In this case, we treat site as an additional
284 * parameter and the parameters of the class containing the method as
285 * additional type variables that get instantiated.
286 *
287 * @param env The current environment
288 * @param site The type of which the method is a member.
289 * @param m The method symbol.
290 * @param argtypes The invocation's given value arguments.
291 * @param typeargtypes The invocation's given type arguments.
292 * @param allowBoxing Allow boxing conversions of arguments.
293 * @param useVarargs Box trailing arguments into an array for varargs.
294 */
295 Type rawInstantiate(Env<AttrContext> env,
296 Type site,
297 Symbol m,
298 List<Type> argtypes,
299 List<Type> typeargtypes,
300 boolean allowBoxing,
301 boolean useVarargs,
302 Warner warn)
303 throws Infer.InferenceException {
304 if (useVarargs && (m.flags() & VARARGS) == 0) return null;
305 Type mt = types.memberType(site, m);
307 // tvars is the list of formal type variables for which type arguments
308 // need to inferred.
309 List<Type> tvars = env.info.tvars;
310 if (typeargtypes == null) typeargtypes = List.nil();
311 if (mt.tag != FORALL && typeargtypes.nonEmpty()) {
312 // This is not a polymorphic method, but typeargs are supplied
313 // which is fine, see JLS3 15.12.2.1
314 } else if (mt.tag == FORALL && typeargtypes.nonEmpty()) {
315 ForAll pmt = (ForAll) mt;
316 if (typeargtypes.length() != pmt.tvars.length())
317 return null;
318 // Check type arguments are within bounds
319 List<Type> formals = pmt.tvars;
320 List<Type> actuals = typeargtypes;
321 while (formals.nonEmpty() && actuals.nonEmpty()) {
322 List<Type> bounds = types.subst(types.getBounds((TypeVar)formals.head),
323 pmt.tvars, typeargtypes);
324 for (; bounds.nonEmpty(); bounds = bounds.tail)
325 if (!types.isSubtypeUnchecked(actuals.head, bounds.head, warn))
326 return null;
327 formals = formals.tail;
328 actuals = actuals.tail;
329 }
330 mt = types.subst(pmt.qtype, pmt.tvars, typeargtypes);
331 } else if (mt.tag == FORALL) {
332 ForAll pmt = (ForAll) mt;
333 List<Type> tvars1 = types.newInstances(pmt.tvars);
334 tvars = tvars.appendList(tvars1);
335 mt = types.subst(pmt.qtype, pmt.tvars, tvars1);
336 }
338 // find out whether we need to go the slow route via infer
339 boolean instNeeded = tvars.tail != null/*inlined: tvars.nonEmpty()*/;
340 for (List<Type> l = argtypes;
341 l.tail != null/*inlined: l.nonEmpty()*/ && !instNeeded;
342 l = l.tail) {
343 if (l.head.tag == FORALL) instNeeded = true;
344 }
346 if (instNeeded)
347 return
348 infer.instantiateMethod(tvars,
349 (MethodType)mt,
350 argtypes,
351 allowBoxing,
352 useVarargs,
353 warn);
354 return
355 argumentsAcceptable(argtypes, mt.getParameterTypes(),
356 allowBoxing, useVarargs, warn)
357 ? mt
358 : null;
359 }
361 /** Same but returns null instead throwing a NoInstanceException
362 */
363 Type instantiate(Env<AttrContext> env,
364 Type site,
365 Symbol m,
366 List<Type> argtypes,
367 List<Type> typeargtypes,
368 boolean allowBoxing,
369 boolean useVarargs,
370 Warner warn) {
371 try {
372 return rawInstantiate(env, site, m, argtypes, typeargtypes,
373 allowBoxing, useVarargs, warn);
374 } catch (Infer.InferenceException ex) {
375 return null;
376 }
377 }
379 /** Check if a parameter list accepts a list of args.
380 */
381 boolean argumentsAcceptable(List<Type> argtypes,
382 List<Type> formals,
383 boolean allowBoxing,
384 boolean useVarargs,
385 Warner warn) {
386 Type varargsFormal = useVarargs ? formals.last() : null;
387 while (argtypes.nonEmpty() && formals.head != varargsFormal) {
388 boolean works = allowBoxing
389 ? types.isConvertible(argtypes.head, formals.head, warn)
390 : types.isSubtypeUnchecked(argtypes.head, formals.head, warn);
391 if (!works) return false;
392 argtypes = argtypes.tail;
393 formals = formals.tail;
394 }
395 if (formals.head != varargsFormal) return false; // not enough args
396 if (!useVarargs)
397 return argtypes.isEmpty();
398 Type elt = types.elemtype(varargsFormal);
399 while (argtypes.nonEmpty()) {
400 if (!types.isConvertible(argtypes.head, elt, warn))
401 return false;
402 argtypes = argtypes.tail;
403 }
404 return true;
405 }
407 /* ***************************************************************************
408 * Symbol lookup
409 * the following naming conventions for arguments are used
410 *
411 * env is the environment where the symbol was mentioned
412 * site is the type of which the symbol is a member
413 * name is the symbol's name
414 * if no arguments are given
415 * argtypes are the value arguments, if we search for a method
416 *
417 * If no symbol was found, a ResolveError detailing the problem is returned.
418 ****************************************************************************/
420 /** Find field. Synthetic fields are always skipped.
421 * @param env The current environment.
422 * @param site The original type from where the selection takes place.
423 * @param name The name of the field.
424 * @param c The class to search for the field. This is always
425 * a superclass or implemented interface of site's class.
426 */
427 Symbol findField(Env<AttrContext> env,
428 Type site,
429 Name name,
430 TypeSymbol c) {
431 while (c.type.tag == TYPEVAR)
432 c = c.type.getUpperBound().tsym;
433 Symbol bestSoFar = varNotFound;
434 Symbol sym;
435 Scope.Entry e = c.members().lookup(name);
436 while (e.scope != null) {
437 if (e.sym.kind == VAR && (e.sym.flags_field & SYNTHETIC) == 0) {
438 return isAccessible(env, site, e.sym)
439 ? e.sym : new AccessError(env, site, e.sym);
440 }
441 e = e.next();
442 }
443 Type st = types.supertype(c.type);
444 if (st != null && (st.tag == CLASS || st.tag == TYPEVAR)) {
445 sym = findField(env, site, name, st.tsym);
446 if (sym.kind < bestSoFar.kind) bestSoFar = sym;
447 }
448 for (List<Type> l = types.interfaces(c.type);
449 bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
450 l = l.tail) {
451 sym = findField(env, site, name, l.head.tsym);
452 if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
453 sym.owner != bestSoFar.owner)
454 bestSoFar = new AmbiguityError(bestSoFar, sym);
455 else if (sym.kind < bestSoFar.kind)
456 bestSoFar = sym;
457 }
458 return bestSoFar;
459 }
461 /** Resolve a field identifier, throw a fatal error if not found.
462 * @param pos The position to use for error reporting.
463 * @param env The environment current at the method invocation.
464 * @param site The type of the qualifying expression, in which
465 * identifier is searched.
466 * @param name The identifier's name.
467 */
468 public VarSymbol resolveInternalField(DiagnosticPosition pos, Env<AttrContext> env,
469 Type site, Name name) {
470 Symbol sym = findField(env, site, name, site.tsym);
471 if (sym.kind == VAR) return (VarSymbol)sym;
472 else throw new FatalError(
473 diags.fragment("fatal.err.cant.locate.field",
474 name));
475 }
477 /** Find unqualified variable or field with given name.
478 * Synthetic fields always skipped.
479 * @param env The current environment.
480 * @param name The name of the variable or field.
481 */
482 Symbol findVar(Env<AttrContext> env, Name name) {
483 Symbol bestSoFar = varNotFound;
484 Symbol sym;
485 Env<AttrContext> env1 = env;
486 boolean staticOnly = false;
487 while (env1.outer != null) {
488 if (isStatic(env1)) staticOnly = true;
489 Scope.Entry e = env1.info.scope.lookup(name);
490 while (e.scope != null &&
491 (e.sym.kind != VAR ||
492 (e.sym.flags_field & SYNTHETIC) != 0))
493 e = e.next();
494 sym = (e.scope != null)
495 ? e.sym
496 : findField(
497 env1, env1.enclClass.sym.type, name, env1.enclClass.sym);
498 if (sym.exists()) {
499 if (staticOnly &&
500 sym.kind == VAR &&
501 sym.owner.kind == TYP &&
502 (sym.flags() & STATIC) == 0)
503 return new StaticError(sym);
504 else
505 return sym;
506 } else if (sym.kind < bestSoFar.kind) {
507 bestSoFar = sym;
508 }
510 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
511 env1 = env1.outer;
512 }
514 sym = findField(env, syms.predefClass.type, name, syms.predefClass);
515 if (sym.exists())
516 return sym;
517 if (bestSoFar.exists())
518 return bestSoFar;
520 Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
521 for (; e.scope != null; e = e.next()) {
522 sym = e.sym;
523 Type origin = e.getOrigin().owner.type;
524 if (sym.kind == VAR) {
525 if (e.sym.owner.type != origin)
526 sym = sym.clone(e.getOrigin().owner);
527 return isAccessible(env, origin, sym)
528 ? sym : new AccessError(env, origin, sym);
529 }
530 }
532 Symbol origin = null;
533 e = env.toplevel.starImportScope.lookup(name);
534 for (; e.scope != null; e = e.next()) {
535 sym = e.sym;
536 if (sym.kind != VAR)
537 continue;
538 // invariant: sym.kind == VAR
539 if (bestSoFar.kind < AMBIGUOUS && sym.owner != bestSoFar.owner)
540 return new AmbiguityError(bestSoFar, sym);
541 else if (bestSoFar.kind >= VAR) {
542 origin = e.getOrigin().owner;
543 bestSoFar = isAccessible(env, origin.type, sym)
544 ? sym : new AccessError(env, origin.type, sym);
545 }
546 }
547 if (bestSoFar.kind == VAR && bestSoFar.owner.type != origin.type)
548 return bestSoFar.clone(origin);
549 else
550 return bestSoFar;
551 }
553 Warner noteWarner = new Warner();
555 /** Select the best method for a call site among two choices.
556 * @param env The current environment.
557 * @param site The original type from where the
558 * selection takes place.
559 * @param argtypes The invocation's value arguments,
560 * @param typeargtypes The invocation's type arguments,
561 * @param sym Proposed new best match.
562 * @param bestSoFar Previously found best match.
563 * @param allowBoxing Allow boxing conversions of arguments.
564 * @param useVarargs Box trailing arguments into an array for varargs.
565 */
566 Symbol selectBest(Env<AttrContext> env,
567 Type site,
568 List<Type> argtypes,
569 List<Type> typeargtypes,
570 Symbol sym,
571 Symbol bestSoFar,
572 boolean allowBoxing,
573 boolean useVarargs,
574 boolean operator) {
575 if (sym.kind == ERR) return bestSoFar;
576 if (!sym.isInheritedIn(site.tsym, types)) return bestSoFar;
577 assert sym.kind < AMBIGUOUS;
578 try {
579 if (rawInstantiate(env, site, sym, argtypes, typeargtypes,
580 allowBoxing, useVarargs, Warner.noWarnings) == null) {
581 // inapplicable
582 switch (bestSoFar.kind) {
583 case ABSENT_MTH: return wrongMethod.setWrongSym(sym);
584 case WRONG_MTH: return wrongMethods;
585 default: return bestSoFar;
586 }
587 }
588 } catch (Infer.InferenceException ex) {
589 switch (bestSoFar.kind) {
590 case ABSENT_MTH:
591 return wrongMethod.setWrongSym(sym, ex.getDiagnostic());
592 case WRONG_MTH:
593 return wrongMethods;
594 default:
595 return bestSoFar;
596 }
597 }
598 if (!isAccessible(env, site, sym)) {
599 return (bestSoFar.kind == ABSENT_MTH)
600 ? new AccessError(env, site, sym)
601 : bestSoFar;
602 }
603 return (bestSoFar.kind > AMBIGUOUS)
604 ? sym
605 : mostSpecific(sym, bestSoFar, env, site,
606 allowBoxing && operator, useVarargs);
607 }
609 /* Return the most specific of the two methods for a call,
610 * given that both are accessible and applicable.
611 * @param m1 A new candidate for most specific.
612 * @param m2 The previous most specific candidate.
613 * @param env The current environment.
614 * @param site The original type from where the selection
615 * takes place.
616 * @param allowBoxing Allow boxing conversions of arguments.
617 * @param useVarargs Box trailing arguments into an array for varargs.
618 */
619 Symbol mostSpecific(Symbol m1,
620 Symbol m2,
621 Env<AttrContext> env,
622 final Type site,
623 boolean allowBoxing,
624 boolean useVarargs) {
625 switch (m2.kind) {
626 case MTH:
627 if (m1 == m2) return m1;
628 Type mt1 = types.memberType(site, m1);
629 noteWarner.unchecked = false;
630 boolean m1SignatureMoreSpecific =
631 (instantiate(env, site, m2, types.lowerBoundArgtypes(mt1), null,
632 allowBoxing, false, noteWarner) != null ||
633 useVarargs && instantiate(env, site, m2, types.lowerBoundArgtypes(mt1), null,
634 allowBoxing, true, noteWarner) != null) &&
635 !noteWarner.unchecked;
636 Type mt2 = types.memberType(site, m2);
637 noteWarner.unchecked = false;
638 boolean m2SignatureMoreSpecific =
639 (instantiate(env, site, m1, types.lowerBoundArgtypes(mt2), null,
640 allowBoxing, false, noteWarner) != null ||
641 useVarargs && instantiate(env, site, m1, types.lowerBoundArgtypes(mt2), null,
642 allowBoxing, true, noteWarner) != null) &&
643 !noteWarner.unchecked;
644 if (m1SignatureMoreSpecific && m2SignatureMoreSpecific) {
645 if (!types.overrideEquivalent(mt1, mt2))
646 return new AmbiguityError(m1, m2);
647 // same signature; select (a) the non-bridge method, or
648 // (b) the one that overrides the other, or (c) the concrete
649 // one, or (d) merge both abstract signatures
650 if ((m1.flags() & BRIDGE) != (m2.flags() & BRIDGE)) {
651 return ((m1.flags() & BRIDGE) != 0) ? m2 : m1;
652 }
653 // if one overrides or hides the other, use it
654 TypeSymbol m1Owner = (TypeSymbol)m1.owner;
655 TypeSymbol m2Owner = (TypeSymbol)m2.owner;
656 if (types.asSuper(m1Owner.type, m2Owner) != null &&
657 ((m1.owner.flags_field & INTERFACE) == 0 ||
658 (m2.owner.flags_field & INTERFACE) != 0) &&
659 m1.overrides(m2, m1Owner, types, false))
660 return m1;
661 if (types.asSuper(m2Owner.type, m1Owner) != null &&
662 ((m2.owner.flags_field & INTERFACE) == 0 ||
663 (m1.owner.flags_field & INTERFACE) != 0) &&
664 m2.overrides(m1, m2Owner, types, false))
665 return m2;
666 boolean m1Abstract = (m1.flags() & ABSTRACT) != 0;
667 boolean m2Abstract = (m2.flags() & ABSTRACT) != 0;
668 if (m1Abstract && !m2Abstract) return m2;
669 if (m2Abstract && !m1Abstract) return m1;
670 // both abstract or both concrete
671 if (!m1Abstract && !m2Abstract)
672 return new AmbiguityError(m1, m2);
673 // check that both signatures have the same erasure
674 if (!types.isSameTypes(m1.erasure(types).getParameterTypes(),
675 m2.erasure(types).getParameterTypes()))
676 return new AmbiguityError(m1, m2);
677 // both abstract, neither overridden; merge throws clause and result type
678 Symbol mostSpecific;
679 Type result2 = mt2.getReturnType();
680 if (mt2.tag == FORALL)
681 result2 = types.subst(result2, ((ForAll)mt2).tvars, ((ForAll)mt1).tvars);
682 if (types.isSubtype(mt1.getReturnType(), result2)) {
683 mostSpecific = m1;
684 } else if (types.isSubtype(result2, mt1.getReturnType())) {
685 mostSpecific = m2;
686 } else {
687 // Theoretically, this can't happen, but it is possible
688 // due to error recovery or mixing incompatible class files
689 return new AmbiguityError(m1, m2);
690 }
691 MethodSymbol result = new MethodSymbol(
692 mostSpecific.flags(),
693 mostSpecific.name,
694 null,
695 mostSpecific.owner) {
696 @Override
697 public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult) {
698 if (origin == site.tsym)
699 return this;
700 else
701 return super.implementation(origin, types, checkResult);
702 }
703 };
704 result.type = (Type)mostSpecific.type.clone();
705 result.type.setThrown(chk.intersect(mt1.getThrownTypes(),
706 mt2.getThrownTypes()));
707 return result;
708 }
709 if (m1SignatureMoreSpecific) return m1;
710 if (m2SignatureMoreSpecific) return m2;
711 return new AmbiguityError(m1, m2);
712 case AMBIGUOUS:
713 AmbiguityError e = (AmbiguityError)m2;
714 Symbol err1 = mostSpecific(m1, e.sym, env, site, allowBoxing, useVarargs);
715 Symbol err2 = mostSpecific(m1, e.sym2, env, site, allowBoxing, useVarargs);
716 if (err1 == err2) return err1;
717 if (err1 == e.sym && err2 == e.sym2) return m2;
718 if (err1 instanceof AmbiguityError &&
719 err2 instanceof AmbiguityError &&
720 ((AmbiguityError)err1).sym == ((AmbiguityError)err2).sym)
721 return new AmbiguityError(m1, m2);
722 else
723 return new AmbiguityError(err1, err2);
724 default:
725 throw new AssertionError();
726 }
727 }
729 /** Find best qualified method matching given name, type and value
730 * arguments.
731 * @param env The current environment.
732 * @param site The original type from where the selection
733 * takes place.
734 * @param name The method's name.
735 * @param argtypes The method's value arguments.
736 * @param typeargtypes The method's type arguments
737 * @param allowBoxing Allow boxing conversions of arguments.
738 * @param useVarargs Box trailing arguments into an array for varargs.
739 */
740 Symbol findMethod(Env<AttrContext> env,
741 Type site,
742 Name name,
743 List<Type> argtypes,
744 List<Type> typeargtypes,
745 boolean allowBoxing,
746 boolean useVarargs,
747 boolean operator) {
748 return findMethod(env,
749 site,
750 name,
751 argtypes,
752 typeargtypes,
753 site.tsym.type,
754 true,
755 methodNotFound,
756 allowBoxing,
757 useVarargs,
758 operator);
759 }
760 // where
761 private Symbol findMethod(Env<AttrContext> env,
762 Type site,
763 Name name,
764 List<Type> argtypes,
765 List<Type> typeargtypes,
766 Type intype,
767 boolean abstractok,
768 Symbol bestSoFar,
769 boolean allowBoxing,
770 boolean useVarargs,
771 boolean operator) {
772 for (Type ct = intype; ct.tag == CLASS || ct.tag == TYPEVAR; ct = types.supertype(ct)) {
773 while (ct.tag == TYPEVAR)
774 ct = ct.getUpperBound();
775 ClassSymbol c = (ClassSymbol)ct.tsym;
776 if ((c.flags() & (ABSTRACT | INTERFACE | ENUM)) == 0)
777 abstractok = false;
778 for (Scope.Entry e = c.members().lookup(name);
779 e.scope != null;
780 e = e.next()) {
781 //- System.out.println(" e " + e.sym);
782 if (e.sym.kind == MTH &&
783 (e.sym.flags_field & SYNTHETIC) == 0) {
784 bestSoFar = selectBest(env, site, argtypes, typeargtypes,
785 e.sym, bestSoFar,
786 allowBoxing,
787 useVarargs,
788 operator);
789 }
790 }
791 //- System.out.println(" - " + bestSoFar);
792 if (abstractok) {
793 Symbol concrete = methodNotFound;
794 if ((bestSoFar.flags() & ABSTRACT) == 0)
795 concrete = bestSoFar;
796 for (List<Type> l = types.interfaces(c.type);
797 l.nonEmpty();
798 l = l.tail) {
799 bestSoFar = findMethod(env, site, name, argtypes,
800 typeargtypes,
801 l.head, abstractok, bestSoFar,
802 allowBoxing, useVarargs, operator);
803 }
804 if (concrete != bestSoFar &&
805 concrete.kind < ERR && bestSoFar.kind < ERR &&
806 types.isSubSignature(concrete.type, bestSoFar.type))
807 bestSoFar = concrete;
808 }
809 }
810 return bestSoFar;
811 }
813 /** Find unqualified method matching given name, type and value arguments.
814 * @param env The current environment.
815 * @param name The method's name.
816 * @param argtypes The method's value arguments.
817 * @param typeargtypes The method's type arguments.
818 * @param allowBoxing Allow boxing conversions of arguments.
819 * @param useVarargs Box trailing arguments into an array for varargs.
820 */
821 Symbol findFun(Env<AttrContext> env, Name name,
822 List<Type> argtypes, List<Type> typeargtypes,
823 boolean allowBoxing, boolean useVarargs) {
824 Symbol bestSoFar = methodNotFound;
825 Symbol sym;
826 Env<AttrContext> env1 = env;
827 boolean staticOnly = false;
828 while (env1.outer != null) {
829 if (isStatic(env1)) staticOnly = true;
830 sym = findMethod(
831 env1, env1.enclClass.sym.type, name, argtypes, typeargtypes,
832 allowBoxing, useVarargs, false);
833 if (sym.exists()) {
834 if (staticOnly &&
835 sym.kind == MTH &&
836 sym.owner.kind == TYP &&
837 (sym.flags() & STATIC) == 0) return new StaticError(sym);
838 else return sym;
839 } else if (sym.kind < bestSoFar.kind) {
840 bestSoFar = sym;
841 }
842 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
843 env1 = env1.outer;
844 }
846 sym = findMethod(env, syms.predefClass.type, name, argtypes,
847 typeargtypes, allowBoxing, useVarargs, false);
848 if (sym.exists())
849 return sym;
851 Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
852 for (; e.scope != null; e = e.next()) {
853 sym = e.sym;
854 Type origin = e.getOrigin().owner.type;
855 if (sym.kind == MTH) {
856 if (e.sym.owner.type != origin)
857 sym = sym.clone(e.getOrigin().owner);
858 if (!isAccessible(env, origin, sym))
859 sym = new AccessError(env, origin, sym);
860 bestSoFar = selectBest(env, origin,
861 argtypes, typeargtypes,
862 sym, bestSoFar,
863 allowBoxing, useVarargs, false);
864 }
865 }
866 if (bestSoFar.exists())
867 return bestSoFar;
869 e = env.toplevel.starImportScope.lookup(name);
870 for (; e.scope != null; e = e.next()) {
871 sym = e.sym;
872 Type origin = e.getOrigin().owner.type;
873 if (sym.kind == MTH) {
874 if (e.sym.owner.type != origin)
875 sym = sym.clone(e.getOrigin().owner);
876 if (!isAccessible(env, origin, sym))
877 sym = new AccessError(env, origin, sym);
878 bestSoFar = selectBest(env, origin,
879 argtypes, typeargtypes,
880 sym, bestSoFar,
881 allowBoxing, useVarargs, false);
882 }
883 }
884 return bestSoFar;
885 }
887 /** Find or create an implicit method of exactly the given type (after erasure).
888 * Searches in a side table, not the main scope of the site.
889 * This emulates the lookup process required by JSR 292 in JVM.
890 * @param env The current environment.
891 * @param site The original type from where the selection
892 * takes place.
893 * @param name The method's name.
894 * @param argtypes The method's value arguments.
895 * @param typeargtypes The method's type arguments
896 */
897 Symbol findImplicitMethod(Env<AttrContext> env,
898 Type site,
899 Name name,
900 List<Type> argtypes,
901 List<Type> typeargtypes) {
902 assert allowInvokedynamic;
903 assert site == syms.invokeDynamicType || (site == syms.methodHandleType && name == names.invoke);
904 ClassSymbol c = (ClassSymbol) site.tsym;
905 Scope implicit = c.members().next;
906 if (implicit == null) {
907 c.members().next = implicit = new Scope(c);
908 }
909 Type restype;
910 if (typeargtypes.isEmpty()) {
911 restype = syms.objectType;
912 } else {
913 restype = typeargtypes.head;
914 if (!typeargtypes.tail.isEmpty())
915 return methodNotFound;
916 }
917 List<Type> paramtypes = Type.map(argtypes, implicitArgType);
918 MethodType mtype = new MethodType(paramtypes,
919 restype,
920 List.<Type>nil(),
921 syms.methodClass);
922 int flags = PUBLIC | ABSTRACT;
923 if (site == syms.invokeDynamicType) flags |= STATIC;
924 Symbol m = null;
925 for (Scope.Entry e = implicit.lookup(name);
926 e.scope != null;
927 e = e.next()) {
928 Symbol sym = e.sym;
929 assert sym.kind == MTH;
930 if (types.isSameType(mtype, sym.type)
931 && (sym.flags() & STATIC) == (flags & STATIC)) {
932 m = sym;
933 break;
934 }
935 }
936 if (m == null) {
937 // create the desired method
938 m = new MethodSymbol(flags, name, mtype, c);
939 implicit.enter(m);
940 }
941 assert argumentsAcceptable(argtypes, types.memberType(site, m).getParameterTypes(),
942 false, false, Warner.noWarnings);
943 assert null != instantiate(env, site, m, argtypes, typeargtypes, false, false, Warner.noWarnings);
944 return m;
945 }
946 //where
947 Mapping implicitArgType = new Mapping ("implicitArgType") {
948 public Type apply(Type t) { return implicitArgType(t); }
949 };
950 Type implicitArgType(Type argType) {
951 argType = types.erasure(argType);
952 if (argType.tag == BOT)
953 // nulls type as the marker type Null (which has no instances)
954 // TO DO: figure out how to access java.lang.Null safely, else throw nice error
955 //argType = types.boxedClass(syms.botType).type;
956 argType = types.boxedClass(syms.voidType).type; // REMOVE
957 return argType;
958 }
960 /** Load toplevel or member class with given fully qualified name and
961 * verify that it is accessible.
962 * @param env The current environment.
963 * @param name The fully qualified name of the class to be loaded.
964 */
965 Symbol loadClass(Env<AttrContext> env, Name name) {
966 try {
967 ClassSymbol c = reader.loadClass(name);
968 return isAccessible(env, c) ? c : new AccessError(c);
969 } catch (ClassReader.BadClassFile err) {
970 throw err;
971 } catch (CompletionFailure ex) {
972 return typeNotFound;
973 }
974 }
976 /** Find qualified member type.
977 * @param env The current environment.
978 * @param site The original type from where the selection takes
979 * place.
980 * @param name The type's name.
981 * @param c The class to search for the member type. This is
982 * always a superclass or implemented interface of
983 * site's class.
984 */
985 Symbol findMemberType(Env<AttrContext> env,
986 Type site,
987 Name name,
988 TypeSymbol c) {
989 Symbol bestSoFar = typeNotFound;
990 Symbol sym;
991 Scope.Entry e = c.members().lookup(name);
992 while (e.scope != null) {
993 if (e.sym.kind == TYP) {
994 return isAccessible(env, site, e.sym)
995 ? e.sym
996 : new AccessError(env, site, e.sym);
997 }
998 e = e.next();
999 }
1000 Type st = types.supertype(c.type);
1001 if (st != null && st.tag == CLASS) {
1002 sym = findMemberType(env, site, name, st.tsym);
1003 if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1004 }
1005 for (List<Type> l = types.interfaces(c.type);
1006 bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
1007 l = l.tail) {
1008 sym = findMemberType(env, site, name, l.head.tsym);
1009 if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
1010 sym.owner != bestSoFar.owner)
1011 bestSoFar = new AmbiguityError(bestSoFar, sym);
1012 else if (sym.kind < bestSoFar.kind)
1013 bestSoFar = sym;
1014 }
1015 return bestSoFar;
1016 }
1018 /** Find a global type in given scope and load corresponding class.
1019 * @param env The current environment.
1020 * @param scope The scope in which to look for the type.
1021 * @param name The type's name.
1022 */
1023 Symbol findGlobalType(Env<AttrContext> env, Scope scope, Name name) {
1024 Symbol bestSoFar = typeNotFound;
1025 for (Scope.Entry e = scope.lookup(name); e.scope != null; e = e.next()) {
1026 Symbol sym = loadClass(env, e.sym.flatName());
1027 if (bestSoFar.kind == TYP && sym.kind == TYP &&
1028 bestSoFar != sym)
1029 return new AmbiguityError(bestSoFar, sym);
1030 else if (sym.kind < bestSoFar.kind)
1031 bestSoFar = sym;
1032 }
1033 return bestSoFar;
1034 }
1036 /** Find an unqualified type symbol.
1037 * @param env The current environment.
1038 * @param name The type's name.
1039 */
1040 Symbol findType(Env<AttrContext> env, Name name) {
1041 Symbol bestSoFar = typeNotFound;
1042 Symbol sym;
1043 boolean staticOnly = false;
1044 for (Env<AttrContext> env1 = env; env1.outer != null; env1 = env1.outer) {
1045 if (isStatic(env1)) staticOnly = true;
1046 for (Scope.Entry e = env1.info.scope.lookup(name);
1047 e.scope != null;
1048 e = e.next()) {
1049 if (e.sym.kind == TYP) {
1050 if (staticOnly &&
1051 e.sym.type.tag == TYPEVAR &&
1052 e.sym.owner.kind == TYP) return new StaticError(e.sym);
1053 return e.sym;
1054 }
1055 }
1057 sym = findMemberType(env1, env1.enclClass.sym.type, name,
1058 env1.enclClass.sym);
1059 if (staticOnly && sym.kind == TYP &&
1060 sym.type.tag == CLASS &&
1061 sym.type.getEnclosingType().tag == CLASS &&
1062 env1.enclClass.sym.type.isParameterized() &&
1063 sym.type.getEnclosingType().isParameterized())
1064 return new StaticError(sym);
1065 else if (sym.exists()) return sym;
1066 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1068 JCClassDecl encl = env1.baseClause ? (JCClassDecl)env1.tree : env1.enclClass;
1069 if ((encl.sym.flags() & STATIC) != 0)
1070 staticOnly = true;
1071 }
1073 if (env.tree.getTag() != JCTree.IMPORT) {
1074 sym = findGlobalType(env, env.toplevel.namedImportScope, name);
1075 if (sym.exists()) return sym;
1076 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1078 sym = findGlobalType(env, env.toplevel.packge.members(), name);
1079 if (sym.exists()) return sym;
1080 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1082 sym = findGlobalType(env, env.toplevel.starImportScope, name);
1083 if (sym.exists()) return sym;
1084 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1085 }
1087 return bestSoFar;
1088 }
1090 /** Find an unqualified identifier which matches a specified kind set.
1091 * @param env The current environment.
1092 * @param name The indentifier's name.
1093 * @param kind Indicates the possible symbol kinds
1094 * (a subset of VAL, TYP, PCK).
1095 */
1096 Symbol findIdent(Env<AttrContext> env, Name name, int kind) {
1097 Symbol bestSoFar = typeNotFound;
1098 Symbol sym;
1100 if ((kind & VAR) != 0) {
1101 sym = findVar(env, name);
1102 if (sym.exists()) return sym;
1103 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1104 }
1106 if ((kind & TYP) != 0) {
1107 sym = findType(env, name);
1108 if (sym.exists()) return sym;
1109 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1110 }
1112 if ((kind & PCK) != 0) return reader.enterPackage(name);
1113 else return bestSoFar;
1114 }
1116 /** Find an identifier in a package which matches a specified kind set.
1117 * @param env The current environment.
1118 * @param name The identifier's name.
1119 * @param kind Indicates the possible symbol kinds
1120 * (a nonempty subset of TYP, PCK).
1121 */
1122 Symbol findIdentInPackage(Env<AttrContext> env, TypeSymbol pck,
1123 Name name, int kind) {
1124 Name fullname = TypeSymbol.formFullName(name, pck);
1125 Symbol bestSoFar = typeNotFound;
1126 PackageSymbol pack = null;
1127 if ((kind & PCK) != 0) {
1128 pack = reader.enterPackage(fullname);
1129 if (pack.exists()) return pack;
1130 }
1131 if ((kind & TYP) != 0) {
1132 Symbol sym = loadClass(env, fullname);
1133 if (sym.exists()) {
1134 // don't allow programs to use flatnames
1135 if (name == sym.name) return sym;
1136 }
1137 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1138 }
1139 return (pack != null) ? pack : bestSoFar;
1140 }
1142 /** Find an identifier among the members of a given type `site'.
1143 * @param env The current environment.
1144 * @param site The type containing the symbol to be found.
1145 * @param name The identifier's name.
1146 * @param kind Indicates the possible symbol kinds
1147 * (a subset of VAL, TYP).
1148 */
1149 Symbol findIdentInType(Env<AttrContext> env, Type site,
1150 Name name, int kind) {
1151 Symbol bestSoFar = typeNotFound;
1152 Symbol sym;
1153 if ((kind & VAR) != 0) {
1154 sym = findField(env, site, name, site.tsym);
1155 if (sym.exists()) return sym;
1156 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1157 }
1159 if ((kind & TYP) != 0) {
1160 sym = findMemberType(env, site, name, site.tsym);
1161 if (sym.exists()) return sym;
1162 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1163 }
1164 return bestSoFar;
1165 }
1167 /* ***************************************************************************
1168 * Access checking
1169 * The following methods convert ResolveErrors to ErrorSymbols, issuing
1170 * an error message in the process
1171 ****************************************************************************/
1173 /** If `sym' is a bad symbol: report error and return errSymbol
1174 * else pass through unchanged,
1175 * additional arguments duplicate what has been used in trying to find the
1176 * symbol (--> flyweight pattern). This improves performance since we
1177 * expect misses to happen frequently.
1178 *
1179 * @param sym The symbol that was found, or a ResolveError.
1180 * @param pos The position to use for error reporting.
1181 * @param site The original type from where the selection took place.
1182 * @param name The symbol's name.
1183 * @param argtypes The invocation's value arguments,
1184 * if we looked for a method.
1185 * @param typeargtypes The invocation's type arguments,
1186 * if we looked for a method.
1187 */
1188 Symbol access(Symbol sym,
1189 DiagnosticPosition pos,
1190 Type site,
1191 Name name,
1192 boolean qualified,
1193 List<Type> argtypes,
1194 List<Type> typeargtypes) {
1195 if (sym.kind >= AMBIGUOUS) {
1196 ResolveError errSym = (ResolveError)sym;
1197 if (!site.isErroneous() &&
1198 !Type.isErroneous(argtypes) &&
1199 (typeargtypes==null || !Type.isErroneous(typeargtypes)))
1200 logResolveError(errSym, pos, site, name, argtypes, typeargtypes);
1201 sym = errSym.access(name, qualified ? site.tsym : syms.noSymbol);
1202 }
1203 return sym;
1204 }
1206 /** Same as above, but without type arguments and arguments.
1207 */
1208 Symbol access(Symbol sym,
1209 DiagnosticPosition pos,
1210 Type site,
1211 Name name,
1212 boolean qualified) {
1213 if (sym.kind >= AMBIGUOUS)
1214 return access(sym, pos, site, name, qualified, List.<Type>nil(), null);
1215 else
1216 return sym;
1217 }
1219 /** Check that sym is not an abstract method.
1220 */
1221 void checkNonAbstract(DiagnosticPosition pos, Symbol sym) {
1222 if ((sym.flags() & ABSTRACT) != 0)
1223 log.error(pos, "abstract.cant.be.accessed.directly",
1224 kindName(sym), sym, sym.location());
1225 }
1227 /* ***************************************************************************
1228 * Debugging
1229 ****************************************************************************/
1231 /** print all scopes starting with scope s and proceeding outwards.
1232 * used for debugging.
1233 */
1234 public void printscopes(Scope s) {
1235 while (s != null) {
1236 if (s.owner != null)
1237 System.err.print(s.owner + ": ");
1238 for (Scope.Entry e = s.elems; e != null; e = e.sibling) {
1239 if ((e.sym.flags() & ABSTRACT) != 0)
1240 System.err.print("abstract ");
1241 System.err.print(e.sym + " ");
1242 }
1243 System.err.println();
1244 s = s.next;
1245 }
1246 }
1248 void printscopes(Env<AttrContext> env) {
1249 while (env.outer != null) {
1250 System.err.println("------------------------------");
1251 printscopes(env.info.scope);
1252 env = env.outer;
1253 }
1254 }
1256 public void printscopes(Type t) {
1257 while (t.tag == CLASS) {
1258 printscopes(t.tsym.members());
1259 t = types.supertype(t);
1260 }
1261 }
1263 /* ***************************************************************************
1264 * Name resolution
1265 * Naming conventions are as for symbol lookup
1266 * Unlike the find... methods these methods will report access errors
1267 ****************************************************************************/
1269 /** Resolve an unqualified (non-method) identifier.
1270 * @param pos The position to use for error reporting.
1271 * @param env The environment current at the identifier use.
1272 * @param name The identifier's name.
1273 * @param kind The set of admissible symbol kinds for the identifier.
1274 */
1275 Symbol resolveIdent(DiagnosticPosition pos, Env<AttrContext> env,
1276 Name name, int kind) {
1277 return access(
1278 findIdent(env, name, kind),
1279 pos, env.enclClass.sym.type, name, false);
1280 }
1282 /** Resolve an unqualified method identifier.
1283 * @param pos The position to use for error reporting.
1284 * @param env The environment current at the method invocation.
1285 * @param name The identifier's name.
1286 * @param argtypes The types of the invocation's value arguments.
1287 * @param typeargtypes The types of the invocation's type arguments.
1288 */
1289 Symbol resolveMethod(DiagnosticPosition pos,
1290 Env<AttrContext> env,
1291 Name name,
1292 List<Type> argtypes,
1293 List<Type> typeargtypes) {
1294 Symbol sym = methodNotFound;
1295 List<MethodResolutionPhase> steps = methodResolutionSteps;
1296 while (steps.nonEmpty() &&
1297 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
1298 sym.kind >= ERRONEOUS) {
1299 sym = findFun(env, name, argtypes, typeargtypes,
1300 steps.head.isBoxingRequired,
1301 env.info.varArgs = steps.head.isVarargsRequired);
1302 methodResolutionCache.put(steps.head, sym);
1303 steps = steps.tail;
1304 }
1305 if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error
1306 MethodResolutionPhase errPhase =
1307 firstErroneousResolutionPhase();
1308 sym = access(methodResolutionCache.get(errPhase),
1309 pos, env.enclClass.sym.type, name, false, argtypes, typeargtypes);
1310 env.info.varArgs = errPhase.isVarargsRequired;
1311 }
1312 return sym;
1313 }
1315 /** Resolve a qualified method identifier
1316 * @param pos The position to use for error reporting.
1317 * @param env The environment current at the method invocation.
1318 * @param site The type of the qualifying expression, in which
1319 * identifier is searched.
1320 * @param name The identifier's name.
1321 * @param argtypes The types of the invocation's value arguments.
1322 * @param typeargtypes The types of the invocation's type arguments.
1323 */
1324 Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
1325 Type site, Name name, List<Type> argtypes,
1326 List<Type> typeargtypes) {
1327 Symbol sym = methodNotFound;
1328 List<MethodResolutionPhase> steps = methodResolutionSteps;
1329 while (steps.nonEmpty() &&
1330 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
1331 sym.kind >= ERRONEOUS) {
1332 sym = findMethod(env, site, name, argtypes, typeargtypes,
1333 steps.head.isBoxingRequired(),
1334 env.info.varArgs = steps.head.isVarargsRequired(), false);
1335 methodResolutionCache.put(steps.head, sym);
1336 steps = steps.tail;
1337 }
1338 if (sym.kind >= AMBIGUOUS &&
1339 allowInvokedynamic &&
1340 (site == syms.invokeDynamicType ||
1341 site == syms.methodHandleType && name == names.invoke)) {
1342 // lookup failed; supply an exactly-typed implicit method
1343 sym = findImplicitMethod(env, site, name, argtypes, typeargtypes);
1344 env.info.varArgs = false;
1345 }
1346 if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error
1347 MethodResolutionPhase errPhase =
1348 firstErroneousResolutionPhase();
1349 sym = access(methodResolutionCache.get(errPhase),
1350 pos, site, name, true, argtypes, typeargtypes);
1351 env.info.varArgs = errPhase.isVarargsRequired;
1352 }
1353 return sym;
1354 }
1356 /** Resolve a qualified method identifier, throw a fatal error if not
1357 * found.
1358 * @param pos The position to use for error reporting.
1359 * @param env The environment current at the method invocation.
1360 * @param site The type of the qualifying expression, in which
1361 * identifier is searched.
1362 * @param name The identifier's name.
1363 * @param argtypes The types of the invocation's value arguments.
1364 * @param typeargtypes The types of the invocation's type arguments.
1365 */
1366 public MethodSymbol resolveInternalMethod(DiagnosticPosition pos, Env<AttrContext> env,
1367 Type site, Name name,
1368 List<Type> argtypes,
1369 List<Type> typeargtypes) {
1370 Symbol sym = resolveQualifiedMethod(
1371 pos, env, site, name, argtypes, typeargtypes);
1372 if (sym.kind == MTH) return (MethodSymbol)sym;
1373 else throw new FatalError(
1374 diags.fragment("fatal.err.cant.locate.meth",
1375 name));
1376 }
1378 /** Resolve constructor.
1379 * @param pos The position to use for error reporting.
1380 * @param env The environment current at the constructor invocation.
1381 * @param site The type of class for which a constructor is searched.
1382 * @param argtypes The types of the constructor invocation's value
1383 * arguments.
1384 * @param typeargtypes The types of the constructor invocation's type
1385 * arguments.
1386 */
1387 Symbol resolveConstructor(DiagnosticPosition pos,
1388 Env<AttrContext> env,
1389 Type site,
1390 List<Type> argtypes,
1391 List<Type> typeargtypes) {
1392 Symbol sym = methodNotFound;
1393 List<MethodResolutionPhase> steps = methodResolutionSteps;
1394 while (steps.nonEmpty() &&
1395 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
1396 sym.kind >= ERRONEOUS) {
1397 sym = resolveConstructor(pos, env, site, argtypes, typeargtypes,
1398 steps.head.isBoxingRequired(),
1399 env.info.varArgs = steps.head.isVarargsRequired());
1400 methodResolutionCache.put(steps.head, sym);
1401 steps = steps.tail;
1402 }
1403 if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error
1404 MethodResolutionPhase errPhase = firstErroneousResolutionPhase();
1405 sym = access(methodResolutionCache.get(errPhase),
1406 pos, site, names.init, true, argtypes, typeargtypes);
1407 env.info.varArgs = errPhase.isVarargsRequired();
1408 }
1409 return sym;
1410 }
1412 /** Resolve constructor.
1413 * @param pos The position to use for error reporting.
1414 * @param env The environment current at the constructor invocation.
1415 * @param site The type of class for which a constructor is searched.
1416 * @param argtypes The types of the constructor invocation's value
1417 * arguments.
1418 * @param typeargtypes The types of the constructor invocation's type
1419 * arguments.
1420 * @param allowBoxing Allow boxing and varargs conversions.
1421 * @param useVarargs Box trailing arguments into an array for varargs.
1422 */
1423 Symbol resolveConstructor(DiagnosticPosition pos, Env<AttrContext> env,
1424 Type site, List<Type> argtypes,
1425 List<Type> typeargtypes,
1426 boolean allowBoxing,
1427 boolean useVarargs) {
1428 Symbol sym = findMethod(env, site,
1429 names.init, argtypes,
1430 typeargtypes, allowBoxing,
1431 useVarargs, false);
1432 if ((sym.flags() & DEPRECATED) != 0 &&
1433 (env.info.scope.owner.flags() & DEPRECATED) == 0 &&
1434 env.info.scope.owner.outermostClass() != sym.outermostClass())
1435 chk.warnDeprecated(pos, sym);
1436 return sym;
1437 }
1439 /** Resolve a constructor, throw a fatal error if not found.
1440 * @param pos The position to use for error reporting.
1441 * @param env The environment current at the method invocation.
1442 * @param site The type to be constructed.
1443 * @param argtypes The types of the invocation's value arguments.
1444 * @param typeargtypes The types of the invocation's type arguments.
1445 */
1446 public MethodSymbol resolveInternalConstructor(DiagnosticPosition pos, Env<AttrContext> env,
1447 Type site,
1448 List<Type> argtypes,
1449 List<Type> typeargtypes) {
1450 Symbol sym = resolveConstructor(
1451 pos, env, site, argtypes, typeargtypes);
1452 if (sym.kind == MTH) return (MethodSymbol)sym;
1453 else throw new FatalError(
1454 diags.fragment("fatal.err.cant.locate.ctor", site));
1455 }
1457 /** Resolve operator.
1458 * @param pos The position to use for error reporting.
1459 * @param optag The tag of the operation tree.
1460 * @param env The environment current at the operation.
1461 * @param argtypes The types of the operands.
1462 */
1463 Symbol resolveOperator(DiagnosticPosition pos, int optag,
1464 Env<AttrContext> env, List<Type> argtypes) {
1465 Name name = treeinfo.operatorName(optag);
1466 Symbol sym = findMethod(env, syms.predefClass.type, name, argtypes,
1467 null, false, false, true);
1468 if (boxingEnabled && sym.kind >= WRONG_MTHS)
1469 sym = findMethod(env, syms.predefClass.type, name, argtypes,
1470 null, true, false, true);
1471 return access(sym, pos, env.enclClass.sym.type, name,
1472 false, argtypes, null);
1473 }
1475 /** Resolve operator.
1476 * @param pos The position to use for error reporting.
1477 * @param optag The tag of the operation tree.
1478 * @param env The environment current at the operation.
1479 * @param arg The type of the operand.
1480 */
1481 Symbol resolveUnaryOperator(DiagnosticPosition pos, int optag, Env<AttrContext> env, Type arg) {
1482 return resolveOperator(pos, optag, env, List.of(arg));
1483 }
1485 /** Resolve binary operator.
1486 * @param pos The position to use for error reporting.
1487 * @param optag The tag of the operation tree.
1488 * @param env The environment current at the operation.
1489 * @param left The types of the left operand.
1490 * @param right The types of the right operand.
1491 */
1492 Symbol resolveBinaryOperator(DiagnosticPosition pos,
1493 int optag,
1494 Env<AttrContext> env,
1495 Type left,
1496 Type right) {
1497 return resolveOperator(pos, optag, env, List.of(left, right));
1498 }
1500 /**
1501 * Resolve `c.name' where name == this or name == super.
1502 * @param pos The position to use for error reporting.
1503 * @param env The environment current at the expression.
1504 * @param c The qualifier.
1505 * @param name The identifier's name.
1506 */
1507 Symbol resolveSelf(DiagnosticPosition pos,
1508 Env<AttrContext> env,
1509 TypeSymbol c,
1510 Name name) {
1511 Env<AttrContext> env1 = env;
1512 boolean staticOnly = false;
1513 while (env1.outer != null) {
1514 if (isStatic(env1)) staticOnly = true;
1515 if (env1.enclClass.sym == c) {
1516 Symbol sym = env1.info.scope.lookup(name).sym;
1517 if (sym != null) {
1518 if (staticOnly) sym = new StaticError(sym);
1519 return access(sym, pos, env.enclClass.sym.type,
1520 name, true);
1521 }
1522 }
1523 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
1524 env1 = env1.outer;
1525 }
1526 log.error(pos, "not.encl.class", c);
1527 return syms.errSymbol;
1528 }
1530 /**
1531 * Resolve `c.this' for an enclosing class c that contains the
1532 * named member.
1533 * @param pos The position to use for error reporting.
1534 * @param env The environment current at the expression.
1535 * @param member The member that must be contained in the result.
1536 */
1537 Symbol resolveSelfContaining(DiagnosticPosition pos,
1538 Env<AttrContext> env,
1539 Symbol member) {
1540 Name name = names._this;
1541 Env<AttrContext> env1 = env;
1542 boolean staticOnly = false;
1543 while (env1.outer != null) {
1544 if (isStatic(env1)) staticOnly = true;
1545 if (env1.enclClass.sym.isSubClass(member.owner, types) &&
1546 isAccessible(env, env1.enclClass.sym.type, member)) {
1547 Symbol sym = env1.info.scope.lookup(name).sym;
1548 if (sym != null) {
1549 if (staticOnly) sym = new StaticError(sym);
1550 return access(sym, pos, env.enclClass.sym.type,
1551 name, true);
1552 }
1553 }
1554 if ((env1.enclClass.sym.flags() & STATIC) != 0)
1555 staticOnly = true;
1556 env1 = env1.outer;
1557 }
1558 log.error(pos, "encl.class.required", member);
1559 return syms.errSymbol;
1560 }
1562 /**
1563 * Resolve an appropriate implicit this instance for t's container.
1564 * JLS2 8.8.5.1 and 15.9.2
1565 */
1566 Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t) {
1567 Type thisType = (((t.tsym.owner.kind & (MTH|VAR)) != 0)
1568 ? resolveSelf(pos, env, t.getEnclosingType().tsym, names._this)
1569 : resolveSelfContaining(pos, env, t.tsym)).type;
1570 if (env.info.isSelfCall && thisType.tsym == env.enclClass.sym)
1571 log.error(pos, "cant.ref.before.ctor.called", "this");
1572 return thisType;
1573 }
1575 /* ***************************************************************************
1576 * ResolveError classes, indicating error situations when accessing symbols
1577 ****************************************************************************/
1579 public void logAccessError(Env<AttrContext> env, JCTree tree, Type type) {
1580 AccessError error = new AccessError(env, type.getEnclosingType(), type.tsym);
1581 logResolveError(error, tree.pos(), type.getEnclosingType(), null, null, null);
1582 }
1583 //where
1584 private void logResolveError(ResolveError error,
1585 DiagnosticPosition pos,
1586 Type site,
1587 Name name,
1588 List<Type> argtypes,
1589 List<Type> typeargtypes) {
1590 JCDiagnostic d = error.getDiagnostic(JCDiagnostic.DiagnosticType.ERROR,
1591 pos, site, name, argtypes, typeargtypes);
1592 if (d != null)
1593 log.report(d);
1594 }
1596 private final LocalizedString noArgs = new LocalizedString("compiler.misc.no.args");
1598 public Object methodArguments(List<Type> argtypes) {
1599 return argtypes.isEmpty() ? noArgs : argtypes;
1600 }
1602 /**
1603 * Root class for resolution errors. Subclass of ResolveError
1604 * represent a different kinds of resolution error - as such they must
1605 * specify how they map into concrete compiler diagnostics.
1606 */
1607 private abstract class ResolveError extends Symbol {
1609 /** The name of the kind of error, for debugging only. */
1610 final String debugName;
1612 ResolveError(int kind, String debugName) {
1613 super(kind, 0, null, null, null);
1614 this.debugName = debugName;
1615 }
1617 @Override
1618 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
1619 throw new AssertionError();
1620 }
1622 @Override
1623 public String toString() {
1624 return debugName;
1625 }
1627 @Override
1628 public boolean exists() {
1629 return false;
1630 }
1632 /**
1633 * Create an external representation for this erroneous symbol to be
1634 * used during attribution - by default this returns the symbol of a
1635 * brand new error type which stores the original type found
1636 * during resolution.
1637 *
1638 * @param name the name used during resolution
1639 * @param location the location from which the symbol is accessed
1640 */
1641 protected Symbol access(Name name, TypeSymbol location) {
1642 return types.createErrorType(name, location, syms.errSymbol.type).tsym;
1643 }
1645 /**
1646 * Create a diagnostic representing this resolution error.
1647 *
1648 * @param dkind The kind of the diagnostic to be created (e.g error).
1649 * @param pos The position to be used for error reporting.
1650 * @param site The original type from where the selection took place.
1651 * @param name The name of the symbol to be resolved.
1652 * @param argtypes The invocation's value arguments,
1653 * if we looked for a method.
1654 * @param typeargtypes The invocation's type arguments,
1655 * if we looked for a method.
1656 */
1657 abstract JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
1658 DiagnosticPosition pos,
1659 Type site,
1660 Name name,
1661 List<Type> argtypes,
1662 List<Type> typeargtypes);
1664 /**
1665 * A name designates an operator if it consists
1666 * of a non-empty sequence of operator symbols +-~!/*%&|^<>=
1667 */
1668 boolean isOperator(Name name) {
1669 int i = 0;
1670 while (i < name.getByteLength() &&
1671 "+-~!*/%&|^<>=".indexOf(name.getByteAt(i)) >= 0) i++;
1672 return i > 0 && i == name.getByteLength();
1673 }
1674 }
1676 /**
1677 * This class is the root class of all resolution errors caused by
1678 * an invalid symbol being found during resolution.
1679 */
1680 abstract class InvalidSymbolError extends ResolveError {
1682 /** The invalid symbol found during resolution */
1683 Symbol sym;
1685 InvalidSymbolError(int kind, Symbol sym, String debugName) {
1686 super(kind, debugName);
1687 this.sym = sym;
1688 }
1690 @Override
1691 public boolean exists() {
1692 return true;
1693 }
1695 @Override
1696 public String toString() {
1697 return super.toString() + " wrongSym=" + sym;
1698 }
1700 @Override
1701 public Symbol access(Name name, TypeSymbol location) {
1702 if (sym.kind >= AMBIGUOUS)
1703 return ((ResolveError)sym).access(name, location);
1704 else if ((sym.kind & ERRONEOUS) == 0 && (sym.kind & TYP) != 0)
1705 return types.createErrorType(name, location, sym.type).tsym;
1706 else
1707 return sym;
1708 }
1709 }
1711 /**
1712 * InvalidSymbolError error class indicating that a symbol matching a
1713 * given name does not exists in a given site.
1714 */
1715 class SymbolNotFoundError extends ResolveError {
1717 SymbolNotFoundError(int kind) {
1718 super(kind, "symbol not found error");
1719 }
1721 @Override
1722 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
1723 DiagnosticPosition pos,
1724 Type site,
1725 Name name,
1726 List<Type> argtypes,
1727 List<Type> typeargtypes) {
1728 argtypes = argtypes == null ? List.<Type>nil() : argtypes;
1729 typeargtypes = typeargtypes == null ? List.<Type>nil() : typeargtypes;
1730 if (name == names.error)
1731 return null;
1733 if (isOperator(name)) {
1734 return diags.create(dkind, false, log.currentSource(), pos,
1735 "operator.cant.be.applied", name, argtypes);
1736 }
1737 boolean hasLocation = false;
1738 if (!site.tsym.name.isEmpty()) {
1739 if (site.tsym.kind == PCK && !site.tsym.exists()) {
1740 return diags.create(dkind, false, log.currentSource(), pos,
1741 "doesnt.exist", site.tsym);
1742 }
1743 hasLocation = true;
1744 }
1745 boolean isConstructor = kind == ABSENT_MTH &&
1746 name == names.table.names.init;
1747 KindName kindname = isConstructor ? KindName.CONSTRUCTOR : absentKind(kind);
1748 Name idname = isConstructor ? site.tsym.name : name;
1749 String errKey = getErrorKey(kindname, typeargtypes.nonEmpty(), hasLocation);
1750 if (hasLocation) {
1751 return diags.create(dkind, false, log.currentSource(), pos,
1752 errKey, kindname, idname, //symbol kindname, name
1753 typeargtypes, argtypes, //type parameters and arguments (if any)
1754 typeKindName(site), site); //location kindname, type
1755 }
1756 else {
1757 return diags.create(dkind, false, log.currentSource(), pos,
1758 errKey, kindname, idname, //symbol kindname, name
1759 typeargtypes, argtypes); //type parameters and arguments (if any)
1760 }
1761 }
1762 //where
1763 private String getErrorKey(KindName kindname, boolean hasTypeArgs, boolean hasLocation) {
1764 String key = "cant.resolve";
1765 String suffix = hasLocation ? ".location" : "";
1766 switch (kindname) {
1767 case METHOD:
1768 case CONSTRUCTOR: {
1769 suffix += ".args";
1770 suffix += hasTypeArgs ? ".params" : "";
1771 }
1772 }
1773 return key + suffix;
1774 }
1775 }
1777 /**
1778 * InvalidSymbolError error class indicating that a given symbol
1779 * (either a method, a constructor or an operand) is not applicable
1780 * given an actual arguments/type argument list.
1781 */
1782 class InapplicableSymbolError extends InvalidSymbolError {
1784 /** An auxiliary explanation set in case of instantiation errors. */
1785 JCDiagnostic explanation;
1787 InapplicableSymbolError(Symbol sym) {
1788 super(WRONG_MTH, sym, "inapplicable symbol error");
1789 }
1791 /** Update sym and explanation and return this.
1792 */
1793 InapplicableSymbolError setWrongSym(Symbol sym, JCDiagnostic explanation) {
1794 this.sym = sym;
1795 this.explanation = explanation;
1796 return this;
1797 }
1799 /** Update sym and return this.
1800 */
1801 InapplicableSymbolError setWrongSym(Symbol sym) {
1802 this.sym = sym;
1803 this.explanation = null;
1804 return this;
1805 }
1807 @Override
1808 public String toString() {
1809 return super.toString() + " explanation=" + explanation;
1810 }
1812 @Override
1813 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
1814 DiagnosticPosition pos,
1815 Type site,
1816 Name name,
1817 List<Type> argtypes,
1818 List<Type> typeargtypes) {
1819 if (name == names.error)
1820 return null;
1822 if (isOperator(name)) {
1823 return diags.create(dkind, false, log.currentSource(),
1824 pos, "operator.cant.be.applied", name, argtypes);
1825 }
1826 else {
1827 Symbol ws = sym.asMemberOf(site, types);
1828 return diags.create(dkind, false, log.currentSource(), pos,
1829 "cant.apply.symbol" + (explanation != null ? ".1" : ""),
1830 kindName(ws),
1831 ws.name == names.init ? ws.owner.name : ws.name,
1832 methodArguments(ws.type.getParameterTypes()),
1833 methodArguments(argtypes),
1834 kindName(ws.owner),
1835 ws.owner.type,
1836 explanation);
1837 }
1838 }
1840 @Override
1841 public Symbol access(Name name, TypeSymbol location) {
1842 return types.createErrorType(name, location, syms.errSymbol.type).tsym;
1843 }
1844 }
1846 /**
1847 * ResolveError error class indicating that a set of symbols
1848 * (either methods, constructors or operands) is not applicable
1849 * given an actual arguments/type argument list.
1850 */
1851 class InapplicableSymbolsError extends ResolveError {
1852 InapplicableSymbolsError(Symbol sym) {
1853 super(WRONG_MTHS, "inapplicable symbols");
1854 }
1856 @Override
1857 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
1858 DiagnosticPosition pos,
1859 Type site,
1860 Name name,
1861 List<Type> argtypes,
1862 List<Type> typeargtypes) {
1863 return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind, pos,
1864 site, name, argtypes, typeargtypes);
1865 }
1866 }
1868 /**
1869 * An InvalidSymbolError error class indicating that a symbol is not
1870 * accessible from a given site
1871 */
1872 class AccessError extends InvalidSymbolError {
1874 private Env<AttrContext> env;
1875 private Type site;
1877 AccessError(Symbol sym) {
1878 this(null, null, sym);
1879 }
1881 AccessError(Env<AttrContext> env, Type site, Symbol sym) {
1882 super(HIDDEN, sym, "access error");
1883 this.env = env;
1884 this.site = site;
1885 if (debugResolve)
1886 log.error("proc.messager", sym + " @ " + site + " is inaccessible.");
1887 }
1889 @Override
1890 public boolean exists() {
1891 return false;
1892 }
1894 @Override
1895 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
1896 DiagnosticPosition pos,
1897 Type site,
1898 Name name,
1899 List<Type> argtypes,
1900 List<Type> typeargtypes) {
1901 if (sym.owner.type.tag == ERROR)
1902 return null;
1904 if (sym.name == names.init && sym.owner != site.tsym) {
1905 return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind,
1906 pos, site, name, argtypes, typeargtypes);
1907 }
1908 else if ((sym.flags() & PUBLIC) != 0
1909 || (env != null && this.site != null
1910 && !isAccessible(env, this.site))) {
1911 return diags.create(dkind, false, log.currentSource(),
1912 pos, "not.def.access.class.intf.cant.access",
1913 sym, sym.location());
1914 }
1915 else if ((sym.flags() & (PRIVATE | PROTECTED)) != 0) {
1916 return diags.create(dkind, false, log.currentSource(),
1917 pos, "report.access", sym,
1918 asFlagSet(sym.flags() & (PRIVATE | PROTECTED)),
1919 sym.location());
1920 }
1921 else {
1922 return diags.create(dkind, false, log.currentSource(),
1923 pos, "not.def.public.cant.access", sym, sym.location());
1924 }
1925 }
1926 }
1928 /**
1929 * InvalidSymbolError error class indicating that an instance member
1930 * has erroneously been accessed from a static context.
1931 */
1932 class StaticError extends InvalidSymbolError {
1934 StaticError(Symbol sym) {
1935 super(STATICERR, sym, "static error");
1936 }
1938 @Override
1939 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
1940 DiagnosticPosition pos,
1941 Type site,
1942 Name name,
1943 List<Type> argtypes,
1944 List<Type> typeargtypes) {
1945 Symbol errSym = ((sym.kind == TYP && sym.type.tag == CLASS)
1946 ? types.erasure(sym.type).tsym
1947 : sym);
1948 return diags.create(dkind, false, log.currentSource(), pos,
1949 "non-static.cant.be.ref", kindName(sym), errSym);
1950 }
1951 }
1953 /**
1954 * InvalidSymbolError error class indicating that a pair of symbols
1955 * (either methods, constructors or operands) are ambiguous
1956 * given an actual arguments/type argument list.
1957 */
1958 class AmbiguityError extends InvalidSymbolError {
1960 /** The other maximally specific symbol */
1961 Symbol sym2;
1963 AmbiguityError(Symbol sym1, Symbol sym2) {
1964 super(AMBIGUOUS, sym1, "ambiguity error");
1965 this.sym2 = sym2;
1966 }
1968 @Override
1969 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
1970 DiagnosticPosition pos,
1971 Type site,
1972 Name name,
1973 List<Type> argtypes,
1974 List<Type> typeargtypes) {
1975 AmbiguityError pair = this;
1976 while (true) {
1977 if (pair.sym.kind == AMBIGUOUS)
1978 pair = (AmbiguityError)pair.sym;
1979 else if (pair.sym2.kind == AMBIGUOUS)
1980 pair = (AmbiguityError)pair.sym2;
1981 else break;
1982 }
1983 Name sname = pair.sym.name;
1984 if (sname == names.init) sname = pair.sym.owner.name;
1985 return diags.create(dkind, false, log.currentSource(),
1986 pos, "ref.ambiguous", sname,
1987 kindName(pair.sym),
1988 pair.sym,
1989 pair.sym.location(site, types),
1990 kindName(pair.sym2),
1991 pair.sym2,
1992 pair.sym2.location(site, types));
1993 }
1994 }
1996 enum MethodResolutionPhase {
1997 BASIC(false, false),
1998 BOX(true, false),
1999 VARARITY(true, true);
2001 boolean isBoxingRequired;
2002 boolean isVarargsRequired;
2004 MethodResolutionPhase(boolean isBoxingRequired, boolean isVarargsRequired) {
2005 this.isBoxingRequired = isBoxingRequired;
2006 this.isVarargsRequired = isVarargsRequired;
2007 }
2009 public boolean isBoxingRequired() {
2010 return isBoxingRequired;
2011 }
2013 public boolean isVarargsRequired() {
2014 return isVarargsRequired;
2015 }
2017 public boolean isApplicable(boolean boxingEnabled, boolean varargsEnabled) {
2018 return (varargsEnabled || !isVarargsRequired) &&
2019 (boxingEnabled || !isBoxingRequired);
2020 }
2021 }
2023 private Map<MethodResolutionPhase, Symbol> methodResolutionCache =
2024 new HashMap<MethodResolutionPhase, Symbol>(MethodResolutionPhase.values().length);
2026 final List<MethodResolutionPhase> methodResolutionSteps = List.of(BASIC, BOX, VARARITY);
2028 private MethodResolutionPhase firstErroneousResolutionPhase() {
2029 MethodResolutionPhase bestSoFar = BASIC;
2030 Symbol sym = methodNotFound;
2031 List<MethodResolutionPhase> steps = methodResolutionSteps;
2032 while (steps.nonEmpty() &&
2033 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
2034 sym.kind >= WRONG_MTHS) {
2035 sym = methodResolutionCache.get(steps.head);
2036 bestSoFar = steps.head;
2037 steps = steps.tail;
2038 }
2039 return bestSoFar;
2040 }
2041 }