This commit is contained in:
Looly 2023-04-18 23:58:48 +08:00
parent 1ba4f4a86b
commit 7d4fabd201
5 changed files with 33 additions and 8 deletions

View File

@ -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());
}

View File

@ -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) {

View File

@ -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;

View File

@ -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 =

View File

@ -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+)";