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 org.jetbrains.annotations.*;
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(@NotNull Class> aClass)
{
LOADED_CLASSES.put(aClass.getName(), aClass);
}
@NotNull public static Class loadByInternalName(@NotNull String internalClassName)
{
return loadClass(internalClassName.replace('/', '.'));
}
@NotNull public static Class loadClass(@NotNull 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;
}
@Nullable public static Class> loadClass(@Nullable ClassLoader loader, @NotNull String className)
{
try { return Class.forName(className, true, loader); } catch (ClassNotFoundException ignore) { return null; }
}
}