com.twitter.elephantbird.util.Utils Maven / Gradle / Ivy
package com.twitter.elephantbird.util;
public class Utils {
/**
* returns Class.forName(className, true, classLoader).
* Throws a RuntimeExcepiton if the class is not found.
*
* @see {@link Class#forName(String, boolean, ClassLoader)
*/
public static Class> classForName(String className, ClassLoader classLoader) {
try {
return Class.forName(className, true, classLoader);
} catch (ClassNotFoundException e) {
throw new RuntimeException("failed to load class " + className, e);
}
}
/**
* Ensures the classLoader
is 'consistent' with the original
* class loader that created existingClass
. Asserts
* classLoader.loadClass(existingClass.getName()) == existingClass
.
*
*
* If classLoader fails to load the class, this returns silently.
* Throws a RuntimeException with detailed message if the consistency
* check fails.
*
* @param existingClass
* @param classLoader
*/
public static void ensureClassLoaderConsistency(Class> existingClass,
ClassLoader classLoader) {
Class> loadedClass;
try {
loadedClass = Class.forName(existingClass.getName(), true, classLoader);
} catch (ClassNotFoundException e) {
return; // let class loading fail some where else.
}
if (!loadedClass.equals(existingClass)) {
throw new RuntimeException("The class loader is incosistent with the "
+ "class loader that initially loaded "
+ existingClass.getClass()
+ ". This can lead to various unexpected side effects.");
}
}
}