Repository navigation
Expand file tree
/
Copy pathClassUtil.java
More file actions
84 lines (73 loc) · 2 KB
/
Copy pathClassUtil.java
File metadata and controls
84 lines (73 loc) · 2 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package bms.core.common.util;
import java.net.URL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 类操作工具类
*
* @author huangyong
* @since 1.0
*/
public class ClassUtil {
private static final Logger logger = LoggerFactory.getLogger(ClassUtil.class);
/**
* 获取类加载器
*/
public static ClassLoader getClassLoader() {
return Thread.currentThread().getContextClassLoader();
}
/**
* 获取类路径
*/
public static String getClassPath() {
String classpath = "";
URL resource = getClassLoader().getResource("");
if (resource != null) {
classpath = resource.getPath();
}
return classpath;
}
/**
* 加载类(将自动初始化)
*/
public static Class<?> loadClass(String className) {
return loadClass(className, true);
}
/**
* 加载类
*/
public static Class<?> loadClass(String className, boolean isInitialized) {
Class<?> cls;
try {
cls = Class.forName(className, isInitialized, getClassLoader());
} catch (ClassNotFoundException e) {
logger.error("加载类出错!", e);
throw new RuntimeException(e);
}
return cls;
}
/**
* 是否为 int 类型(包括 Integer 类型)
*/
public static boolean isInt(Class<?> type) {
return type.equals(int.class) || type.equals(Integer.class);
}
/**
* 是否为 long 类型(包括 Long 类型)
*/
public static boolean isLong(Class<?> type) {
return type.equals(long.class) || type.equals(Long.class);
}
/**
* 是否为 double 类型(包括 Double 类型)
*/
public static boolean isDouble(Class<?> type) {
return type.equals(double.class) || type.equals(Double.class);
}
/**
* 是否为 String 类型
*/
public static boolean isString(Class<?> type) {
return type.equals(String.class);
}
}