1 4 package com.tc.aspectwerkz.reflect.impl.java; 5 6 import com.tc.backport175.bytecode.AnnotationElement; 7 import com.tc.backport175.bytecode.AnnotationReader; 8 9 import com.tc.aspectwerkz.reflect.ClassInfo; 10 import com.tc.aspectwerkz.reflect.ConstructorInfo; 11 import com.tc.aspectwerkz.reflect.FieldInfo; 12 import com.tc.aspectwerkz.reflect.MethodInfo; 13 import com.tc.aspectwerkz.reflect.ReflectHelper; 14 import com.tc.aspectwerkz.reflect.StaticInitializationInfo; 15 import com.tc.aspectwerkz.reflect.StaticInitializationInfoImpl; 16 import com.tc.aspectwerkz.reflect.impl.asm.AsmClassInfo; 17 import com.tc.aspectwerkz.transform.TransformationConstants; 18 19 import java.lang.reflect.Constructor ; 20 import java.lang.reflect.Field ; 21 import java.lang.reflect.Method ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 25 30 public class JavaClassInfo implements ClassInfo { 31 34 private final Class m_class; 36 37 40 private String m_name; 41 42 45 private String m_signature; 46 47 50 private boolean m_isInterface = false; 51 52 55 private boolean m_isPrimitive = false; 56 57 60 private boolean m_isArray = false; 61 62 65 private final HashMap m_constructors = new HashMap (); 66 67 70 private final HashMap m_methods = new HashMap (); 71 72 75 private final HashMap m_fields = new HashMap (); 76 77 80 private ClassInfo[] m_interfaces = null; 81 82 85 private ClassInfo m_superClass = null; 86 87 90 private ClassInfo m_componentType = null; 91 92 95 private final JavaClassInfoRepository m_classInfoRepository; 96 97 100 private StaticInitializationInfo m_staticInitializer = null; 101 102 107 JavaClassInfo(final Class klass) { 108 if (klass == null) { 109 throw new IllegalArgumentException ("class can not be null"); 110 } 111 m_class = klass; 112 113 m_signature = ReflectHelper.getClassSignature(klass); 114 115 m_classInfoRepository = JavaClassInfoRepository.getRepository(klass.getClassLoader()); 116 m_isInterface = klass.isInterface(); 117 if (klass.isPrimitive()) { 118 m_name = klass.getName(); 119 m_isPrimitive = true; 120 } else if (klass.getComponentType() != null) { 121 m_name = convertJavaArrayTypeNameToHumanTypeName(klass.getName()); 122 m_isArray = true; 123 m_interfaces = new ClassInfo[0]; 124 } else { 125 m_name = klass.getName(); 126 Method [] methods = m_class.getDeclaredMethods(); 127 for (int i = 0; i < methods.length; i++) { 128 Method method = methods[i]; 129 m_methods.put(new Integer (ReflectHelper.calculateHash(method)), new JavaMethodInfo(method, this)); 130 } 131 Constructor [] constructors = m_class.getDeclaredConstructors(); 132 for (int i = 0; i < constructors.length; i++) { 133 Constructor constructor = constructors[i]; 134 m_constructors.put(new Integer (ReflectHelper.calculateHash(constructor)), new JavaConstructorInfo(constructor, this)); 136 } 137 Field [] fields = m_class.getDeclaredFields(); 138 for (int i = 0; i < fields.length; i++) { 139 if (fields[i].getName().startsWith(TransformationConstants.ASPECTWERKZ_PREFIX)) { 140 continue; 141 } 142 Field field = fields[i]; 143 m_fields.put(new Integer (ReflectHelper.calculateHash(field)), new JavaFieldInfo(field, this)); 144 } 145 } 146 m_classInfoRepository.addClassInfo(this); 147 } 148 149 154 public static ClassInfo getClassInfo(final Class clazz) { 155 JavaClassInfoRepository repository = JavaClassInfoRepository.getRepository(clazz.getClassLoader()); 156 ClassInfo classInfo = repository.getClassInfo(clazz.getName()); 157 if (classInfo == null) { 158 classInfo = new JavaClassInfo(clazz); 159 } 160 return classInfo; 161 } 162 163 168 public AnnotationElement.Annotation[] getAnnotations() { 169 return getAnnotationReader().getAnnotationElements(); 170 } 171 172 177 public String getName() { 178 return m_name.replace('/', '.'); 179 } 180 181 186 public boolean hasStaticInitializer() { 187 ClassInfo classInfo = AsmClassInfo.getClassInfo(getName(), getClassLoader()); 188 return classInfo.hasStaticInitializer(); 189 } 190 191 196 public StaticInitializationInfo staticInitializer() { 197 if (hasStaticInitializer() && m_staticInitializer == null) { 198 m_staticInitializer = new StaticInitializationInfoImpl(this); 199 } 200 return m_staticInitializer; 201 } 202 203 208 public String getSignature() { 209 return m_signature; 210 } 211 212 public String getGenericsSignature() { 213 return null; 215 } 216 217 222 public int getModifiers() { 223 return m_class.getModifiers(); 224 } 225 226 231 public ClassLoader getClassLoader() { 232 return m_class.getClassLoader(); 233 } 234 235 241 public ConstructorInfo getConstructor(final int hash) { 242 ConstructorInfo constructor = (ConstructorInfo) m_constructors.get(new Integer (hash)); 243 if (constructor == null && getSuperclass() != null) { 244 constructor = getSuperclass().getConstructor(hash); 245 } 246 return constructor; 247 } 248 249 254 public ConstructorInfo[] getConstructors() { 255 ConstructorInfo[] methodInfos = new ConstructorInfo[m_constructors.size()]; 256 int i = 0; 260 for (Iterator it = m_constructors.values().iterator(); it.hasNext();) { 261 methodInfos[i++] = (ConstructorInfo) it.next(); 262 } 263 return methodInfos; 264 } 265 266 272 public MethodInfo getMethod(final int hash) { 273 MethodInfo method = (MethodInfo) m_methods.get(new Integer (hash)); 274 if (method == null) { 275 for (int i = 0; i < getInterfaces().length; i++) { 276 method = getInterfaces()[i].getMethod(hash); 277 if (method != null) { 278 break; 279 } 280 } 281 } 282 if (method == null && getSuperclass() != null) { 283 method = getSuperclass().getMethod(hash); 284 } 285 return method; 286 } 287 288 293 public MethodInfo[] getMethods() { 294 MethodInfo[] methodInfos = new MethodInfo[m_methods.size()]; 295 int i = 0; 299 for (Iterator it = m_methods.values().iterator(); it.hasNext();) { 300 methodInfos[i++] = (MethodInfo) it.next(); 301 } 302 return methodInfos; 303 } 304 305 311 public FieldInfo getField(final int hash) { 312 FieldInfo field = (FieldInfo) m_fields.get(new Integer (hash)); 313 if (field == null && getSuperclass() != null) { 314 field = getSuperclass().getField(hash); 315 } 316 if (field == null) { 317 ClassInfo[] interfaces = getInterfaces(); 319 for (int i = 0; i < interfaces.length; i++) { 320 ClassInfo ifc = interfaces[i]; 321 field = ifc.getField(hash); 322 if (field != null) 323 break; 324 } 325 } 326 return field; 327 } 328 329 334 public FieldInfo[] getFields() { 335 FieldInfo[] fieldInfos = new FieldInfo[m_fields.size()]; 336 int i = 0; 340 for (Iterator it = m_methods.values().iterator(); it.hasNext();) { 341 fieldInfos[i++] = (FieldInfo) it.next(); 342 } 343 return fieldInfos; 344 } 345 346 351 public synchronized ClassInfo[] getInterfaces() { 352 if (m_interfaces == null) { 353 Class [] interfaces = m_class.getInterfaces(); 354 m_interfaces = new ClassInfo[interfaces.length]; 355 for (int i = 0; i < interfaces.length; i++) { 356 Class anInterface = interfaces[i]; 357 ClassInfo classInfo = JavaClassInfo.getClassInfo(anInterface); 358 m_interfaces[i] = classInfo; 359 if (!m_classInfoRepository.hasClassInfo(anInterface.getName())) { 360 m_classInfoRepository.addClassInfo(classInfo); 361 } 362 } 363 } 364 return m_interfaces; 365 } 366 367 372 public ClassInfo getSuperclass() { 373 if (m_superClass == null) { 374 Class superclass = m_class.getSuperclass(); 375 if (superclass != null) { 376 if (m_classInfoRepository.hasClassInfo(superclass.getName())) { 377 m_superClass = m_classInfoRepository.getClassInfo(superclass.getName()); 378 } else { 379 m_superClass = JavaClassInfo.getClassInfo(superclass); 380 m_classInfoRepository.addClassInfo(m_superClass); 381 } 382 } 383 } 384 return m_superClass; 385 } 386 387 392 public ClassInfo getComponentType() { 393 if (isArray() && (m_componentType == null)) { 394 Class componentType = m_class.getComponentType(); 395 if (m_classInfoRepository.hasClassInfo(componentType.getName())) { 396 m_componentType = m_classInfoRepository.getClassInfo(componentType.getName()); 397 } else { 398 m_componentType = JavaClassInfo.getClassInfo(componentType); 399 m_classInfoRepository.addClassInfo(m_componentType); 400 } 401 } 402 return m_componentType; 403 } 404 405 410 public boolean isInterface() { 411 return m_isInterface; 412 } 413 414 419 public boolean isPrimitive() { 420 return m_isPrimitive; 421 } 422 423 428 public boolean isArray() { 429 return m_isArray; 430 } 431 432 439 public static String convertJavaArrayTypeNameToHumanTypeName(final String typeName) { 440 int index = typeName.lastIndexOf('['); 441 if (index != -1) { 442 StringBuffer arrayType = new StringBuffer (); 443 if (typeName.endsWith("I")) { 444 arrayType.append("int"); 445 } else if (typeName.endsWith("J")) { 446 arrayType.append("long"); 447 } else if (typeName.endsWith("S")) { 448 arrayType.append("short"); 449 } else if (typeName.endsWith("F")) { 450 arrayType.append("float"); 451 } else if (typeName.endsWith("D")) { 452 arrayType.append("double"); 453 } else if (typeName.endsWith("Z")) { 454 arrayType.append("boolean"); 455 } else if (typeName.endsWith("C")) { 456 arrayType.append("char"); 457 } else if (typeName.endsWith("B")) { 458 arrayType.append("byte"); 459 } else { 460 arrayType.append(typeName.substring(index + 2, typeName.length() - 1)); 461 } 462 for (int i = 0; i < (index + 1); i++) { 463 arrayType.append("[]"); 464 } 465 return arrayType.toString(); 466 } else { 467 return typeName; 468 } 469 } 470 471 public boolean equals(Object o) { 472 if (this == o) { 473 return true; 474 } 475 if (!(o instanceof ClassInfo)) { 476 return false; 477 } 478 ClassInfo classInfo = (ClassInfo) o; 479 return m_class.getName().toString().equals(classInfo.getName().toString()); 480 } 481 482 public int hashCode() { 483 return m_class.getName().toString().hashCode(); 484 } 485 486 public String toString() { 487 return getName(); 488 } 489 490 public AnnotationReader getAnnotationReader() { 491 return AnnotationReader.getReaderFor(m_class); 492 } 493 } | Popular Tags |