1 /************************************************* 2 * Copyright (c) Shen Li. All rights reserved. * 3 * http://joyaop.sourceforge.net * 4 * -------------------------------------------- * 5 * Distributable under LGPL license. * 6 * See terms of license at gnu.org. * 7 ************************************************/ 8 package net.sf.joyaop.util; 9 10 import net.sf.cglib.proxy.Factory; 11 import net.sf.cglib.reflect.FastClass; 12 import net.sf.cglib.reflect.FastMethod; 13 14 import java.lang.reflect.InvocationTargetException; 15 import java.util.HashMap; 16 import java.util.Map; 17 18 /*** 19 * @author Shen Li 20 */ 21 public abstract class ClassUtils { 22 public static Object newInstance(Class clazz) { 23 try { 24 return getFastClass(clazz).newInstance(); 25 } catch (InvocationTargetException e) { 26 throw new RuntimeException(e); 27 } 28 } 29 30 public static Object newInstance(Class clazz, Class[] argumentTypes, Object[] arguments) { 31 try { 32 return getFastClass(clazz).newInstance(argumentTypes, arguments); 33 } catch (InvocationTargetException e) { 34 throw new RuntimeException(e); 35 } 36 } 37 38 public static Class loadClass(String name) { 39 try { 40 return getClassLoader().loadClass(name); 41 } catch (ClassNotFoundException e) { 42 throw new RuntimeException(e); 43 } 44 } 45 46 public static FastClass getFastClass(Class clazz) { 47 return FastClass.create(getClassLoader(), clazz); 48 } 49 50 public static FastMethod getFastMethod(Class clazz, String name, Class[] parameterTypes) { 51 return getFastClass(clazz).getMethod(name, parameterTypes); 52 } 53 54 public static Object invokeFastMethod(FastMethod fastMethod, Object target, Object[] args) throws Throwable { 55 try { 56 return fastMethod.invoke(target, args); 57 } catch (InvocationTargetException e) { 58 throw e.getTargetException(); 59 } 60 } 61 62 public static ClassLoader getClassLoader() { 63 return Thread.currentThread().getContextClassLoader(); 64 } 65 66 private static final Map proxyFactories = new HashMap(); 67 68 public static void putProxyFactory(Class originalClass, Factory factory) { 69 synchronized (proxyFactories) { 70 if (proxyFactories.containsKey(originalClass)) { 71 return; 72 } 73 proxyFactories.put(originalClass, factory); 74 } 75 } 76 77 public static Factory getProxyFactory(Class originalClass) { 78 synchronized (proxyFactories) { 79 return (Factory) proxyFactories.get(originalClass); 80 } 81 } 82 }