langtools/src/share/classes/com/sun/tools/javac/comp/Resolve.java
changeset 12335 4725d88691dd
parent 12334 29e1bfdcba4e
child 12915 28cf1e0dafdc
equal deleted inserted replaced
12334:29e1bfdcba4e 12335:4725d88691dd
   397         if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
   397         if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
   398             return true;
   398             return true;
   399         else {
   399         else {
   400             Symbol s2 = ((MethodSymbol)sym).implementation(site.tsym, types, true);
   400             Symbol s2 = ((MethodSymbol)sym).implementation(site.tsym, types, true);
   401             return (s2 == null || s2 == sym || sym.owner == s2.owner ||
   401             return (s2 == null || s2 == sym || sym.owner == s2.owner ||
   402                     s2.isPolymorphicSignatureGeneric() ||
       
   403                     !types.isSubSignature(types.memberType(site, s2), types.memberType(site, sym)));
   402                     !types.isSubSignature(types.memberType(site, s2), types.memberType(site, sym)));
   404         }
   403         }
   405     }
   404     }
   406     //where
   405     //where
   407         /** Is given protected symbol accessible if it is selected from given site
   406         /** Is given protected symbol accessible if it is selected from given site
   447                         List<Type> typeargtypes,
   446                         List<Type> typeargtypes,
   448                         boolean allowBoxing,
   447                         boolean allowBoxing,
   449                         boolean useVarargs,
   448                         boolean useVarargs,
   450                         Warner warn)
   449                         Warner warn)
   451         throws Infer.InferenceException {
   450         throws Infer.InferenceException {
   452         boolean polymorphicSignature = m.isPolymorphicSignatureGeneric() && allowMethodHandles;
       
   453         if (useVarargs && (m.flags() & VARARGS) == 0)
   451         if (useVarargs && (m.flags() & VARARGS) == 0)
   454             throw inapplicableMethodException.setMessage();
   452             throw inapplicableMethodException.setMessage();
   455         Type mt = types.memberType(site, m);
   453         Type mt = types.memberType(site, m);
   456 
   454 
   457         // tvars is the list of formal type variables for which type arguments
   455         // tvars is the list of formal type variables for which type arguments
   488             tvars = tvars.appendList(tvars1);
   486             tvars = tvars.appendList(tvars1);
   489             mt = types.subst(pmt.qtype, pmt.tvars, tvars1);
   487             mt = types.subst(pmt.qtype, pmt.tvars, tvars1);
   490         }
   488         }
   491 
   489 
   492         // find out whether we need to go the slow route via infer
   490         // find out whether we need to go the slow route via infer
   493         boolean instNeeded = tvars.tail != null || /*inlined: tvars.nonEmpty()*/
   491         boolean instNeeded = tvars.tail != null; /*inlined: tvars.nonEmpty()*/
   494                 polymorphicSignature;
       
   495         for (List<Type> l = argtypes;
   492         for (List<Type> l = argtypes;
   496              l.tail != null/*inlined: l.nonEmpty()*/ && !instNeeded;
   493              l.tail != null/*inlined: l.nonEmpty()*/ && !instNeeded;
   497              l = l.tail) {
   494              l = l.tail) {
   498             if (l.head.tag == FORALL) instNeeded = true;
   495             if (l.head.tag == FORALL) instNeeded = true;
   499         }
   496         }
   500 
   497 
   501         if (instNeeded)
   498         if (instNeeded)
   502             return polymorphicSignature ?
   499             return infer.instantiateMethod(env,
   503                 infer.instantiatePolymorphicSignatureInstance(env, site, m.name, (MethodSymbol)m, argtypes) :
       
   504                 infer.instantiateMethod(env,
       
   505                                     tvars,
   500                                     tvars,
   506                                     (MethodType)mt,
   501                                     (MethodType)mt,
   507                                     m,
   502                                     m,
   508                                     argtypes,
   503                                     argtypes,
   509                                     allowBoxing,
   504                                     allowBoxing,
  1738                         env.info.varArgs = steps.head.isVarargsRequired(), false);
  1733                         env.info.varArgs = steps.head.isVarargsRequired(), false);
  1739                 currentResolutionContext.resolutionCache.put(steps.head, sym);
  1734                 currentResolutionContext.resolutionCache.put(steps.head, sym);
  1740                 steps = steps.tail;
  1735                 steps = steps.tail;
  1741             }
  1736             }
  1742             if (sym.kind >= AMBIGUOUS) {
  1737             if (sym.kind >= AMBIGUOUS) {
  1743                 if (site.tsym.isPolymorphicSignatureGeneric()) {
  1738                 //if nothing is found return the 'first' error
  1744                     //polymorphic receiver - synthesize new method symbol
  1739                 MethodResolutionPhase errPhase =
       
  1740                         currentResolutionContext.firstErroneousResolutionPhase();
       
  1741                 sym = access(currentResolutionContext.resolutionCache.get(errPhase),
       
  1742                         pos, location, site, name, true, argtypes, typeargtypes);
       
  1743                 env.info.varArgs = errPhase.isVarargsRequired;
       
  1744             } else if (allowMethodHandles) {
       
  1745                 MethodSymbol msym = (MethodSymbol)sym;
       
  1746                 if (msym.isSignaturePolymorphic(types)) {
  1745                     env.info.varArgs = false;
  1747                     env.info.varArgs = false;
  1746                     sym = findPolymorphicSignatureInstance(env,
  1748                     return findPolymorphicSignatureInstance(env, sym, argtypes);
  1747                             site, name, null, argtypes);
       
  1748                 }
  1749                 }
  1749                 else {
       
  1750                     //if nothing is found return the 'first' error
       
  1751                     MethodResolutionPhase errPhase =
       
  1752                             currentResolutionContext.firstErroneousResolutionPhase();
       
  1753                     sym = access(currentResolutionContext.resolutionCache.get(errPhase),
       
  1754                             pos, location, site, name, true, argtypes, typeargtypes);
       
  1755                     env.info.varArgs = errPhase.isVarargsRequired;
       
  1756                 }
       
  1757             } else if (allowMethodHandles && sym.isPolymorphicSignatureGeneric()) {
       
  1758                 //non-instantiated polymorphic signature - synthesize new method symbol
       
  1759                 env.info.varArgs = false;
       
  1760                 sym = findPolymorphicSignatureInstance(env,
       
  1761                         site, name, (MethodSymbol)sym, argtypes);
       
  1762             }
  1750             }
  1763             return sym;
  1751             return sym;
  1764         }
  1752         }
  1765         finally {
  1753         finally {
  1766             currentResolutionContext = prevResolutionContext;
  1754             currentResolutionContext = prevResolutionContext;
  1769 
  1757 
  1770     /** Find or create an implicit method of exactly the given type (after erasure).
  1758     /** Find or create an implicit method of exactly the given type (after erasure).
  1771      *  Searches in a side table, not the main scope of the site.
  1759      *  Searches in a side table, not the main scope of the site.
  1772      *  This emulates the lookup process required by JSR 292 in JVM.
  1760      *  This emulates the lookup process required by JSR 292 in JVM.
  1773      *  @param env       Attribution environment
  1761      *  @param env       Attribution environment
  1774      *  @param site      The original type from where the selection takes place.
  1762      *  @param spMethod  signature polymorphic method - i.e. MH.invokeExact
  1775      *  @param name      The method's name.
  1763      *  @param argtypes  The required argument types
  1776      *  @param spMethod  A template for the implicit method, or null.
  1764      */
  1777      *  @param argtypes  The required argument types.
  1765     Symbol findPolymorphicSignatureInstance(Env<AttrContext> env,
  1778      *  @param typeargtypes  The required type arguments.
  1766                                             Symbol spMethod,
  1779      */
       
  1780     Symbol findPolymorphicSignatureInstance(Env<AttrContext> env, Type site,
       
  1781                                             Name name,
       
  1782                                             MethodSymbol spMethod,  // sig. poly. method or null if none
       
  1783                                             List<Type> argtypes) {
  1767                                             List<Type> argtypes) {
  1784         Type mtype = infer.instantiatePolymorphicSignatureInstance(env,
  1768         Type mtype = infer.instantiatePolymorphicSignatureInstance(env,
  1785                 site, name, spMethod, argtypes);
  1769                 (MethodSymbol)spMethod, argtypes);
  1786         long flags = ABSTRACT | HYPOTHETICAL | POLYMORPHIC_SIGNATURE |
  1770         for (Symbol sym : polymorphicSignatureScope.getElementsByName(spMethod.name)) {
  1787                     (spMethod != null ?
  1771             if (types.isSameType(mtype, sym.type)) {
  1788                         spMethod.flags() & Flags.AccessFlags :
  1772                return sym;
  1789                         Flags.PUBLIC | Flags.STATIC);
  1773             }
  1790         Symbol m = null;
  1774         }
  1791         for (Scope.Entry e = polymorphicSignatureScope.lookup(name);
  1775 
  1792              e.scope != null;
  1776         // create the desired method
  1793              e = e.next()) {
  1777         long flags = ABSTRACT | HYPOTHETICAL | spMethod.flags() & Flags.AccessFlags;
  1794             Symbol sym = e.sym;
  1778         Symbol msym = new MethodSymbol(flags, spMethod.name, mtype, spMethod.owner);
  1795             if (types.isSameType(mtype, sym.type) &&
  1779         polymorphicSignatureScope.enter(msym);
  1796                 (sym.flags() & Flags.STATIC) == (flags & Flags.STATIC) &&
  1780         return msym;
  1797                 types.isSameType(sym.owner.type, site)) {
       
  1798                m = sym;
       
  1799                break;
       
  1800             }
       
  1801         }
       
  1802         if (m == null) {
       
  1803             // create the desired method
       
  1804             m = new MethodSymbol(flags, name, mtype, site.tsym);
       
  1805             polymorphicSignatureScope.enter(m);
       
  1806         }
       
  1807         return m;
       
  1808     }
  1781     }
  1809 
  1782 
  1810     /** Resolve a qualified method identifier, throw a fatal error if not
  1783     /** Resolve a qualified method identifier, throw a fatal error if not
  1811      *  found.
  1784      *  found.
  1812      *  @param pos       The position to use for error reporting.
  1785      *  @param pos       The position to use for error reporting.