1 29 30 package com.caucho.bytecode; 31 32 import java.lang.ref.SoftReference ; 33 import java.lang.reflect.Constructor ; 34 import java.lang.reflect.Field ; 35 import java.lang.reflect.Method ; 36 import java.lang.reflect.Modifier ; 37 38 41 public class JClassWrapper extends JClass { 42 private JClassLoader _loader; 43 44 private Class _class; 45 46 private SoftReference <JMethod[]> _declaredMethodsRef; 47 48 public JClassWrapper(Class cl, JClassLoader loader) 49 { 50 _loader = loader; 51 52 _class = cl; 53 } 54 55 JClassWrapper(Class cl) 56 { 57 _class = cl; 58 } 59 60 63 public String getName() 64 { 65 return _class.getName(); 66 } 67 68 71 public Class getJavaClass() 72 { 73 return _class; 74 } 75 76 79 public Class getWrappedClass() 80 { 81 return _class; 82 } 83 84 87 public JClassLoader getClassLoader() 88 { 89 if (_loader != null) 90 return _loader; 91 else 92 return JClassLoader.getSystemClassLoader(); 93 } 94 95 98 public boolean isPrimitive() 99 { 100 return _class.isPrimitive(); 101 } 102 103 106 public boolean isArray() 107 { 108 return _class.isArray(); 109 } 110 111 114 public JClass getComponentType() 115 { 116 return getClassLoader().forName(_class.getComponentType().getName()); 117 } 118 119 122 public boolean isPublic() 123 { 124 return Modifier.isPublic(_class.getModifiers()); 125 } 126 127 130 public boolean isAbstract() 131 { 132 return Modifier.isAbstract(_class.getModifiers()); 133 } 134 135 138 public boolean isFinal() 139 { 140 return Modifier.isFinal(_class.getModifiers()); 141 } 142 143 146 public boolean isInterface() 147 { 148 return _class.isInterface(); 149 } 150 151 154 public boolean isAssignableTo(Class cl) 155 { 156 return cl.isAssignableFrom(_class); 157 } 158 159 162 public boolean isAssignableFrom(Class cl) 163 { 164 return _class.isAssignableFrom(cl); 165 } 166 167 170 public boolean isAssignableFrom(JClass cl) 171 { 172 return cl.isAssignableTo(_class); 173 } 174 175 178 public JClass getSuperClass() 179 { 180 Class cl = _class.getSuperclass(); 181 182 if (cl != null) 183 return _loader.forName(cl.getName()); 184 else 185 return null; 186 } 187 188 191 public JClass []getInterfaces() 192 { 193 Class []cl = _class.getInterfaces(); 194 195 JClass []clList = new JClass[cl.length]; 196 197 for (int i = 0; i < cl.length; i++) 198 clList[i] = _loader.forName(cl[i].getName()); 199 200 return clList; 201 } 202 203 206 public JMethod []getDeclaredMethods() 207 { 208 SoftReference <JMethod[]> jMethodsRef = _declaredMethodsRef; 209 JMethod []jMethods = null; 210 211 if (jMethodsRef != null) { 212 jMethods = jMethodsRef.get(); 213 if (jMethods != null) 214 return jMethods; 215 } 216 217 Method []methods = _class.getDeclaredMethods(); 218 219 jMethods = new JMethod[methods.length]; 220 221 for (int i = 0; i < methods.length; i++) { 222 jMethods[i] = new JMethodWrapper(methods[i], getClassLoader()); 223 } 224 225 _declaredMethodsRef = new SoftReference <JMethod[]>(jMethods); 226 227 return jMethods; 228 } 229 230 233 public JMethod []getMethods() 234 { 235 Method []methods = _class.getMethods(); 236 237 JMethod []jMethods = new JMethod[methods.length]; 238 239 for (int i = 0; i < methods.length; i++) { 240 jMethods[i] = new JMethodWrapper(methods[i], getClassLoader()); 241 } 242 243 return jMethods; 244 } 245 246 249 public JMethod getMethod(String name, JClass []types) 250 { 251 JClassLoader jClassLoader = getClassLoader(); 252 253 return getMethod(_class, name, types, jClassLoader); 254 } 255 256 private static JMethod getMethod(Class cl, String name, JClass []types, 257 JClassLoader jClassLoader) 258 { 259 if (cl == null) 260 return null; 261 262 loop: 263 for (Method method : cl.getDeclaredMethods()) { 264 if (! method.getName().equals(name)) 265 continue; 266 267 Class []paramTypes = method.getParameterTypes(); 268 if (types.length != paramTypes.length) 269 continue; 270 271 for (int i = 0; i < types.length; i++) { 272 if (! types[i].getName().equals(paramTypes[i].getName())) 273 continue loop; 274 } 275 276 return new JMethodWrapper(method, jClassLoader); 277 } 278 279 for (Class ifc : cl.getInterfaces()) { 280 JMethod method = getMethod(ifc, name, types, jClassLoader); 281 282 if (method != null) 283 return method; 284 } 285 286 return getMethod(cl.getSuperclass(), name, types, jClassLoader); 287 } 288 289 292 public JMethod []getConstructors() 293 { 294 Constructor []methods = _class.getConstructors(); 295 296 JMethod []jMethods = new JMethod[methods.length]; 297 298 for (int i = 0; i < methods.length; i++) { 299 jMethods[i] = new JConstructorWrapper(methods[i], getClassLoader()); 300 } 301 302 return jMethods; 303 } 304 305 308 public JField []getDeclaredFields() 309 { 310 Field []fields = _class.getDeclaredFields(); 311 312 JField []jFields = new JField[fields.length]; 313 314 for (int i = 0; i < fields.length; i++) { 315 jFields[i] = new JFieldWrapper(fields[i], getClassLoader()); 316 } 317 318 return jFields; 319 } 320 321 324 public JField []getFields() 325 { 326 Field []fields = _class.getFields(); 327 328 JField []jFields = new JField[fields.length]; 329 330 for (int i = 0; i < fields.length; i++) { 331 jFields[i] = new JFieldWrapper(fields[i], getClassLoader()); 332 } 333 334 return jFields; 335 } 336 337 public String toString() 338 { 339 return "JClassWrapper[" + getName() + "]"; 340 } 341 } 342 | Popular Tags |