KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > util > ParameterList


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.util;
20
21 import java.lang.reflect.*;
22 import java.util.*;
23
24 public class ParameterList extends LinkedList
25 {
26     public ParameterList()
27     {
28     }
29
30     public ParameterList(Class[] types)
31     {
32         init(types);
33     }
34
35     public ParameterList(Method template)
36     {
37         Class[] types = template.getParameterTypes();
38         init(types);
39     }
40
41     private void init(Class[] types)
42     {
43         int n = types.length;
44         for (int i = 0; i < n; i++)
45         {
46             Class type = types[i];
47             add(type, "p" + (i + 1));
48         }
49     }
50
51     public ParameterList add(String type, String name)
52     {
53         add(new MethodParameter(type, name));
54         return this;
55     }
56
57     public ParameterList add(Class type, String name)
58     {
59         return add(JavaType.getName(type), name);
60     }
61
62     public ParameterList add(String name)
63     {
64         return add("?", name);
65     }
66
67     public MethodParameter find(String parameter)
68     {
69         for (Iterator i = iterator(); i.hasNext();)
70         {
71             MethodParameter mp = (MethodParameter)i.next();
72             if (mp.name.equals(parameter))
73             {
74                 return mp;
75             }
76         }
77         return null;
78     }
79
80     public MethodParameter getParameter(int index)
81     {
82         return (MethodParameter)get(index);
83     }
84
85     public boolean hasSameTypes(ParameterList that)
86     {
87         if (this.size() != that.size())
88         {
89             return false;
90         }
91         Iterator i = this.iterator();
92         Iterator j = that.iterator();
93         for (; i.hasNext();)
94         {
95             MethodParameter mp1 = (MethodParameter)i.next();
96             MethodParameter mp2 = (MethodParameter)j.next();
97             if (! mp1.type.equals(mp2.type))
98             {
99                 return false;
100             }
101         }
102         return true;
103     }
104
105     public String toString()
106     {
107         StringBuffer sb = new StringBuffer("(");
108         int comma = 0;
109         for (Iterator i = iterator(); i.hasNext(); comma++)
110         {
111             MethodParameter mp = (MethodParameter)i.next();
112             if (comma > 0)
113             {
114                 sb.append(", ");
115             }
116             sb.append(mp.name);
117         }
118         sb.append(")");
119         return sb.toString();
120     }
121
122     public String toStringWithNoSpaces()
123     {
124         StringBuffer sb = new StringBuffer("(");
125         int comma = 0;
126         for (Iterator i = iterator(); i.hasNext(); comma++)
127         {
128             MethodParameter mp = (MethodParameter)i.next();
129             if (comma > 0)
130             {
131                 sb.append(",");
132             }
133             sb.append(mp.name);
134         }
135         sb.append(")");
136         return sb.toString();
137     }
138
139     public String toStringWithTypesOnly()
140     {
141         StringBuffer sb = new StringBuffer("(");
142         int comma = 0;
143         for (Iterator i = iterator(); i.hasNext(); comma++)
144         {
145             MethodParameter mp = (MethodParameter)i.next();
146             if (comma > 0)
147             {
148                 sb.append(", ");
149             }
150             sb.append(mp.type);
151         }
152         sb.append(")");
153         return sb.toString();
154     }
155
156     public String toStringWithTypes()
157     {
158         StringBuffer sb = new StringBuffer("(");
159         int comma = 0;
160         for (Iterator i = iterator(); i.hasNext(); comma++)
161         {
162             MethodParameter mp = (MethodParameter)i.next();
163             if (comma > 0)
164             {
165                 sb.append(", ");
166             }
167             sb.append(mp.type);
168             sb.append(' ');
169             sb.append(mp.name);
170         }
171         sb.append(")");
172         return sb.toString();
173     }
174
175     public String toStringWithFinalTypes()
176     {
177         StringBuffer sb = new StringBuffer("(");
178         int comma = 0;
179         for (Iterator i = iterator(); i.hasNext(); comma++)
180         {
181             MethodParameter mp = (MethodParameter)i.next();
182             if (comma > 0)
183             {
184                 sb.append(", ");
185             }
186             sb.append("final ");
187             sb.append(mp.type);
188             sb.append(' ');
189             sb.append(mp.name);
190         }
191         sb.append(")");
192         return sb.toString();
193     }
194
195     public String getClassArray()
196     {
197         if (isEmpty())
198         {
199             return "gcc.util.ArrayUtil.EMPTY_CLASS_ARRAY";
200         }
201         StringBuffer sb = new StringBuffer("new java.lang.Class[] {");
202         int comma = 0;
203         for (Iterator i = iterator(); i.hasNext(); comma++)
204         {
205             MethodParameter mp = (MethodParameter)i.next();
206             if (comma > 0)
207             {
208                 sb.append(", ");
209             }
210             sb.append(mp.type + ".class");
211         }
212         sb.append("}");
213         return sb.toString();
214     }
215
216     public String getObjectArray()
217     {
218         if (isEmpty())
219         {
220             return "gcc.util.ArrayUtil.EMPTY_OBJECT_ARRAY";
221         }
222         StringBuffer sb = new StringBuffer("new java.lang.Object[] {");
223         int comma = 0;
224         for (Iterator i = iterator(); i.hasNext(); comma++)
225         {
226             MethodParameter mp = (MethodParameter)i.next();
227             if (comma > 0)
228             {
229                 sb.append(", ");
230             }
231             sb.append(mp.getObject());
232         }
233         sb.append("}");
234         return sb.toString();
235     }
236
237     public String wrapAll()
238     {
239         StringBuffer sb = new StringBuffer();
240         int comma = 0;
241         for (Iterator i = iterator(); i.hasNext(); comma++)
242         {
243             MethodParameter mp = (MethodParameter)i.next();
244             if (comma > 0)
245             {
246                 sb.append(", ");
247             }
248             sb.append(mp.getObject());
249         }
250         return sb.toString();
251     }
252 }
253
Popular Tags