/
ClassUtils.java
54 lines (47 loc) · 1.32 KB
/
ClassUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.github.houbb.ioc.util;
import com.github.houbb.heaven.annotation.CommonEager;
import com.github.houbb.heaven.util.common.ArgUtil;
import com.github.houbb.ioc.exception.IocRuntimeException;
/**
* <p> project: ioc-ClassUtils </p>
* <p> create on 2019/11/6 19:43 </p>
*
* @author Administrator
* @since 0.0.1
*/
@CommonEager
public final class ClassUtils {
/**
* 获取当前的 class loader
* @return 当前的 class loader
* @since 0.0.1
*/
public static ClassLoader currentClassLoader() {
return Thread.currentThread().getContextClassLoader();
}
/**
* 获取类信息
* @since 0.0.1
*/
public static Class getClass(final String className) {
ArgUtil.notEmpty(className, "className");
try {
return currentClassLoader().loadClass(className);
} catch (ClassNotFoundException e) {
throw new IocRuntimeException(e);
}
}
/**
* 直接根据 class 无参构造器创建实例
* @param clazz 类信息
* @return 实例
* @since 0.0.1
*/
public static Object newInstance(final Class clazz) {
try {
return clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new IocRuntimeException(e);
}
}
}