< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java

Print this page

  36 import com.sun.tools.javac.comp.DeferredAttr.AttrMode;
  37 import com.sun.tools.javac.comp.DeferredAttr.DeferredAttrContext;
  38 import com.sun.tools.javac.comp.DeferredAttr.DeferredType;
  39 import com.sun.tools.javac.comp.Resolve.MethodResolutionContext.Candidate;
  40 import com.sun.tools.javac.comp.Resolve.MethodResolutionDiagHelper.Template;
  41 import com.sun.tools.javac.comp.Resolve.ReferenceLookupResult.StaticKind;
  42 import com.sun.tools.javac.jvm.*;
  43 import com.sun.tools.javac.main.Option;
  44 import com.sun.tools.javac.resources.CompilerProperties.Errors;
  45 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
  46 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
  47 import com.sun.tools.javac.tree.*;
  48 import com.sun.tools.javac.tree.JCTree.*;
  49 import com.sun.tools.javac.tree.JCTree.JCMemberReference.ReferenceKind;
  50 import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*;
  51 import com.sun.tools.javac.util.*;
  52 import com.sun.tools.javac.util.DefinedBy.Api;
  53 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
  54 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
  55 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
  56 import com.sun.tools.javac.util.JCDiagnostic.Warning;
  57 
  58 import java.util.Arrays;
  59 import java.util.Collection;
  60 import java.util.EnumSet;
  61 import java.util.HashSet;
  62 import java.util.Iterator;
  63 import java.util.LinkedHashMap;
  64 import java.util.Map;
  65 import java.util.Set;
  66 import java.util.function.BiFunction;
  67 import java.util.function.BiPredicate;
  68 import java.util.function.Consumer;
  69 import java.util.function.Function;
  70 import java.util.function.Predicate;
  71 import java.util.stream.Stream;
  72 import java.util.stream.StreamSupport;
  73 
  74 import javax.lang.model.element.ElementVisitor;
  75 
  76 import static com.sun.tools.javac.code.Flags.*;
  77 import static com.sun.tools.javac.code.Flags.BLOCK;
  78 import static com.sun.tools.javac.code.Flags.STATIC;
  79 import static com.sun.tools.javac.code.Kinds.*;
  80 import static com.sun.tools.javac.code.Kinds.Kind.*;
  81 import static com.sun.tools.javac.code.TypeTag.*;
  82 import static com.sun.tools.javac.comp.Resolve.MethodResolutionPhase.*;
  83 import static com.sun.tools.javac.tree.JCTree.Tag.*;
  84 import static com.sun.tools.javac.util.Iterators.createCompoundIterator;
  85 
  86 /** Helper class for name resolution, used mostly by the attribution phase.
  87  *
  88  *  <p><b>This is NOT part of any supported API.

 401      *                as a member.
 402      *  @param sym    The symbol.
 403      */
 404     public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym) {
 405         return isAccessible(env, site, sym, false);
 406     }
 407     public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym, boolean checkInner) {
 408         if (sym.name == names.init && sym.owner != site.tsym) return false;
 409 
 410         /* 15.9.5.1: Note that it is possible for the signature of the anonymous constructor
 411            to refer to an inaccessible type
 412         */
 413         if (env.enclMethod != null && (env.enclMethod.mods.flags & ANONCONSTR) != 0)
 414             return true;
 415 
 416         if (env.info.visitingServiceImplementation &&
 417             env.toplevel.modle == sym.packge().modle) {
 418             return true;
 419         }
 420 
 421         switch ((short)(sym.flags() & AccessFlags)) {
 422         case PRIVATE:
 423             return
 424                 (env.enclClass.sym == sym.owner // fast special case
 425                  ||
 426                  env.enclClass.sym.outermostClass() ==
 427                  sym.owner.outermostClass())
 428                 &&
 429                 sym.isInheritedIn(site.tsym, types);
 430         case 0:
 431             return
 432                 (env.toplevel.packge == sym.owner.owner // fast special case
 433                  ||
 434                  env.toplevel.packge == sym.packge())
 435                 &&
 436                 isAccessible(env, site, checkInner)
 437                 &&
 438                 sym.isInheritedIn(site.tsym, types)
 439                 &&
 440                 notOverriddenIn(site, sym);
 441         case PROTECTED:
 442             return
 443                 (env.toplevel.packge == sym.owner.owner // fast special case
 444                  ||
 445                  env.toplevel.packge == sym.packge()
 446                  ||
 447                  isProtectedAccessible(sym, env.enclClass.sym, site)
 448                  ||
 449                  // OK to select instance method or field from 'super' or type name
 450                  // (but type names should be disallowed elsewhere!)
 451                  env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
 452                 &&
 453                 isAccessible(env, site, checkInner)
 454                 &&
 455                 notOverriddenIn(site, sym);
 456         default: // this case includes erroneous combinations as well
 457             return isAccessible(env, site, checkInner) && notOverriddenIn(site, sym);

















 458         }
 459     }
 460     //where
 461     /* `sym' is accessible only if not overridden by
 462      * another symbol which is a member of `site'
 463      * (because, if it is overridden, `sym' is not strictly
 464      * speaking a member of `site'). A polymorphic signature method
 465      * cannot be overridden (e.g. MH.invokeExact(Object[])).
 466      */
 467     private boolean notOverriddenIn(Type site, Symbol sym) {
 468         if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
 469             return true;
 470         else {
 471             Symbol s2 = ((MethodSymbol)sym).implementation(site.tsym, types, true);
 472             return (s2 == null || s2 == sym || sym.owner == s2.owner || (sym.owner.isInterface() && s2.owner == syms.objectType.tsym) ||
 473                     !types.isSubSignature(types.memberType(site, s2), types.memberType(site, sym)));
 474         }





 475     }
 476     //where
 477         /** Is given protected symbol accessible if it is selected from given site
 478          *  and the selection takes place in given class?
 479          *  @param sym     The symbol with protected access
 480          *  @param c       The class where the access takes place
 481          *  @site          The type of the qualifier
 482          */
 483         private
 484         boolean isProtectedAccessible(Symbol sym, ClassSymbol c, Type site) {
 485             Type newSite = site.hasTag(TYPEVAR) ? site.getUpperBound() : site;
 486             while (c != null &&
 487                    !(c.isSubClass(sym.owner, types) &&
 488                      (c.flags() & INTERFACE) == 0 &&
 489                      // In JLS 2e 6.6.2.1, the subclass restriction applies
 490                      // only to instance fields and methods -- types are excluded
 491                      // regardless of whether they are declared 'static' or not.
 492                      ((sym.flags() & STATIC) != 0 || sym.kind == TYP || newSite.tsym.isSubClass(c, types))))
 493                 c = c.owner.enclClass();
 494             return c != null;

1669 
1670                 // same signature; select (a) the non-bridge method, or
1671                 // (b) the one that overrides the other, or (c) the concrete
1672                 // one, or (d) merge both abstract signatures
1673                 if ((m1.flags() & BRIDGE) != (m2.flags() & BRIDGE))
1674                     return ((m1.flags() & BRIDGE) != 0) ? m2 : m1;
1675 
1676                 if (m1.baseSymbol() == m2.baseSymbol()) {
1677                     // this is the same imported symbol which has been cloned twice.
1678                     // Return the first one (either will do).
1679                     return m1;
1680                 }
1681 
1682                 // if one overrides or hides the other, use it
1683                 TypeSymbol m1Owner = (TypeSymbol)m1.owner;
1684                 TypeSymbol m2Owner = (TypeSymbol)m2.owner;
1685                 // the two owners can never be the same if the target methods are compiled from source,
1686                 // but we need to protect against cases where the methods are defined in some classfile
1687                 // and make sure we issue an ambiguity error accordingly (by skipping the logic below).
1688                 if (m1Owner != m2Owner) {
1689                     if (types.asSuper(m1Owner.type, m2Owner) != null &&
1690                         ((m1.owner.flags_field & INTERFACE) == 0 ||
1691                          (m2.owner.flags_field & INTERFACE) != 0) &&
1692                         m1.overrides(m2, m1Owner, types, false))
1693                         return m1;
1694                     if (types.asSuper(m2Owner.type, m1Owner) != null &&
1695                         ((m2.owner.flags_field & INTERFACE) == 0 ||
1696                          (m1.owner.flags_field & INTERFACE) != 0) &&
1697                         m2.overrides(m1, m2Owner, types, false))
1698                         return m2;
1699                 }
1700                 boolean m1Abstract = (m1.flags() & ABSTRACT) != 0;
1701                 boolean m2Abstract = (m2.flags() & ABSTRACT) != 0;
1702                 if (m1Abstract && !m2Abstract) return m2;
1703                 if (m2Abstract && !m1Abstract) return m1;
1704                 // both abstract or both concrete
1705                 return ambiguityError(m1, m2);
1706             }
1707             if (m1SignatureMoreSpecific) return m1;
1708             if (m2SignatureMoreSpecific) return m2;
1709             return ambiguityError(m1, m2);
1710         case AMBIGUOUS:
1711             //compare m1 to ambiguous methods in m2
1712             AmbiguityError e = (AmbiguityError)m2.baseSymbol();
1713             boolean m1MoreSpecificThanAnyAmbiguous = true;
1714             boolean allAmbiguousMoreSpecificThanM1 = true;

2276                 bestSoFar = new AmbiguityError(bestSoFar, sym);
2277             else
2278                 bestSoFar = bestOf(bestSoFar, sym);
2279         }
2280         return bestSoFar;
2281     }
2282 
2283     /** Find qualified member type.
2284      *  @param env       The current environment.
2285      *  @param site      The original type from where the selection takes
2286      *                   place.
2287      *  @param name      The type's name.
2288      *  @param c         The class to search for the member type. This is
2289      *                   always a superclass or implemented interface of
2290      *                   site's class.
2291      */
2292     Symbol findMemberType(Env<AttrContext> env,
2293                           Type site,
2294                           Name name,
2295                           TypeSymbol c) {
















2296         Symbol sym = findImmediateMemberType(env, site, name, c);
2297 
2298         if (sym != typeNotFound)
2299             return sym;
2300 
2301         return findInheritedMemberType(env, site, name, c);
2302 
2303     }
2304 
2305     /** Find a global type in given scope and load corresponding class.
2306      *  @param env       The current environment.
2307      *  @param scope     The scope in which to look for the type.
2308      *  @param name      The type's name.
2309      */
2310     Symbol findGlobalType(Env<AttrContext> env, Scope scope, Name name, RecoveryLoadClass recoveryLoadClass) {
2311         Symbol bestSoFar = typeNotFound;
2312         for (Symbol s : scope.getSymbolsByName(name)) {
2313             Symbol sym = loadClass(env, s.flatName(), recoveryLoadClass);
2314             if (bestSoFar.kind == TYP && sym.kind == TYP &&
2315                 bestSoFar != sym)

2324         for (Symbol sym : env.info.scope.getSymbolsByName(name)) {
2325             if (sym.kind == TYP) {
2326                 if (sym.type.hasTag(TYPEVAR) &&
2327                         (staticOnly || (isStatic(env) && sym.owner.kind == TYP)))
2328                     // if staticOnly is set, it means that we have recursed through a static declaration,
2329                     // so type variable symbols should not be accessible. If staticOnly is unset, but
2330                     // we are in a static declaration (field or method), we should not allow type-variables
2331                     // defined in the enclosing class to "leak" into this context.
2332                     return new StaticError(sym);
2333                 return sym;
2334             }
2335         }
2336         return typeNotFound;
2337     }
2338 
2339     /** Find an unqualified type symbol.
2340      *  @param env       The current environment.
2341      *  @param name      The type's name.
2342      */
2343     Symbol findType(Env<AttrContext> env, Name name) {








2344         if (name == names.empty)
2345             return typeNotFound; // do not allow inadvertent "lookup" of anonymous types
2346         Symbol bestSoFar = typeNotFound;
2347         Symbol sym;
2348         boolean staticOnly = false;
2349         for (Env<AttrContext> env1 = env; env1.outer != null; env1 = env1.outer) {
2350             // First, look for a type variable and the first member type
2351             final Symbol tyvar = findTypeVar(env1, name, staticOnly);
2352             if (isStatic(env1)) staticOnly = true;
2353             sym = findImmediateMemberType(env1, env1.enclClass.sym.type,
2354                                           name, env1.enclClass.sym);
2355 
2356             // Return the type variable if we have it, and have no
2357             // immediate member, OR the type variable is for a method.
2358             if (tyvar != typeNotFound) {
2359                 if (env.baseClause || sym == typeNotFound ||
2360                     (tyvar.kind == TYP && tyvar.exists() &&
2361                      tyvar.owner.kind == MTH)) {
2362                     return tyvar;
2363                 }

3538         /** The original method reference lookup site. */
3539         Type originalSite;
3540 
3541         MethodReferenceLookupHelper(JCMemberReference referenceTree, Name name, Type site,
3542                 List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
3543             super(referenceTree, name, types.skipTypeVars(site, true), argtypes, typeargtypes, maxPhase);
3544             this.originalSite = site;
3545         }
3546 
3547         @Override
3548         final Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
3549             return findMethod(env, site, name, argtypes, typeargtypes,
3550                     phase.isBoxingRequired(), phase.isVarargsRequired());
3551         }
3552 
3553         @Override
3554         ReferenceLookupHelper unboundLookup(InferenceContext inferenceContext) {
3555             if (TreeInfo.isStaticSelector(referenceTree.expr, names)) {
3556                 if (argtypes.nonEmpty() &&
3557                         (argtypes.head.hasTag(NONE) ||
3558                         types.isSubtypeUnchecked(inferenceContext.asUndetVar(argtypes.head), originalSite))) {
3559                     return new UnboundMethodReferenceLookupHelper(referenceTree, name,
3560                             originalSite, argtypes, typeargtypes, maxPhase);
3561                 } else {
3562                     return new ReferenceLookupHelper(referenceTree, name, site, argtypes, typeargtypes, maxPhase) {
3563                         @Override
3564                         ReferenceLookupHelper unboundLookup(InferenceContext inferenceContext) {
3565                             return this;
3566                         }
3567 
3568                         @Override
3569                         Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
3570                             return methodNotFound;
3571                         }
3572 
3573                         @Override
3574                         ReferenceKind referenceKind(Symbol sym) {
3575                             Assert.error();
3576                             return null;
3577                         }
3578                     };

3591                 return selName != null && selName == names._super ?
3592                         ReferenceKind.SUPER :
3593                         ReferenceKind.BOUND;
3594             }
3595         }
3596     }
3597 
3598     /**
3599      * Helper class for unbound method reference lookup. Essentially the same
3600      * as the basic method reference lookup helper; main difference is that static
3601      * lookup results are thrown away. If qualifier type is raw, an attempt to
3602      * infer a parameterized type is made using the first actual argument (that
3603      * would otherwise be ignored during the lookup).
3604      */
3605     class UnboundMethodReferenceLookupHelper extends MethodReferenceLookupHelper {
3606 
3607         UnboundMethodReferenceLookupHelper(JCMemberReference referenceTree, Name name, Type site,
3608                 List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
3609             super(referenceTree, name, site, argtypes.tail, typeargtypes, maxPhase);
3610             if (site.isRaw() && !argtypes.head.hasTag(NONE)) {
3611                 Type asSuperSite = types.asSuper(argtypes.head, site.tsym);
3612                 this.site = types.skipTypeVars(asSuperSite, true);
3613             }
3614         }
3615 
3616         @Override
3617         ReferenceLookupHelper unboundLookup(InferenceContext inferenceContext) {
3618             return this;
3619         }
3620 
3621         @Override
3622         ReferenceKind referenceKind(Symbol sym) {
3623             return ReferenceKind.UNBOUND;
3624         }
3625     }
3626 
3627     /**
3628      * Helper class for array constructor lookup; an array constructor lookup
3629      * is simulated by looking up a method that returns the array type specified
3630      * as qualifier, and that accepts a single int parameter (size of the array).
3631      */

3650             return ReferenceKind.ARRAY_CTOR;
3651         }
3652     }
3653 
3654     /**
3655      * Helper class for constructor reference lookup. The lookup logic is based
3656      * upon either Resolve.findMethod or Resolve.findDiamond - depending on
3657      * whether the constructor reference needs diamond inference (this is the case
3658      * if the qualifier type is raw). A special erroneous symbol is returned
3659      * if the lookup returns the constructor of an inner class and there's no
3660      * enclosing instance in scope.
3661      */
3662     class ConstructorReferenceLookupHelper extends ReferenceLookupHelper {
3663 
3664         boolean needsInference;
3665 
3666         ConstructorReferenceLookupHelper(JCMemberReference referenceTree, Type site, List<Type> argtypes,
3667                 List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
3668             super(referenceTree, names.init, site, argtypes, typeargtypes, maxPhase);
3669             if (site.isRaw()) {
3670                 this.site = new ClassType(site.getEnclosingType(), site.tsym.type.getTypeArguments(), site.tsym, site.getMetadata());
3671                 needsInference = true;
3672             }
3673         }
3674 
3675         @Override
3676         protected Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
3677             Symbol sym = needsInference ?
3678                 findDiamond(env, site, argtypes, typeargtypes, phase.isBoxingRequired(), phase.isVarargsRequired()) :
3679                 findMethod(env, site, name, argtypes, typeargtypes,
3680                         phase.isBoxingRequired(), phase.isVarargsRequired());
3681             return enclosingInstanceMissing(env, site) ? new BadConstructorReferenceError(sym) : sym;
3682         }
3683 
3684         @Override
3685         ReferenceKind referenceKind(Symbol sym) {
3686             return site.getEnclosingType().hasTag(NONE) ?
3687                     ReferenceKind.TOPLEVEL : ReferenceKind.IMPLICIT_INNER;
3688         }
3689     }
3690 

3740             if (isStatic(env1)) staticOnly = true;
3741             if (env1.enclClass.sym == c) {
3742                 Symbol sym = env1.info.scope.findFirst(name);
3743                 if (sym != null) {
3744                     if (staticOnly) sym = new StaticError(sym);
3745                     return accessBase(sym, pos, env.enclClass.sym.type,
3746                                   name, true);
3747                 }
3748             }
3749             if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
3750             env1 = env1.outer;
3751         }
3752         if (c.isInterface() &&
3753             name == names._super && !isStatic(env) &&
3754             types.isDirectSuperInterface(c, env.enclClass.sym)) {
3755             //this might be a default super call if one of the superinterfaces is 'c'
3756             for (Type t : pruneInterfaces(env.enclClass.type)) {
3757                 if (t.tsym == c) {
3758                     env.info.defaultSuperCallSite = t;
3759                     return new VarSymbol(0, names._super,
3760                             types.asSuper(env.enclClass.type, c), env.enclClass.sym);
3761                 }
3762             }
3763             //find a direct super type that is a subtype of 'c'
3764             for (Type i : types.directSupertypes(env.enclClass.type)) {
3765                 if (i.tsym.isSubClass(c, types) && i.tsym != c) {
3766                     log.error(pos,
3767                               Errors.IllegalDefaultSuperCall(c,
3768                                                              Fragments.RedundantSupertype(c, i)));
3769                     return syms.errSymbol;
3770                 }
3771             }
3772             Assert.error();
3773         }
3774         log.error(pos, Errors.NotEnclClass(c));
3775         return syms.errSymbol;
3776     }
3777     //where
3778     private List<Type> pruneInterfaces(Type t) {
3779         ListBuffer<Type> result = new ListBuffer<>();
3780         for (Type t1 : types.interfaces(t)) {

  36 import com.sun.tools.javac.comp.DeferredAttr.AttrMode;
  37 import com.sun.tools.javac.comp.DeferredAttr.DeferredAttrContext;
  38 import com.sun.tools.javac.comp.DeferredAttr.DeferredType;
  39 import com.sun.tools.javac.comp.Resolve.MethodResolutionContext.Candidate;
  40 import com.sun.tools.javac.comp.Resolve.MethodResolutionDiagHelper.Template;
  41 import com.sun.tools.javac.comp.Resolve.ReferenceLookupResult.StaticKind;
  42 import com.sun.tools.javac.jvm.*;
  43 import com.sun.tools.javac.main.Option;
  44 import com.sun.tools.javac.resources.CompilerProperties.Errors;
  45 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
  46 import com.sun.tools.javac.resources.CompilerProperties.Warnings;
  47 import com.sun.tools.javac.tree.*;
  48 import com.sun.tools.javac.tree.JCTree.*;
  49 import com.sun.tools.javac.tree.JCTree.JCMemberReference.ReferenceKind;
  50 import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*;
  51 import com.sun.tools.javac.util.*;
  52 import com.sun.tools.javac.util.DefinedBy.Api;
  53 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
  54 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
  55 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;

  56 
  57 import java.util.Arrays;
  58 import java.util.Collection;
  59 import java.util.EnumSet;
  60 import java.util.HashSet;
  61 import java.util.Iterator;
  62 import java.util.LinkedHashMap;
  63 import java.util.Map;
  64 import java.util.Set;
  65 import java.util.function.BiFunction;
  66 import java.util.function.BiPredicate;

  67 import java.util.function.Function;
  68 import java.util.function.Predicate;
  69 import java.util.stream.Stream;
  70 import java.util.stream.StreamSupport;
  71 
  72 import javax.lang.model.element.ElementVisitor;
  73 
  74 import static com.sun.tools.javac.code.Flags.*;
  75 import static com.sun.tools.javac.code.Flags.BLOCK;
  76 import static com.sun.tools.javac.code.Flags.STATIC;
  77 import static com.sun.tools.javac.code.Kinds.*;
  78 import static com.sun.tools.javac.code.Kinds.Kind.*;
  79 import static com.sun.tools.javac.code.TypeTag.*;
  80 import static com.sun.tools.javac.comp.Resolve.MethodResolutionPhase.*;
  81 import static com.sun.tools.javac.tree.JCTree.Tag.*;
  82 import static com.sun.tools.javac.util.Iterators.createCompoundIterator;
  83 
  84 /** Helper class for name resolution, used mostly by the attribution phase.
  85  *
  86  *  <p><b>This is NOT part of any supported API.

 399      *                as a member.
 400      *  @param sym    The symbol.
 401      */
 402     public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym) {
 403         return isAccessible(env, site, sym, false);
 404     }
 405     public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym, boolean checkInner) {
 406         if (sym.name == names.init && sym.owner != site.tsym) return false;
 407 
 408         /* 15.9.5.1: Note that it is possible for the signature of the anonymous constructor
 409            to refer to an inaccessible type
 410         */
 411         if (env.enclMethod != null && (env.enclMethod.mods.flags & ANONCONSTR) != 0)
 412             return true;
 413 
 414         if (env.info.visitingServiceImplementation &&
 415             env.toplevel.modle == sym.packge().modle) {
 416             return true;
 417         }
 418 
 419         ClassSymbol enclosingCsym = env.enclClass.sym;
 420         if (sym.kind == MTH || sym.kind == VAR) {
 421             /* If any primitive class types are involved, ask the same question in the reference universe,
 422                where the hierarchy is navigable
 423             */
 424             if (site.isPrimitiveClass())
 425                 site = site.referenceProjection();
 426         } else if (sym.kind == TYP) {
 427             // A type is accessible in a reference projection if it was
 428             // accessible in the value projection.
 429             if (site.isReferenceProjection())
 430                 site = site.valueProjection();
 431         }
 432         try {
 433             switch ((short)(sym.flags() & AccessFlags)) {
 434                 case PRIVATE:
 435                     return
 436                             (env.enclClass.sym == sym.owner // fast special case
 437                                     ||
 438                                     env.enclClass.sym.outermostClass() ==
 439                                             sym.owner.outermostClass())
 440                                     &&
 441                                     sym.isInheritedIn(site.tsym, types);
 442                 case 0:
 443                     return
 444                             (env.toplevel.packge == sym.owner.owner // fast special case
 445                                     ||
 446                                     env.toplevel.packge == sym.packge())
 447                                     &&
 448                                     isAccessible(env, site, checkInner)
 449                                     &&
 450                                     sym.isInheritedIn(site.tsym, types)
 451                                     &&
 452                                     notOverriddenIn(site, sym);
 453                 case PROTECTED:
 454                     return
 455                             (env.toplevel.packge == sym.owner.owner // fast special case
 456                                     ||
 457                                     env.toplevel.packge == sym.packge()
 458                                     ||
 459                                     isProtectedAccessible(sym, env.enclClass.sym, site)
 460                                     ||
 461                                     // OK to select instance method or field from 'super' or type name
 462                                     // (but type names should be disallowed elsewhere!)
 463                                     env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
 464                                     &&
 465                                     isAccessible(env, site, checkInner)
 466                                     &&
 467                                     notOverriddenIn(site, sym);
 468                 default: // this case includes erroneous combinations as well
 469                     return isAccessible(env, site, checkInner) && notOverriddenIn(site, sym);
 470             }
 471         } finally {
 472             env.enclClass.sym = enclosingCsym;
 473         }
 474     }
 475     //where
 476     /* `sym' is accessible only if not overridden by
 477      * another symbol which is a member of `site'
 478      * (because, if it is overridden, `sym' is not strictly
 479      * speaking a member of `site'). A polymorphic signature method
 480      * cannot be overridden (e.g. MH.invokeExact(Object[])).
 481      */
 482     private boolean notOverriddenIn(Type site, Symbol sym) {
 483         if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
 484             return true;
 485 
 486         /* If any primitive class types are involved, ask the same question in the reference universe,
 487            where the hierarchy is navigable
 488         */
 489         if (site.isPrimitiveClass())
 490             site = site.referenceProjection();
 491 
 492         Symbol s2 = ((MethodSymbol)sym).implementation(site.tsym, types, true);
 493         return (s2 == null || s2 == sym || sym.owner == s2.owner || (sym.owner.isInterface() && s2.owner == syms.objectType.tsym) ||
 494                 !types.isSubSignature(types.memberType(site, s2), types.memberType(site, sym)));
 495     }
 496     //where
 497         /** Is given protected symbol accessible if it is selected from given site
 498          *  and the selection takes place in given class?
 499          *  @param sym     The symbol with protected access
 500          *  @param c       The class where the access takes place
 501          *  @site          The type of the qualifier
 502          */
 503         private
 504         boolean isProtectedAccessible(Symbol sym, ClassSymbol c, Type site) {
 505             Type newSite = site.hasTag(TYPEVAR) ? site.getUpperBound() : site;
 506             while (c != null &&
 507                    !(c.isSubClass(sym.owner, types) &&
 508                      (c.flags() & INTERFACE) == 0 &&
 509                      // In JLS 2e 6.6.2.1, the subclass restriction applies
 510                      // only to instance fields and methods -- types are excluded
 511                      // regardless of whether they are declared 'static' or not.
 512                      ((sym.flags() & STATIC) != 0 || sym.kind == TYP || newSite.tsym.isSubClass(c, types))))
 513                 c = c.owner.enclClass();
 514             return c != null;

1689 
1690                 // same signature; select (a) the non-bridge method, or
1691                 // (b) the one that overrides the other, or (c) the concrete
1692                 // one, or (d) merge both abstract signatures
1693                 if ((m1.flags() & BRIDGE) != (m2.flags() & BRIDGE))
1694                     return ((m1.flags() & BRIDGE) != 0) ? m2 : m1;
1695 
1696                 if (m1.baseSymbol() == m2.baseSymbol()) {
1697                     // this is the same imported symbol which has been cloned twice.
1698                     // Return the first one (either will do).
1699                     return m1;
1700                 }
1701 
1702                 // if one overrides or hides the other, use it
1703                 TypeSymbol m1Owner = (TypeSymbol)m1.owner;
1704                 TypeSymbol m2Owner = (TypeSymbol)m2.owner;
1705                 // the two owners can never be the same if the target methods are compiled from source,
1706                 // but we need to protect against cases where the methods are defined in some classfile
1707                 // and make sure we issue an ambiguity error accordingly (by skipping the logic below).
1708                 if (m1Owner != m2Owner) {
1709                     if (types.asSuper(m1Owner.type.referenceProjectionOrSelf(), m2Owner) != null &&
1710                         ((m1.owner.flags_field & INTERFACE) == 0 ||
1711                          (m2.owner.flags_field & INTERFACE) != 0) &&
1712                         m1.overrides(m2, m1Owner, types, false))
1713                         return m1;
1714                     if (types.asSuper(m2Owner.type.referenceProjectionOrSelf(), m1Owner) != null &&
1715                         ((m2.owner.flags_field & INTERFACE) == 0 ||
1716                          (m1.owner.flags_field & INTERFACE) != 0) &&
1717                         m2.overrides(m1, m2Owner, types, false))
1718                         return m2;
1719                 }
1720                 boolean m1Abstract = (m1.flags() & ABSTRACT) != 0;
1721                 boolean m2Abstract = (m2.flags() & ABSTRACT) != 0;
1722                 if (m1Abstract && !m2Abstract) return m2;
1723                 if (m2Abstract && !m1Abstract) return m1;
1724                 // both abstract or both concrete
1725                 return ambiguityError(m1, m2);
1726             }
1727             if (m1SignatureMoreSpecific) return m1;
1728             if (m2SignatureMoreSpecific) return m2;
1729             return ambiguityError(m1, m2);
1730         case AMBIGUOUS:
1731             //compare m1 to ambiguous methods in m2
1732             AmbiguityError e = (AmbiguityError)m2.baseSymbol();
1733             boolean m1MoreSpecificThanAnyAmbiguous = true;
1734             boolean allAmbiguousMoreSpecificThanM1 = true;

2296                 bestSoFar = new AmbiguityError(bestSoFar, sym);
2297             else
2298                 bestSoFar = bestOf(bestSoFar, sym);
2299         }
2300         return bestSoFar;
2301     }
2302 
2303     /** Find qualified member type.
2304      *  @param env       The current environment.
2305      *  @param site      The original type from where the selection takes
2306      *                   place.
2307      *  @param name      The type's name.
2308      *  @param c         The class to search for the member type. This is
2309      *                   always a superclass or implemented interface of
2310      *                   site's class.
2311      */
2312     Symbol findMemberType(Env<AttrContext> env,
2313                           Type site,
2314                           Name name,
2315                           TypeSymbol c) {
2316         return findMemberTypeInternal(env,site, name, c);
2317     }
2318 
2319     /** Find qualified member type.
2320      *  @param env       The current environment.
2321      *  @param site      The original type from where the selection takes
2322      *                   place.
2323      *  @param name      The type's name.
2324      *  @param c         The class to search for the member type. This is
2325      *                   always a superclass or implemented interface of
2326      *                   site's class.
2327      */
2328     Symbol findMemberTypeInternal(Env<AttrContext> env,
2329                           Type site,
2330                           Name name,
2331                           TypeSymbol c) {
2332         Symbol sym = findImmediateMemberType(env, site, name, c);
2333 
2334         if (sym != typeNotFound)
2335             return sym;
2336 
2337         return findInheritedMemberType(env, site, name, c);
2338 
2339     }
2340 
2341     /** Find a global type in given scope and load corresponding class.
2342      *  @param env       The current environment.
2343      *  @param scope     The scope in which to look for the type.
2344      *  @param name      The type's name.
2345      */
2346     Symbol findGlobalType(Env<AttrContext> env, Scope scope, Name name, RecoveryLoadClass recoveryLoadClass) {
2347         Symbol bestSoFar = typeNotFound;
2348         for (Symbol s : scope.getSymbolsByName(name)) {
2349             Symbol sym = loadClass(env, s.flatName(), recoveryLoadClass);
2350             if (bestSoFar.kind == TYP && sym.kind == TYP &&
2351                 bestSoFar != sym)

2360         for (Symbol sym : env.info.scope.getSymbolsByName(name)) {
2361             if (sym.kind == TYP) {
2362                 if (sym.type.hasTag(TYPEVAR) &&
2363                         (staticOnly || (isStatic(env) && sym.owner.kind == TYP)))
2364                     // if staticOnly is set, it means that we have recursed through a static declaration,
2365                     // so type variable symbols should not be accessible. If staticOnly is unset, but
2366                     // we are in a static declaration (field or method), we should not allow type-variables
2367                     // defined in the enclosing class to "leak" into this context.
2368                     return new StaticError(sym);
2369                 return sym;
2370             }
2371         }
2372         return typeNotFound;
2373     }
2374 
2375     /** Find an unqualified type symbol.
2376      *  @param env       The current environment.
2377      *  @param name      The type's name.
2378      */
2379     Symbol findType(Env<AttrContext> env, Name name) {
2380         return findTypeInternal(env, name);
2381     }
2382 
2383     /** Find an unqualified type symbol.
2384      *  @param env       The current environment.
2385      *  @param name      The type's name.
2386      */
2387     Symbol findTypeInternal(Env<AttrContext> env, Name name) {
2388         if (name == names.empty)
2389             return typeNotFound; // do not allow inadvertent "lookup" of anonymous types
2390         Symbol bestSoFar = typeNotFound;
2391         Symbol sym;
2392         boolean staticOnly = false;
2393         for (Env<AttrContext> env1 = env; env1.outer != null; env1 = env1.outer) {
2394             // First, look for a type variable and the first member type
2395             final Symbol tyvar = findTypeVar(env1, name, staticOnly);
2396             if (isStatic(env1)) staticOnly = true;
2397             sym = findImmediateMemberType(env1, env1.enclClass.sym.type,
2398                                           name, env1.enclClass.sym);
2399 
2400             // Return the type variable if we have it, and have no
2401             // immediate member, OR the type variable is for a method.
2402             if (tyvar != typeNotFound) {
2403                 if (env.baseClause || sym == typeNotFound ||
2404                     (tyvar.kind == TYP && tyvar.exists() &&
2405                      tyvar.owner.kind == MTH)) {
2406                     return tyvar;
2407                 }

3582         /** The original method reference lookup site. */
3583         Type originalSite;
3584 
3585         MethodReferenceLookupHelper(JCMemberReference referenceTree, Name name, Type site,
3586                 List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
3587             super(referenceTree, name, types.skipTypeVars(site, true), argtypes, typeargtypes, maxPhase);
3588             this.originalSite = site;
3589         }
3590 
3591         @Override
3592         final Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
3593             return findMethod(env, site, name, argtypes, typeargtypes,
3594                     phase.isBoxingRequired(), phase.isVarargsRequired());
3595         }
3596 
3597         @Override
3598         ReferenceLookupHelper unboundLookup(InferenceContext inferenceContext) {
3599             if (TreeInfo.isStaticSelector(referenceTree.expr, names)) {
3600                 if (argtypes.nonEmpty() &&
3601                         (argtypes.head.hasTag(NONE) ||
3602                         types.isSubtypeUnchecked(inferenceContext.asUndetVar(argtypes.head.referenceProjectionOrSelf()), originalSite))) {
3603                     return new UnboundMethodReferenceLookupHelper(referenceTree, name,
3604                             originalSite, argtypes, typeargtypes, maxPhase);
3605                 } else {
3606                     return new ReferenceLookupHelper(referenceTree, name, site, argtypes, typeargtypes, maxPhase) {
3607                         @Override
3608                         ReferenceLookupHelper unboundLookup(InferenceContext inferenceContext) {
3609                             return this;
3610                         }
3611 
3612                         @Override
3613                         Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
3614                             return methodNotFound;
3615                         }
3616 
3617                         @Override
3618                         ReferenceKind referenceKind(Symbol sym) {
3619                             Assert.error();
3620                             return null;
3621                         }
3622                     };

3635                 return selName != null && selName == names._super ?
3636                         ReferenceKind.SUPER :
3637                         ReferenceKind.BOUND;
3638             }
3639         }
3640     }
3641 
3642     /**
3643      * Helper class for unbound method reference lookup. Essentially the same
3644      * as the basic method reference lookup helper; main difference is that static
3645      * lookup results are thrown away. If qualifier type is raw, an attempt to
3646      * infer a parameterized type is made using the first actual argument (that
3647      * would otherwise be ignored during the lookup).
3648      */
3649     class UnboundMethodReferenceLookupHelper extends MethodReferenceLookupHelper {
3650 
3651         UnboundMethodReferenceLookupHelper(JCMemberReference referenceTree, Name name, Type site,
3652                 List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
3653             super(referenceTree, name, site, argtypes.tail, typeargtypes, maxPhase);
3654             if (site.isRaw() && !argtypes.head.hasTag(NONE)) {
3655                 Type asSuperSite = types.asSuper(argtypes.head.referenceProjectionOrSelf(), site.tsym);
3656                 this.site = types.skipTypeVars(asSuperSite, true);
3657             }
3658         }
3659 
3660         @Override
3661         ReferenceLookupHelper unboundLookup(InferenceContext inferenceContext) {
3662             return this;
3663         }
3664 
3665         @Override
3666         ReferenceKind referenceKind(Symbol sym) {
3667             return ReferenceKind.UNBOUND;
3668         }
3669     }
3670 
3671     /**
3672      * Helper class for array constructor lookup; an array constructor lookup
3673      * is simulated by looking up a method that returns the array type specified
3674      * as qualifier, and that accepts a single int parameter (size of the array).
3675      */

3694             return ReferenceKind.ARRAY_CTOR;
3695         }
3696     }
3697 
3698     /**
3699      * Helper class for constructor reference lookup. The lookup logic is based
3700      * upon either Resolve.findMethod or Resolve.findDiamond - depending on
3701      * whether the constructor reference needs diamond inference (this is the case
3702      * if the qualifier type is raw). A special erroneous symbol is returned
3703      * if the lookup returns the constructor of an inner class and there's no
3704      * enclosing instance in scope.
3705      */
3706     class ConstructorReferenceLookupHelper extends ReferenceLookupHelper {
3707 
3708         boolean needsInference;
3709 
3710         ConstructorReferenceLookupHelper(JCMemberReference referenceTree, Type site, List<Type> argtypes,
3711                 List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
3712             super(referenceTree, names.init, site, argtypes, typeargtypes, maxPhase);
3713             if (site.isRaw()) {
3714                 this.site = new ClassType(site.getEnclosingType(), site.tsym.type.getTypeArguments(), site.tsym, site.getMetadata(), site.getFlavor());
3715                 needsInference = true;
3716             }
3717         }
3718 
3719         @Override
3720         protected Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
3721             Symbol sym = needsInference ?
3722                 findDiamond(env, site, argtypes, typeargtypes, phase.isBoxingRequired(), phase.isVarargsRequired()) :
3723                 findMethod(env, site, name, argtypes, typeargtypes,
3724                         phase.isBoxingRequired(), phase.isVarargsRequired());
3725             return enclosingInstanceMissing(env, site) ? new BadConstructorReferenceError(sym) : sym;
3726         }
3727 
3728         @Override
3729         ReferenceKind referenceKind(Symbol sym) {
3730             return site.getEnclosingType().hasTag(NONE) ?
3731                     ReferenceKind.TOPLEVEL : ReferenceKind.IMPLICIT_INNER;
3732         }
3733     }
3734 

3784             if (isStatic(env1)) staticOnly = true;
3785             if (env1.enclClass.sym == c) {
3786                 Symbol sym = env1.info.scope.findFirst(name);
3787                 if (sym != null) {
3788                     if (staticOnly) sym = new StaticError(sym);
3789                     return accessBase(sym, pos, env.enclClass.sym.type,
3790                                   name, true);
3791                 }
3792             }
3793             if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
3794             env1 = env1.outer;
3795         }
3796         if (c.isInterface() &&
3797             name == names._super && !isStatic(env) &&
3798             types.isDirectSuperInterface(c, env.enclClass.sym)) {
3799             //this might be a default super call if one of the superinterfaces is 'c'
3800             for (Type t : pruneInterfaces(env.enclClass.type)) {
3801                 if (t.tsym == c) {
3802                     env.info.defaultSuperCallSite = t;
3803                     return new VarSymbol(0, names._super,
3804                             types.asSuper(env.enclClass.type.referenceProjectionOrSelf(), c), env.enclClass.sym);
3805                 }
3806             }
3807             //find a direct super type that is a subtype of 'c'
3808             for (Type i : types.directSupertypes(env.enclClass.type)) {
3809                 if (i.tsym.isSubClass(c, types) && i.tsym != c) {
3810                     log.error(pos,
3811                               Errors.IllegalDefaultSuperCall(c,
3812                                                              Fragments.RedundantSupertype(c, i)));
3813                     return syms.errSymbol;
3814                 }
3815             }
3816             Assert.error();
3817         }
3818         log.error(pos, Errors.NotEnclClass(c));
3819         return syms.errSymbol;
3820     }
3821     //where
3822     private List<Type> pruneInterfaces(Type t) {
3823         ListBuffer<Type> result = new ListBuffer<>();
3824         for (Type t1 : types.interfaces(t)) {
< prev index next >