KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > mapping > WrongArguments


1 package gnu.mapping;
2
3 public class WrongArguments extends IllegalArgumentException JavaDoc {
4   //-- negative indicates that the right number of arguments was used
5
public int number;
6   //-- usage description for procedure
7
public String JavaDoc usage;
8   //-- Procedure name that threw the exception
9
public String JavaDoc 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 JavaDoc checkArgCount (Procedure proc, int argCount)
20   {
21     int num = proc.numArgs();
22     int min = num & 0xfff;
23     int max = num >> 12;
24     String JavaDoc pname = proc.getName();
25     if (pname == null)
26       pname = proc.getClass().getName();
27     return checkArgCount(pname, min, max, argCount);
28   }
29
30   public static String JavaDoc checkArgCount (String JavaDoc pname, int min, int max, int argCount)
31   {
32     boolean tooMany;
33     if (argCount < min)
34       tooMany = false;
35     else if (max >= 0 && argCount > max)
36       tooMany = true;
37     else
38       return null;
39     StringBuffer JavaDoc buf = new StringBuffer JavaDoc(100);
40     buf.append("call to ");
41     if (pname == null)
42       buf.append("unnamed procedure");
43     else
44       {
45         buf.append('\'');
46         buf.append(pname);
47         buf.append('\'');
48       }
49     buf.append(tooMany ? " has too many" : " has too few");
50     buf.append(" arguments (");
51     buf.append(argCount);
52     if (min == max)
53       {
54     buf.append("; must be ");
55     buf.append(min);
56       }
57     else
58       {
59     buf.append("; min=");
60     buf.append(min);
61     if (max >= 0)
62       {
63         buf.append(", max=");
64         buf.append(max);
65       }
66       }
67     buf.append(')');
68     return buf.toString();
69   }
70
71   public String JavaDoc getMessage()
72   {
73     if (proc != null)
74       {
75     String JavaDoc msg = checkArgCount(proc, number);
76     if (msg != null)
77       return msg;
78       }
79     return super.getMessage();
80   }
81
82   public WrongArguments(Procedure proc, int argCount)
83   {
84     this.proc = proc;
85     number = argCount;
86   }
87
88    public WrongArguments(java.lang.String JavaDoc name,int n,java.lang.String JavaDoc u) {
89       procname = name;
90       number = n;
91       usage = u;
92    }
93 }
94
Popular Tags