PageRenderTime 19ms CodeModel.GetById 12ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://ambienttalk.googlecode.com/
Java | 134 lines | 50 code | 12 blank | 72 comment | 6 complexity | 867feae59bf5e1f82c47a9c564abed13 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		buff.append(symbiont.toString() + "." + Reflection.downSelector(selector) + Evaluator.printElements(atArgs, "(",",",")").javaValue);
 75		for (Iterator iter = choices.iterator(); iter.hasNext();) {
 76			buff.append("\n" + iter.next().toString());
 77		}
 78		message_ = buff.toString();
 79	}
 80	
 81	/**
 82	 * Reports that an overloaded method could not be resolved to a unique implementation
 83	 * because there are no matches for any static types.
 84	 * @param symbiont the Java object upon which the overloaded symbiotic invocation failed.
 85	 * @param selector the name of the invoked overloaded method
 86	 * @param atArgs the actual arguments to the overloaded invocation
 87	 */
 88	public XSymbiosisFailure(Object symbiont, Method method, ATObject[] atArgs) throws InterpreterException {
 89		StringBuffer buff = new StringBuffer("Overloaded Java invocation has no matches:\n");
 90		if (symbiont == null) {
 91			symbiont = method.getDeclaringClass().getName();
 92		}
 93		buff.append(symbiont + "." + Reflection.downSelector(method.getName()) + Evaluator.printElements(atArgs, "(",",",")").javaValue);
 94		message_ = buff.toString();
 95	}
 96	
 97	/**
 98	 * Reports that an overloaded method could not be resolved to a unique implementation
 99	 * because there are no matches on arity.
100	 * @param symbiont the Java object upon which the overloaded symbiotic invocation failed.
101	 * @param selector the name of the invoked overloaded method
102	 * @param numArgs the number of actual arguments to the overloaded invocation
103	 */
104	public XSymbiosisFailure(Method method, int numArgs) throws InterpreterException {
105		message_ = "Wrong number of arguments supplied for "
106			+ method.getName() + ", given: " + numArgs;
107	}
108	
109	/**
110	 * Reports that an overloaded constructor could not be resolved to a unique implementation.
111	 * @param failedClass the Java class whose constructor could not be resolved.
112	 * @param choices all applicable constructors (may contain null values, corresponding to non-applicable choices)
113	 * @param numMatchingCtors the number of matching constructors (the number of non-null values in choices)
114	 */
115	public XSymbiosisFailure(Class failedClass, Constructor[] choices, ATObject[] atArgs, int numMatchingCtors) throws InterpreterException {
116		StringBuffer buff = new StringBuffer("Overloaded Java constructor has " + numMatchingCtors + " matches:\n");
117		buff.append(Evaluator.getSimpleName(failedClass) + ".new"+ Evaluator.printElements(atArgs, "(",",",")").javaValue);
118		for (int i = 0; i < choices.length; i++) {
119			if (choices[i] != null) {
120				buff.append("\n" + choices[i].toString());
121			}
122		}
123		message_ = buff.toString();
124	}
125	
126	public String getMessage() {
127		return message_;
128	}
129	
130	public ATTypeTag getType() {
131		return NativeTypeTags._SYMBIOSISFAILURE_;
132	}
133
134}