1 22 package org.jboss.reflect.plugins.javassist; 23 24 import java.lang.annotation.Annotation ; 25 26 import javassist.ClassPool; 27 import javassist.CtClass; 28 import javassist.CtMember; 29 import javassist.CtMethod; 30 import javassist.CtPrimitiveType; 31 import javassist.NotFoundException; 32 33 import org.jboss.reflect.plugins.AnnotationAttributeImpl; 34 import org.jboss.reflect.plugins.AnnotationHelper; 35 import org.jboss.reflect.plugins.AnnotationValueFactory; 36 import org.jboss.reflect.plugins.AnnotationValueImpl; 37 import org.jboss.reflect.spi.AnnotationInfo; 38 import org.jboss.reflect.spi.AnnotationValue; 39 import org.jboss.reflect.spi.PrimitiveInfo; 40 import org.jboss.reflect.spi.TypeInfo; 41 import org.jboss.reflect.spi.TypeInfoFactory; 42 import org.jboss.util.JBossStringBuilder; 43 import org.jboss.util.collection.WeakClassCache; 44 45 50 public class JavassistTypeInfoFactoryImpl extends WeakClassCache implements TypeInfoFactory, AnnotationHelper 51 { 52 static final ClassPool pool = ClassPool.getDefault(); 53 54 static final AnnotationValue[] NO_ANNOTATIONS = new AnnotationValue[0]; 55 63 public static NoClassDefFoundError raiseClassNotFound(String name, NotFoundException e) throws NoClassDefFoundError 64 { 65 NoClassDefFoundError ex = new NoClassDefFoundError ("Unable to find class"); 66 if (e.getCause() != null) 67 ex.initCause(e.getCause()); throw ex; 69 } 70 71 79 public static NoClassDefFoundError raiseClassNotFound(String name, ClassNotFoundException e) throws NoClassDefFoundError 80 { 81 NoClassDefFoundError ex = new NoClassDefFoundError ("Unable to find class"); 82 ex.initCause(e); 83 throw ex; 84 } 85 86 94 public static NoClassDefFoundError raiseMethodNotFound(String name, NotFoundException e) throws NoClassDefFoundError 95 { 96 NoSuchMethodError ex = new NoSuchMethodError ("Unable to find method " + name); 97 if (e.getCause() != null) 98 ex.initCause(e.getCause()); throw ex; 100 } 101 102 110 public static NoClassDefFoundError raiseFieldNotFound(String name, NotFoundException e) throws NoClassDefFoundError 111 { 112 NoSuchFieldError ex = new NoSuchFieldError ("Unable to find field"); 113 if (e.getCause() != null) 114 ex.initCause(e.getCause()); throw ex; 116 } 117 118 @SuppressWarnings ("unchecked") 119 protected Object instantiate(Class clazz) 120 { 121 try 122 { 123 CtClass ctClass = getCtClass(clazz.getName()); 124 125 if (clazz.isArray()) 126 { 127 TypeInfo componentType = getTypeInfo(clazz.getComponentType()); 128 return new JavassistArrayInfoImpl(componentType); 129 } 130 131 if (ctClass.isAnnotation()) 132 { 133 JavassistAnnotationInfo result = new JavassistAnnotationInfo(this, ctClass, clazz); 134 CtMethod[] methods = ctClass.getDeclaredMethods(); 135 AnnotationAttributeImpl[] atttributes = new AnnotationAttributeImpl[methods.length]; 136 for (int i = 0 ; i < methods.length ; i++) 137 { 138 atttributes[i] = new AnnotationAttributeImpl(methods[i].getName(), getTypeInfo(methods[i].getReturnType()), null); 139 } 140 result.setAttributes(atttributes); 141 return result; 142 143 } 144 else if (ctClass.isEnum()) 145 { 146 return new JavassistEnumInfo(this, ctClass, clazz); 147 } 148 149 150 return new JavassistTypeInfo(this, ctClass, clazz); 151 } 152 catch (NotFoundException e) 153 { 154 throw new RuntimeException (e); 155 } 156 } 157 158 164 protected TypeInfo getTypeInfo(CtClass ctClass) 165 { 166 try 167 { 168 String name = convertName(ctClass); 169 return getTypeInfo(name, null); 170 } 171 catch (ClassNotFoundException e) 172 { 173 throw raiseClassNotFound(ctClass.getName(), e); 174 } 175 } 176 177 183 protected String convertName(CtClass clazz) 184 { 185 CtClass temp = clazz; 186 if (temp.isArray()) 187 { 188 JBossStringBuilder buffer = new JBossStringBuilder(); 189 try 190 { 191 while (temp.isArray()) 192 { 193 buffer.append('['); 194 temp = temp.getComponentType(); 195 } 196 if (temp.isPrimitive()) 197 { 198 CtPrimitiveType primitive = (CtPrimitiveType) temp; 199 buffer.append(Character.toString(primitive.getDescriptor())); 200 } 201 else 202 { 203 buffer.append('L'); 204 buffer.append(temp.getName()); 205 buffer.append(';'); 206 } 207 return buffer.toString(); 208 } 209 catch (NotFoundException e) 210 { 211 throw raiseClassNotFound(clazz.getName(), e); 212 } 213 } 214 return clazz.getName(); 215 } 216 217 223 protected CtClass getCtClass(String name) 224 { 225 try 226 { 227 return pool.get(name); 228 } 229 catch (NotFoundException e) 230 { 231 throw raiseClassNotFound(name, e); 232 } 233 } 234 235 protected void generate(Class clazz, Object result) 236 { 237 } 239 240 public TypeInfo getTypeInfo(Class clazz) 241 { 242 if (clazz == null) 243 throw new IllegalArgumentException ("Null class"); 244 245 TypeInfo primitive = PrimitiveInfo.valueOf(clazz.getName()); 246 if (primitive != null) 247 return primitive; 248 249 return (TypeInfo) get(clazz); 250 } 251 252 public TypeInfo getTypeInfo(String name, ClassLoader cl) throws ClassNotFoundException 253 { 254 if (name == null) 255 throw new IllegalArgumentException ("Null class name"); 256 if (cl == null) 257 cl = Thread.currentThread().getContextClassLoader(); 258 259 TypeInfo primitive = PrimitiveInfo.valueOf(name); 260 if (primitive != null) 261 return primitive; 262 263 Class clazz = cl.loadClass(name); 264 return getTypeInfo(clazz); 265 } 266 267 public AnnotationValue[] getAnnotations(Object obj) 268 { 269 try 270 { 271 Object [] annotations; 272 if (obj instanceof CtMember) 273 { 274 annotations = ((CtMember)obj).getAvailableAnnotations(); 275 } 276 else if (obj instanceof CtClass) 277 { 278 annotations = ((CtClass)obj).getAvailableAnnotations(); 279 } 280 else 281 { 282 throw new RuntimeException ("Attempt was made to read annotations from unsupported type " + obj.getClass().getName() + ": " + obj); 283 } 284 285 if (annotations.length == 0) 286 { 287 return NO_ANNOTATIONS; 288 } 289 290 AnnotationValue[] annotationValues = new AnnotationValueImpl[annotations.length]; 291 for (int i = 0 ; i < annotations.length ; i++) 292 { 293 Class clazz = ((Annotation )annotations[i]).annotationType(); 294 295 AnnotationInfo info = (AnnotationInfo)getTypeInfo(clazz); 296 annotationValues[i] = AnnotationValueFactory.createAnnotationValue(this, this, info, annotations[i]); 297 } 298 return annotationValues; 299 } 300 catch (ClassNotFoundException e) 301 { 302 throw new RuntimeException (e); 303 } 304 catch (Throwable t) 305 { 306 throw new RuntimeException (t); 307 } 308 } 309 310 public AnnotationValue createAnnotationValue(AnnotationInfo info, Object ann) 311 { 312 return AnnotationValueFactory.createAnnotationValue(this, this, info, ann); 313 } 314 315 } 316 | Popular Tags |