use of cn.hutool.core.exceptions.UtilException in project hutool by looly.
the class ReflectUtil method getFieldValue.
/**
* 获取字段值
*
* @param obj 对象,static字段则此字段为null
* @param field 字段
* @return 字段值
* @throws UtilException 包装IllegalAccessException异常
*/
public static Object getFieldValue(Object obj, Field field) throws UtilException {
if (null == field) {
return null;
}
if (obj instanceof Class) {
// 静态字段获取时对象为null
obj = null;
}
setAccessible(field);
Object result;
try {
result = field.get(obj);
} catch (IllegalAccessException e) {
throw new UtilException(e, "IllegalAccess for {}.{}", field.getDeclaringClass(), field.getName());
}
return result;
}
use of cn.hutool.core.exceptions.UtilException in project hutool by looly.
the class ReflectUtil method newInstance.
/**
* 实例化对象
*
* @param <T> 对象类型
* @param clazz 类
* @param params 构造函数参数
* @return 对象
* @throws UtilException 包装各类异常
*/
public static <T> T newInstance(Class<T> clazz, Object... params) throws UtilException {
if (ArrayUtil.isEmpty(params)) {
final Constructor<T> constructor = getConstructor(clazz);
try {
return constructor.newInstance();
} catch (Exception e) {
throw new UtilException(e, "Instance class [{}] error!", clazz);
}
}
final Class<?>[] paramTypes = ClassUtil.getClasses(params);
final Constructor<T> constructor = getConstructor(clazz, paramTypes);
if (null == constructor) {
throw new UtilException("No Constructor matched for parameter types: [{}]", new Object[] { paramTypes });
}
try {
return constructor.newInstance(params);
} catch (Exception e) {
throw new UtilException(e, "Instance class [{}] error!", clazz);
}
}
use of cn.hutool.core.exceptions.UtilException in project hutool by looly.
the class ReflectUtil method invoke.
/**
* 执行对象中指定方法
* 如果需要传递的参数为null,请使用NullWrapperBean来传递,不然会丢失类型信息
*
* @param <T> 返回对象类型
* @param obj 方法所在对象
* @param methodName 方法名
* @param args 参数列表
* @return 执行结果
* @throws UtilException IllegalAccessException包装
* @see NullWrapperBean
* @since 3.1.2
*/
public static <T> T invoke(Object obj, String methodName, Object... args) throws UtilException {
Assert.notNull(obj, "Object to get method must be not null!");
Assert.notBlank(methodName, "Method name must be not blank!");
final Method method = getMethodOfObj(obj, methodName, args);
if (null == method) {
throw new UtilException("No such method: [{}] from [{}]", methodName, obj.getClass());
}
return invoke(obj, method, args);
}
use of cn.hutool.core.exceptions.UtilException in project hutool by looly.
the class ServletUtil method write.
/**
* 返回数据给客户端
*
* @param response 响应对象{@link HttpServletResponse}
* @param in 需要返回客户端的内容
* @param bufferSize 缓存大小
*/
public static void write(HttpServletResponse response, InputStream in, int bufferSize) {
ServletOutputStream out = null;
try {
out = response.getOutputStream();
IoUtil.copy(in, out, bufferSize);
} catch (IOException e) {
throw new UtilException(e);
} finally {
IoUtil.close(out);
IoUtil.close(in);
}
}
use of cn.hutool.core.exceptions.UtilException in project hutool by looly.
the class SpringUtil method unregisterBean.
/**
* 注销bean
* <p>
* 将Spring中的bean注销,请谨慎使用
*
* @param beanName bean名称
* @author shadow
* @since 5.7.7
*/
public static void unregisterBean(String beanName) {
final ConfigurableListableBeanFactory factory = getConfigurableBeanFactory();
if (factory instanceof DefaultSingletonBeanRegistry) {
DefaultSingletonBeanRegistry registry = (DefaultSingletonBeanRegistry) factory;
registry.destroySingleton(beanName);
} else {
throw new UtilException("Can not unregister bean, the factory is not a DefaultSingletonBeanRegistry!");
}
}
Aggregations