Search in sources :

Example 16 with UtilException

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;
}
Also used : UtilException(cn.hutool.core.exceptions.UtilException) AccessibleObject(java.lang.reflect.AccessibleObject)

Example 17 with UtilException

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);
    }
}
Also used : UtilException(cn.hutool.core.exceptions.UtilException) UtilException(cn.hutool.core.exceptions.UtilException)

Example 18 with UtilException

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);
}
Also used : UtilException(cn.hutool.core.exceptions.UtilException) Method(java.lang.reflect.Method)

Example 19 with UtilException

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);
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) UtilException(cn.hutool.core.exceptions.UtilException) IOException(java.io.IOException)

Example 20 with UtilException

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!");
    }
}
Also used : DefaultSingletonBeanRegistry(org.springframework.beans.factory.support.DefaultSingletonBeanRegistry) UtilException(cn.hutool.core.exceptions.UtilException) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Aggregations

UtilException (cn.hutool.core.exceptions.UtilException)50 IOException (java.io.IOException)19 FastByteArrayOutputStream (cn.hutool.core.io.FastByteArrayOutputStream)5 IORuntimeException (cn.hutool.core.io.IORuntimeException)5 URL (java.net.URL)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 File (java.io.File)3 ObjectInputStream (java.io.ObjectInputStream)3 PrintWriter (java.io.PrintWriter)3 AccessibleObject (java.lang.reflect.AccessibleObject)3 Method (java.lang.reflect.Method)3 MalformedURLException (java.net.MalformedURLException)3 SocketException (java.net.SocketException)3 HashSet (java.util.HashSet)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 XPathExpressionException (javax.xml.xpath.XPathExpressionException)3 ConcurrentHashSet (cn.hutool.core.collection.ConcurrentHashSet)2 BufferedInputStream (java.io.BufferedInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2