472 return true; |
472 return true; |
473 } catch (InapplicableMethodException ex) { |
473 } catch (InapplicableMethodException ex) { |
474 return false; |
474 return false; |
475 } |
475 } |
476 } |
476 } |
|
477 /** |
|
478 * A check handler is used by the main method applicability routine in order |
|
479 * to handle specific method applicability failures. It is assumed that a class |
|
480 * implementing this interface should throw exceptions that are a subtype of |
|
481 * InapplicableMethodException (see below). Such exception will terminate the |
|
482 * method applicability check and propagate important info outwards (for the |
|
483 * purpose of generating better diagnostics). |
|
484 */ |
|
485 interface MethodCheckHandler { |
|
486 /* The number of actuals and formals differ */ |
|
487 InapplicableMethodException arityMismatch(); |
|
488 /* An actual argument type does not conform to the corresponding formal type */ |
|
489 InapplicableMethodException argumentMismatch(boolean varargs, Type found, Type expected); |
|
490 /* The element type of a varargs is not accessible in the current context */ |
|
491 InapplicableMethodException inaccessibleVarargs(Symbol location, Type expected); |
|
492 } |
|
493 |
|
494 /** |
|
495 * Basic method check handler used within Resolve - all methods end up |
|
496 * throwing InapplicableMethodException; a diagnostic fragment that describes |
|
497 * the cause as to why the method is not applicable is set on the exception |
|
498 * before it is thrown. |
|
499 */ |
|
500 MethodCheckHandler resolveHandler = new MethodCheckHandler() { |
|
501 public InapplicableMethodException arityMismatch() { |
|
502 return inapplicableMethodException.setMessage("arg.length.mismatch"); |
|
503 } |
|
504 public InapplicableMethodException argumentMismatch(boolean varargs, Type found, Type expected) { |
|
505 String key = varargs ? |
|
506 "varargs.argument.mismatch" : |
|
507 "no.conforming.assignment.exists"; |
|
508 return inapplicableMethodException.setMessage(key, |
|
509 found, expected); |
|
510 } |
|
511 public InapplicableMethodException inaccessibleVarargs(Symbol location, Type expected) { |
|
512 return inapplicableMethodException.setMessage("inaccessible.varargs.type", |
|
513 expected, Kinds.kindName(location), location); |
|
514 } |
|
515 }; |
|
516 |
477 void checkRawArgumentsAcceptable(Env<AttrContext> env, |
517 void checkRawArgumentsAcceptable(Env<AttrContext> env, |
478 List<Type> argtypes, |
518 List<Type> argtypes, |
479 List<Type> formals, |
519 List<Type> formals, |
480 boolean allowBoxing, |
520 boolean allowBoxing, |
481 boolean useVarargs, |
521 boolean useVarargs, |
482 Warner warn) { |
522 Warner warn) { |
|
523 checkRawArgumentsAcceptable(env, List.<Type>nil(), argtypes, formals, |
|
524 allowBoxing, useVarargs, warn, resolveHandler); |
|
525 } |
|
526 |
|
527 /** |
|
528 * Main method applicability routine. Given a list of actual types A, |
|
529 * a list of formal types F, determines whether the types in A are |
|
530 * compatible (by method invocation conversion) with the types in F. |
|
531 * |
|
532 * Since this routine is shared between overload resolution and method |
|
533 * type-inference, it is crucial that actual types are converted to the |
|
534 * corresponding 'undet' form (i.e. where inference variables are replaced |
|
535 * with undetvars) so that constraints can be propagated and collected. |
|
536 * |
|
537 * Moreover, if one or more types in A is a poly type, this routine calls |
|
538 * Infer.instantiateArg in order to complete the poly type (this might involve |
|
539 * deferred attribution). |
|
540 * |
|
541 * A method check handler (see above) is used in order to report errors. |
|
542 */ |
|
543 List<Type> checkRawArgumentsAcceptable(Env<AttrContext> env, |
|
544 List<Type> undetvars, |
|
545 List<Type> argtypes, |
|
546 List<Type> formals, |
|
547 boolean allowBoxing, |
|
548 boolean useVarargs, |
|
549 Warner warn, |
|
550 MethodCheckHandler handler) { |
483 Type varargsFormal = useVarargs ? formals.last() : null; |
551 Type varargsFormal = useVarargs ? formals.last() : null; |
|
552 ListBuffer<Type> checkedArgs = ListBuffer.lb(); |
|
553 |
484 if (varargsFormal == null && |
554 if (varargsFormal == null && |
485 argtypes.size() != formals.size()) { |
555 argtypes.size() != formals.size()) { |
486 throw inapplicableMethodException.setMessage("arg.length.mismatch"); // not enough args |
556 throw handler.arityMismatch(); // not enough args |
487 } |
557 } |
488 |
558 |
489 while (argtypes.nonEmpty() && formals.head != varargsFormal) { |
559 while (argtypes.nonEmpty() && formals.head != varargsFormal) { |
490 boolean works = allowBoxing |
560 Type undetFormal = infer.asUndetType(formals.head, undetvars); |
491 ? types.isConvertible(argtypes.head, formals.head, warn) |
561 Type capturedActual = types.capture(argtypes.head); |
492 : types.isSubtypeUnchecked(argtypes.head, formals.head, warn); |
562 boolean works = allowBoxing ? |
493 if (!works) |
563 types.isConvertible(capturedActual, undetFormal, warn) : |
494 throw inapplicableMethodException.setMessage("no.conforming.assignment.exists", |
564 types.isSubtypeUnchecked(capturedActual, undetFormal, warn); |
495 argtypes.head, |
565 if (!works) { |
496 formals.head); |
566 throw handler.argumentMismatch(false, argtypes.head, formals.head); |
|
567 } |
|
568 checkedArgs.append(capturedActual); |
497 argtypes = argtypes.tail; |
569 argtypes = argtypes.tail; |
498 formals = formals.tail; |
570 formals = formals.tail; |
499 } |
571 } |
500 |
572 |
501 if (formals.head != varargsFormal) |
573 if (formals.head != varargsFormal) { |
502 throw inapplicableMethodException.setMessage("arg.length.mismatch"); // not enough args |
574 throw handler.arityMismatch(); // not enough args |
|
575 } |
503 |
576 |
504 if (useVarargs) { |
577 if (useVarargs) { |
|
578 //note: if applicability check is triggered by most specific test, |
|
579 //the last argument of a varargs is _not_ an array type (see JLS 15.12.2.5) |
505 Type elt = types.elemtype(varargsFormal); |
580 Type elt = types.elemtype(varargsFormal); |
|
581 Type eltUndet = infer.asUndetType(elt, undetvars); |
506 while (argtypes.nonEmpty()) { |
582 while (argtypes.nonEmpty()) { |
507 if (!types.isConvertible(argtypes.head, elt, warn)) |
583 Type capturedActual = types.capture(argtypes.head); |
508 throw inapplicableMethodException.setMessage("varargs.argument.mismatch", |
584 if (!types.isConvertible(capturedActual, eltUndet, warn)) { |
509 argtypes.head, |
585 throw handler.argumentMismatch(true, argtypes.head, elt); |
510 elt); |
586 } |
|
587 checkedArgs.append(capturedActual); |
511 argtypes = argtypes.tail; |
588 argtypes = argtypes.tail; |
512 } |
589 } |
513 //check varargs element type accessibility |
590 //check varargs element type accessibility |
514 if (!isAccessible(env, elt)) { |
591 if (undetvars.isEmpty() && !isAccessible(env, elt)) { |
515 Symbol location = env.enclClass.sym; |
592 Symbol location = env.enclClass.sym; |
516 throw inapplicableMethodException.setMessage("inaccessible.varargs.type", |
593 throw handler.inaccessibleVarargs(location, elt); |
517 elt, |
594 } |
518 Kinds.kindName(location), |
595 } |
519 location); |
596 return checkedArgs.toList(); |
520 } |
|
521 } |
|
522 return; |
|
523 } |
597 } |
524 // where |
598 // where |
525 public static class InapplicableMethodException extends RuntimeException { |
599 public static class InapplicableMethodException extends RuntimeException { |
526 private static final long serialVersionUID = 0; |
600 private static final long serialVersionUID = 0; |
527 |
601 |