< prev index next >

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

Print this page

  98     Symtab syms;
  99     Attr attr;
 100     AttrRecover attrRecover;
 101     DeferredAttr deferredAttr;
 102     Check chk;
 103     Infer infer;
 104     Preview preview;
 105     ClassFinder finder;
 106     ModuleFinder moduleFinder;
 107     Types types;
 108     JCDiagnostic.Factory diags;
 109     public final boolean allowModules;
 110     public final boolean allowRecords;
 111     private final boolean compactMethodDiags;
 112     private final boolean allowLocalVariableTypeInference;
 113     private final boolean allowYieldStatement;
 114     private final boolean allowPrivateMembersInPermitsClause;
 115     final EnumSet<VerboseResolutionMode> verboseResolutionMode;
 116     final boolean dumpMethodReferenceSearchResults;
 117     final boolean dumpStacktraceOnError;

 118 
 119     WriteableScope polymorphicSignatureScope;
 120 
 121     @SuppressWarnings("this-escape")
 122     protected Resolve(Context context) {
 123         context.put(resolveKey, this);
 124         syms = Symtab.instance(context);
 125 
 126         varNotFound = new SymbolNotFoundError(ABSENT_VAR);
 127         methodNotFound = new SymbolNotFoundError(ABSENT_MTH);
 128         typeNotFound = new SymbolNotFoundError(ABSENT_TYP);
 129         referenceNotFound = ReferenceLookupResult.error(methodNotFound);
 130 
 131         names = Names.instance(context);
 132         log = Log.instance(context);
 133         attr = Attr.instance(context);
 134         attrRecover = AttrRecover.instance(context);
 135         deferredAttr = DeferredAttr.instance(context);
 136         chk = Check.instance(context);
 137         infer = Infer.instance(context);
 138         finder = ClassFinder.instance(context);
 139         moduleFinder = ModuleFinder.instance(context);
 140         types = Types.instance(context);
 141         diags = JCDiagnostic.Factory.instance(context);
 142         preview = Preview.instance(context);
 143         Source source = Source.instance(context);
 144         Options options = Options.instance(context);
 145         compactMethodDiags = options.isSet(Option.XDIAGS, "compact") ||
 146                 options.isUnset(Option.XDIAGS) && options.isUnset("rawDiagnostics");
 147         verboseResolutionMode = VerboseResolutionMode.getVerboseResolutionMode(options);
 148         Target target = Target.instance(context);
 149         allowLocalVariableTypeInference = Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source);
 150         allowYieldStatement = Feature.SWITCH_EXPRESSION.allowedInSource(source);
 151         allowPrivateMembersInPermitsClause = Feature.PRIVATE_MEMBERS_IN_PERMITS_CLAUSE.allowedInSource(source);
 152         polymorphicSignatureScope = WriteableScope.create(syms.noSymbol);
 153         allowModules = Feature.MODULES.allowedInSource(source);
 154         allowRecords = Feature.RECORDS.allowedInSource(source);
 155         dumpMethodReferenceSearchResults = options.isSet("debug.dumpMethodReferenceSearchResults");
 156         dumpStacktraceOnError = options.isSet("dev") || options.isSet(DOE);

 157     }
 158 
 159     /** error symbols, which are returned when resolution fails
 160      */
 161     private final SymbolNotFoundError varNotFound;
 162     private final SymbolNotFoundError methodNotFound;
 163     private final SymbolNotFoundError typeNotFound;
 164 
 165     /** empty reference lookup result */
 166     private final ReferenceLookupResult referenceNotFound;
 167 
 168     public static Resolve instance(Context context) {
 169         Resolve instance = context.get(resolveKey);
 170         if (instance == null)
 171             instance = new Resolve(context);
 172         return instance;
 173     }
 174 
 175     private static Symbol bestOf(Symbol s1,
 176                                  Symbol s2) {

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





 463         }
 464     }
 465 
 466     private boolean privateMemberInPermitsClauseIfAllowed(Env<AttrContext> env, Symbol sym) {
 467         return allowPrivateMembersInPermitsClause &&
 468             env.info.isPermitsClause &&
 469             ((JCClassDecl) env.tree).sym.outermostClass() == sym.owner.outermostClass();
 470     }
 471 
 472     //where
 473     /* `sym' is accessible only if not overridden by
 474      * another symbol which is a member of `site'
 475      * (because, if it is overridden, `sym' is not strictly
 476      * speaking a member of `site'). A polymorphic signature method
 477      * cannot be overridden (e.g. MH.invokeExact(Object[])).
 478      */
 479     private boolean notOverriddenIn(Type site, Symbol sym) {
 480         if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
 481             return true;
 482         else {

1513             Symbol sym = null;
1514             for (Symbol s : env1.info.scope.getSymbolsByName(name)) {
1515                 if (s.kind == VAR && (s.flags_field & SYNTHETIC) == 0) {
1516                     sym = s;
1517                     if (staticOnly) {
1518                         return new StaticError(sym);
1519                     }
1520                     break;
1521                 }
1522             }
1523             if (isStatic(env1)) staticOnly = true;
1524             if (sym == null) {
1525                 sym = findField(env1, env1.enclClass.sym.type, name, env1.enclClass.sym);
1526             }
1527             if (sym.exists()) {
1528                 if (sym.kind == VAR &&
1529                         sym.owner.kind == TYP &&
1530                         (sym.flags() & STATIC) == 0) {
1531                     if (staticOnly)
1532                         return new StaticError(sym);
1533                     if (env1.info.ctorPrologue && !isAllowedEarlyReference(pos, env1, (VarSymbol)sym))
1534                         return new RefBeforeCtorCalledError(sym);








1535                 }
1536                 return sym;
1537             } else {
1538                 bestSoFar = bestOf(bestSoFar, sym);
1539             }
1540 
1541             if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
1542             env1 = env1.outer;
1543         }
1544 
1545         Symbol sym = findField(env, syms.predefClass.type, name, syms.predefClass);
1546         if (sym.exists())
1547             return sym;
1548         if (bestSoFar.exists())
1549             return bestSoFar;
1550 
1551         Symbol origin = null;
1552         for (Scope sc : new Scope[] { env.toplevel.namedImportScope, env.toplevel.starImportScope }) {
1553             for (Symbol currentSymbol : sc.getSymbolsByName(name)) {
1554                 if (currentSymbol.kind != VAR)

4297                 List<Type> argtypes,
4298                 List<Type> typeargtypes) {
4299             if (name == names.error)
4300                 return null;
4301 
4302             Pair<Symbol, JCDiagnostic> c = errCandidate();
4303             Symbol ws = c.fst.asMemberOf(site, types);
4304             UnaryOperator<JCDiagnostic> rewriter = compactMethodDiags ?
4305               d -> MethodResolutionDiagHelper.rewrite(diags, pos, log.currentSource(), dkind, c.snd) : null;
4306 
4307             // If the problem is due to type arguments, then the method parameters aren't relevant,
4308             // so use the error message that omits them to avoid confusion.
4309             switch (c.snd.getCode()) {
4310                 case "compiler.misc.wrong.number.type.args":
4311                 case "compiler.misc.explicit.param.do.not.conform.to.bounds":
4312                     return diags.create(dkind, log.currentSource(), pos,
4313                               "cant.apply.symbol.noargs",
4314                               rewriter,
4315                               kindName(ws),
4316                               ws.name == names.init ? ws.owner.name : ws.name,
4317                               kindName(ws.owner),
4318                               ws.owner.type,
4319                               c.snd);
4320                 default:
4321                     // Avoid saying "constructor Array in class Array"
4322                     if (ws.owner == syms.arrayClass && ws.name == names.init) {
4323                         return diags.create(dkind, log.currentSource(), pos,
4324                                   "cant.apply.array.ctor",
4325                                   rewriter,
4326                                   methodArguments(ws.type.getParameterTypes()),
4327                                   methodArguments(argtypes),
4328                                   c.snd);
4329                     }
4330                     return diags.create(dkind, log.currentSource(), pos,
4331                               "cant.apply.symbol",
4332                               rewriter,
4333                               kindName(ws),
4334                               ws.name == names.init ? ws.owner.name : ws.name,
4335                               methodArguments(ws.type.getParameterTypes()),
4336                               methodArguments(argtypes),
4337                               kindName(ws.owner),

5198             final MethodResolutionPhase step;
5199             final Symbol sym;
5200             final JCDiagnostic details;
5201             final Type mtype;
5202 
5203             private Candidate(MethodResolutionPhase step, Symbol sym, JCDiagnostic details, Type mtype) {
5204                 this.step = step;
5205                 this.sym = sym;
5206                 this.details = details;
5207                 this.mtype = mtype;
5208             }
5209 
5210             boolean isApplicable() {
5211                 return mtype != null;
5212             }
5213         }
5214 
5215         DeferredAttr.AttrMode attrMode() {
5216             return attrMode;
5217         }
5218 
5219         boolean internal() {
5220             return internalResolution;
5221         }
5222     }
5223 
5224     MethodResolutionContext currentResolutionContext = null;
5225 }

  98     Symtab syms;
  99     Attr attr;
 100     AttrRecover attrRecover;
 101     DeferredAttr deferredAttr;
 102     Check chk;
 103     Infer infer;
 104     Preview preview;
 105     ClassFinder finder;
 106     ModuleFinder moduleFinder;
 107     Types types;
 108     JCDiagnostic.Factory diags;
 109     public final boolean allowModules;
 110     public final boolean allowRecords;
 111     private final boolean compactMethodDiags;
 112     private final boolean allowLocalVariableTypeInference;
 113     private final boolean allowYieldStatement;
 114     private final boolean allowPrivateMembersInPermitsClause;
 115     final EnumSet<VerboseResolutionMode> verboseResolutionMode;
 116     final boolean dumpMethodReferenceSearchResults;
 117     final boolean dumpStacktraceOnError;
 118     private final LocalProxyVarsGen localProxyVarsGen;
 119 
 120     WriteableScope polymorphicSignatureScope;
 121 
 122     @SuppressWarnings("this-escape")
 123     protected Resolve(Context context) {
 124         context.put(resolveKey, this);
 125         syms = Symtab.instance(context);
 126 
 127         varNotFound = new SymbolNotFoundError(ABSENT_VAR);
 128         methodNotFound = new SymbolNotFoundError(ABSENT_MTH);
 129         typeNotFound = new SymbolNotFoundError(ABSENT_TYP);
 130         referenceNotFound = ReferenceLookupResult.error(methodNotFound);
 131 
 132         names = Names.instance(context);
 133         log = Log.instance(context);
 134         attr = Attr.instance(context);
 135         attrRecover = AttrRecover.instance(context);
 136         deferredAttr = DeferredAttr.instance(context);
 137         chk = Check.instance(context);
 138         infer = Infer.instance(context);
 139         finder = ClassFinder.instance(context);
 140         moduleFinder = ModuleFinder.instance(context);
 141         types = Types.instance(context);
 142         diags = JCDiagnostic.Factory.instance(context);
 143         preview = Preview.instance(context);
 144         Source source = Source.instance(context);
 145         Options options = Options.instance(context);
 146         compactMethodDiags = options.isSet(Option.XDIAGS, "compact") ||
 147                 options.isUnset(Option.XDIAGS) && options.isUnset("rawDiagnostics");
 148         verboseResolutionMode = VerboseResolutionMode.getVerboseResolutionMode(options);
 149         Target target = Target.instance(context);
 150         allowLocalVariableTypeInference = Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source);
 151         allowYieldStatement = Feature.SWITCH_EXPRESSION.allowedInSource(source);
 152         allowPrivateMembersInPermitsClause = Feature.PRIVATE_MEMBERS_IN_PERMITS_CLAUSE.allowedInSource(source);
 153         polymorphicSignatureScope = WriteableScope.create(syms.noSymbol);
 154         allowModules = Feature.MODULES.allowedInSource(source);
 155         allowRecords = Feature.RECORDS.allowedInSource(source);
 156         dumpMethodReferenceSearchResults = options.isSet("debug.dumpMethodReferenceSearchResults");
 157         dumpStacktraceOnError = options.isSet("dev") || options.isSet(DOE);
 158         localProxyVarsGen = LocalProxyVarsGen.instance(context);
 159     }
 160 
 161     /** error symbols, which are returned when resolution fails
 162      */
 163     private final SymbolNotFoundError varNotFound;
 164     private final SymbolNotFoundError methodNotFound;
 165     private final SymbolNotFoundError typeNotFound;
 166 
 167     /** empty reference lookup result */
 168     private final ReferenceLookupResult referenceNotFound;
 169 
 170     public static Resolve instance(Context context) {
 171         Resolve instance = context.get(resolveKey);
 172         if (instance == null)
 173             instance = new Resolve(context);
 174         return instance;
 175     }
 176 
 177     private static Symbol bestOf(Symbol s1,
 178                                  Symbol s2) {

 406      *                as a member.
 407      *  @param sym    The symbol.
 408      */
 409     public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym) {
 410         return isAccessible(env, site, sym, false);
 411     }
 412     public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym, boolean checkInner) {
 413         if (sym.name == names.init && sym.owner != site.tsym) return false;
 414 
 415         /* 15.9.5.1: Note that it is possible for the signature of the anonymous constructor
 416            to refer to an inaccessible type
 417         */
 418         if (env.enclMethod != null && (env.enclMethod.mods.flags & ANONCONSTR) != 0)
 419             return true;
 420 
 421         if (env.info.visitingServiceImplementation &&
 422             env.toplevel.modle == sym.packge().modle) {
 423             return true;
 424         }
 425 
 426         ClassSymbol enclosingCsym = env.enclClass.sym;
 427         try {
 428             switch ((short)(sym.flags() & AccessFlags)) {
 429                 case PRIVATE:
 430                     return
 431                             (env.enclClass.sym == sym.owner // fast special case
 432                                     ||
 433                                     env.enclClass.sym.outermostClass() ==
 434                                     sym.owner.outermostClass()
 435                                     ||
 436                                     privateMemberInPermitsClauseIfAllowed(env, sym))
 437                                 &&
 438                                     sym.isInheritedIn(site.tsym, types);
 439                 case 0:
 440                     return
 441                             (env.toplevel.packge == sym.owner.owner // fast special case
 442                                     ||
 443                                     env.toplevel.packge == sym.packge())
 444                                     &&
 445                                     isAccessible(env, site, checkInner)
 446                                     &&
 447                                     sym.isInheritedIn(site.tsym, types)
 448                                     &&
 449                                     notOverriddenIn(site, sym);
 450                 case PROTECTED:
 451                     return
 452                             (env.toplevel.packge == sym.owner.owner // fast special case
 453                                     ||
 454                                     env.toplevel.packge == sym.packge()
 455                                     ||
 456                                     isProtectedAccessible(sym, env.enclClass.sym, site)
 457                                     ||
 458                                     // OK to select instance method or field from 'super' or type name
 459                                     // (but type names should be disallowed elsewhere!)
 460                                     env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
 461                                     &&
 462                                     isAccessible(env, site, checkInner)
 463                                     &&
 464                                     notOverriddenIn(site, sym);
 465                 default: // this case includes erroneous combinations as well
 466                     return isAccessible(env, site, checkInner) && notOverriddenIn(site, sym);
 467             }
 468         } finally {
 469             env.enclClass.sym = enclosingCsym;
 470         }
 471     }
 472 
 473     private boolean privateMemberInPermitsClauseIfAllowed(Env<AttrContext> env, Symbol sym) {
 474         return allowPrivateMembersInPermitsClause &&
 475             env.info.isPermitsClause &&
 476             ((JCClassDecl) env.tree).sym.outermostClass() == sym.owner.outermostClass();
 477     }
 478 
 479     //where
 480     /* `sym' is accessible only if not overridden by
 481      * another symbol which is a member of `site'
 482      * (because, if it is overridden, `sym' is not strictly
 483      * speaking a member of `site'). A polymorphic signature method
 484      * cannot be overridden (e.g. MH.invokeExact(Object[])).
 485      */
 486     private boolean notOverriddenIn(Type site, Symbol sym) {
 487         if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
 488             return true;
 489         else {

1520             Symbol sym = null;
1521             for (Symbol s : env1.info.scope.getSymbolsByName(name)) {
1522                 if (s.kind == VAR && (s.flags_field & SYNTHETIC) == 0) {
1523                     sym = s;
1524                     if (staticOnly) {
1525                         return new StaticError(sym);
1526                     }
1527                     break;
1528                 }
1529             }
1530             if (isStatic(env1)) staticOnly = true;
1531             if (sym == null) {
1532                 sym = findField(env1, env1.enclClass.sym.type, name, env1.enclClass.sym);
1533             }
1534             if (sym.exists()) {
1535                 if (sym.kind == VAR &&
1536                         sym.owner.kind == TYP &&
1537                         (sym.flags() & STATIC) == 0) {
1538                     if (staticOnly)
1539                         return new StaticError(sym);
1540                     if (env1.info.ctorPrologue && !isAllowedEarlyReference(pos, env1, (VarSymbol)sym)) {
1541                         if (!env.tree.hasTag(ASSIGN) || !TreeInfo.isIdentOrThisDotIdent(((JCAssign)env.tree).lhs)) {
1542                             if (!sym.isStrictInstance()) {
1543                                 return new RefBeforeCtorCalledError(sym);
1544                             } else {
1545                                 localProxyVarsGen.addStrictFieldReadInPrologue(env.enclMethod, sym);
1546                                 return sym;
1547                             }
1548                         }
1549                     }
1550                 }
1551                 return sym;
1552             } else {
1553                 bestSoFar = bestOf(bestSoFar, sym);
1554             }
1555 
1556             if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
1557             env1 = env1.outer;
1558         }
1559 
1560         Symbol sym = findField(env, syms.predefClass.type, name, syms.predefClass);
1561         if (sym.exists())
1562             return sym;
1563         if (bestSoFar.exists())
1564             return bestSoFar;
1565 
1566         Symbol origin = null;
1567         for (Scope sc : new Scope[] { env.toplevel.namedImportScope, env.toplevel.starImportScope }) {
1568             for (Symbol currentSymbol : sc.getSymbolsByName(name)) {
1569                 if (currentSymbol.kind != VAR)

4312                 List<Type> argtypes,
4313                 List<Type> typeargtypes) {
4314             if (name == names.error)
4315                 return null;
4316 
4317             Pair<Symbol, JCDiagnostic> c = errCandidate();
4318             Symbol ws = c.fst.asMemberOf(site, types);
4319             UnaryOperator<JCDiagnostic> rewriter = compactMethodDiags ?
4320               d -> MethodResolutionDiagHelper.rewrite(diags, pos, log.currentSource(), dkind, c.snd) : null;
4321 
4322             // If the problem is due to type arguments, then the method parameters aren't relevant,
4323             // so use the error message that omits them to avoid confusion.
4324             switch (c.snd.getCode()) {
4325                 case "compiler.misc.wrong.number.type.args":
4326                 case "compiler.misc.explicit.param.do.not.conform.to.bounds":
4327                     return diags.create(dkind, log.currentSource(), pos,
4328                               "cant.apply.symbol.noargs",
4329                               rewriter,
4330                               kindName(ws),
4331                               ws.name == names.init ? ws.owner.name : ws.name,

4332                               ws.owner.type,
4333                               c.snd);
4334                 default:
4335                     // Avoid saying "constructor Array in class Array"
4336                     if (ws.owner == syms.arrayClass && ws.name == names.init) {
4337                         return diags.create(dkind, log.currentSource(), pos,
4338                                   "cant.apply.array.ctor",
4339                                   rewriter,
4340                                   methodArguments(ws.type.getParameterTypes()),
4341                                   methodArguments(argtypes),
4342                                   c.snd);
4343                     }
4344                     return diags.create(dkind, log.currentSource(), pos,
4345                               "cant.apply.symbol",
4346                               rewriter,
4347                               kindName(ws),
4348                               ws.name == names.init ? ws.owner.name : ws.name,
4349                               methodArguments(ws.type.getParameterTypes()),
4350                               methodArguments(argtypes),
4351                               kindName(ws.owner),

5212             final MethodResolutionPhase step;
5213             final Symbol sym;
5214             final JCDiagnostic details;
5215             final Type mtype;
5216 
5217             private Candidate(MethodResolutionPhase step, Symbol sym, JCDiagnostic details, Type mtype) {
5218                 this.step = step;
5219                 this.sym = sym;
5220                 this.details = details;
5221                 this.mtype = mtype;
5222             }
5223 
5224             boolean isApplicable() {
5225                 return mtype != null;
5226             }
5227         }
5228 
5229         DeferredAttr.AttrMode attrMode() {
5230             return attrMode;
5231         }




5232     }
5233 
5234     MethodResolutionContext currentResolutionContext = null;
5235 }
< prev index next >