Search in sources :

Example 36 with UtilException

use of cn.hutool.core.exceptions.UtilException in project hutool by looly.

the class URLUtil method completeUrl.

/**
 * 补全相对路径
 *
 * @param baseUrl      基准URL
 * @param relativePath 相对URL
 * @return 相对路径
 * @throws UtilException MalformedURLException
 */
public static String completeUrl(String baseUrl, String relativePath) {
    baseUrl = normalize(baseUrl, false);
    if (StrUtil.isBlank(baseUrl)) {
        return null;
    }
    try {
        final URL absoluteUrl = new URL(baseUrl);
        final URL parseUrl = new URL(absoluteUrl, relativePath);
        return parseUrl.toString();
    } catch (MalformedURLException e) {
        throw new UtilException(e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) UtilException(cn.hutool.core.exceptions.UtilException) URL(java.net.URL)

Example 37 with UtilException

use of cn.hutool.core.exceptions.UtilException in project hutool by looly.

the class NetUtil method localAddressList.

/**
 * 获取所有满足过滤条件的本地IP地址对象
 *
 * @param addressFilter 过滤器,null表示不过滤,获取所有地址
 * @return 过滤后的地址对象列表
 * @since 4.5.17
 */
public static LinkedHashSet<InetAddress> localAddressList(Filter<InetAddress> addressFilter) {
    Enumeration<NetworkInterface> networkInterfaces;
    try {
        networkInterfaces = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        throw new UtilException(e);
    }
    if (networkInterfaces == null) {
        throw new UtilException("Get network interface error!");
    }
    final LinkedHashSet<InetAddress> ipSet = new LinkedHashSet<>();
    while (networkInterfaces.hasMoreElements()) {
        final NetworkInterface networkInterface = networkInterfaces.nextElement();
        final Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
        while (inetAddresses.hasMoreElements()) {
            final InetAddress inetAddress = inetAddresses.nextElement();
            if (inetAddress != null && (null == addressFilter || addressFilter.accept(inetAddress))) {
                ipSet.add(inetAddress);
            }
        }
    }
    return ipSet;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SocketException(java.net.SocketException) UtilException(cn.hutool.core.exceptions.UtilException) NetworkInterface(java.net.NetworkInterface) InetAddress(java.net.InetAddress)

Example 38 with UtilException

use of cn.hutool.core.exceptions.UtilException in project hutool by looly.

the class MethodHandleUtil method findMethod.

/**
 * 查找指定方法的方法句柄<br>
 * 此方法只会查找:
 * <ul>
 *     <li>当前类的方法(包括构造方法和private方法)</li>
 *     <li>父类的方法(包括构造方法和private方法)</li>
 *     <li>当前类的static方法</li>
 * </ul>
 *
 * @param callerClass 方法所在类或接口
 * @param name        方法名称,{@code null}或者空则查找构造方法
 * @param type        返回类型和参数类型
 * @return 方法句柄 {@link MethodHandle},{@code null}表示未找到方法
 */
public static MethodHandle findMethod(Class<?> callerClass, String name, MethodType type) {
    if (StrUtil.isBlank(name)) {
        return findConstructor(callerClass, type);
    }
    MethodHandle handle = null;
    final MethodHandles.Lookup lookup = lookup(callerClass);
    try {
        handle = lookup.findVirtual(callerClass, name, type);
    } catch (IllegalAccessException | NoSuchMethodException ignore) {
    // ignore
    }
    // static方法
    if (null == handle) {
        try {
            handle = lookup.findStatic(callerClass, name, type);
        } catch (IllegalAccessException | NoSuchMethodException ignore) {
        // ignore
        }
    }
    // 特殊方法,包括构造方法、私有方法等
    if (null == handle) {
        try {
            handle = lookup.findSpecial(callerClass, name, type, callerClass);
        } catch (NoSuchMethodException ignore) {
        // ignore
        } catch (IllegalAccessException e) {
            throw new UtilException(e);
        }
    }
    return handle;
}
Also used : MethodHandles(java.lang.invoke.MethodHandles) UtilException(cn.hutool.core.exceptions.UtilException) MethodHandle(java.lang.invoke.MethodHandle)

Example 39 with UtilException

use of cn.hutool.core.exceptions.UtilException in project hutool by looly.

the class MethodHandleUtil method invokeSpecial.

/**
 * 执行接口或对象中的特殊方法(private、static等)<br>
 *
 * <pre class="code">
 *     interface Duck {
 *         default String quack() {
 *             return "Quack";
 *         }
 *     }
 *
 *     Duck duck = (Duck) Proxy.newProxyInstance(
 *         ClassLoaderUtil.getClassLoader(),
 *         new Class[] { Duck.class },
 *         MethodHandleUtil::invokeDefault);
 * </pre>
 *
 * @param <T>        返回结果类型
 * @param obj        接口的子对象或代理对象
 * @param methodName 方法名称
 * @param args       参数
 * @return 结果
 */
public static <T> T invokeSpecial(Object obj, String methodName, Object... args) {
    Assert.notNull(obj, "Object to get method must be not null!");
    Assert.notBlank(methodName, "Method name must be not blank!");
    final Method method = ReflectUtil.getMethodOfObj(obj, methodName, args);
    if (null == method) {
        throw new UtilException("No such method: [{}] from [{}]", methodName, obj.getClass());
    }
    return invokeSpecial(obj, method, args);
}
Also used : UtilException(cn.hutool.core.exceptions.UtilException) Method(java.lang.reflect.Method)

Example 40 with UtilException

use of cn.hutool.core.exceptions.UtilException in project hutool by looly.

the class StackTraceCaller method getCallerCaller.

@Override
public Class<?> getCallerCaller() {
    final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
    if (OFFSET + 2 >= stackTrace.length) {
        return null;
    }
    final String className = stackTrace[OFFSET + 2].getClassName();
    try {
        return Class.forName(className);
    } catch (ClassNotFoundException e) {
        throw new UtilException(e, "[{}] not found!", className);
    }
}
Also used : UtilException(cn.hutool.core.exceptions.UtilException)

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