402 * as a member.
403 * @param sym The symbol.
404 */
405 public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym) {
406 return isAccessible(env, site, sym, false);
407 }
408 public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym, boolean checkInner) {
409 if (sym.name == names.init && sym.owner != site.tsym) return false;
410
411 /* 15.9.5.1: Note that it is possible for the signature of the anonymous constructor
412 to refer to an inaccessible type
413 */
414 if (env.enclMethod != null && (env.enclMethod.mods.flags & ANONCONSTR) != 0)
415 return true;
416
417 if (env.info.visitingServiceImplementation &&
418 env.toplevel.modle == sym.packge().modle) {
419 return true;
420 }
421
422 switch ((short)(sym.flags() & AccessFlags)) {
423 case PRIVATE:
424 return
425 (env.enclClass.sym == sym.owner // fast special case
426 ||
427 env.enclClass.sym.outermostClass() ==
428 sym.owner.outermostClass())
429 &&
430 sym.isInheritedIn(site.tsym, types);
431 case 0:
432 return
433 (env.toplevel.packge == sym.owner.owner // fast special case
434 ||
435 env.toplevel.packge == sym.packge())
436 &&
437 isAccessible(env, site, checkInner)
438 &&
439 sym.isInheritedIn(site.tsym, types)
440 &&
441 notOverriddenIn(site, sym);
442 case PROTECTED:
443 return
444 (env.toplevel.packge == sym.owner.owner // fast special case
445 ||
446 env.toplevel.packge == sym.packge()
447 ||
448 isProtectedAccessible(sym, env.enclClass.sym, site)
449 ||
450 // OK to select instance method or field from 'super' or type name
451 // (but type names should be disallowed elsewhere!)
452 env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
453 &&
454 isAccessible(env, site, checkInner)
455 &&
456 notOverriddenIn(site, sym);
457 default: // this case includes erroneous combinations as well
458 return isAccessible(env, site, checkInner) && notOverriddenIn(site, sym);
459 }
460 }
461 //where
462 /* `sym' is accessible only if not overridden by
463 * another symbol which is a member of `site'
464 * (because, if it is overridden, `sym' is not strictly
465 * speaking a member of `site'). A polymorphic signature method
466 * cannot be overridden (e.g. MH.invokeExact(Object[])).
467 */
468 private boolean notOverriddenIn(Type site, Symbol sym) {
469 if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
470 return true;
471 else {
472 Symbol s2 = ((MethodSymbol)sym).implementation(site.tsym, types, true);
473 return (s2 == null || s2 == sym || sym.owner == s2.owner || (sym.owner.isInterface() && s2.owner == syms.objectType.tsym) ||
474 !types.isSubSignature(types.memberType(site, s2), types.memberType(site, sym)));
475 }
476 }
477 //where
478 /** Is given protected symbol accessible if it is selected from given site
1502 Symbol sym = null;
1503 for (Symbol s : env1.info.scope.getSymbolsByName(name)) {
1504 if (s.kind == VAR && (s.flags_field & SYNTHETIC) == 0) {
1505 sym = s;
1506 if (staticOnly) {
1507 return new StaticError(sym);
1508 }
1509 break;
1510 }
1511 }
1512 if (isStatic(env1)) staticOnly = true;
1513 if (sym == null) {
1514 sym = findField(env1, env1.enclClass.sym.type, name, env1.enclClass.sym);
1515 }
1516 if (sym.exists()) {
1517 if (sym.kind == VAR &&
1518 sym.owner.kind == TYP &&
1519 (sym.flags() & STATIC) == 0) {
1520 if (staticOnly)
1521 return new StaticError(sym);
1522 if (env1.info.ctorPrologue && !isAllowedEarlyReference(pos, env1, (VarSymbol)sym))
1523 return new RefBeforeCtorCalledError(sym);
1524 }
1525 return sym;
1526 } else {
1527 bestSoFar = bestOf(bestSoFar, sym);
1528 }
1529
1530 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
1531 env1 = env1.outer;
1532 }
1533
1534 Symbol sym = findField(env, syms.predefClass.type, name, syms.predefClass);
1535 if (sym.exists())
1536 return sym;
1537 if (bestSoFar.exists())
1538 return bestSoFar;
1539
1540 Symbol origin = null;
1541 for (Scope sc : new Scope[] { env.toplevel.namedImportScope, env.toplevel.starImportScope }) {
1542 for (Symbol currentSymbol : sc.getSymbolsByName(name)) {
1543 if (currentSymbol.kind != VAR)
4284 List<Type> argtypes,
4285 List<Type> typeargtypes) {
4286 if (name == names.error)
4287 return null;
4288
4289 Pair<Symbol, JCDiagnostic> c = errCandidate();
4290 Symbol ws = c.fst.asMemberOf(site, types);
4291 UnaryOperator<JCDiagnostic> rewriter = compactMethodDiags ?
4292 d -> MethodResolutionDiagHelper.rewrite(diags, pos, log.currentSource(), dkind, c.snd) : null;
4293
4294 // If the problem is due to type arguments, then the method parameters aren't relevant,
4295 // so use the error message that omits them to avoid confusion.
4296 switch (c.snd.getCode()) {
4297 case "compiler.misc.wrong.number.type.args":
4298 case "compiler.misc.explicit.param.do.not.conform.to.bounds":
4299 return diags.create(dkind, log.currentSource(), pos,
4300 "cant.apply.symbol.noargs",
4301 rewriter,
4302 kindName(ws),
4303 ws.name == names.init ? ws.owner.name : ws.name,
4304 kindName(ws.owner),
4305 ws.owner.type,
4306 c.snd);
4307 default:
4308 // Avoid saying "constructor Array in class Array"
4309 if (ws.owner == syms.arrayClass && ws.name == names.init) {
4310 return diags.create(dkind, log.currentSource(), pos,
4311 "cant.apply.array.ctor",
4312 rewriter,
4313 methodArguments(ws.type.getParameterTypes()),
4314 methodArguments(argtypes),
4315 c.snd);
4316 }
4317 return diags.create(dkind, log.currentSource(), pos,
4318 "cant.apply.symbol",
4319 rewriter,
4320 kindName(ws),
4321 ws.name == names.init ? ws.owner.name : ws.name,
4322 methodArguments(ws.type.getParameterTypes()),
4323 methodArguments(argtypes),
4324 kindName(ws.owner),
5181 final MethodResolutionPhase step;
5182 final Symbol sym;
5183 final JCDiagnostic details;
5184 final Type mtype;
5185
5186 private Candidate(MethodResolutionPhase step, Symbol sym, JCDiagnostic details, Type mtype) {
5187 this.step = step;
5188 this.sym = sym;
5189 this.details = details;
5190 this.mtype = mtype;
5191 }
5192
5193 boolean isApplicable() {
5194 return mtype != null;
5195 }
5196 }
5197
5198 DeferredAttr.AttrMode attrMode() {
5199 return attrMode;
5200 }
5201
5202 boolean internal() {
5203 return internalResolution;
5204 }
5205 }
5206
5207 MethodResolutionContext currentResolutionContext = null;
5208 }
|
402 * as a member.
403 * @param sym The symbol.
404 */
405 public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym) {
406 return isAccessible(env, site, sym, false);
407 }
408 public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym, boolean checkInner) {
409 if (sym.name == names.init && sym.owner != site.tsym) return false;
410
411 /* 15.9.5.1: Note that it is possible for the signature of the anonymous constructor
412 to refer to an inaccessible type
413 */
414 if (env.enclMethod != null && (env.enclMethod.mods.flags & ANONCONSTR) != 0)
415 return true;
416
417 if (env.info.visitingServiceImplementation &&
418 env.toplevel.modle == sym.packge().modle) {
419 return true;
420 }
421
422 ClassSymbol enclosingCsym = env.enclClass.sym;
423 try {
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 sym.isInheritedIn(site.tsym, types);
433 case 0:
434 return
435 (env.toplevel.packge == sym.owner.owner // fast special case
436 ||
437 env.toplevel.packge == sym.packge())
438 &&
439 isAccessible(env, site, checkInner)
440 &&
441 sym.isInheritedIn(site.tsym, types)
442 &&
443 notOverriddenIn(site, sym);
444 case PROTECTED:
445 return
446 (env.toplevel.packge == sym.owner.owner // fast special case
447 ||
448 env.toplevel.packge == sym.packge()
449 ||
450 isProtectedAccessible(sym, env.enclClass.sym, site)
451 ||
452 // OK to select instance method or field from 'super' or type name
453 // (but type names should be disallowed elsewhere!)
454 env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
455 &&
456 isAccessible(env, site, checkInner)
457 &&
458 notOverriddenIn(site, sym);
459 default: // this case includes erroneous combinations as well
460 return isAccessible(env, site, checkInner) && notOverriddenIn(site, sym);
461 }
462 } finally {
463 env.enclClass.sym = enclosingCsym;
464 }
465 }
466 //where
467 /* `sym' is accessible only if not overridden by
468 * another symbol which is a member of `site'
469 * (because, if it is overridden, `sym' is not strictly
470 * speaking a member of `site'). A polymorphic signature method
471 * cannot be overridden (e.g. MH.invokeExact(Object[])).
472 */
473 private boolean notOverriddenIn(Type site, Symbol sym) {
474 if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
475 return true;
476 else {
477 Symbol s2 = ((MethodSymbol)sym).implementation(site.tsym, types, true);
478 return (s2 == null || s2 == sym || sym.owner == s2.owner || (sym.owner.isInterface() && s2.owner == syms.objectType.tsym) ||
479 !types.isSubSignature(types.memberType(site, s2), types.memberType(site, sym)));
480 }
481 }
482 //where
483 /** Is given protected symbol accessible if it is selected from given site
1507 Symbol sym = null;
1508 for (Symbol s : env1.info.scope.getSymbolsByName(name)) {
1509 if (s.kind == VAR && (s.flags_field & SYNTHETIC) == 0) {
1510 sym = s;
1511 if (staticOnly) {
1512 return new StaticError(sym);
1513 }
1514 break;
1515 }
1516 }
1517 if (isStatic(env1)) staticOnly = true;
1518 if (sym == null) {
1519 sym = findField(env1, env1.enclClass.sym.type, name, env1.enclClass.sym);
1520 }
1521 if (sym.exists()) {
1522 if (sym.kind == VAR &&
1523 sym.owner.kind == TYP &&
1524 (sym.flags() & STATIC) == 0) {
1525 if (staticOnly)
1526 return new StaticError(sym);
1527 if (env1.info.ctorPrologue && !isAllowedEarlyReference(pos, env1, (VarSymbol)sym)) {
1528 if (!env.tree.hasTag(ASSIGN) || !TreeInfo.isIdentOrThisDotIdent(((JCAssign)env.tree).lhs)) {
1529 return new RefBeforeCtorCalledError(sym);
1530 }
1531 }
1532 }
1533 return sym;
1534 } else {
1535 bestSoFar = bestOf(bestSoFar, sym);
1536 }
1537
1538 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
1539 env1 = env1.outer;
1540 }
1541
1542 Symbol sym = findField(env, syms.predefClass.type, name, syms.predefClass);
1543 if (sym.exists())
1544 return sym;
1545 if (bestSoFar.exists())
1546 return bestSoFar;
1547
1548 Symbol origin = null;
1549 for (Scope sc : new Scope[] { env.toplevel.namedImportScope, env.toplevel.starImportScope }) {
1550 for (Symbol currentSymbol : sc.getSymbolsByName(name)) {
1551 if (currentSymbol.kind != VAR)
4292 List<Type> argtypes,
4293 List<Type> typeargtypes) {
4294 if (name == names.error)
4295 return null;
4296
4297 Pair<Symbol, JCDiagnostic> c = errCandidate();
4298 Symbol ws = c.fst.asMemberOf(site, types);
4299 UnaryOperator<JCDiagnostic> rewriter = compactMethodDiags ?
4300 d -> MethodResolutionDiagHelper.rewrite(diags, pos, log.currentSource(), dkind, c.snd) : null;
4301
4302 // If the problem is due to type arguments, then the method parameters aren't relevant,
4303 // so use the error message that omits them to avoid confusion.
4304 switch (c.snd.getCode()) {
4305 case "compiler.misc.wrong.number.type.args":
4306 case "compiler.misc.explicit.param.do.not.conform.to.bounds":
4307 return diags.create(dkind, log.currentSource(), pos,
4308 "cant.apply.symbol.noargs",
4309 rewriter,
4310 kindName(ws),
4311 ws.name == names.init ? ws.owner.name : ws.name,
4312 ws.owner.type,
4313 c.snd);
4314 default:
4315 // Avoid saying "constructor Array in class Array"
4316 if (ws.owner == syms.arrayClass && ws.name == names.init) {
4317 return diags.create(dkind, log.currentSource(), pos,
4318 "cant.apply.array.ctor",
4319 rewriter,
4320 methodArguments(ws.type.getParameterTypes()),
4321 methodArguments(argtypes),
4322 c.snd);
4323 }
4324 return diags.create(dkind, log.currentSource(), pos,
4325 "cant.apply.symbol",
4326 rewriter,
4327 kindName(ws),
4328 ws.name == names.init ? ws.owner.name : ws.name,
4329 methodArguments(ws.type.getParameterTypes()),
4330 methodArguments(argtypes),
4331 kindName(ws.owner),
5188 final MethodResolutionPhase step;
5189 final Symbol sym;
5190 final JCDiagnostic details;
5191 final Type mtype;
5192
5193 private Candidate(MethodResolutionPhase step, Symbol sym, JCDiagnostic details, Type mtype) {
5194 this.step = step;
5195 this.sym = sym;
5196 this.details = details;
5197 this.mtype = mtype;
5198 }
5199
5200 boolean isApplicable() {
5201 return mtype != null;
5202 }
5203 }
5204
5205 DeferredAttr.AttrMode attrMode() {
5206 return attrMode;
5207 }
5208 }
5209
5210 MethodResolutionContext currentResolutionContext = null;
5211 }
|