1 22 package org.jboss.reflect.plugins; 23 24 import java.lang.reflect.Array ; 25 import java.lang.reflect.Modifier ; 26 import java.util.HashMap ; 27 28 import org.jboss.reflect.spi.AnnotationValue; 29 import org.jboss.reflect.spi.ClassInfo; 30 import org.jboss.reflect.spi.ConstructorInfo; 31 import org.jboss.reflect.spi.FieldInfo; 32 import org.jboss.reflect.spi.InterfaceInfo; 33 import org.jboss.reflect.spi.MethodInfo; 34 import org.jboss.reflect.spi.TypeInfo; 35 import org.jboss.util.JBossStringBuilder; 36 import org.jboss.util.UnreachableStatementException; 37 38 45 public class ClassInfoImpl extends InheritableAnnotationHolder implements ClassInfo 46 { 47 48 private static final long serialVersionUID = 3545798779904340792L; 49 50 51 static final ClassInfo UNKNOWN_CLASS = new UnknownClassInfo(); 52 53 54 static final InterfaceInfo[] UNKNOWN_INTERFACES = new InterfaceInfo[0]; 55 56 57 static final ConstructorInfo[] UNKNOWN_CONSTRUCTORS = new ConstructorInfo[0]; 58 59 60 static final MethodInfo[] UNKNOWN_METHODS = new MethodInfo[0]; 61 62 63 private static final FieldInfo[] UNKNOWN_FIELDS = new FieldInfo[0]; 64 65 66 protected String name; 67 68 69 protected int modifiers; 70 71 72 protected InterfaceInfo[] interfaces = UNKNOWN_INTERFACES; 73 74 75 protected MethodInfo[] methods = UNKNOWN_METHODS; 76 77 78 protected FieldInfo[] fields = UNKNOWN_FIELDS; 79 80 81 protected HashMap fieldMap; 82 83 84 protected ClassInfo superclass = UNKNOWN_CLASS; 85 86 87 protected ConstructorInfo[] constructors = UNKNOWN_CONSTRUCTORS; 88 89 90 protected ClassInfoHelper classInfoHelper; 91 92 100 public static MethodInfo findMethod(MethodInfo[] methods, String name, TypeInfo[] parameters) 101 { 102 if (methods == null) return null; 103 for (int i = 0; i < methods.length; i++) 104 { 105 if (methods[i].getName().equals(name)) 106 { 107 final int length = (parameters != null) ? parameters.length : 0; 108 if (methods[i].getParameterTypes().length == length) 109 { 110 boolean ok = true; 111 for (int j = 0; j < length; j++) 112 { 113 if (!parameters[j].equals(methods[i].getParameterTypes()[j])) 114 { 115 ok = false; 116 break; 117 } 118 } 119 if (ok) return methods[i]; 120 } 121 } 122 } 123 return null; 124 } 125 126 134 public static Class getArrayClass(Class clazz, int depth) 135 { 136 return Array.newInstance(clazz, depth).getClass(); 137 } 138 139 142 public ClassInfoImpl() 143 { 144 } 145 146 151 public ClassInfoImpl(String name) 152 { 153 this.name = name; 154 } 155 156 164 public ClassInfoImpl(String name, int modifiers, InterfaceInfo[] interfaces, 165 ClassInfoImpl superclass) 166 { 167 this.name = name; 168 this.modifiers = modifiers; 169 this.interfaces = interfaces; 170 this.superclass = superclass; 171 } 172 173 178 public void setClassInfoHelper(ClassInfoHelper helper) 179 { 180 this.classInfoHelper = helper; 181 } 182 183 188 public void setType(Class type) 189 { 190 setAnnotatedElement(type); 191 } 192 193 198 public void setInterfaces(InterfaceInfo[] interfaces) 199 { 200 this.interfaces = interfaces; 201 } 202 203 208 public void setDeclaredMethods(MethodInfoImpl[] methods) 209 { 210 this.methods = methods; 211 if (methods != null) 212 { 213 for (int i = 0; i < methods.length; i++) 214 methods[i].declaringClass = this; 215 } 216 } 217 218 223 public void setDeclaredFields(FieldInfoImpl[] fields) 224 { 225 this.fields = fields; 226 if (fields != null) 227 { 228 for (int i = 0; i < fields.length; i++) 229 fields[i].declaringClass = this; 230 } 231 } 232 233 238 public void setDeclaredConstructors(ConstructorInfoImpl[] constructors) 239 { 240 this.constructors = constructors; 241 if (constructors != null) 242 { 243 for (int i = 0; i < constructors.length; i++) 244 constructors[i].declaringClass = this; 245 } 246 } 247 248 253 public void setSuperclass(ClassInfoImpl superInfo) 254 { 255 this.superclass = superInfo; 256 } 257 258 259 public boolean isInterface() 260 { 261 return false; 262 } 263 264 public InterfaceInfo[] getInterfaces() 265 { 266 if (interfaces == UNKNOWN_INTERFACES) 267 setInterfaces(classInfoHelper.getInterfaces(this)); 268 return interfaces; 269 } 270 271 public MethodInfo getDeclaredMethod(String name, TypeInfo[] parameters) 272 { 273 if (methods == UNKNOWN_METHODS) 274 setDeclaredMethods(classInfoHelper.getMethods(this)); 275 return findMethod(methods, name, parameters); 276 } 277 278 public MethodInfo[] getDeclaredMethods() 279 { 280 if (methods == UNKNOWN_METHODS) 281 setDeclaredMethods(classInfoHelper.getMethods(this)); 282 return methods; 283 } 284 285 public FieldInfo getDeclaredField(String name) 286 { 287 if (fields == UNKNOWN_FIELDS) 288 setDeclaredFields(classInfoHelper.getFields(this)); 289 return (FieldInfo) fieldMap.get(name); 290 } 291 292 public FieldInfo[] getDeclaredFields() 293 { 294 if (fields == UNKNOWN_FIELDS) 295 setDeclaredFields(classInfoHelper.getFields(this)); 296 return fields; 297 } 298 299 public ConstructorInfo[] getDeclaredConstructors() 300 { 301 if (constructors == UNKNOWN_CONSTRUCTORS) 302 setDeclaredConstructors(classInfoHelper.getConstructors(this)); 303 return constructors; 304 } 305 306 public ClassInfo getSuperclass() 307 { 308 if (superclass == UNKNOWN_CLASS) 309 setSuperclass(classInfoHelper.getSuperClass(this)); 310 return superclass; 311 } 312 313 public int getModifiers() 314 { 315 return modifiers; 316 } 317 318 public boolean isStatic() 319 { 320 return Modifier.isStatic(modifiers); 321 } 322 323 public boolean isPublic() 324 { 325 return Modifier.isPublic(modifiers); 326 } 327 328 public String getName() 329 { 330 return name; 331 } 332 333 public Class <? extends Object > getType() 334 { 335 return (Class <? extends Object >) annotatedElement; 336 } 337 338 public Object convertValue(Object value) throws Throwable 339 { 340 return ValueConvertor.convertValue(getType(), value); 341 } 342 343 public boolean isArray() 344 { 345 return getType().isArray(); 346 } 347 348 public TypeInfo getArrayType(int depth) 349 { 350 Class arrayClass = getArrayClass(getType(), depth); 351 return classInfoHelper.getTypeInfo(arrayClass); 352 } 353 354 public Object [] newArrayInstance(int size) throws Throwable 355 { 356 Class clazz = getType(); 357 if (clazz.isArray() == false) 358 throw new ClassCastException (clazz + " is not an array."); 359 return (Object []) Array.newInstance(clazz.getComponentType(), size); 360 } 361 362 protected InheritableAnnotationHolder getSuperHolder() 363 { 364 return (ClassInfoImpl) getSuperclass(); 365 } 366 367 protected void toString(JBossStringBuilder buffer) 368 { 369 buffer.append("name=").append(getName()); 370 } 371 372 public boolean equals(Object obj) 373 { 374 if (this == obj) 375 return true; 376 if (obj == null || obj instanceof ClassInfo == false) 377 return false; 378 379 final ClassInfo other = (ClassInfo) obj; 380 381 if (name != null ? name.equals(other.getName()) == false : other.getName() != null) 382 return false; 383 return true; 384 } 385 386 public int hashCode() 387 { 388 return (name != null ? name.hashCode() : 0); 389 } 390 391 static class UnknownClassInfo implements ClassInfo 392 { 393 public ConstructorInfo[] getDeclaredConstructors() 394 { 395 throw new UnreachableStatementException(); 396 } 397 398 public FieldInfo getDeclaredField(String name) 399 { 400 throw new UnreachableStatementException(); 401 } 402 403 public FieldInfo[] getDeclaredFields() 404 { 405 throw new UnreachableStatementException(); 406 } 407 408 public MethodInfo getDeclaredMethod(String name, TypeInfo[] parameters) 409 { 410 throw new UnreachableStatementException(); 411 } 412 413 public MethodInfo[] getDeclaredMethods() 414 { 415 throw new UnreachableStatementException(); 416 } 417 418 public InterfaceInfo[] getInterfaces() 419 { 420 throw new UnreachableStatementException(); 421 } 422 423 public String getName() 424 { 425 throw new UnreachableStatementException(); 426 } 427 428 public ClassInfo getSuperclass() 429 { 430 throw new UnreachableStatementException(); 431 } 432 433 public boolean isInterface() 434 { 435 throw new UnreachableStatementException(); 436 } 437 438 public AnnotationValue getAnnotation(String name) 439 { 440 throw new UnreachableStatementException(); 441 } 442 443 public AnnotationValue[] getAnnotations() 444 { 445 throw new UnreachableStatementException(); 446 } 447 448 public boolean isAnnotationPresent(String name) 449 { 450 throw new UnreachableStatementException(); 451 } 452 453 public String toShortString() 454 { 455 throw new UnreachableStatementException(); 456 } 457 458 public void toShortString(JBossStringBuilder buffer) 459 { 460 throw new UnreachableStatementException(); 461 } 462 463 public Class getType() 464 { 465 throw new UnreachableStatementException(); 466 } 467 468 public Object convertValue(Object value) throws Throwable 469 { 470 throw new UnreachableStatementException(); 471 } 472 473 public TypeInfo getArrayType(int depth) 474 { 475 throw new UnreachableStatementException(); 476 } 477 478 public boolean isArray() 479 { 480 throw new UnreachableStatementException(); 481 } 482 483 public Object [] newArrayInstance(int size) throws Throwable 484 { 485 throw new UnreachableStatementException(); 486 } 487 488 public int getModifiers() 489 { 490 throw new UnreachableStatementException(); 491 } 492 493 public boolean isPublic() 494 { 495 throw new UnreachableStatementException(); 496 } 497 498 public boolean isStatic() 499 { 500 throw new UnreachableStatementException(); 501 } 502 503 public Object clone() 504 { 505 throw new UnreachableStatementException(); 506 } 507 } 508 } 509 | Popular Tags |