01: package gnu.mapping;
02:
03: public class WrongArguments extends IllegalArgumentException {
04: //-- negative indicates that the right number of arguments was used
05: public int number;
06: //-- usage description for procedure
07: public String usage;
08: //-- Procedure name that threw the exception
09: public String procname;
10:
11: Procedure proc;
12:
13: /** Returns an error message if the number of arguments in a call is invalid.
14: * @param proc the Procedure being called
15: * @param argCount the number of arguments in the call
16: * @return null, if the number of arguments is ok;
17: * otherwise a suitable error message
18: */
19: public static String checkArgCount(Procedure proc, int argCount) {
20: int num = proc.numArgs();
21: int min = num & 0xfff;
22: int max = num >> 12;
23: String pname = proc.getName();
24: if (pname == null)
25: pname = proc.getClass().getName();
26: return checkArgCount(pname, min, max, argCount);
27: }
28:
29: public static String checkArgCount(String pname, int min, int max,
30: int argCount) {
31: boolean tooMany;
32: if (argCount < min)
33: tooMany = false;
34: else if (max >= 0 && argCount > max)
35: tooMany = true;
36: else
37: return null;
38: StringBuffer buf = new StringBuffer(100);
39: buf.append("call to ");
40: if (pname == null)
41: buf.append("unnamed procedure");
42: else {
43: buf.append('\'');
44: buf.append(pname);
45: buf.append('\'');
46: }
47: buf.append(tooMany ? " has too many" : " has too few");
48: buf.append(" arguments (");
49: buf.append(argCount);
50: if (min == max) {
51: buf.append("; must be ");
52: buf.append(min);
53: } else {
54: buf.append("; min=");
55: buf.append(min);
56: if (max >= 0) {
57: buf.append(", max=");
58: buf.append(max);
59: }
60: }
61: buf.append(')');
62: return buf.toString();
63: }
64:
65: public String getMessage() {
66: if (proc != null) {
67: String msg = checkArgCount(proc, number);
68: if (msg != null)
69: return msg;
70: }
71: return super .getMessage();
72: }
73:
74: public WrongArguments(Procedure proc, int argCount) {
75: this .proc = proc;
76: number = argCount;
77: }
78:
79: public WrongArguments(java.lang.String name, int n,
80: java.lang.String u) {
81: procname = name;
82: number = n;
83: usage = u;
84: }
85: }
|