1 22 package org.jboss.joinpoint.plugins; 23 24 import java.util.Arrays ; 25 26 import org.jboss.joinpoint.spi.ConstructorJoinpoint; 27 import org.jboss.joinpoint.spi.FieldGetJoinpoint; 28 import org.jboss.joinpoint.spi.FieldSetJoinpoint; 29 import org.jboss.joinpoint.spi.JoinpointException; 30 import org.jboss.joinpoint.spi.JoinpointFactory; 31 import org.jboss.joinpoint.spi.MethodJoinpoint; 32 import org.jboss.logging.Logger; 33 import org.jboss.reflect.spi.ClassInfo; 34 import org.jboss.reflect.spi.ConstructorInfo; 35 import org.jboss.reflect.spi.FieldInfo; 36 import org.jboss.reflect.spi.MethodInfo; 37 import org.jboss.reflect.spi.TypeInfo; 38 39 45 public class Config 46 { 47 48 protected static final Logger log = Logger.getLogger(Config.class); 49 50 51 private static final String [] NO_PARAMS_TYPES = new String [0]; 52 53 54 private static final Object [] NO_PARAMS = new Object [0]; 55 56 65 public static Object instantiate(JoinpointFactory jpf, String [] paramTypes, Object [] params) throws Throwable 66 { 67 ConstructorJoinpoint joinpoint = getConstructorJoinpoint(jpf, paramTypes, params); 68 return joinpoint.dispatch(); 69 } 70 71 80 public static void configure(Object object, JoinpointFactory jpf, String name, Object value) throws Throwable 81 { 82 FieldSetJoinpoint joinpoint = getFieldSetJoinpoint(object, jpf, name, value); 83 joinpoint.dispatch(); 84 } 85 86 94 public static void unconfigure(Object object, JoinpointFactory jpf, String name) throws Throwable 95 { 96 FieldSetJoinpoint joinpoint = getFieldSetJoinpoint(object, jpf, name, null); 97 joinpoint.dispatch(); 98 } 99 100 111 public static Object invoke(Object object, JoinpointFactory jpf, String name, String [] paramTypes, Object [] params) throws Throwable 112 { 113 MethodJoinpoint joinpoint = getMethodJoinpoint(object, jpf, name, paramTypes, params); 114 return joinpoint.dispatch(); 115 } 116 117 124 public static ConstructorJoinpoint getConstructorJoinpoint(JoinpointFactory jpf) throws Throwable 125 { 126 return getConstructorJoinpoint(jpf, null, null); 127 } 128 129 138 public static ConstructorJoinpoint getConstructorJoinpoint(JoinpointFactory jpf, String [] paramTypes, Object [] params) throws Throwable 139 { 140 if (paramTypes == null) 141 paramTypes = NO_PARAMS_TYPES; 142 143 if (params == null) 144 params = NO_PARAMS; 145 146 boolean trace = log.isTraceEnabled(); 147 if (trace) 148 log.trace("Get constructor Joinpoint jpf=" + jpf + " paramTypes=" + Arrays.asList(paramTypes) + " params=" + Arrays.asList(params)); 149 150 ConstructorInfo constructorInfo = findConstructorInfo(jpf.getClassInfo(), paramTypes); 151 ConstructorJoinpoint joinpoint = jpf.getConstructorJoinpoint(constructorInfo); 152 joinpoint.setArguments(params); 153 return joinpoint; 154 } 155 156 165 public static FieldGetJoinpoint getFieldGetJoinpoint(Object object, JoinpointFactory jpf, String name) throws Throwable 166 { 167 boolean trace = log.isTraceEnabled(); 168 if (trace) 169 log.trace("Get field get Joinpoint jpf=" + jpf + " target=" + object + " name=" + name); 170 171 FieldInfo fieldInfo = findFieldInfo(jpf.getClassInfo(), name); 172 FieldGetJoinpoint joinpoint = jpf.getFieldGetJoinpoint(fieldInfo); 173 joinpoint.setTarget(object); 174 return joinpoint; 175 } 176 177 187 public static FieldSetJoinpoint getFieldSetJoinpoint(Object object, JoinpointFactory jpf, String name, Object value) throws Throwable 188 { 189 boolean trace = log.isTraceEnabled(); 190 if (trace) 191 log.trace("Get field set Joinpoint jpf=" + jpf + " target=" + object + " name=" + name + " value=" + value); 192 193 FieldInfo fieldInfo = findFieldInfo(jpf.getClassInfo(), name); 194 FieldSetJoinpoint joinpoint = jpf.getFieldSetJoinpoint(fieldInfo); 195 joinpoint.setTarget(object); 196 joinpoint.setValue(value); 197 return joinpoint; 198 } 199 200 211 public static MethodJoinpoint getMethodJoinpoint(Object object, JoinpointFactory jpf, String name, String [] paramTypes, Object [] params) throws Throwable 212 { 213 boolean trace = log.isTraceEnabled(); 214 if (trace) 215 { 216 if (paramTypes != null) 217 log.trace("Get method Joinpoint jpf=" + jpf + " target=" + object + " name=" + name + " paramTypes=" + Arrays.asList(paramTypes)); 218 else 219 log.trace("Get method Joinpoint jpf=" + jpf + " target=" + object + " name=" + name + " paramTypes=()"); 220 } 221 222 MethodInfo methodInfo = findMethodInfo(jpf.getClassInfo(), name, paramTypes); 223 MethodJoinpoint joinpoint = jpf.getMethodJoinpoint(methodInfo); 224 joinpoint.setTarget(object); 225 joinpoint.setArguments(params); 226 return joinpoint; 227 } 228 229 237 public static ConstructorInfo findConstructorInfo(ClassInfo classInfo, String [] paramTypes) throws JoinpointException 238 { 239 ConstructorInfo[] constructors = classInfo.getDeclaredConstructors(); 240 if (constructors != null) 241 { 242 for (int i = 0; i < constructors.length; ++i) 243 { 244 if (equals(paramTypes, constructors[i].getParameterTypes())) 245 return constructors[i]; 246 } 247 throw new JoinpointException("Constructor not found " + classInfo.getName() + Arrays.asList(paramTypes) + " in " + Arrays.asList(constructors)); 248 } 249 throw new JoinpointException("Constructor not found " + classInfo.getName() + Arrays.asList(paramTypes) + " no constructors"); 250 } 251 252 260 public static FieldInfo findFieldInfo(ClassInfo classInfo, String name) throws JoinpointException 261 { 262 ClassInfo current = classInfo; 263 while (current != null) 264 { 265 FieldInfo result = locateFieldInfo(current, name); 266 if (result != null) 267 return result; 268 current = current.getSuperclass(); 269 } 270 throw new JoinpointException("Field not found '" + name + "' for class " + classInfo.getName()); 271 } 272 273 280 private static FieldInfo locateFieldInfo(ClassInfo classInfo, String name) 281 { 282 FieldInfo[] fields = classInfo.getDeclaredFields(); 283 if (fields != null) 284 { 285 for (int i = 0; i < fields.length; ++i) 286 { 287 if (name.equals(fields[i].getName())) 288 return fields[i]; 289 } 290 } 291 return null; 292 } 293 294 303 public static MethodInfo findMethodInfo(ClassInfo classInfo, String name, String [] paramTypes) throws JoinpointException 304 { 305 return findMethodInfo(classInfo, name, paramTypes, false, true); 306 } 307 308 319 public static MethodInfo findMethodInfo(ClassInfo classInfo, String name, String [] paramTypes, boolean isStatic, boolean isPublic) throws JoinpointException 320 { 321 if (paramTypes == null) 322 paramTypes = NO_PARAMS_TYPES; 323 324 ClassInfo current = classInfo; 325 while (current != null) 326 { 327 MethodInfo result = locateMethodInfo(current, name, paramTypes, isStatic, isPublic); 328 if (result != null) 329 return result; 330 current = current.getSuperclass(); 331 } 332 throw new JoinpointException("Method not found " + name + Arrays.asList(paramTypes) + " for class " + classInfo.getName()); 333 } 334 335 345 private static MethodInfo locateMethodInfo(ClassInfo classInfo, String name, String [] paramTypes, boolean isStatic, boolean isPublic) 346 { 347 MethodInfo[] methods = classInfo.getDeclaredMethods(); 348 if (methods != null) 349 { 350 for (int i = 0; i < methods.length; ++i) 351 { 352 if (name.equals(methods[i].getName()) && 353 equals(paramTypes, methods[i].getParameterTypes()) && 354 methods[i].isStatic() == isStatic && 355 methods[i].isPublic() == isPublic) 356 return methods[i]; 357 } 358 } 359 return null; 360 } 361 362 369 public static boolean equals(String [] typeNames, TypeInfo[] typeInfos) 370 { 371 if (typeNames == null || typeInfos == null) 372 return false; 373 if (typeNames.length != typeInfos.length) 374 return false; 375 for (int i = 0; i < typeNames.length; ++i) 376 { 377 if (typeNames[i] != null && typeNames[i].equals(typeInfos[i].getName()) == false) 378 return false; 379 } 380 return true; 381 } 382 } 383 | Popular Tags |