Skip to content

Commit

Permalink
v1.0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
yangtu222 committed Aug 6, 2018
1 parent ee6c22f commit 4563236
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This BeanUtils library is a Java bean copy utility with powerful functionality a
<dependency>
<groupId>com.github.yangtu222</groupId>
<artifactId>BeanUtils</artifactId>
<version>1.0.7</version>
<version>1.0.8</version>
</dependency>
~~~

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>com.github.yangtu222</groupId>
<artifactId>BeanUtils</artifactId>
<name>BeanUtils</name>
<version>1.0.7</version>
<version>1.0.8</version>
<description>BeanUtils library is a Java bean copy utility with powerful functionality and high performance.</description>
<url>https://github.com/yangtu222/BeanUtils</url>

Expand All @@ -27,7 +27,7 @@
<connection>scm:git:https://github.com/yangtu222/BeanUtils.git</connection>
<developerConnection>scm:git:https://github.com/yangtu222/BeanUtils.git</developerConnection>
<url>https://github.com/yangtu222/BeanUtils.git</url>
<tag>v1.0.7</tag>
<tag>v1.0.8</tag>
</scm>

<profiles>
Expand Down
47 changes: 27 additions & 20 deletions src/main/java/com/tuyang/beanutils/internal/dump/BeanCopyDump.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,55 +54,62 @@ public static void dumpPropertyMapping(Class<?> sourceClass, Class<?> targetClas
}

public static void dumpPropertyMapping(Class<?> sourceClass, Class<?> targetClass , Class<?> optionClass, List<BeanCopyPropertyItem> itemList) {

List<Long> stackCacheKeyList = localDumpStack.get();
if( stackCacheKeyList == null ) {
stackCacheKeyList = new ArrayList<>();
localDumpStack.set(stackCacheKeyList);
}

long cacheKey = (((long)sourceClass.hashCode() ) << 16 )+ (long) targetClass.hashCode();
if( optionClass != null ) {
cacheKey = (cacheKey <<16) + (long)optionClass.hashCode();
} else {
cacheKey = (cacheKey <<16) + (long)targetClass.hashCode();
}

if( stackCacheKeyList.contains(cacheKey) ) {
return;
}
if( stackCacheKeyList.isEmpty() ) {
logger.info("=============================================================================================");
logger.info("Dump Bean Copy Property Mapping:");
}

Integer dumpLevel = localDumpLevel.get();
if( dumpLevel == null ) {
dumpLevel = -1;
}
if(dumpLevel == -1 ) {
localDumpStack.set(new ArrayList<Long>());
}

localDumpLevel.set(++dumpLevel);

dumpPropertyMappingInternal(sourceClass, targetClass, optionClass, itemList);

localDumpLevel.set(--dumpLevel);

if(dumpLevel == -1 && localDumpStack.get().size() > 0 ) {
logger.info("=============================================================================================");
}
}

public static void dumpPropertyMappingInternal(Class<?> sourceClass, Class<?> targetClass , Class<?> optionClass, List<BeanCopyPropertyItem> itemList) {
private static void dumpPropertyMappingInternal(Class<?> sourceClass, Class<?> targetClass , Class<?> optionClass, List<BeanCopyPropertyItem> itemList ) {
if( optionClass == null )
optionClass = targetClass;

Integer dumpLevel = localDumpLevel.get();
List<Long> stackCacheKeyList = localDumpStack.get();
if( stackCacheKeyList == null ) {
stackCacheKeyList = new ArrayList<>();
localDumpStack.set(stackCacheKeyList);
}

long cacheKey = (((long)sourceClass.hashCode() ) << 16 )+ (long) targetClass.hashCode();
if( optionClass != null ) {
cacheKey = (cacheKey <<16) + (long)optionClass.hashCode();
} else {
cacheKey = (cacheKey <<16) + (long)targetClass.hashCode();
}

if( stackCacheKeyList.contains(cacheKey) ) {
return;
}
if( stackCacheKeyList.isEmpty() ) {
logger.info("=============================================================================================");
logger.info("Dump Bean Copy Property Mapping:");
}
stackCacheKeyList.add(cacheKey);
dumpPropertyMappingInternal(sourceClass, targetClass, optionClass, dumpLevel, itemList);
}

private static void dumpPropertyMappingInternal(Class<?> sourceClass, Class<?> targetClass , Class<?> optionClass, int level, List<BeanCopyPropertyItem> itemList ) {
if( optionClass == null )
optionClass = targetClass;

logger.info("---------------------------------------------------------------------------------------------");
logger.info("From: [" + sourceClass.getSimpleName() + "] To: [" + targetClass.getSimpleName() + "] Option: [" + optionClass.getSimpleName() + "]");
Expand All @@ -115,7 +122,7 @@ private static void dumpPropertyMappingInternal(Class<?> sourceClass, Class<?> t
BeanCopySource copyAnnotation = targetClass.getAnnotation(BeanCopySource.class);
features = copyAnnotation.features();
}
if( features == null ) {
if( features == null || features.length == 0) {
logger.info("CopyFeature: (NONE)");
} else {
logger.info("CopyFeature:");
Expand Down Expand Up @@ -251,7 +258,7 @@ private static void dumpPropertyMappingInternal(Class<?> sourceClass, Class<?> t
continue;
if( sourceProertyType.isEnum() || targetPropertyType.isEnum() )
continue;
dumpPropertyMappingInternal(sourceProertyType, targetPropertyType, item.optionClass, level+1, null );
dumpPropertyMappingInternal(sourceProertyType, targetPropertyType, item.optionClass, null );
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/tuyang/test/testRecursion2/FromBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.tuyang.test.testRecursion2;

public class FromBean {

private String name;
private FromBean parent;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public FromBean getParent() {
return parent;
}
public void setParent(FromBean parent) {
this.parent = parent;
}



}
33 changes: 33 additions & 0 deletions src/test/java/com/tuyang/test/testRecursion2/TestRecursion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.tuyang.test.testRecursion2;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import com.tuyang.beanutils.BeanCopyUtils;

public class TestRecursion {

private FromBean generateBean(int i) {

FromBean newBean = new FromBean();
newBean.setName("S" + i);
if(i == 0) {
newBean.setParent(null);
}
else {
FromBean parentBean = generateBean(i-1);
newBean.setParent(parentBean);
}
return newBean;
}

@Test
public void testRecursion() {
FromBean fromBean = generateBean(10);
ToBean toBean = BeanCopyUtils.copyBean(fromBean, ToBean.class);
assertEquals(toBean.getParent().getParent().getParent().getParent().getName(),
fromBean.getParent().getParent().getParent().getParent().getName());

}
}
25 changes: 25 additions & 0 deletions src/test/java/com/tuyang/test/testRecursion2/ToBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.tuyang.test.testRecursion2;

import com.tuyang.beanutils.annotation.BeanCopySource;
import com.tuyang.beanutils.annotation.CopyProperty;

@BeanCopySource(source=FromBean.class)
public class ToBean {

private String name;

@CopyProperty(optionClass=ToBean.class)
private ToBean parent;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ToBean getParent() {
return parent;
}
public void setParent(ToBean parent) {
this.parent = parent;
}
}

0 comments on commit 4563236

Please sign in to comment.