|
@@ -33,6 +33,16 @@ import java.util.List;
|
|
|
*/
|
|
|
public final class ClassUtils {
|
|
|
|
|
|
+ private static ClassLoader systemClassLoader;
|
|
|
+
|
|
|
+ static {
|
|
|
+ try {
|
|
|
+ systemClassLoader = ClassLoader.getSystemClassLoader();
|
|
|
+ } catch (SecurityException ignored) {
|
|
|
+ // AccessControlException on Google App Engine
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private static final char PACKAGE_SEPARATOR = '.';
|
|
|
|
|
|
/**
|
|
@@ -146,15 +156,28 @@ public final class ClassUtils {
|
|
|
* @return 返回转换后的 Class
|
|
|
*/
|
|
|
public static Class<?> toClassConfident(String name) {
|
|
|
+ return toClassConfident(name, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Class<?> toClassConfident(String name, ClassLoader classLoader) {
|
|
|
try {
|
|
|
- return Resources.classForName(name);
|
|
|
+ return loadClass(name, getClassLoaders(classLoader));
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
- try {
|
|
|
- return Class.forName(name);
|
|
|
- } catch (ClassNotFoundException ex) {
|
|
|
- throw ExceptionUtils.mpe("找不到指定的class!请仅在明确确定会有 class 的时候,调用该方法", e);
|
|
|
+ throw ExceptionUtils.mpe("找不到指定的class!请仅在明确确定会有 class 的时候,调用该方法", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Class<?> loadClass(String className, ClassLoader[] classLoaders) throws ClassNotFoundException {
|
|
|
+ for (ClassLoader classLoader : classLoaders) {
|
|
|
+ if (classLoader != null) {
|
|
|
+ try {
|
|
|
+ return Class.forName(className, true, classLoader);
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
+ // ignore
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+ throw new ClassNotFoundException("Cannot find class: " + className);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -201,6 +224,7 @@ public final class ClassUtils {
|
|
|
* @see ClassLoader#getSystemClassLoader()
|
|
|
* @since 3.3.2
|
|
|
*/
|
|
|
+ @Deprecated
|
|
|
public static ClassLoader getDefaultClassLoader() {
|
|
|
ClassLoader cl = null;
|
|
|
try {
|
|
@@ -222,4 +246,13 @@ public final class ClassUtils {
|
|
|
}
|
|
|
return cl;
|
|
|
}
|
|
|
+
|
|
|
+ private static ClassLoader[] getClassLoaders(ClassLoader classLoader) {
|
|
|
+ return new ClassLoader[]{
|
|
|
+ classLoader,
|
|
|
+ Resources.getDefaultClassLoader(),
|
|
|
+ Thread.currentThread().getContextClassLoader(),
|
|
|
+ ClassUtils.class.getClassLoader(),
|
|
|
+ systemClassLoader};
|
|
|
+ }
|
|
|
}
|