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
1ba4f4a86b
commit
7d4fabd201
@ -12,8 +12,8 @@
|
||||
|
||||
package org.dromara.hutool.core.reflect;
|
||||
|
||||
import org.dromara.hutool.core.exceptions.UtilException;
|
||||
import org.dromara.hutool.core.array.ArrayUtil;
|
||||
import org.dromara.hutool.core.exceptions.UtilException;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Member;
|
||||
@ -272,27 +272,36 @@ public class ModifierUtil {
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* <p>JDK9+此方法抛出NoSuchFieldException异常,原因是除非开放,否则模块外无法访问属性</p>
|
||||
*
|
||||
* @param field 被修改的field,不可以为空
|
||||
* @throws UtilException IllegalAccessException等异常包装
|
||||
* @author dazer
|
||||
* @since 5.8.8
|
||||
*/
|
||||
public static void removeFinalModify(final Field field) {
|
||||
if (null == field || !hasModifier(field, ModifierUtil.ModifierType.FINAL)) {
|
||||
if (!hasModifier(field, ModifierType.FINAL)) {
|
||||
return;
|
||||
}
|
||||
|
||||
//将字段的访问权限设为true:即去除private修饰符的影响
|
||||
ReflectUtil.setAccessible(field);
|
||||
|
||||
//去除final修饰符的影响,将字段设为可修改的
|
||||
final Field modifiersField;
|
||||
try{
|
||||
modifiersField = Field.class.getDeclaredField("modifiers");
|
||||
} catch (final NoSuchFieldException e){
|
||||
throw new UtilException(e, "Field [modifiers] not exist!");
|
||||
}
|
||||
|
||||
try {
|
||||
//去除final修饰符的影响,将字段设为可修改的
|
||||
final Field modifiersField = Field.class.getDeclaredField("modifiers");
|
||||
//Field 的 modifiers 是私有的
|
||||
modifiersField.setAccessible(true);
|
||||
//& :位与运算符,按位与; 运算规则:两个数都转为二进制,然后从高位开始比较,如果两个数都为1则为1,否则为0。
|
||||
//~ :位非运算符,按位取反;运算规则:转成二进制,如果位为0,结果是1,如果位为1,结果是0.
|
||||
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
|
||||
} catch (final NoSuchFieldException | IllegalAccessException e) {
|
||||
} catch (final IllegalAccessException e) {
|
||||
//内部,工具类,基本不抛出异常
|
||||
throw new UtilException(e, "IllegalAccess for [{}.{}]", field.getDeclaringClass(), field.getName());
|
||||
}
|
||||
|
@ -185,7 +185,7 @@ public class LookupUtil {
|
||||
* @return 构造方法句柄
|
||||
*/
|
||||
public static MethodHandle findConstructor(final Class<?> callerClass, final MethodType type) {
|
||||
final MethodHandles.Lookup lookup = LookupUtil.lookup(callerClass);
|
||||
final MethodHandles.Lookup lookup = lookup(callerClass);
|
||||
try {
|
||||
return lookup.findConstructor(callerClass, type);
|
||||
} catch (final NoSuchMethodException e) {
|
||||
|
@ -17,9 +17,9 @@ import org.dromara.hutool.core.collection.set.SetUtil;
|
||||
import org.dromara.hutool.core.comparator.CompareUtil;
|
||||
import org.dromara.hutool.core.comparator.StrLengthComparator;
|
||||
import org.dromara.hutool.core.convert.Convert;
|
||||
import org.dromara.hutool.core.func.SerFunction;
|
||||
import org.dromara.hutool.core.lang.Assert;
|
||||
import org.dromara.hutool.core.lang.Validator;
|
||||
import org.dromara.hutool.core.func.SerFunction;
|
||||
import org.dromara.hutool.core.lang.mutable.Mutable;
|
||||
import org.dromara.hutool.core.lang.mutable.MutableObj;
|
||||
import org.dromara.hutool.core.map.MapUtil;
|
||||
@ -247,6 +247,8 @@ public class ReUtil {
|
||||
* result : year: 2021, month: 10, day: 11
|
||||
* </pre>
|
||||
*
|
||||
* <p>jdk9+之后,因为此方法无效</p>
|
||||
*
|
||||
* @param pattern 匹配的正则
|
||||
* @param content 被匹配的内容
|
||||
* @return 命名捕获组,key为分组名,value为对应值
|
||||
@ -260,7 +262,9 @@ public class ReUtil {
|
||||
final Map<String, String> result = MapUtil.newHashMap(m.groupCount());
|
||||
if (m.find()) {
|
||||
// 通过反射获取 namedGroups 方法
|
||||
final Map<String, Integer> map = MethodUtil.invoke(pattern, "namedGroups");
|
||||
final Map<String, Integer> map =
|
||||
//MethodHandleUtil.invokeSpecial(pattern, "namedGroups");
|
||||
MethodUtil.invoke(pattern, "namedGroups");
|
||||
map.forEach((key, value) -> result.put(key, m.group(value)));
|
||||
}
|
||||
return result;
|
||||
|
@ -4,6 +4,7 @@ import org.dromara.hutool.core.reflect.FieldUtil;
|
||||
import org.dromara.hutool.core.reflect.ModifierUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.EnabledForJreRange;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
@ -26,6 +27,15 @@ public class ModifierUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnabledForJreRange(max = org.junit.jupiter.api.condition.JRE.JAVA_8)
|
||||
void removeFinalModifyTest() {
|
||||
final String fieldName = "DIALECTS";
|
||||
final Field field = FieldUtil.getField(JdbcDialects.class, fieldName);
|
||||
ModifierUtil.removeFinalModify(field);
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnabledForJreRange(max = org.junit.jupiter.api.condition.JRE.JAVA_8)
|
||||
public void setFinalFieldValueTest() {
|
||||
final String fieldName = "DIALECTS";
|
||||
final List<Number> dialects =
|
||||
|
@ -7,6 +7,7 @@ import org.dromara.hutool.core.regex.ReUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.EnabledForJreRange;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -193,6 +194,7 @@ public class ReUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnabledForJreRange(max = org.junit.jupiter.api.condition.JRE.JAVA_8)
|
||||
public void getAllGroupNamesTest() {
|
||||
final String content = "2021-10-11";
|
||||
final String regex = "(?<year>\\d+)-(?<month>\\d+)-(?<day>\\d+)";
|
||||
|
Loading…
Reference in New Issue
Block a user