KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > mapping > WrongType


1 // Copyright (c) 1999, 2004 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.mapping;
5 import gnu.bytecode.Type;
6
7 /** Exception thrown when a procedure parameter has the wrong type. */
8
9 public class WrongType extends WrappedException
10 {
11   /** Number of the argument, 1-origin.
12    * <br>
13    * Can be an integer >= 1, or one of the values <code>ARG_UNKNOWN</code>,
14    * <code>ARG_VARNAME</code>, or <code>ARG_DESCRIPTION</code>.
15    */

16   public int number;
17
18   /** <code>number==ARG_UNKNOWN</code> means unknown argument number. */
19   public static final int ARG_UNKNOWN = -1;
20
21   /** <code>number==ARG_VARNAME</code> means not a call,
22    * <code>procname</code> is a variable name.*/

23   public static final int ARG_VARNAME = -2;
24
25   /** <code>number==ARG_DESCRIPTION</code> means not a call,
26    * <code>procname</code> describes the target. (deprecated/unused) */

27   public static final int ARG_DESCRIPTION = -3;
28
29   /** <code>number==ARG_CAST</code> means a general cast. */
30   public static final int ARG_CAST = -4;
31
32   /** Name of <code>Procedure</code> that threw the exception (if non-null). */
33   public String JavaDoc procname;
34
35   /** The <code>Procedure</code> that threw the exception (if non-null). */
36   public Procedure proc;
37
38   /** The actual argument that was bad. */
39   public Object JavaDoc argValue;
40
41   /** The expected parameter type (a Type or TypeValue), or a string name/description. */
42   public Object JavaDoc expectedType;
43
44   public WrongType(String JavaDoc name, int n, String JavaDoc 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 JavaDoc ex)
53   {
54     super(ex);
55     this.proc = proc;
56     this.procname = proc.getName();
57     this.number = n;
58   }
59
60   public WrongType(ClassCastException JavaDoc ex, Procedure proc, int n,
61                    Object JavaDoc argValue)
62   {
63     this(proc, n, ex);
64     this.argValue = argValue;
65   }
66
67   public WrongType(Procedure proc, int n, Object JavaDoc 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 JavaDoc 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 JavaDoc argValue, String JavaDoc expectedType)
85   {
86     this(proc.getName(), n, argValue, expectedType);
87     this.proc = proc;
88   }
89
90   public WrongType(String JavaDoc procName, int n, Object JavaDoc argValue, String JavaDoc expectedType)
91   {
92     this.procname = procName;
93     this.number = n;
94     this.argValue = argValue;
95     this.expectedType = expectedType;
96   }
97
98   public WrongType(String JavaDoc procname, int n, ClassCastException JavaDoc ex)
99   {
100     super(ex);
101     this.procname = procname;
102     this.number = n;
103   }
104  
105   public WrongType (ClassCastException JavaDoc ex, String JavaDoc procname, int n,
106                     Object JavaDoc argValue)
107   {
108     this(procname, n, ex);
109     this.argValue = argValue;
110   }
111
112   /** @deprecated */
113   public static WrongType make(ClassCastException JavaDoc ex, Procedure proc, int n)
114   {
115     return new WrongType(proc, n, ex);
116   }
117
118   /** @deprecated */
119   public static WrongType make(ClassCastException JavaDoc ex, String JavaDoc procname, int n)
120   {
121     return new WrongType(procname, n, ex);
122   }
123
124   /** This interface is designed for a compact call sequence. */
125   public static WrongType make(ClassCastException JavaDoc ex, Procedure proc, int n,
126                    Object JavaDoc argValue)
127   {
128     WrongType wex = new WrongType(proc, n, ex);
129     wex.argValue = argValue;
130     return wex;
131   }
132
133   /** This interface is designed for a compact call sequence. */
134   public static WrongType make(ClassCastException JavaDoc ex, String JavaDoc procname, int n,
135                    Object JavaDoc argValue)
136   {
137     WrongType wex = new WrongType(procname, n, ex);
138     wex.argValue = argValue;
139     return wex;
140   }
141
142   public String JavaDoc getMessage()
143   {
144     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(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 JavaDoc 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 JavaDoc 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 JavaDoc)
197       expectType = ((Class JavaDoc) expectType).getName();
198     sbuf.append(expectType);
199     sbuf.append(")");
200       }
201     Throwable JavaDoc ex = getCause();
202     if (ex != null)
203       {
204     String JavaDoc 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