final String outerName = Joiner.on(".").join(components.subList(0, length));
final String innerName = outerName + "$" + components.get(length);
return cls.loadClass(innerName);
- } else {
- throw e;
}
+
+ throw e;
}
}
}
public static Class<?> loadClassWithTCCL(final String name) throws ClassNotFoundException {
- return loadClass(Thread.currentThread().getContextClassLoader(), name);
+ final Thread thread = Thread.currentThread();
+ final ClassLoader tccl = thread.getContextClassLoader();
+ if (tccl == null) {
+ throw new ClassNotFoundException("Thread " + thread + " does not have a Context Class Loader, cannot load "
+ + name);
+ }
+ return loadClass(tccl, name);
}
- public static Class<?> tryToLoadClassWithTCCL(final String fullyQualifiedName) {
+ public static Class<?> tryToLoadClassWithTCCL(final String fullyQualifiedClassName) {
+ final Thread thread = Thread.currentThread();
+ final ClassLoader tccl = thread.getContextClassLoader();
+ if (tccl == null) {
+ LOG.debug("Thread {} does not have a Context Class Loader, not loading class {}", thread,
+ fullyQualifiedClassName);
+ return null;
+ }
+
try {
- return loadClassWithTCCL(fullyQualifiedName);
+ return loadClass(tccl, fullyQualifiedClassName);
} catch (final ClassNotFoundException e) {
- LOG.debug("Failed to load class {}", fullyQualifiedName, e);
+ LOG.debug("Failed to load class {}", fullyQualifiedClassName, e);
return null;
}
}
public static <S,G,P> Class<P> findFirstGenericArgument(final Class<S> scannedClass, final Class<G> genericType) {
- return withClassLoader(scannedClass.getClassLoader(),
- ClassLoaderUtils.findFirstGenericArgumentTask(scannedClass, genericType));
+ return withClassLoader(scannedClass.getClassLoader(), findFirstGenericArgumentTask(scannedClass, genericType));
}
+ @SuppressWarnings("unchecked")
private static <S, G, P> Supplier<Class<P>> findFirstGenericArgumentTask(final Class<S> scannedClass,
final Class<G> genericType) {
- return new Supplier<Class<P>>() {
- @Override
- @SuppressWarnings("unchecked")
- public Class<P> get() {
- final ParameterizedType augmentationGeneric = findParameterizedType(scannedClass, genericType);
- if (augmentationGeneric != null) {
- return (Class<P>) augmentationGeneric.getActualTypeArguments()[0];
- }
- return null;
+ return () -> {
+ final ParameterizedType augmentationGeneric = findParameterizedType(scannedClass, genericType);
+ if (augmentationGeneric != null) {
+ return (Class<P>) augmentationGeneric.getActualTypeArguments()[0];
}
+ return null;
};
}