net.dreamlu.mica.core.utils.BeanUtil Java Examples

The following examples show how to use net.dreamlu.mica.core.utils.BeanUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BeanUtilTest.java    From mica with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void test1() {
	Map<String, Object> map = new HashMap<>();
	map.put("id", 1);
	map.put("name", "张三");
	map.put("six", "女");

	User user = BeanUtil.toBean(map, User.class);
	System.out.println(user);

	Object userx = BeanUtil.generator(User.class, new BeanProperty("xxxx", Boolean.class));
	System.out.println(userx);

	BeanUtil.setProperty(user, "xx", "xx");
	System.out.println(user);

	Object name = BeanUtil.getProperty(user, "name");
	Assert.assertEquals(name, "张三");
}
 
Example #2
Source File: UserCopy3Test.java    From mica-example with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
	// 设置 cglib 源码生成目录
	String sourcePath = "/Users/lcm/git/mica/mica-example/web-example/src/test/java";
	System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, sourcePath);

	// 1. 初始化 user,赋值
	User user = new User();
	user.setId(250);
	user.setName("如梦技术");
	user.setAge(30);
	user.setBirthday(LocalDateTime.now());

	// 2. 使用 mica 的 BeanUtil copy 方法
	UserVo userVo = BeanUtil.copy(user, UserVo.class);

	// 3. 打印结果:UserVo(name=如梦技术, age=30, birthday=null)
	System.out.println(userVo);
}
 
Example #3
Source File: UserCopy4Test.java    From mica-example with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
	// 设置 cglib 源码生成目录
	String sourcePath = "/Users/lcm/git/mica/mica-example/web-example/src/test/java";
	System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, sourcePath);

	// 1. 初始化 user,赋值
	User user = new User();
	user.setId(250);
	user.setName("如梦技术");
	user.setAge(30);
	user.setBirthday(LocalDateTime.now());

	// 2. 使用 mica 的 BeanUtil copyWithConvert 方法
	UserVo userVo = BeanUtil.copyWithConvert(user, UserVo.class);

	// 3. 打印结果:UserVo(name=如梦技术, age=30, birthday=19-4-30 下午10:04)
	System.out.println(userVo);
}
 
Example #4
Source File: BeanTest1.java    From mica with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) {
		List<Test1> list = new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			Test1 test1 = new Test1();
			test1.setId(i);
			test1.setName("name" + i);
			test1.setAge(18 + i);
			list.add(test1);
		}
		Class<?> clazz = Test2.class;
		TestN1 testN1 = new TestN1();
		testN1.setList(list);
		TestN2 testN2 = BeanUtil.copy(testN1, TestN2.class);
		System.out.println(testN2);

//		ResolvableType.forMethodParameter(writeMethod, 0)
//			.getGeneric(0)
//			.resolve()

	}
 
Example #5
Source File: BeanUtilTest.java    From mica with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void test4() {
	User1 user1 = new User1();
	user1.setId("1");
	user1.setName("张三");
	user1.setSix("男");

	User userx = BeanUtil.copyWithConvert(user1, User.class);
	System.out.println(userx);

	User user = new User();
	user.setXx("123123");
	user.setPhoto("www.dreamlu.net/img/1");
	BeanUtil.copy(user1, user);
	System.out.println(user);

	Assert.assertNull(user.getId());
	Assert.assertEquals("张三", user.getName());
}
 
Example #6
Source File: BeanUtilTest.java    From mica with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void test5() {
	List<User1> list = new ArrayList<>();
	for (int i = 0; i < 10; i++) {
		User1 user1 = new User1();
		user1.setId(i + "");
		user1.setName("张三" + i);
		list.add(user1);
	}
	List<User> copy1 = BeanUtil.copy(list, User.class);
	copy1.forEach(System.out::println);
	List<User> copy2 = BeanUtil.copyWithConvert(list, User.class);
	copy2.forEach(System.out::println);
}
 
Example #7
Source File: BeanUtilTest.java    From mica with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void test2() {
	Map<String, Object> map = new HashMap<>();
	map.put("id", 1);
	map.put("name", "张三");
	map.put("data", "1,2,3");
	map.put("gender", "女");

	User1 user = BeanUtil.copyWithConvert(map, User1.class);

	System.out.println(user);
	Assert.assertEquals(user.getId(), "1");
}
 
Example #8
Source File: BeanCopySimpleBenchmark.java    From mica-jmh with MIT License 5 votes vote down vote up
@Benchmark
public ToUser springBeanCopyBMillion() {
	ToUser toUser = null;
	for (int i = 0; i < 10000; i++) {
		toUser = BeanUtil.copyProperties(user, ToUser.class);
	}
	return toUser;
}
 
Example #9
Source File: BeanCopySimpleBenchmark.java    From mica-jmh with MIT License 5 votes vote down vote up
@Benchmark
public ToUser hutoolBeanCopyBMillion() {
	ToUser toUser = null;
	for (int i = 0; i < 10000; i++) {
		toUser = cn.hutool.core.bean.BeanUtil.toBean(user, ToUser.class);
	}
	return toUser;
}
 
Example #10
Source File: BeanCopySimpleBenchmark.java    From mica-jmh with MIT License 5 votes vote down vote up
@Benchmark
public ToUser micaBeanCopyBMillion() {
	ToUser toUser = null;
	for (int i = 0; i < 10000; i++) {
		toUser = BeanUtil.copy(user, ToUser.class);
	}
	return toUser;
}
 
Example #11
Source File: BeanCopyBenchmark.java    From mica-jmh with MIT License 5 votes vote down vote up
@Benchmark
public ToUser cglibBeanCopy() {
	ToUser toUser = BeanUtil.newInstance(ToUser.class);
	BeanCopier beanCopier = BeanCopier.create(FormUser.class, ToUser.class, false);
	beanCopier.copy(user, toUser, null);
	return toUser;
}
 
Example #12
Source File: BeanUtilTest.java    From mica with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void test3() {
	Map<String, String> map = new HashMap<>();
	map.put("id", "1");
	map.put("idInt", "123");
	map.put("name", "张三");
	map.put("data", "1,2,3");
	map.put("gender", "男");

	User1 user = BeanUtil.copyWithConvert(map, User1.class);

	System.out.println(user);
	Assert.assertEquals(user.getIdInt().intValue(), 123);
}
 
Example #13
Source File: BeanUtilTest.java    From mica with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void test6() {
	User user = new User();
	user.setXx("123123");
	user.setPhoto("www.dreamlu.net/img/1");
	user.setBirthday(LocalDateTime.now());
	User1 user1 = BeanUtil.copyWithConvert(user, User1.class);
	System.out.println(user1);
}
 
Example #14
Source File: BeanCopyUtilTest.java    From mica with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static <T> T copyWithConvert(Object source, Class<T> targetClazz) {
	Class<?> sourceClass = source.getClass();
	MicaBeanCopier copier = MicaBeanCopier.create(source.getClass(), targetClazz, true, false);
	T to = BeanUtil.newInstance(targetClazz);
	copier.copy(source, to, new MicaConverter(sourceClass, targetClazz));
	return to;
}
 
Example #15
Source File: MapToBeanTest.java    From mica with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) {
	// 设置 cglib 源码生成目录
	String sourcePath = BeanCopyUtilTest.class.getResource("/").getPath().split("mica-core")[0];
	System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, sourcePath + "gen_code");

	Map<String, Object> map = new HashMap<>();
	map.put("id", 1);
	map.put("name", "张三");
	map.put("six", "女");
	map.put("gender", "男");
	map.put("xx", "xx");
	map.put("xInt", 100);
	map.put("xxInt", 101);
	map.put("xLong", 10000L);

	User1 user1 = BeanUtil.copy(map, User1.class);
	System.out.println(user1);
	System.out.println(BeanUtil.toMap(user1));

	User1 userx = new User1();
	BeanUtil.copy(user1, userx);
	System.out.println(userx);

	User1 user2 = BeanUtil.copyWithConvert(map, User1.class);
	System.out.println(user2);

	User user3 = BeanUtil.copy(map, User.class);
	System.out.println(user3);

	User user4 = BeanUtil.copyWithConvert(map, User.class);
	System.out.println(user4);

	User user5 = BeanUtil.copy(user2, User.class);
	System.out.println(user5);
}
 
Example #16
Source File: BeanCopySimpleBenchmark.java    From mica-jmh with MIT License 4 votes vote down vote up
@Benchmark
public ToUser hutoolBeanCopyAOne() {
	return cn.hutool.core.bean.BeanUtil.toBean(user, ToUser.class);
}
 
Example #17
Source File: MicaBeanCopier.java    From mica with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
protected Object firstInstance(Class type) {
	return BeanUtil.newInstance(type);
}
 
Example #18
Source File: BeanConvertTest.java    From mica-jmh with MIT License 4 votes vote down vote up
@Benchmark
public ToUser MapToBeanMica() {
	return BeanUtil.copy(userMap, ToUser.class);
}
 
Example #19
Source File: BeanCopyConvertBenchmark.java    From mica-jmh with MIT License 4 votes vote down vote up
@Benchmark
public ToConvertUser micaBeanCopy() {
	return BeanUtil.copyWithConvert(user, ToConvertUser.class);
}
 
Example #20
Source File: BeanCopyMapBenchmark.java    From mica-jmh with MIT License 4 votes vote down vote up
@Benchmark
public ToUser hutoolBeanCopy() {
	return cn.hutool.core.bean.BeanUtil.toBean(userMap, ToUser.class);
}
 
Example #21
Source File: BeanCopyMapBenchmark.java    From mica-jmh with MIT License 4 votes vote down vote up
@Benchmark
public ToUser micaBeanCopy() {
	return BeanUtil.copy(userMap, ToUser.class);
}
 
Example #22
Source File: BeanUtilTest.java    From mica with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static void main(String[] args) {
	// 设置 cglib 源码生成目录
	String sourcePath = BeanUtilTest.class.getResource("/").getPath().split("mica-core")[0];
	System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, sourcePath + "gen_code");

	User1 user1 = new User1();
	user1.setId("1");
	user1.setId1(11);
	user1.setIds(new Integer[]{1, 2, 3});
	user1.setIdss(new int[]{1, 2, 3, 4, 5, 6});
	user1.setIdx(new long[]{1, 2, 3, 4, 5, 6});
	user1.setName("张三");

	BeanUtil.toMap(user1);

	User user = new User();
	user.setXx("123123");
	user.setPhoto("www.dreamlu.net/img/1");

	BeanUtil.copy(user1, user);
	System.out.println(user);

	User userx = BeanUtil.copyWithConvert(user1, User.class);
	System.out.println(userx);

	User userxx = new User();
	userxx.setXx("123123");
	userxx.setPhoto("www.dreamlu.net/img/1");
	BeanUtil.copyNonNull(user1, userxx);
	System.out.println(userxx);

	User userxxx = BeanUtil.copyWithConvert(user1, User.class);
	System.out.println(userxxx);

	UserChain userChain = BeanUtil.copy(user, UserChain.class);
	System.out.println(userChain);

	Map<String, Object> data = new HashMap<>();
	data.put("id", 1);
	UserChain userChainx = BeanUtil.copy(data, UserChain.class);
	System.out.println(userChainx);

	Map<String, Object> data1 = new HashMap<>();
	data1.put("id", 1);
	data1.put("name", 1);
	data1.put("photo", 1);
	data1.put("xx", 1);
	UserChain userChainx1 = BeanUtil.copyWithConvert(data1, UserChain.class);
	System.out.println(userChainx1);

	Map<String, Object> data2 = new HashMap<>();
	data2.put("id", 1);
	data2.put("name", "1");
	data2.put("photo", "1");
	data2.put("xx", "1");
	data2.put("a", new int[]{1, 2, 3});
	data2.put("allowNull", null);

	UserChain userChainxxxx = BeanUtil.toBean(data2, UserChain.class);
	System.out.println(userChainxxxx);
	Map<String, Object> dataxxxx = BeanUtil.toMap(userChainxxxx);
	System.out.println(dataxxxx);

	UserChain userChainNull = new UserChain();
	userChainNull.setAllowNull(10000);
	BeanUtil.copyNonNull(data2, userChainNull);
	System.out.println(userChainNull);

	UserChain userChainNonNull = new UserChain();
	userChainNonNull.setAllowNull(10000);
	BeanUtil.copy(data2, userChainNonNull);
	System.out.println(userChainNonNull);
}
 
Example #23
Source File: BeanCopySimpleBenchmark.java    From mica-jmh with MIT License 4 votes vote down vote up
@Benchmark
public ToUser springBeanCopyAOne() {
	return BeanUtil.copyProperties(user, ToUser.class);
}
 
Example #24
Source File: BeanCopyUtilTest.java    From mica with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static <T> T copy(Object source, Class<T> targetClazz) {
	MicaBeanCopier copier = MicaBeanCopier.create(source.getClass(), targetClazz, false, false);
	T to = BeanUtil.newInstance(targetClazz);
	copier.copy(source, to, null);
	return to;
}
 
Example #25
Source File: BeanCopySimpleBenchmark.java    From mica-jmh with MIT License 4 votes vote down vote up
@Benchmark
public ToUser micaBeanCopyAOne() {
	return BeanUtil.copy(user, ToUser.class);
}
 
Example #26
Source File: BeanCopyBenchmark.java    From mica-jmh with MIT License 4 votes vote down vote up
@Benchmark
public ToUser cglibMapperBeanCopy() {
	ToUser toUser = BeanUtil.newInstance(ToUser.class);
	CglibMapper.MAPPER.copy(user, toUser, null);
	return toUser;
}
 
Example #27
Source File: BeanCopyUtilTest.java    From mica with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static void main(String[] args) {
	// 设置 cglib 源码生成目录
	String sourcePath = BeanCopyUtilTest.class.getResource("/").getPath().split("mica-core")[0];
	System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, sourcePath + "gen_code");

	User1 user1 = new User1();
	user1.setId("1");
	user1.setName("张三");

	User user = new User();
	user.setXx("123123");
	user.setPhoto("www.dreamlu.net/img/1");

	copy(user1, User.class);
	System.out.println(user);

	User userx = copyWithConvert(user1, User.class);
	System.out.println(userx);

	UserChain userChain = copy(user, UserChain.class);
	System.out.println(userChain);

	Map<String, Object> data = new HashMap<>();
	data.put("id", 1);
	UserChain userChainx = copy(data, UserChain.class);
	System.out.println(userChainx);

	Map<String, Object> data1 = new HashMap<>();
	data1.put("id", 1);
	data1.put("name", 1);
	data1.put("photo", 1);
	data1.put("xx", 1);
	UserChain userChainx1 = copyWithConvert(data1, UserChain.class);
	System.out.println(userChainx1);

	Map<String, Object> data2 = new HashMap<>();
	data2.put("id", 1);
	data2.put("name", "1");
	data2.put("photo", "1");
	data2.put("xx", "1");
	data2.put("a", new int[]{1,2,3});
	data2.put("allowNull", null);

	UserChain userChainxxxx = copy(data2, UserChain.class);
	System.out.println(userChainxxxx);
	Map<String, Object> dataxxxx = BeanUtil.toMap(userChainxxxx);
	System.out.println(dataxxxx);
}
 
Example #28
Source File: BeanCopyBenchmark.java    From mica-jmh with MIT License 4 votes vote down vote up
@Benchmark
public ToUser springBeanCopy() {
	return BeanUtil.copyProperties(user, ToUser.class);
}
 
Example #29
Source File: BeanCopyBenchmark.java    From mica-jmh with MIT License 4 votes vote down vote up
@Benchmark
public ToUser hutoolBeanCopy() {
	return cn.hutool.core.bean.BeanUtil.toBean(user, ToUser.class);
}
 
Example #30
Source File: BeanCopyBenchmark.java    From mica-jmh with MIT License 4 votes vote down vote up
@Benchmark
public ToUser micaBeanCopy() {
	return BeanUtil.copy(user, ToUser.class);
}