| 3 | import java.lang.reflect.Constructor; |
| 4 | |
| 5 | public final class ClassUtil { |
| 6 | |
| 7 | /** |
| 8 | * 通过反射构造类对象 |
| 9 | * |
| 10 | * @param className |
| 11 | * 反射的类名称 |
| 12 | * @param t |
| 13 | * 反射类的类型Class对象 |
| 14 | * @param args |
| 15 | * 构造参数 |
| 16 | * |
| 17 | * */ |
| 18 | @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 19 | public static <T> T instantiate(String className, Class<T> t, |
| 20 | Object... args) { |
| 21 | try { |
| 22 | Constructor constructor = (Constructor) Class.forName(className) |
| 23 | .getConstructor(ClassUtil.toClassType(args)); |
| 24 | return (T) constructor.newInstance(args); |
| 25 | } catch (Exception e) { |
| 26 | throw new IllegalArgumentException(e); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | private static Class<?>[] toClassType(Object[] args) { |
| 31 | Class<?>[] clazzs = new Class<?>[args.length]; |
| 32 | |
| 33 | for (int i = 0, length = args.length; i < length; i++) { |
| 34 | clazzs[i] = args[i].getClass(); |
| 35 | } |
| 36 | |
| 37 | return clazzs; |
| 38 | } |
| 39 | |
| 40 | } |
nothing calls this directly
no outgoing calls
no test coverage detected