|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ClassLoaderUtil.java | - | 28.6% | 100% | 37.5% |
|
1 |
/*
|
|
2 |
* Copyright (c) 2002-2003 by OpenSymphony
|
|
3 |
* All rights reserved.
|
|
4 |
*/
|
|
5 |
package com.opensymphony.oscache.util;
|
|
6 |
|
|
7 |
|
|
8 |
/**
|
|
9 |
* <p>This code is borrowed directly from OSCore, but is duplicated
|
|
10 |
* here to avoid having to add a dependency on the entire OSCore jar.</p>
|
|
11 |
*
|
|
12 |
* <p>If much more code from OSCore is needed then it might be wiser to
|
|
13 |
* bite the bullet and add a dependency.</p>
|
|
14 |
*/
|
|
15 |
public class ClassLoaderUtil { |
|
16 |
/**
|
|
17 |
* Load a class with a given name.
|
|
18 |
*
|
|
19 |
* It will try to load the class in the following order:
|
|
20 |
* <ul>
|
|
21 |
* <li>From Thread.currentThread().getContextClassLoader()
|
|
22 |
* <li>Using the basic Class.forName()
|
|
23 |
* <li>From ClassLoaderUtil.class.getClassLoader()
|
|
24 |
* <li>From the callingClass.getClassLoader()
|
|
25 |
* </ul>
|
|
26 |
*
|
|
27 |
* @param className The name of the class to load
|
|
28 |
* @param callingClass The Class object of the calling object
|
|
29 |
* @throws ClassNotFoundException If the class cannot be found anywhere.
|
|
30 |
*/
|
|
31 | 44 |
public static Class loadClass(String className, Class callingClass) throws ClassNotFoundException { |
32 | 44 |
try {
|
33 | 44 |
return Thread.currentThread().getContextClassLoader().loadClass(className);
|
34 |
} catch (ClassNotFoundException e) {
|
|
35 | 0 |
try {
|
36 | 0 |
return Class.forName(className);
|
37 |
} catch (ClassNotFoundException ex) {
|
|
38 | 0 |
try {
|
39 | 0 |
return ClassLoaderUtil.class.getClassLoader().loadClass(className); |
40 |
} catch (ClassNotFoundException exc) {
|
|
41 | 0 |
return callingClass.getClassLoader().loadClass(className);
|
42 |
} |
|
43 |
} |
|
44 |
} |
|
45 |
} |
|
46 |
} |
|
47 |
|
|