mockit.internal.util.ClassLoad Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jmockit Show documentation
Show all versions of jmockit Show documentation
JMockit is a Java toolkit for developer (unit/integration) testing.
It contains mocking APIs and other tools, supporting both JUnit and TestNG.
The mocking APIs allow all kinds of Java code, without testability restrictions, to be tested
in isolation from selected dependencies.
/*
* 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; }
}
}