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 valid.
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: buf.append(pname);
41: buf.append("' has too ");
42: buf.append(tooMany ? "many" : "few");
43: buf.append(" arguments (");
44: buf.append(argCount);
45: if (min == max) {
46: buf.append("; must be ");
47: buf.append(min);
48: } else {
49: buf.append("; min=");
50: buf.append(min);
51: if (max >= 0) {
52: buf.append(", max=");
53: buf.append(max);
54: }
55: }
56: buf.append(')');
57: return buf.toString();
58: }
59:
60: public String getMessage() {
61: if (proc != null) {
62: String msg = checkArgCount(proc, number);
63: if (msg != null)
64: return msg;
65: }
66: return super .getMessage();
67: }
68:
69: public WrongArguments(Procedure proc, int argCount) {
70: this .proc = proc;
71: number = argCount;
72: }
73:
74: public WrongArguments(java.lang.String name, int n,
75: java.lang.String u) {
76: procname = name;
77: number = n;
78: usage = u;
79: }
80: }
|