1 4 package gnu.mapping; 5 import gnu.bytecode.Type; 6 7 8 9 public class WrongType extends WrappedException 10 { 11 16 public int number; 17 18 19 public static final int ARG_UNKNOWN = -1; 20 21 23 public static final int ARG_VARNAME = -2; 24 25 27 public static final int ARG_DESCRIPTION = -3; 28 29 30 public static final int ARG_CAST = -4; 31 32 33 public String procname; 34 35 36 public Procedure proc; 37 38 39 public Object argValue; 40 41 42 public Object expectedType; 43 44 public WrongType(String name, int n, String u) 45 { 46 super(null, null); 47 procname = name; 48 number = n; 49 expectedType = u; 50 } 51 52 public WrongType(Procedure proc, int n, ClassCastException ex) 53 { 54 super(ex); 55 this.proc = proc; 56 this.procname = proc.getName(); 57 this.number = n; 58 } 59 60 public WrongType(ClassCastException ex, Procedure proc, int n, 61 Object argValue) 62 { 63 this(proc, n, ex); 64 this.argValue = argValue; 65 } 66 67 public WrongType(Procedure proc, int n, Object argValue) 68 { 69 this.proc = proc; 70 this.procname = proc.getName(); 71 this.number = n; 72 this.argValue = argValue; 73 } 74 75 public WrongType(Procedure proc, int n, Object argValue, Type expectedType) 76 { 77 this.proc = proc; 78 this.procname = proc.getName(); 79 this.number = n; 80 this.argValue = argValue; 81 this.expectedType = expectedType; 82 } 83 84 public WrongType(Procedure proc, int n, Object argValue, String expectedType) 85 { 86 this(proc.getName(), n, argValue, expectedType); 87 this.proc = proc; 88 } 89 90 public WrongType(String procName, int n, Object argValue, String expectedType) 91 { 92 this.procname = procName; 93 this.number = n; 94 this.argValue = argValue; 95 this.expectedType = expectedType; 96 } 97 98 public WrongType(String procname, int n, ClassCastException ex) 99 { 100 super(ex); 101 this.procname = procname; 102 this.number = n; 103 } 104 105 public WrongType (ClassCastException ex, String procname, int n, 106 Object argValue) 107 { 108 this(procname, n, ex); 109 this.argValue = argValue; 110 } 111 112 113 public static WrongType make(ClassCastException ex, Procedure proc, int n) 114 { 115 return new WrongType(proc, n, ex); 116 } 117 118 119 public static WrongType make(ClassCastException ex, String procname, int n) 120 { 121 return new WrongType(procname, n, ex); 122 } 123 124 125 public static WrongType make(ClassCastException ex, Procedure proc, int n, 126 Object argValue) 127 { 128 WrongType wex = new WrongType(proc, n, ex); 129 wex.argValue = argValue; 130 return wex; 131 } 132 133 134 public static WrongType make(ClassCastException ex, String procname, int n, 135 Object argValue) 136 { 137 WrongType wex = new WrongType(procname, n, ex); 138 wex.argValue = argValue; 139 return wex; 140 } 141 142 public String getMessage() 143 { 144 StringBuffer sbuf = new StringBuffer (100); 145 if (number == ARG_DESCRIPTION) 146 { 147 sbuf.append(procname); 148 } 149 else if (number == ARG_CAST || number == ARG_VARNAME) 150 { 151 sbuf.append("Value"); 152 } 153 else 154 { 155 sbuf.append("Argument "); 156 if (number > 0) 157 { 158 sbuf.append('#'); 159 sbuf.append(number); 160 } 161 } 162 if (argValue != null) 163 { 164 sbuf.append(" ("); 165 String argString = argValue.toString(); 166 if (argString.length() > 50) 167 { 168 sbuf.append(argString.substring(0, 47)); 169 sbuf.append("..."); 170 } 171 else 172 sbuf.append(argString); 173 sbuf.append(")"); 174 } 175 if (procname != null && number != ARG_DESCRIPTION) 176 { 177 sbuf.append(number == ARG_VARNAME ? " for variable '" : " to '"); 178 sbuf.append(procname); 179 sbuf.append("'"); 180 } 181 sbuf.append(" has wrong type"); 182 if (argValue != null) 183 { 184 sbuf.append(" ("); 185 sbuf.append(argValue.getClass().getName()); 186 sbuf.append(")"); 187 } 188 Object expectType = expectedType; 189 if (expectType == null && number > 0 && proc instanceof MethodProc) 190 expectType = ((MethodProc) proc).getParameterType(number-1); 191 if (expectType != null && expectType != Type.pointer_type) 192 { 193 sbuf.append(" (expected: "); 194 if (expectType instanceof Type) 195 expectType = ((Type) expectType).getName(); 196 else if (expectType instanceof Class ) 197 expectType = ((Class ) expectType).getName(); 198 sbuf.append(expectType); 199 sbuf.append(")"); 200 } 201 Throwable ex = getCause(); 202 if (ex != null) 203 { 204 String msg = ex.getMessage(); 205 if (msg != null) 206 { 207 sbuf.append(" ("); 208 sbuf.append(msg); 209 sbuf.append(')'); 210 } 211 } 212 return sbuf.toString(); 213 } 214 } 215 | Popular Tags |