1 15 package org.josql.internal; 16 17 import java.util.List ; 18 import java.util.ArrayList ; 19 20 import java.lang.reflect.Method ; 21 import java.lang.reflect.Modifier ; 22 import java.lang.reflect.InvocationTargetException ; 23 24 import com.gentlyweb.utils.GeneralFilter; 25 import com.gentlyweb.utils.FilterException; 26 27 public class MethodFilter 28 { 29 30 private Class c = null; 31 private List ps = null; 32 private String name = null; 33 private int type = -1; 34 private List modifiers = null; 35 36 public MethodFilter () 37 { 38 39 } 40 41 public MethodFilter (Class c) 42 { 43 44 this.c = c; 45 46 } 47 48 public void setClass (Class 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 (); 62 63 } 64 65 this.modifiers.add (new Integer (m)); 66 67 } 68 69 public List filter () 70 throws IllegalAccessException , 71 InvocationTargetException , 72 FilterException 73 { 74 75 Method [] ms = this.c.getMethods (); 76 77 List res = new ArrayList (); 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 .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 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 if (!this.hasModifiers (m)) 117 { 118 119 continue; 120 121 } 122 123 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 m) 140 { 141 142 if (this.ps == null) 143 { 144 145 return true; 146 147 } 148 149 Class [] 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 c = mpt[i]; 163 164 Class pc = (Class ) this.ps.get (i); 165 166 if (pc == null) 167 { 168 169 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 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 ) 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 pt) 216 { 217 218 this.ps = pt; 219 220 } 221 222 public void setName (String n, 223 int type) 224 { 225 226 this.name = n; 227 this.type = type; 228 229 } 230 231 } 232 | Popular Tags |