KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > josql > internal > MethodFilter


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

15 package org.josql.internal;
16
17 import java.util.List JavaDoc;
18 import java.util.ArrayList JavaDoc;
19
20 import java.lang.reflect.Method JavaDoc;
21 import java.lang.reflect.Modifier JavaDoc;
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23
24 import com.gentlyweb.utils.GeneralFilter;
25 import com.gentlyweb.utils.FilterException;
26
27 public class MethodFilter
28 {
29
30     private Class JavaDoc c = null;
31     private List JavaDoc ps = null;
32     private String JavaDoc name = null;
33     private int type = -1;
34     private List JavaDoc modifiers = null;
35     
36     public MethodFilter ()
37     {
38
39     }
40
41     public MethodFilter (Class JavaDoc c)
42     {
43
44     this.c = c;
45
46     }
47
48     public void setClass (Class JavaDoc c)
49     {
50
51     this.c = c;
52
53     }
54     
55     public void addModifier (int m)
56     {
57     
58     if (this.modifiers == null)
59     {
60
61         this.modifiers = new ArrayList JavaDoc ();
62         
63     }
64     
65     this.modifiers.add (new Integer JavaDoc (m));
66     
67     }
68     
69     public List JavaDoc filter ()
70                     throws IllegalAccessException JavaDoc,
71                            InvocationTargetException JavaDoc,
72                            FilterException
73     {
74
75     Method JavaDoc[] ms = this.c.getMethods ();
76     
77     List JavaDoc res = new ArrayList JavaDoc ();
78     
79     if (ms.length == 0)
80     {
81
82         return res;
83         
84     }
85     
86     GeneralFilter gf = null;
87     
88     if (this.name != null)
89     {
90
91         gf = new GeneralFilter (Method JavaDoc.class);
92         gf.addField ("name",
93              name,
94              this.type);
95         
96     }
97     
98     for (int i = 0; i < ms.length; i++)
99     {
100
101         Method JavaDoc m = ms[i];
102         
103         if (gf != null)
104         {
105
106         if (!gf.accept (m))
107         {
108
109             continue;
110             
111         }
112         
113         }
114         
115         // Now check that it has the correct modifiers...
116
if (!this.hasModifiers (m))
117         {
118
119         continue;
120         
121         }
122         
123         // Now check the parm types.
124
if (!this.hasParameters (m))
125         {
126
127         continue;
128         
129         }
130         
131         res.add (m);
132         
133     }
134     
135     return res;
136     
137     }
138     
139     private boolean hasParameters (Method JavaDoc m)
140     {
141
142     if (this.ps == null)
143     {
144
145         return true;
146         
147     }
148     
149     // Allowing for widening of types here.
150
Class JavaDoc[] mpt = m.getParameterTypes ();
151     
152     if (mpt.length != this.ps.size ())
153     {
154
155         return false;
156
157     }
158         
159     for (int i = 0; i < mpt.length; i++)
160     {
161
162         Class JavaDoc c = mpt[i];
163         
164         Class JavaDoc pc = (Class JavaDoc) this.ps.get (i);
165         
166         if (pc == null)
167         {
168
169         // Skip this one because we can't compare...
170
continue;
171         
172         }
173         
174         if (!c.isAssignableFrom (pc))
175         {
176
177         return false;
178         
179         }
180         
181     }
182     
183     return true;
184     
185     }
186
187     private boolean hasModifiers (Method JavaDoc m)
188     {
189
190     if (this.modifiers != null)
191     {
192
193         int mmods = m.getModifiers ();
194         
195         for (int i = 0; i < this.modifiers.size (); i++)
196         {
197
198         int in = ((Integer JavaDoc) this.modifiers.get (i)).intValue ();
199         
200         if ((mmods & in) == 0)
201         {
202
203             return false;
204             
205         }
206         
207         }
208         
209     }
210     
211     return true;
212     
213     }
214     
215     public void setParameterTypes (List JavaDoc pt)
216     {
217
218     this.ps = pt;
219     
220     }
221     
222     public void setName (String JavaDoc n,
223              int type)
224     {
225     
226     this.name = n;
227     this.type = type;
228     
229     }
230     
231 }
232
Popular Tags