/interpreter/tags/at2dist041108/src/edu/vub/at/exceptions/XSymbiosisFailure.java

http://ambienttalk.googlecode.com/ · Java · 137 lines · 53 code · 12 blank · 72 comment · 8 complexity · 08dba512c98f0572c0a81c6042b4d8fa MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * XSymbiosisFailure.java created on 13-nov-2006 at 14:35:22
  4. * (c) Programming Technology Lab, 2006 - 2007
  5. * Authors: Tom Van Cutsem & Stijn Mostinckx
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use,
  11. * copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following
  14. * conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. package edu.vub.at.exceptions;
  29. import edu.vub.at.eval.Evaluator;
  30. import edu.vub.at.objects.ATObject;
  31. import edu.vub.at.objects.ATTypeTag;
  32. import edu.vub.at.objects.coercion.NativeTypeTags;
  33. import edu.vub.at.objects.mirrors.Reflection;
  34. import java.lang.reflect.Constructor;
  35. import java.lang.reflect.Method;
  36. import java.util.Iterator;
  37. import java.util.LinkedList;
  38. /**
  39. * An instance of this class is raised whenever a symbiotic method invocation fails due to overloading
  40. * which could not be resolved given the actual arguments. Dedicated constructors are provided to
  41. * deal with:
  42. *
  43. * <ul>
  44. * <li>the case when no specific match could be identified,</li>
  45. * <li>the case where multiple matches are possible based on the actual arguments,</li>
  46. * <li>the case where the overloaded method is a constructor call.</li>
  47. * </ul>
  48. *
  49. * Note that the symbiosis support only evaluates whether the actual arguments can match with
  50. * the types specified in the method signature. If multiple possibilities remain, the closest
  51. * possible match is not sought for, instead this exception is raised.
  52. *
  53. * @author tvcutsem
  54. */
  55. public class XSymbiosisFailure extends InterpreterException {
  56. private static final long serialVersionUID = -4161446826939837849L;
  57. private final String message_;
  58. /**
  59. * Reports that an overloaded method could not be resolved to a unique implementation
  60. * because there are multiple matches.
  61. * @param symbiont the Java object upon which the overloaded symbiotic invocation failed.
  62. * @param selector the name of the invoked overloaded method
  63. * @param choices a linked list of all applicable java.lang.Method objects
  64. * @param atArgs the actual arguments to the overloaded invocation
  65. */
  66. public XSymbiosisFailure(Object symbiont, String selector, LinkedList choices, ATObject[] atArgs) throws InterpreterException {
  67. StringBuffer buff = new StringBuffer("Overloaded Java invocation has " + choices.size() + " matches:\n");
  68. if (symbiont == null) {
  69. symbiont = ((Method) choices.getFirst()).getDeclaringClass().getName();
  70. }
  71. buff.append(symbiont.toString() + "." + Reflection.downSelector(selector) + Evaluator.printElements(atArgs, "(",",",")").javaValue);
  72. for (Iterator iter = choices.iterator(); iter.hasNext();) {
  73. buff.append("\n" + iter.next().toString());
  74. }
  75. message_ = buff.toString();
  76. }
  77. /**
  78. * Reports that an overloaded method could not be resolved to a unique implementation
  79. * because there are no matches for any static types.
  80. * @param symbiont the Java object upon which the overloaded symbiotic invocation failed.
  81. * @param selector the name of the invoked overloaded method
  82. * @param atArgs the actual arguments to the overloaded invocation
  83. */
  84. public XSymbiosisFailure(Object symbiont, Method method, ATObject[] atArgs) throws InterpreterException {
  85. StringBuffer buff = new StringBuffer("Overloaded Java invocation has no matches:\n");
  86. if (symbiont == null) {
  87. symbiont = method.getDeclaringClass().getName();
  88. }
  89. buff.append(symbiont + "." + Reflection.downSelector(method.getName()) + Evaluator.printElements(atArgs, "(",",",")").javaValue);
  90. message_ = buff.toString();
  91. }
  92. /**
  93. * Reports that an overloaded method could not be resolved to a unique implementation
  94. * because there are no matches on arity.
  95. * @param symbiont the Java object upon which the overloaded symbiotic invocation failed.
  96. * @param selector the name of the invoked overloaded method
  97. * @param numArgs the number of actual arguments to the overloaded invocation
  98. */
  99. public XSymbiosisFailure(Method method, int numArgs) throws InterpreterException {
  100. message_ = "Wrong number of arguments supplied for "
  101. + method.getName() + ", given: " + numArgs;
  102. }
  103. /**
  104. * Reports that an overloaded constructor could not be resolved to a unique implementation.
  105. * @param failedClass the Java class whose constructor could not be resolved.
  106. * @param choices all applicable constructors (may contain null values, corresponding to non-applicable choices)
  107. * @param numMatchingCtors the number of matching constructors (the number of non-null values in choices)
  108. */
  109. public XSymbiosisFailure(Class failedClass, Constructor[] choices, ATObject[] atArgs, int numMatchingCtors) throws InterpreterException {
  110. StringBuffer buff = new StringBuffer("Overloaded Java constructor has " + numMatchingCtors + " matches:\n");
  111. buff.append(Evaluator.getSimpleName(failedClass) + ".new"+ Evaluator.printElements(atArgs, "(",",",")").javaValue);
  112. for (int i = 0; i < choices.length; i++) {
  113. if (choices[i] != null) {
  114. buff.append("\n" + choices[i].toString());
  115. }
  116. }
  117. message_ = buff.toString();
  118. }
  119. public String getMessage() {
  120. return message_;
  121. }
  122. public ATTypeTag getType() {
  123. return NativeTypeTags._SYMBIOSISFAILURE_;
  124. }
  125. }