1 package gnu.mapping; 2 3 public class WrongArguments extends IllegalArgumentException { 4 public int number; 6 public String usage; 8 public String procname; 10 11 Procedure proc; 12 13 19 public static String checkArgCount (Procedure proc, int argCount) 20 { 21 int num = proc.numArgs(); 22 int min = num & 0xfff; 23 int max = num >> 12; 24 String 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 checkArgCount (String 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 buf = new StringBuffer (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 getMessage() 72 { 73 if (proc != null) 74 { 75 String 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 name,int n,java.lang.String u) { 89 procname = name; 90 number = n; 91 usage = u; 92 } 93 } 94 | Popular Tags |