mockit.internal.util.ClassLoad Maven / Gradle / Ivy
/*
* Copyright (c) 2006-2013 Rogério Liesenfeld
* This file is subject to the terms of the MIT license (see LICENSE.txt).
*/
package mockit.internal.util;
import java.util.*;
import java.util.concurrent.*;
import mockit.internal.state.*;
public final class ClassLoad
{
private static final ClassLoader THIS_CL = ClassLoad.class.getClassLoader();
private static final Map> LOADED_CLASSES = new ConcurrentHashMap>();
public static void registerLoadedClass(Class> aClass)
{
LOADED_CLASSES.put(aClass.getName(), aClass);
}
public static Class loadByInternalName(String internalClassName)
{
return loadClass(internalClassName.replace('/', '.'));
}
public static Class loadClass(String className)
{
Class> loadedClass = LOADED_CLASSES.get(className);
try {
if (loadedClass == null) {
loadedClass = loadClass(THIS_CL, className);
if (loadedClass == null) {
Class> testClass = TestRun.getCurrentTestClass();
loadedClass = testClass == null ? null : loadClass(testClass.getClassLoader(), className);
if (loadedClass == null) {
loadedClass = loadClass(Thread.currentThread().getContextClassLoader(), className);
if (loadedClass == null) {
throw new IllegalArgumentException("No class with name \"" + className + "\" found");
}
}
}
}
}
catch (LinkageError e) {
e.printStackTrace();
throw e;
}
//noinspection unchecked
return (Class) loadedClass;
}
private static Class> loadClass(ClassLoader loader, String className)
{
try { return Class.forName(className, true, loader); } catch (ClassNotFoundException ignore) { return null; }
}
}