398 * as a member.
399 * @param sym The symbol.
400 */
401 public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym) {
402 return isAccessible(env, site, sym, false);
403 }
404 public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym, boolean checkInner) {
405 if (sym.name == names.init && sym.owner != site.tsym) return false;
406
407 /* 15.9.5.1: Note that it is possible for the signature of the anonymous constructor
408 to refer to an inaccessible type
409 */
410 if (env.enclMethod != null && (env.enclMethod.mods.flags & ANONCONSTR) != 0)
411 return true;
412
413 if (env.info.visitingServiceImplementation &&
414 env.toplevel.modle == sym.packge().modle) {
415 return true;
416 }
417
418 switch ((short)(sym.flags() & AccessFlags)) {
419 case PRIVATE:
420 return
421 (env.enclClass.sym == sym.owner // fast special case
422 ||
423 env.enclClass.sym.outermostClass() ==
424 sym.owner.outermostClass())
425 &&
426 sym.isInheritedIn(site.tsym, types);
427 case 0:
428 return
429 (env.toplevel.packge == sym.owner.owner // fast special case
430 ||
431 env.toplevel.packge == sym.packge())
432 &&
433 isAccessible(env, site, checkInner)
434 &&
435 sym.isInheritedIn(site.tsym, types)
436 &&
437 notOverriddenIn(site, sym);
438 case PROTECTED:
439 return
440 (env.toplevel.packge == sym.owner.owner // fast special case
441 ||
442 env.toplevel.packge == sym.packge()
443 ||
444 isProtectedAccessible(sym, env.enclClass.sym, site)
445 ||
446 // OK to select instance method or field from 'super' or type name
447 // (but type names should be disallowed elsewhere!)
448 env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
449 &&
450 isAccessible(env, site, checkInner)
451 &&
452 notOverriddenIn(site, sym);
453 default: // this case includes erroneous combinations as well
454 return isAccessible(env, site, checkInner) && notOverriddenIn(site, sym);
455 }
456 }
457 //where
458 /* `sym' is accessible only if not overridden by
459 * another symbol which is a member of `site'
460 * (because, if it is overridden, `sym' is not strictly
461 * speaking a member of `site'). A polymorphic signature method
462 * cannot be overridden (e.g. MH.invokeExact(Object[])).
463 */
464 private boolean notOverriddenIn(Type site, Symbol sym) {
465 if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
466 return true;
467 else {
468 Symbol s2 = ((MethodSymbol)sym).implementation(site.tsym, types, true);
469 return (s2 == null || s2 == sym || sym.owner == s2.owner || (sym.owner.isInterface() && s2.owner == syms.objectType.tsym) ||
470 !types.isSubSignature(types.memberType(site, s2), types.memberType(site, sym)));
471 }
472 }
473 //where
474 /** Is given protected symbol accessible if it is selected from given site
1491 Symbol sym = null;
1492 for (Symbol s : env1.info.scope.getSymbolsByName(name)) {
1493 if (s.kind == VAR && (s.flags_field & SYNTHETIC) == 0) {
1494 sym = s;
1495 if (staticOnly) {
1496 return new StaticError(sym);
1497 }
1498 break;
1499 }
1500 }
1501 if (isStatic(env1)) staticOnly = true;
1502 if (sym == null) {
1503 sym = findField(env1, env1.enclClass.sym.type, name, env1.enclClass.sym);
1504 }
1505 if (sym.exists()) {
1506 if (sym.kind == VAR &&
1507 sym.owner.kind == TYP &&
1508 (sym.flags() & STATIC) == 0) {
1509 if (staticOnly)
1510 return new StaticError(sym);
1511 if (env1.info.ctorPrologue && (sym.flags_field & SYNTHETIC) == 0)
1512 return new RefBeforeCtorCalledError(sym);
1513 }
1514 return sym;
1515 } else {
1516 bestSoFar = bestOf(bestSoFar, sym);
1517 }
1518
1519 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
1520 env1 = env1.outer;
1521 }
1522
1523 Symbol sym = findField(env, syms.predefClass.type, name, syms.predefClass);
1524 if (sym.exists())
1525 return sym;
1526 if (bestSoFar.exists())
1527 return bestSoFar;
1528
1529 Symbol origin = null;
1530 for (Scope sc : new Scope[] { env.toplevel.namedImportScope, env.toplevel.starImportScope }) {
1531 for (Symbol currentSymbol : sc.getSymbolsByName(name)) {
1532 if (currentSymbol.kind != VAR)
4190 List<Type> argtypes,
4191 List<Type> typeargtypes) {
4192 if (name == names.error)
4193 return null;
4194
4195 Pair<Symbol, JCDiagnostic> c = errCandidate();
4196 Symbol ws = c.fst.asMemberOf(site, types);
4197 UnaryOperator<JCDiagnostic> rewriter = compactMethodDiags ?
4198 d -> MethodResolutionDiagHelper.rewrite(diags, pos, log.currentSource(), dkind, c.snd) : null;
4199
4200 // If the problem is due to type arguments, then the method parameters aren't relevant,
4201 // so use the error message that omits them to avoid confusion.
4202 switch (c.snd.getCode()) {
4203 case "compiler.misc.wrong.number.type.args":
4204 case "compiler.misc.explicit.param.do.not.conform.to.bounds":
4205 return diags.create(dkind, log.currentSource(), pos,
4206 "cant.apply.symbol.noargs",
4207 rewriter,
4208 kindName(ws),
4209 ws.name == names.init ? ws.owner.name : ws.name,
4210 kindName(ws.owner),
4211 ws.owner.type,
4212 c.snd);
4213 default:
4214 // Avoid saying "constructor Array in class Array"
4215 if (ws.owner == syms.arrayClass && ws.name == names.init) {
4216 return diags.create(dkind, log.currentSource(), pos,
4217 "cant.apply.array.ctor",
4218 rewriter,
4219 methodArguments(ws.type.getParameterTypes()),
4220 methodArguments(argtypes),
4221 c.snd);
4222 }
4223 return diags.create(dkind, log.currentSource(), pos,
4224 "cant.apply.symbol",
4225 rewriter,
4226 kindName(ws),
4227 ws.name == names.init ? ws.owner.name : ws.name,
4228 methodArguments(ws.type.getParameterTypes()),
4229 methodArguments(argtypes),
4230 kindName(ws.owner),
5087 final MethodResolutionPhase step;
5088 final Symbol sym;
5089 final JCDiagnostic details;
5090 final Type mtype;
5091
5092 private Candidate(MethodResolutionPhase step, Symbol sym, JCDiagnostic details, Type mtype) {
5093 this.step = step;
5094 this.sym = sym;
5095 this.details = details;
5096 this.mtype = mtype;
5097 }
5098
5099 boolean isApplicable() {
5100 return mtype != null;
5101 }
5102 }
5103
5104 DeferredAttr.AttrMode attrMode() {
5105 return attrMode;
5106 }
5107
5108 boolean internal() {
5109 return internalResolution;
5110 }
5111 }
5112
5113 MethodResolutionContext currentResolutionContext = null;
5114 }
|
398 * as a member.
399 * @param sym The symbol.
400 */
401 public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym) {
402 return isAccessible(env, site, sym, false);
403 }
404 public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym, boolean checkInner) {
405 if (sym.name == names.init && sym.owner != site.tsym) return false;
406
407 /* 15.9.5.1: Note that it is possible for the signature of the anonymous constructor
408 to refer to an inaccessible type
409 */
410 if (env.enclMethod != null && (env.enclMethod.mods.flags & ANONCONSTR) != 0)
411 return true;
412
413 if (env.info.visitingServiceImplementation &&
414 env.toplevel.modle == sym.packge().modle) {
415 return true;
416 }
417
418 ClassSymbol enclosingCsym = env.enclClass.sym;
419 try {
420 switch ((short)(sym.flags() & AccessFlags)) {
421 case PRIVATE:
422 return
423 (env.enclClass.sym == sym.owner // fast special case
424 ||
425 env.enclClass.sym.outermostClass() ==
426 sym.owner.outermostClass())
427 &&
428 sym.isInheritedIn(site.tsym, types);
429 case 0:
430 return
431 (env.toplevel.packge == sym.owner.owner // fast special case
432 ||
433 env.toplevel.packge == sym.packge())
434 &&
435 isAccessible(env, site, checkInner)
436 &&
437 sym.isInheritedIn(site.tsym, types)
438 &&
439 notOverriddenIn(site, sym);
440 case PROTECTED:
441 return
442 (env.toplevel.packge == sym.owner.owner // fast special case
443 ||
444 env.toplevel.packge == sym.packge()
445 ||
446 isProtectedAccessible(sym, env.enclClass.sym, site)
447 ||
448 // OK to select instance method or field from 'super' or type name
449 // (but type names should be disallowed elsewhere!)
450 env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
451 &&
452 isAccessible(env, site, checkInner)
453 &&
454 notOverriddenIn(site, sym);
455 default: // this case includes erroneous combinations as well
456 return isAccessible(env, site, checkInner) && notOverriddenIn(site, sym);
457 }
458 } finally {
459 env.enclClass.sym = enclosingCsym;
460 }
461 }
462 //where
463 /* `sym' is accessible only if not overridden by
464 * another symbol which is a member of `site'
465 * (because, if it is overridden, `sym' is not strictly
466 * speaking a member of `site'). A polymorphic signature method
467 * cannot be overridden (e.g. MH.invokeExact(Object[])).
468 */
469 private boolean notOverriddenIn(Type site, Symbol sym) {
470 if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
471 return true;
472 else {
473 Symbol s2 = ((MethodSymbol)sym).implementation(site.tsym, types, true);
474 return (s2 == null || s2 == sym || sym.owner == s2.owner || (sym.owner.isInterface() && s2.owner == syms.objectType.tsym) ||
475 !types.isSubSignature(types.memberType(site, s2), types.memberType(site, sym)));
476 }
477 }
478 //where
479 /** Is given protected symbol accessible if it is selected from given site
1496 Symbol sym = null;
1497 for (Symbol s : env1.info.scope.getSymbolsByName(name)) {
1498 if (s.kind == VAR && (s.flags_field & SYNTHETIC) == 0) {
1499 sym = s;
1500 if (staticOnly) {
1501 return new StaticError(sym);
1502 }
1503 break;
1504 }
1505 }
1506 if (isStatic(env1)) staticOnly = true;
1507 if (sym == null) {
1508 sym = findField(env1, env1.enclClass.sym.type, name, env1.enclClass.sym);
1509 }
1510 if (sym.exists()) {
1511 if (sym.kind == VAR &&
1512 sym.owner.kind == TYP &&
1513 (sym.flags() & STATIC) == 0) {
1514 if (staticOnly)
1515 return new StaticError(sym);
1516 if (env1.info.ctorPrologue && (sym.flags_field & SYNTHETIC) == 0) {
1517 if (!env.tree.hasTag(ASSIGN) || !TreeInfo.isIdentOrThisDotIdent(((JCAssign)env.tree).lhs)) {
1518 return new RefBeforeCtorCalledError(sym);
1519 }
1520 }
1521 }
1522 return sym;
1523 } else {
1524 bestSoFar = bestOf(bestSoFar, sym);
1525 }
1526
1527 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
1528 env1 = env1.outer;
1529 }
1530
1531 Symbol sym = findField(env, syms.predefClass.type, name, syms.predefClass);
1532 if (sym.exists())
1533 return sym;
1534 if (bestSoFar.exists())
1535 return bestSoFar;
1536
1537 Symbol origin = null;
1538 for (Scope sc : new Scope[] { env.toplevel.namedImportScope, env.toplevel.starImportScope }) {
1539 for (Symbol currentSymbol : sc.getSymbolsByName(name)) {
1540 if (currentSymbol.kind != VAR)
4198 List<Type> argtypes,
4199 List<Type> typeargtypes) {
4200 if (name == names.error)
4201 return null;
4202
4203 Pair<Symbol, JCDiagnostic> c = errCandidate();
4204 Symbol ws = c.fst.asMemberOf(site, types);
4205 UnaryOperator<JCDiagnostic> rewriter = compactMethodDiags ?
4206 d -> MethodResolutionDiagHelper.rewrite(diags, pos, log.currentSource(), dkind, c.snd) : null;
4207
4208 // If the problem is due to type arguments, then the method parameters aren't relevant,
4209 // so use the error message that omits them to avoid confusion.
4210 switch (c.snd.getCode()) {
4211 case "compiler.misc.wrong.number.type.args":
4212 case "compiler.misc.explicit.param.do.not.conform.to.bounds":
4213 return diags.create(dkind, log.currentSource(), pos,
4214 "cant.apply.symbol.noargs",
4215 rewriter,
4216 kindName(ws),
4217 ws.name == names.init ? ws.owner.name : ws.name,
4218 ws.owner.type,
4219 c.snd);
4220 default:
4221 // Avoid saying "constructor Array in class Array"
4222 if (ws.owner == syms.arrayClass && ws.name == names.init) {
4223 return diags.create(dkind, log.currentSource(), pos,
4224 "cant.apply.array.ctor",
4225 rewriter,
4226 methodArguments(ws.type.getParameterTypes()),
4227 methodArguments(argtypes),
4228 c.snd);
4229 }
4230 return diags.create(dkind, log.currentSource(), pos,
4231 "cant.apply.symbol",
4232 rewriter,
4233 kindName(ws),
4234 ws.name == names.init ? ws.owner.name : ws.name,
4235 methodArguments(ws.type.getParameterTypes()),
4236 methodArguments(argtypes),
4237 kindName(ws.owner),
5094 final MethodResolutionPhase step;
5095 final Symbol sym;
5096 final JCDiagnostic details;
5097 final Type mtype;
5098
5099 private Candidate(MethodResolutionPhase step, Symbol sym, JCDiagnostic details, Type mtype) {
5100 this.step = step;
5101 this.sym = sym;
5102 this.details = details;
5103 this.mtype = mtype;
5104 }
5105
5106 boolean isApplicable() {
5107 return mtype != null;
5108 }
5109 }
5110
5111 DeferredAttr.AttrMode attrMode() {
5112 return attrMode;
5113 }
5114 }
5115
5116 MethodResolutionContext currentResolutionContext = null;
5117 }
|