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);
}
}
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;
}
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;
}
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);
}
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);
}
}
Aggregations