001: // Copyright (c) 1999, 2004 Per M.A. Bothner.
002: // This is free software; for terms and warranty disclaimer see ./COPYING.
003:
004: package gnu.mapping;
005:
006: import gnu.bytecode.Type;
007:
008: /** Exception thrown when a procedure parameter has the wrong type. */
009:
010: public class WrongType extends WrappedException {
011: /** Number of the argument, 1-origin.
012: * <br>
013: * Can be an integer >= 1, or one of the values <code>ARG_UNKNOWN</code>,
014: * <code>ARG_VARNAME</code>, or <code>ARG_DESCRIPTION</code>.
015: */
016: public int number;
017:
018: /** <code>number==ARG_UNKNOWN</code> means unknown argument number. */
019: public static final int ARG_UNKNOWN = -1;
020:
021: /** <code>number==ARG_VARNAME</code> means not a call,
022: * <code>procname</code> is a variable name.*/
023: public static final int ARG_VARNAME = -2;
024:
025: /** <code>number==ARG_DESCRIPTION</code> means not a call,
026: * <code>procname</code> describes the target. (deprecated/unused) */
027: public static final int ARG_DESCRIPTION = -3;
028:
029: /** <code>number==ARG_CAST</code> means a general cast. */
030: public static final int ARG_CAST = -4;
031:
032: /** Name of <code>Procedure</code> that threw the exception (if non-null). */
033: public String procname;
034:
035: /** The <code>Procedure</code> that threw the exception (if non-null). */
036: public Procedure proc;
037:
038: /** The actual argument that was bad. */
039: public Object argValue;
040:
041: /** The expected parameter type (a Type or TypeValue), or a string name/description. */
042: public Object expectedType;
043:
044: public WrongType(String name, int n, String u) {
045: super (null, null);
046: procname = name;
047: number = n;
048: expectedType = u;
049: }
050:
051: public WrongType(Procedure proc, int n, ClassCastException ex) {
052: super (ex);
053: this .proc = proc;
054: this .procname = proc.getName();
055: this .number = n;
056: }
057:
058: public WrongType(ClassCastException ex, Procedure proc, int n,
059: Object argValue) {
060: this (proc, n, ex);
061: this .argValue = argValue;
062: }
063:
064: public WrongType(Procedure proc, int n, Object argValue) {
065: this .proc = proc;
066: this .procname = proc.getName();
067: this .number = n;
068: this .argValue = argValue;
069: }
070:
071: public WrongType(Procedure proc, int n, Object argValue,
072: Type expectedType) {
073: this .proc = proc;
074: this .procname = proc.getName();
075: this .number = n;
076: this .argValue = argValue;
077: this .expectedType = expectedType;
078: }
079:
080: public WrongType(Procedure proc, int n, Object argValue,
081: String expectedType) {
082: this (proc.getName(), n, argValue, expectedType);
083: this .proc = proc;
084: }
085:
086: public WrongType(String procName, int n, Object argValue,
087: String expectedType) {
088: this .procname = procName;
089: this .number = n;
090: this .argValue = argValue;
091: this .expectedType = expectedType;
092: }
093:
094: public WrongType(String procname, int n, ClassCastException ex) {
095: super (ex);
096: this .procname = procname;
097: this .number = n;
098: }
099:
100: public WrongType(ClassCastException ex, String procname, int n,
101: Object argValue) {
102: this (procname, n, ex);
103: this .argValue = argValue;
104: }
105:
106: /** @deprecated */
107: public static WrongType make(ClassCastException ex, Procedure proc,
108: int n) {
109: return new WrongType(proc, n, ex);
110: }
111:
112: /** @deprecated */
113: public static WrongType make(ClassCastException ex,
114: String procname, int n) {
115: return new WrongType(procname, n, ex);
116: }
117:
118: /** This interface is designed for a compact call sequence. */
119: public static WrongType make(ClassCastException ex, Procedure proc,
120: int n, Object argValue) {
121: WrongType wex = new WrongType(proc, n, ex);
122: wex.argValue = argValue;
123: return wex;
124: }
125:
126: /** This interface is designed for a compact call sequence. */
127: public static WrongType make(ClassCastException ex,
128: String procname, int n, Object argValue) {
129: WrongType wex = new WrongType(procname, n, ex);
130: wex.argValue = argValue;
131: return wex;
132: }
133:
134: public String getMessage() {
135: StringBuffer sbuf = new StringBuffer(100);
136: if (number == ARG_DESCRIPTION) {
137: sbuf.append(procname);
138: } else if (number == ARG_CAST || number == ARG_VARNAME) {
139: sbuf.append("Value");
140: } else {
141: sbuf.append("Argument ");
142: if (number > 0) {
143: sbuf.append('#');
144: sbuf.append(number);
145: }
146: }
147: if (argValue != null) {
148: sbuf.append(" (");
149: String argString = argValue.toString();
150: if (argString.length() > 50) {
151: sbuf.append(argString.substring(0, 47));
152: sbuf.append("...");
153: } else
154: sbuf.append(argString);
155: sbuf.append(")");
156: }
157: if (procname != null && number != ARG_DESCRIPTION) {
158: sbuf.append(number == ARG_VARNAME ? " for variable '"
159: : " to '");
160: sbuf.append(procname);
161: sbuf.append("'");
162: }
163: sbuf.append(" has wrong type");
164: if (argValue != null) {
165: sbuf.append(" (");
166: sbuf.append(argValue.getClass().getName());
167: sbuf.append(")");
168: }
169: Object expectType = expectedType;
170: if (expectType == null && number > 0
171: && proc instanceof MethodProc)
172: expectType = ((MethodProc) proc)
173: .getParameterType(number - 1);
174: if (expectType != null && expectType != Type.pointer_type) {
175: sbuf.append(" (expected: ");
176: if (expectType instanceof Type)
177: expectType = ((Type) expectType).getName();
178: else if (expectType instanceof Class)
179: expectType = ((Class) expectType).getName();
180: sbuf.append(expectType);
181: sbuf.append(")");
182: }
183: Throwable ex = getCause();
184: if (ex != null) {
185: String msg = ex.getMessage();
186: if (msg != null) {
187: sbuf.append(" (");
188: sbuf.append(msg);
189: sbuf.append(')');
190: }
191: }
192: return sbuf.toString();
193: }
194: }
|