1
2
3
4
5
6
7
8
9
10
11
12
13 package org.jadira.scanner.core.utils.reflection;
14
15
16
17
18 public final class ClassLoaderUtils {
19
20 private ClassLoaderUtils() {
21 }
22
23
24
25
26
27
28
29 public static ClassLoader[] getClassLoaders(ClassLoader... classLoaders) {
30
31 if (classLoaders != null && classLoaders.length > 0) {
32 return classLoaders;
33 }
34
35 final ClassLoader ctxClassLoader = Thread.currentThread()
36 .getContextClassLoader();
37 final ClassLoader staticClassLoader = ClassLoaderUtils.class
38 .getClassLoader();
39
40 if (ctxClassLoader == null) {
41 if (staticClassLoader == null) {
42 return new ClassLoader[] {};
43 } else {
44 return new ClassLoader[] { staticClassLoader };
45 }
46 } else {
47 if (staticClassLoader == null) {
48 return new ClassLoader[] { ctxClassLoader };
49 } else {
50 return new ClassLoader[] { ctxClassLoader, staticClassLoader };
51 }
52 }
53 }
54
55
56
57
58
59
60
61
62
63 public static Class<?> classForName(String name,
64 ClassLoader... classLoaders) throws ClassNotFoundException {
65
66 ClassLoader[] cls = getClassLoaders(classLoaders);
67
68 for (ClassLoader cl : cls) {
69 try {
70 if (cl != null) {
71 return cl.loadClass(name);
72 }
73 } catch (ClassNotFoundException e) {
74
75 }
76 }
77
78 return Class.forName(name);
79 }
80 }