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)
4295 List<Type> argtypes,
4296 List<Type> typeargtypes) {
4297 if (name == names.error)
4298 return null;
4299
4300 Pair<Symbol, JCDiagnostic> c = errCandidate();
4301 Symbol ws = c.fst.asMemberOf(site, types);
4302 UnaryOperator<JCDiagnostic> rewriter = compactMethodDiags ?
4303 d -> MethodResolutionDiagHelper.rewrite(diags, pos, log.currentSource(), dkind, c.snd) : null;
4304
4305 // If the problem is due to type arguments, then the method parameters aren't relevant,
4306 // so use the error message that omits them to avoid confusion.
4307 switch (c.snd.getCode()) {
4308 case "compiler.misc.wrong.number.type.args":
4309 case "compiler.misc.explicit.param.do.not.conform.to.bounds":
4310 return diags.create(dkind, log.currentSource(), pos,
4311 "cant.apply.symbol.noargs",
4312 rewriter,
4313 kindName(ws),
4314 ws.name == names.init ? ws.owner.name : ws.name,
4315 kindName(ws.owner),
4316 ws.owner.type,
4317 c.snd);
4318 default:
4319 // Avoid saying "constructor Array in class Array"
4320 if (ws.owner == syms.arrayClass && ws.name == names.init) {
4321 return diags.create(dkind, log.currentSource(), pos,
4322 "cant.apply.array.ctor",
4323 rewriter,
4324 methodArguments(ws.type.getParameterTypes()),
4325 methodArguments(argtypes),
4326 c.snd);
4327 }
4328 return diags.create(dkind, log.currentSource(), pos,
4329 "cant.apply.symbol",
4330 rewriter,
4331 kindName(ws),
4332 ws.name == names.init ? ws.owner.name : ws.name,
4333 methodArguments(ws.type.getParameterTypes()),
4334 methodArguments(argtypes),
4335 kindName(ws.owner),
5192 final MethodResolutionPhase step;
5193 final Symbol sym;
5194 final JCDiagnostic details;
5195 final Type mtype;
5196
5197 private Candidate(MethodResolutionPhase step, Symbol sym, JCDiagnostic details, Type mtype) {
5198 this.step = step;
5199 this.sym = sym;
5200 this.details = details;
5201 this.mtype = mtype;
5202 }
5203
5204 boolean isApplicable() {
5205 return mtype != null;
5206 }
5207 }
5208
5209 DeferredAttr.AttrMode attrMode() {
5210 return attrMode;
5211 }
5212
5213 boolean internal() {
5214 return internalResolution;
5215 }
5216 }
5217
5218 MethodResolutionContext currentResolutionContext = null;
5219 }
|
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 ClassSymbol enclosingCsym = env.enclClass.sym;
425 try {
426 switch ((short)(sym.flags() & AccessFlags)) {
427 case PRIVATE:
428 return
429 (env.enclClass.sym == sym.owner // fast special case
430 ||
431 env.enclClass.sym.outermostClass() ==
432 sym.owner.outermostClass()
433 ||
434 privateMemberInPermitsClauseIfAllowed(env, sym))
435 &&
436 sym.isInheritedIn(site.tsym, types);
437 case 0:
438 return
439 (env.toplevel.packge == sym.owner.owner // fast special case
440 ||
441 env.toplevel.packge == sym.packge())
442 &&
443 isAccessible(env, site, checkInner)
444 &&
445 sym.isInheritedIn(site.tsym, types)
446 &&
447 notOverriddenIn(site, sym);
448 case PROTECTED:
449 return
450 (env.toplevel.packge == sym.owner.owner // fast special case
451 ||
452 env.toplevel.packge == sym.packge()
453 ||
454 isProtectedAccessible(sym, env.enclClass.sym, site)
455 ||
456 // OK to select instance method or field from 'super' or type name
457 // (but type names should be disallowed elsewhere!)
458 env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
459 &&
460 isAccessible(env, site, checkInner)
461 &&
462 notOverriddenIn(site, sym);
463 default: // this case includes erroneous combinations as well
464 return isAccessible(env, site, checkInner) && notOverriddenIn(site, sym);
465 }
466 } finally {
467 env.enclClass.sym = enclosingCsym;
468 }
469 }
470
471 private boolean privateMemberInPermitsClauseIfAllowed(Env<AttrContext> env, Symbol sym) {
472 return allowPrivateMembersInPermitsClause &&
473 env.info.isPermitsClause &&
474 ((JCClassDecl) env.tree).sym.outermostClass() == sym.owner.outermostClass();
475 }
476
477 //where
478 /* `sym' is accessible only if not overridden by
479 * another symbol which is a member of `site'
480 * (because, if it is overridden, `sym' is not strictly
481 * speaking a member of `site'). A polymorphic signature method
482 * cannot be overridden (e.g. MH.invokeExact(Object[])).
483 */
484 private boolean notOverriddenIn(Type site, Symbol sym) {
485 if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
486 return true;
487 else {
1518 Symbol sym = null;
1519 for (Symbol s : env1.info.scope.getSymbolsByName(name)) {
1520 if (s.kind == VAR && (s.flags_field & SYNTHETIC) == 0) {
1521 sym = s;
1522 if (staticOnly) {
1523 return new StaticError(sym);
1524 }
1525 break;
1526 }
1527 }
1528 if (isStatic(env1)) staticOnly = true;
1529 if (sym == null) {
1530 sym = findField(env1, env1.enclClass.sym.type, name, env1.enclClass.sym);
1531 }
1532 if (sym.exists()) {
1533 if (sym.kind == VAR &&
1534 sym.owner.kind == TYP &&
1535 (sym.flags() & STATIC) == 0) {
1536 if (staticOnly)
1537 return new StaticError(sym);
1538 if (env1.info.ctorPrologue && !isAllowedEarlyReference(pos, env1, (VarSymbol)sym)) {
1539 if (!env.tree.hasTag(ASSIGN) || !TreeInfo.isIdentOrThisDotIdent(((JCAssign)env.tree).lhs)) {
1540 return new RefBeforeCtorCalledError(sym);
1541 }
1542 }
1543 }
1544 return sym;
1545 } else {
1546 bestSoFar = bestOf(bestSoFar, sym);
1547 }
1548
1549 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
1550 env1 = env1.outer;
1551 }
1552
1553 Symbol sym = findField(env, syms.predefClass.type, name, syms.predefClass);
1554 if (sym.exists())
1555 return sym;
1556 if (bestSoFar.exists())
1557 return bestSoFar;
1558
1559 Symbol origin = null;
1560 for (Scope sc : new Scope[] { env.toplevel.namedImportScope, env.toplevel.starImportScope }) {
1561 for (Symbol currentSymbol : sc.getSymbolsByName(name)) {
1562 if (currentSymbol.kind != VAR)
4303 List<Type> argtypes,
4304 List<Type> typeargtypes) {
4305 if (name == names.error)
4306 return null;
4307
4308 Pair<Symbol, JCDiagnostic> c = errCandidate();
4309 Symbol ws = c.fst.asMemberOf(site, types);
4310 UnaryOperator<JCDiagnostic> rewriter = compactMethodDiags ?
4311 d -> MethodResolutionDiagHelper.rewrite(diags, pos, log.currentSource(), dkind, c.snd) : null;
4312
4313 // If the problem is due to type arguments, then the method parameters aren't relevant,
4314 // so use the error message that omits them to avoid confusion.
4315 switch (c.snd.getCode()) {
4316 case "compiler.misc.wrong.number.type.args":
4317 case "compiler.misc.explicit.param.do.not.conform.to.bounds":
4318 return diags.create(dkind, log.currentSource(), pos,
4319 "cant.apply.symbol.noargs",
4320 rewriter,
4321 kindName(ws),
4322 ws.name == names.init ? ws.owner.name : ws.name,
4323 ws.owner.type,
4324 c.snd);
4325 default:
4326 // Avoid saying "constructor Array in class Array"
4327 if (ws.owner == syms.arrayClass && ws.name == names.init) {
4328 return diags.create(dkind, log.currentSource(), pos,
4329 "cant.apply.array.ctor",
4330 rewriter,
4331 methodArguments(ws.type.getParameterTypes()),
4332 methodArguments(argtypes),
4333 c.snd);
4334 }
4335 return diags.create(dkind, log.currentSource(), pos,
4336 "cant.apply.symbol",
4337 rewriter,
4338 kindName(ws),
4339 ws.name == names.init ? ws.owner.name : ws.name,
4340 methodArguments(ws.type.getParameterTypes()),
4341 methodArguments(argtypes),
4342 kindName(ws.owner),
5199 final MethodResolutionPhase step;
5200 final Symbol sym;
5201 final JCDiagnostic details;
5202 final Type mtype;
5203
5204 private Candidate(MethodResolutionPhase step, Symbol sym, JCDiagnostic details, Type mtype) {
5205 this.step = step;
5206 this.sym = sym;
5207 this.details = details;
5208 this.mtype = mtype;
5209 }
5210
5211 boolean isApplicable() {
5212 return mtype != null;
5213 }
5214 }
5215
5216 DeferredAttr.AttrMode attrMode() {
5217 return attrMode;
5218 }
5219 }
5220
5221 MethodResolutionContext currentResolutionContext = null;
5222 }
|