|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ClassLoaderUtil.java | - | 0% | 0% | 0% |
|
||||||||||||||
| 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 | 0 | private ClassLoaderUtil() { |
| 18 | } | |
| 19 | ||
| 20 | /** | |
| 21 | * Load a class with a given name. | |
| 22 | * | |
| 23 | * It will try to load the class in the following order: | |
| 24 | * <ul> | |
| 25 | * <li>From Thread.currentThread().getContextClassLoader() | |
| 26 | * <li>Using the basic Class.forName() | |
| 27 | * <li>From ClassLoaderUtil.class.getClassLoader() | |
| 28 | * <li>From the callingClass.getClassLoader() | |
| 29 | * </ul> | |
| 30 | * | |
| 31 | * @param className The name of the class to load | |
| 32 | * @param callingClass The Class object of the calling object | |
| 33 | * @throws ClassNotFoundException If the class cannot be found anywhere. | |
| 34 | */ | |
| 35 | 0 | public static Class loadClass(String className, Class callingClass) throws ClassNotFoundException { |
| 36 | 0 | try { |
| 37 | 0 | return Thread.currentThread().getContextClassLoader().loadClass(className); |
| 38 | } catch (ClassNotFoundException e) { | |
| 39 | 0 | try { |
| 40 | 0 | return Class.forName(className); |
| 41 | } catch (ClassNotFoundException ex) { | |
| 42 | 0 | try { |
| 43 | 0 | return ClassLoaderUtil.class.getClassLoader().loadClass(className); |
| 44 | } catch (ClassNotFoundException exc) { | |
| 45 | 0 | return callingClass.getClassLoader().loadClass(className); |
| 46 | } | |
| 47 | } | |
| 48 | } | |
| 49 | } | |
| 50 | } |
|
||||||||||