< 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                 }

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

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

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

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

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

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

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

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