forked from haixyyz/halo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d1b53a0
commit 2bf7789
Showing
3 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<groupId>org.xujin.halo</groupId> | ||
<artifactId>halo</artifactId> | ||
<version>1.0.4</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>halo-utils</artifactId> | ||
<version>1.0.4</version> | ||
<name>halo-utils</name> | ||
|
||
<dependencies> | ||
|
||
<!--bean copy--> | ||
<dependency> | ||
<groupId>ma.glasnost.orika</groupId> | ||
<artifactId>orika-core</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
|
||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-source-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<id>attach-sources</id> | ||
<phase>verify</phase> | ||
<goals> | ||
<goal>jar-no-fork</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
90 changes: 90 additions & 0 deletions
90
halo-utils/src/main/java/org/xujin/halo/utils/reflect/BeanMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package org.xujin.halo.utils.reflect; | ||
|
||
import java.util.List; | ||
|
||
import ma.glasnost.orika.MapperFacade; | ||
import ma.glasnost.orika.MapperFactory; | ||
import ma.glasnost.orika.impl.DefaultMapperFactory; | ||
import ma.glasnost.orika.metadata.Type; | ||
import ma.glasnost.orika.metadata.TypeFactory; | ||
|
||
/** | ||
* @author xujin</br> | ||
* 简单封装orika, 实现深度的BeanOfClasssA<->BeanOfClassB复制 不要是用Apache Common | ||
* BeanUtils进行类复制,每次就行反射查询对象的属性列表, 非常缓慢. | ||
*/ | ||
public class BeanMapper { | ||
|
||
private static MapperFacade mapper; | ||
|
||
static { | ||
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build(); | ||
mapper = mapperFactory.getMapperFacade(); | ||
} | ||
|
||
/** | ||
* 简单的复制出新类型对象. | ||
* | ||
* 通过source.getClass() 获得源Class | ||
*/ | ||
public static <S, D> D map(S source, Class<D> destinationClass) { | ||
return mapper.map(source, destinationClass); | ||
} | ||
|
||
/** | ||
* 极致性能的复制出新类型对象. | ||
* | ||
* 预先通过BeanMapper.getType() 静态获取并缓存Type类型,在此处传入 | ||
*/ | ||
public static <S, D> D map(S source, Type<S> sourceType, Type<D> destinationType) { | ||
return mapper.map(source, sourceType, destinationType); | ||
} | ||
|
||
/** | ||
* 简单的复制出新对象列表到ArrayList | ||
* | ||
* 不建议使用mapper.mapAsList(Iterable<S>,Class<D>)接口, sourceClass需要反射,实在有点慢 | ||
*/ | ||
public static <S, D> List<D> mapList(Iterable<S> sourceList, Class<S> sourceClass, | ||
Class<D> destinationClass) { | ||
return mapper.mapAsList(sourceList, TypeFactory.valueOf(sourceClass), | ||
TypeFactory.valueOf(destinationClass)); | ||
} | ||
|
||
/** | ||
* 极致性能的复制出新类型对象到ArrayList. | ||
* | ||
* 预先通过BeanMapper.getType() 静态获取并缓存Type类型,在此处传入 | ||
*/ | ||
public static <S, D> List<D> mapList(Iterable<S> sourceList, Type<S> sourceType, | ||
Type<D> destinationType) { | ||
return mapper.mapAsList(sourceList, sourceType, destinationType); | ||
} | ||
|
||
/** | ||
* 简单复制出新对象列表到数组 | ||
* | ||
* 通过source.getComponentType() 获得源Class | ||
*/ | ||
public static <S, D> D[] mapArray(final D[] destination, final S[] source, | ||
final Class<D> destinationClass) { | ||
return mapper.mapAsArray(destination, source, destinationClass); | ||
} | ||
|
||
/** | ||
* 极致性能的复制出新类型对象到数组 | ||
* | ||
* 预先通过BeanMapper.getType() 静态获取并缓存Type类型,在此处传入 | ||
*/ | ||
public static <S, D> D[] mapArray(D[] destination, S[] source, Type<S> sourceType, | ||
Type<D> destinationType) { | ||
return mapper.mapAsArray(destination, source, sourceType, destinationType); | ||
} | ||
|
||
/** | ||
* 预先获取orika转换所需要的Type,避免每次转换. | ||
*/ | ||
public static <E> Type<E> getType(final Class<E> rawType) { | ||
return TypeFactory.valueOf(rawType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters