ModifierUtil和ReflectUtil增加removeFinalModify

This commit is contained in:
Looly
2022-09-21 18:31:31 +08:00
parent 0941210553
commit eef7943d8a
4 changed files with 71 additions and 36 deletions

View File

@@ -74,7 +74,7 @@ public class ReflectUtilTest {
@Test
public void getFieldTest() {
// 能够获取到父类字段
Field privateField = ReflectUtil.getField(TestSubClass.class, "privateField");
final Field privateField = ReflectUtil.getField(TestSubClass.class, "privateField");
Assert.assertNotNull(privateField);
}
@@ -87,14 +87,14 @@ public class ReflectUtilTest {
@Test
public void setFieldTest() {
TestClass testClass = new TestClass();
final TestClass testClass = new TestClass();
ReflectUtil.setFieldValue(testClass, "a", "111");
Assert.assertEquals(111, testClass.getA());
}
@Test
public void invokeTest() {
TestClass testClass = new TestClass();
final TestClass testClass = new TestClass();
ReflectUtil.invoke(testClass, "setA", 10);
Assert.assertEquals(10, testClass.getA());
}
@@ -155,7 +155,7 @@ public class ReflectUtilTest {
private String n;
}
public static Method getMethodWithReturnTypeCheck(Class<?> clazz, boolean ignoreCase, String methodName, Class<?>... paramTypes) throws SecurityException {
public static Method getMethodWithReturnTypeCheck(final Class<?> clazz, final boolean ignoreCase, final String methodName, final Class<?>... paramTypes) throws SecurityException {
if (null == clazz || StrUtil.isBlank(methodName)) {
return null;
}
@@ -163,7 +163,7 @@ public class ReflectUtilTest {
Method res = null;
final Method[] methods = ReflectUtil.getMethods(clazz);
if (ArrayUtil.isNotEmpty(methods)) {
for (Method method : methods) {
for (final Method method : methods) {
if (StrUtil.equals(methodName, method.getName(), ignoreCase)
&& ClassUtil.isAllAssignableFrom(method.getParameterTypes(), paramTypes)
&& (res == null
@@ -252,22 +252,22 @@ public class ReflectUtilTest {
@Test
public void newInstanceIfPossibleTest(){
//noinspection ConstantConditions
int intValue = ReflectUtil.newInstanceIfPossible(int.class);
final int intValue = ReflectUtil.newInstanceIfPossible(int.class);
Assert.assertEquals(0, intValue);
Integer integer = ReflectUtil.newInstanceIfPossible(Integer.class);
final Integer integer = ReflectUtil.newInstanceIfPossible(Integer.class);
Assert.assertEquals(new Integer(0), integer);
Map<?, ?> map = ReflectUtil.newInstanceIfPossible(Map.class);
final Map<?, ?> map = ReflectUtil.newInstanceIfPossible(Map.class);
Assert.assertNotNull(map);
Collection<?> collection = ReflectUtil.newInstanceIfPossible(Collection.class);
final Collection<?> collection = ReflectUtil.newInstanceIfPossible(Collection.class);
Assert.assertNotNull(collection);
Week week = ReflectUtil.newInstanceIfPossible(Week.class);
final Week week = ReflectUtil.newInstanceIfPossible(Week.class);
Assert.assertEquals(Week.SUNDAY, week);
int[] intArray = ReflectUtil.newInstanceIfPossible(int[].class);
final int[] intArray = ReflectUtil.newInstanceIfPossible(int[].class);
Assert.assertArrayEquals(new int[0], intArray);
}
@@ -277,8 +277,8 @@ public class ReflectUtilTest {
}
@Test
public void setFieldValueTest() {
String fieldName = "DIALECTS";
public void setFieldValueWithFinalTest() {
final String fieldName = "DIALECTS";
final List<Number> dialects =
Arrays.asList(
1,
@@ -286,7 +286,7 @@ public class ReflectUtilTest {
3,
99
);
Field field = ReflectUtil.getField(JdbcDialects.class, fieldName);
final Field field = ReflectUtil.getField(JdbcDialects.class, fieldName);
ReflectUtil.removeFinalModify(field);
ReflectUtil.setFieldValue(JdbcDialects.class, fieldName, dialects);