01: // Copyright (c) 1999 Per M.A. Bothner.
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.mapping;
05:
06: public class WrongType extends WrappedException {
07: //-- number of the argument, 0-origin
08: // ARG_UNKNOWN mean unknown argument number
09: // ARG_VARNAME means not a call, procname is a variable name.
10: // ARG_DESCRIPTION means not a call, procname describes the target.
11: public int number;
12:
13: public static final int ARG_UNKNOWN = -1;
14: public static final int ARG_VARNAME = -2;
15: public static final int ARG_DESCRIPTION = -3;
16:
17: //-- type of the argument
18: public String typeExpected;
19: //-- Procedure name that threw the exception
20: public String procname;
21: public Procedure proc;
22:
23: public WrongType(String name, int n, String u) {
24: super (null, null);
25: procname = name;
26: number = n - 1;
27: typeExpected = u;
28: }
29:
30: public WrongType(Procedure proc, int n, ClassCastException ex) {
31: super (ex);
32: this .proc = proc;
33: this .procname = proc.getName();
34: this .number = n;
35: }
36:
37: public WrongType(String procname, int n, ClassCastException ex) {
38: super (ex);
39: this .procname = procname;
40: this .number = n;
41: }
42:
43: /** This interface is designed for a compact call sequence. */
44: public static WrongType make(ClassCastException ex, Procedure proc,
45: int n) {
46: return new WrongType(proc, n, ex);
47: }
48:
49: /** This interface is designed for a compact call sequence. */
50: public static WrongType make(ClassCastException ex,
51: String procname, int n) {
52: return new WrongType(procname, n, ex);
53: }
54:
55: public String getMessage() {
56: StringBuffer sbuf = new StringBuffer(100);
57: if (number == ARG_VARNAME) {
58: sbuf.append("Value for variable '");
59: sbuf.append(procname);
60: sbuf.append("' has wrong type");
61: } else if (number == ARG_DESCRIPTION) {
62: sbuf.append(procname);
63: sbuf.append(" has wrong type");
64: } else {
65: sbuf.append("Argument ");
66: if (number >= 0) {
67: sbuf.append('#');
68: sbuf.append(number);
69: }
70: sbuf.append(" to '");
71: sbuf.append(procname);
72: sbuf.append("' has wrong type");
73: }
74: Throwable ex = getCause();
75: if (ex != null) {
76: String msg = ex.getMessage();
77: if (msg != null) {
78: sbuf.append(" (");
79: sbuf.append(msg);
80: sbuf.append(')');
81: }
82: }
83: return sbuf.toString();
84: }
85: }
|