mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
package cn.hutool.core.compiler;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.reflect.ReflectUtil;
|
||||
import cn.hutool.core.compress.ZipUtil;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.reflect.ConstructorUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -36,7 +36,7 @@ public class JavaSourceCompilerTest {
|
||||
// .addLibrary(FileUtil.file("D:\\m2_repo\\cn\\hutool\\hutool-all\\5.5.7\\hutool-all-5.5.7.jar"))
|
||||
.compile();
|
||||
final Class<?> clazz = classLoader.loadClass("c.C");
|
||||
final Object obj = ReflectUtil.newInstance(clazz);
|
||||
final Object obj = ConstructorUtil.newInstance(clazz);
|
||||
Assert.assertTrue(String.valueOf(obj).startsWith("c.C@"));
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,39 @@
|
||||
package cn.hutool.core.reflect;
|
||||
|
||||
import cn.hutool.core.date.Week;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
public class ConstructorUtilTest {
|
||||
@Test
|
||||
public void noneStaticInnerClassTest() {
|
||||
final ReflectUtilTest.NoneStaticClass testAClass = ConstructorUtil.newInstanceIfPossible(ReflectUtilTest.NoneStaticClass.class);
|
||||
Assert.assertNotNull(testAClass);
|
||||
Assert.assertEquals(2, testAClass.getA());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newInstanceIfPossibleTest(){
|
||||
//noinspection ConstantConditions
|
||||
final int intValue = ConstructorUtil.newInstanceIfPossible(int.class);
|
||||
Assert.assertEquals(0, intValue);
|
||||
|
||||
final Integer integer = ConstructorUtil.newInstanceIfPossible(Integer.class);
|
||||
Assert.assertEquals(new Integer(0), integer);
|
||||
|
||||
final Map<?, ?> map = ConstructorUtil.newInstanceIfPossible(Map.class);
|
||||
Assert.assertNotNull(map);
|
||||
|
||||
final Collection<?> collection = ConstructorUtil.newInstanceIfPossible(Collection.class);
|
||||
Assert.assertNotNull(collection);
|
||||
|
||||
final Week week = ConstructorUtil.newInstanceIfPossible(Week.class);
|
||||
Assert.assertEquals(Week.SUNDAY, week);
|
||||
|
||||
final int[] intArray = ConstructorUtil.newInstanceIfPossible(int[].class);
|
||||
Assert.assertArrayEquals(new int[0], intArray);
|
||||
}
|
||||
}
|
@@ -0,0 +1,42 @@
|
||||
package cn.hutool.core.reflect;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
public class FieldUtilTest {
|
||||
@Test
|
||||
public void getFieldTest() {
|
||||
// 能够获取到父类字段
|
||||
final Field privateField = FieldUtil.getField(ReflectUtilTest.TestSubClass.class, "privateField");
|
||||
Assert.assertNotNull(privateField);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getFieldsTest() {
|
||||
// 能够获取到父类字段
|
||||
final Field[] fields = FieldUtil.getFields(ReflectUtilTest.TestSubClass.class);
|
||||
Assert.assertEquals(4, fields.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setFieldTest() {
|
||||
final ReflectUtilTest.AClass testClass = new ReflectUtilTest.AClass();
|
||||
FieldUtil.setFieldValue(testClass, "a", "111");
|
||||
Assert.assertEquals(111, testClass.getA());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDeclaredField() {
|
||||
final Field noField = FieldUtil.getField(ReflectUtilTest.TestSubClass.class, "noField");
|
||||
Assert.assertNull(noField);
|
||||
|
||||
// 获取不到父类字段
|
||||
final Field field = FieldUtil.getDeClearField(ReflectUtilTest.TestSubClass.class, "field");
|
||||
Assert.assertNull(field);
|
||||
|
||||
final Field subField = FieldUtil.getField(ReflectUtilTest.TestSubClass.class, "subField");
|
||||
Assert.assertNotNull(subField);
|
||||
}
|
||||
}
|
@@ -1,13 +1,6 @@
|
||||
package cn.hutool.core.reflect;
|
||||
|
||||
import cn.hutool.core.date.Week;
|
||||
import lombok.Data;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 反射工具类单元测试
|
||||
@@ -16,34 +9,6 @@ import java.util.Map;
|
||||
*/
|
||||
public class ReflectUtilTest {
|
||||
|
||||
@Test
|
||||
public void getFieldTest() {
|
||||
// 能够获取到父类字段
|
||||
final Field privateField = ReflectUtil.getField(TestSubClass.class, "privateField");
|
||||
Assert.assertNotNull(privateField);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getFieldsTest() {
|
||||
// 能够获取到父类字段
|
||||
final Field[] fields = ReflectUtil.getFields(TestSubClass.class);
|
||||
Assert.assertEquals(4, fields.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setFieldTest() {
|
||||
final AClass testClass = new AClass();
|
||||
ReflectUtil.setFieldValue(testClass, "a", "111");
|
||||
Assert.assertEquals(111, testClass.getA());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noneStaticInnerClassTest() {
|
||||
final NoneStaticClass testAClass = ReflectUtil.newInstanceIfPossible(NoneStaticClass.class);
|
||||
Assert.assertNotNull(testAClass);
|
||||
Assert.assertEquals(2, testAClass.getA());
|
||||
}
|
||||
|
||||
@Data
|
||||
static class AClass {
|
||||
private int a;
|
||||
@@ -117,41 +82,6 @@ public class ReflectUtilTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void newInstanceIfPossibleTest(){
|
||||
//noinspection ConstantConditions
|
||||
final int intValue = ReflectUtil.newInstanceIfPossible(int.class);
|
||||
Assert.assertEquals(0, intValue);
|
||||
|
||||
final Integer integer = ReflectUtil.newInstanceIfPossible(Integer.class);
|
||||
Assert.assertEquals(new Integer(0), integer);
|
||||
|
||||
final Map<?, ?> map = ReflectUtil.newInstanceIfPossible(Map.class);
|
||||
Assert.assertNotNull(map);
|
||||
|
||||
final Collection<?> collection = ReflectUtil.newInstanceIfPossible(Collection.class);
|
||||
Assert.assertNotNull(collection);
|
||||
|
||||
final Week week = ReflectUtil.newInstanceIfPossible(Week.class);
|
||||
Assert.assertEquals(Week.SUNDAY, week);
|
||||
|
||||
final int[] intArray = ReflectUtil.newInstanceIfPossible(int[].class);
|
||||
Assert.assertArrayEquals(new int[0], intArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getDeclaredField() {
|
||||
final Field noField = ReflectUtil.getField(TestSubClass.class, "noField");
|
||||
Assert.assertNull(noField);
|
||||
|
||||
// 获取不到父类字段
|
||||
final Field field = ReflectUtil.getDeClearField(TestSubClass.class, "field");
|
||||
Assert.assertNull(field);
|
||||
|
||||
final Field subField = ReflectUtil.getField(TestSubClass.class, "subField");
|
||||
Assert.assertNotNull(subField);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
static class TestClass {
|
||||
private String privateField;
|
||||
|
@@ -1,7 +1,7 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import cn.hutool.core.reflect.FieldUtil;
|
||||
import cn.hutool.core.reflect.MethodUtil;
|
||||
import cn.hutool.core.reflect.ReflectUtil;
|
||||
import cn.hutool.core.reflect.TypeUtil;
|
||||
import lombok.Data;
|
||||
import org.junit.Assert;
|
||||
@@ -65,7 +65,7 @@ public class TypeUtilTest {
|
||||
public void getActualTypesTest(){
|
||||
// 测试多层级泛型参数是否能获取成功
|
||||
final Type idType = TypeUtil.getActualType(Level3.class,
|
||||
ReflectUtil.getField(Level3.class, "id"));
|
||||
FieldUtil.getField(Level3.class, "id"));
|
||||
|
||||
Assert.assertEquals(Long.class, idType);
|
||||
}
|
||||
|
Reference in New Issue
Block a user