Class<?> clazz = null;
try {
clazz = org.springframework.util.ClassUtils.forName(className, classLoader);
}
catch (ClassNotFoundException e) {
throw new ExecutionException("Unable to find class of type '" + className + "'");
}
Object o = null;
try {
Constructor<?> constructor = ReflectionUtils.findConstructor(clazz, new Class[0]);
if (constructor == null) {
throw new InvalidStateException("Cannot instantiate class '" + clazz + "' as it has no no-args constructor");
}
ReflectionUtils.makeAccessible(constructor);
o = constructor.newInstance();
return o;
}
catch (InvalidStateException e) {
throw e;
}
catch (ClassCastException e) {
String message = "Created object '" + o + "' is an instance of " + o.getClass().getName();
throw new ExecutionException(message, e);
}
catch (Exception e) {
String message = "Error instantiating class of type '" + className + "': " + e.getMessage();
throw new ExecutionException(message, e);
}
}