mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-24 18:04:54 +08:00
fix code
This commit is contained in:
parent
9f519f883c
commit
1ba4f4a86b
@ -13,8 +13,6 @@
|
||||
package org.dromara.hutool.core.reflect;
|
||||
|
||||
import java.lang.reflect.*;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
|
||||
/**
|
||||
* 反射工具类
|
||||
@ -51,11 +49,8 @@ public class ReflectUtil {
|
||||
*/
|
||||
public static <T extends AccessibleObject> T setAccessible(final T accessibleObject) throws SecurityException {
|
||||
if (null != accessibleObject && !accessibleObject.isAccessible()) {
|
||||
return AccessController.doPrivileged((PrivilegedAction<T>) () -> {
|
||||
// 特权访问
|
||||
accessibleObject.setAccessible(true);
|
||||
return accessibleObject;
|
||||
});
|
||||
accessibleObject.setAccessible(true);
|
||||
return accessibleObject;
|
||||
}
|
||||
return accessibleObject;
|
||||
}
|
||||
|
@ -15,8 +15,6 @@ package org.dromara.hutool.core.spi;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.security.AccessControlContext;
|
||||
import java.security.AccessController;
|
||||
|
||||
/**
|
||||
* 抽象服务加载器,提供包括路径前缀、服务类、类加载器、编码、安全相关持有
|
||||
@ -31,7 +29,6 @@ public abstract class AbsServiceLoader<S> implements ServiceLoader<S> {
|
||||
protected final Class<S> serviceClass;
|
||||
protected final ClassLoader classLoader;
|
||||
protected final Charset charset;
|
||||
protected final AccessControlContext acc;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
@ -47,6 +44,5 @@ public abstract class AbsServiceLoader<S> implements ServiceLoader<S> {
|
||||
this.serviceClass = serviceClass;
|
||||
this.classLoader = classLoader;
|
||||
this.charset = charset;
|
||||
this.acc = (System.getSecurityManager() != null) ? AccessController.getContext() : null;
|
||||
}
|
||||
}
|
||||
|
@ -20,7 +20,6 @@ import org.dromara.hutool.core.io.resource.Resource;
|
||||
import org.dromara.hutool.core.io.resource.ResourceUtil;
|
||||
import org.dromara.hutool.core.reflect.ConstructorUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.core.util.AccessUtil;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
@ -144,7 +143,12 @@ public class ListServiceLoader<S> extends AbsServiceLoader<S> {
|
||||
* @return 服务名称对应的实现类
|
||||
*/
|
||||
public Class<S> getServiceClass(final int index) {
|
||||
return AccessUtil.doPrivileged(() -> getServiceClassUnsafe(index), this.acc);
|
||||
final String serviceClassName = this.serviceNames.get(index);
|
||||
if (StrUtil.isBlank(serviceClassName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return ClassLoaderUtil.loadClass(serviceClassName);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -285,24 +289,7 @@ public class ListServiceLoader<S> extends AbsServiceLoader<S> {
|
||||
* @return 服务对象
|
||||
*/
|
||||
private S createService(final String serviceClassName) {
|
||||
return AccessUtil.doPrivileged(() ->
|
||||
ConstructorUtil.newInstance(ClassLoaderUtil.loadClass(serviceClassName)),
|
||||
this.acc);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定服务的实现类
|
||||
*
|
||||
* @param index 服务索引号
|
||||
* @return 服务名称对应的实现类
|
||||
*/
|
||||
private Class<S> getServiceClassUnsafe(final int index) {
|
||||
final String serviceClassName = this.serviceNames.get(index);
|
||||
if (StrUtil.isBlank(serviceClassName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return ClassLoaderUtil.loadClass(serviceClassName);
|
||||
return ConstructorUtil.newInstance(ClassLoaderUtil.loadClass(serviceClassName));
|
||||
}
|
||||
// endregion
|
||||
}
|
||||
|
@ -17,7 +17,6 @@ import org.dromara.hutool.core.classloader.ClassLoaderUtil;
|
||||
import org.dromara.hutool.core.io.resource.ResourceUtil;
|
||||
import org.dromara.hutool.core.reflect.ConstructorUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.core.util.AccessUtil;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.HashMap;
|
||||
@ -136,7 +135,12 @@ public class MapServiceLoader<S> extends AbsServiceLoader<S> {
|
||||
* @return 服务名称对应的实现类
|
||||
*/
|
||||
public Class<S> getServiceClass(final String serviceName) {
|
||||
return AccessUtil.doPrivileged(() -> getServiceClassUnsafe(serviceName), this.acc);
|
||||
final String serviceClassName = this.serviceProperties.getProperty(serviceName);
|
||||
if (StrUtil.isBlank(serviceClassName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return ClassLoaderUtil.loadClass(serviceClassName);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -176,23 +180,7 @@ public class MapServiceLoader<S> extends AbsServiceLoader<S> {
|
||||
* @return 服务对象
|
||||
*/
|
||||
private S createService(final String serviceName) {
|
||||
return AccessUtil.doPrivileged(() ->
|
||||
ConstructorUtil.newInstance(getServiceClassUnsafe(serviceName)), this.acc);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定服务的实现类
|
||||
*
|
||||
* @param serviceName 服务名称
|
||||
* @return 服务名称对应的实现类
|
||||
*/
|
||||
private Class<S> getServiceClassUnsafe(final String serviceName) {
|
||||
final String serviceClassName = this.serviceProperties.getProperty(serviceName);
|
||||
if (StrUtil.isBlank(serviceClassName)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return ClassLoaderUtil.loadClass(serviceClassName);
|
||||
return ConstructorUtil.newInstance(getServiceClass(serviceName));
|
||||
}
|
||||
// endregion
|
||||
}
|
||||
|
@ -370,7 +370,7 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
|
||||
public Opt<BigDecimal> avg(final Function<? super T, BigDecimal> mapper, final int scale,
|
||||
final RoundingMode roundingMode) {
|
||||
//元素列表
|
||||
List<BigDecimal> bigDecimalList = stream.map(mapper).collect(Collectors.toList());
|
||||
final List<BigDecimal> bigDecimalList = stream.map(mapper).collect(Collectors.toList());
|
||||
if (CollUtil.isEmpty(bigDecimalList)) {
|
||||
return Opt.empty();
|
||||
}
|
||||
|
@ -1,41 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2023 looly(loolly@aliyun.com)
|
||||
* Hutool is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.core.util;
|
||||
|
||||
import java.security.AccessControlContext;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
|
||||
/**
|
||||
* {@link AccessController}相关封装
|
||||
*
|
||||
* @author looly
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public class AccessUtil {
|
||||
|
||||
/**
|
||||
* @param action 执行内容
|
||||
* @param context 上下文,当为{@code null}时直接执行内容,而不检查
|
||||
* @param <T> 执行结果类型
|
||||
* @return 结果
|
||||
*/
|
||||
public static <T> T doPrivileged(final PrivilegedAction<T> action,
|
||||
final AccessControlContext context) {
|
||||
if (null == context) {
|
||||
return action.run();
|
||||
}
|
||||
|
||||
return AccessController.doPrivileged(action, context);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user