/interpreter/tags/at2dist220411/src/edu/vub/at/exceptions/XSymbiosisFailure.java
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 */ 28package edu.vub.at.exceptions; 29 30import edu.vub.at.eval.Evaluator; 31import edu.vub.at.objects.ATObject; 32import edu.vub.at.objects.ATTypeTag; 33import edu.vub.at.objects.coercion.NativeTypeTags; 34import edu.vub.at.objects.mirrors.Reflection; 35 36import java.lang.reflect.Constructor; 37import java.lang.reflect.Method; 38import java.util.Iterator; 39import java.util.LinkedList; 40 41/** 42 * An instance of this class is raised whenever a symbiotic method invocation fails due to overloading 43 * which could not be resolved given the actual arguments. Dedicated constructors are provided to 44 * deal with: 45 * 46 * <ul> 47 * <li>the case when no specific match could be identified,</li> 48 * <li>the case where multiple matches are possible based on the actual arguments,</li> 49 * <li>the case where the overloaded method is a constructor call.</li> 50 * </ul> 51 * 52 * Note that the symbiosis support only evaluates whether the actual arguments can match with 53 * the types specified in the method signature. If multiple possibilities remain, the closest 54 * possible match is not sought for, instead this exception is raised. 55 * 56 * @author tvcutsem 57 */ 58public class XSymbiosisFailure extends InterpreterException { 59 60 private static final long serialVersionUID = -4161446826939837849L; 61 62 private final String message_; 63 64 /** 65 * Reports that an overloaded method could not be resolved to a unique implementation 66 * because there are multiple matches. 67 * @param symbiont the Java object upon which the overloaded symbiotic invocation failed. 68 * @param selector the name of the invoked overloaded method 69 * @param choices a linked list of all applicable java.lang.Method objects 70 * @param atArgs the actual arguments to the overloaded invocation 71 */ 72 public XSymbiosisFailure(Object symbiont, String selector, LinkedList choices, ATObject[] atArgs) throws InterpreterException { 73 StringBuffer buff = new StringBuffer("Overloaded Java invocation has " + choices.size() + " matches:\n"); 74 if (symbiont == null) { 75 symbiont = ((Method) choices.getFirst()).getDeclaringClass().getName(); 76 } 77 buff.append(symbiont.toString() + "." + Reflection.downSelector(selector) + Evaluator.printElements(atArgs, "(",",",")").javaValue); 78 for (Iterator iter = choices.iterator(); iter.hasNext();) { 79 buff.append("\n" + iter.next().toString()); 80 } 81 message_ = buff.toString(); 82 } 83 84 /** 85 * Reports that an overloaded method could not be resolved to a unique implementation 86 * because there are no matches for any static types. 87 * @param symbiont the Java object upon which the overloaded symbiotic invocation failed. 88 * @param selector the name of the invoked overloaded method 89 * @param atArgs the actual arguments to the overloaded invocation 90 */ 91 public XSymbiosisFailure(Object symbiont, Method method, ATObject[] atArgs) throws InterpreterException { 92 StringBuffer buff = new StringBuffer("Overloaded Java invocation has no matches:\n"); 93 if (symbiont == null) { 94 symbiont = method.getDeclaringClass().getName(); 95 } 96 buff.append(symbiont + "." + Reflection.downSelector(method.getName()) + Evaluator.printElements(atArgs, "(",",",")").javaValue); 97 message_ = buff.toString(); 98 } 99 100 /** 101 * Reports that an overloaded method could not be resolved to a unique implementation 102 * because there are no matches on arity. 103 * @param symbiont the Java object upon which the overloaded symbiotic invocation failed. 104 * @param selector the name of the invoked overloaded method 105 * @param numArgs the number of actual arguments to the overloaded invocation 106 */ 107 public XSymbiosisFailure(Method method, int numArgs) throws InterpreterException { 108 message_ = "Wrong number of arguments supplied for " 109 + method.getName() + ", given: " + numArgs; 110 } 111 112 /** 113 * Reports that an overloaded constructor could not be resolved to a unique implementation. 114 * @param failedClass the Java class whose constructor could not be resolved. 115 * @param choices all applicable constructors (may contain null values, corresponding to non-applicable choices) 116 * @param numMatchingCtors the number of matching constructors (the number of non-null values in choices) 117 */ 118 public XSymbiosisFailure(Class failedClass, Constructor[] choices, ATObject[] atArgs, int numMatchingCtors) throws InterpreterException { 119 StringBuffer buff = new StringBuffer("Overloaded Java constructor has " + numMatchingCtors + " matches:\n"); 120 buff.append(Evaluator.getSimpleName(failedClass) + ".new"+ Evaluator.printElements(atArgs, "(",",",")").javaValue); 121 for (int i = 0; i < choices.length; i++) { 122 if (choices[i] != null) { 123 buff.append("\n" + choices[i].toString()); 124 } 125 } 126 message_ = buff.toString(); 127 } 128 129 public String getMessage() { 130 return message_; 131 } 132 133 public ATTypeTag getType() { 134 return NativeTypeTags._SYMBIOSISFAILURE_; 135 } 136 137}