Skip to content

Commit d9e3074

Browse files
committed
add the CopyProperty for ignore field
1 parent 6cb04af commit d9e3074

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.github.thecarisma;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* Created by Adewale.Azeez on 11/25/2020
10+
*/
11+
@Retention(RetentionPolicy.RUNTIME)
12+
@Target(ElementType.FIELD)
13+
public @interface CopyProperty {
14+
boolean ignore() default false;
15+
}

src/main/java/io/github/thecarisma/ObjCopier.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.thecarisma;
22

3+
import java.lang.annotation.Annotation;
34
import java.lang.reflect.Field;
45

56
/**
@@ -82,6 +83,12 @@ private static <T> void copyFieldsInternal(String[] excepts, String[] selected,
8283
continue;
8384
}
8485
}
86+
CopyProperty copyProperty = source1Fields[index].getAnnotation(CopyProperty.class);
87+
if (copyProperty != null) {
88+
if (copyProperty.ignore()) {
89+
continue;
90+
}
91+
}
8592
try {
8693
Object[][] fields = new Object[sources.length][2];
8794
for (int counter = 0; counter < fields.length; ++counter) {
@@ -106,7 +113,7 @@ private static <T> void copyFieldsInternal(String[] excepts, String[] selected,
106113
// we trying to get as much data as possible
107114
// from the two source into the target
108115
} catch (IllegalAccessException e) {
109-
throw new FatalObjCopierException("Could not get the value of the property '" + fieldName + "' from the two source");
116+
throw new FatalObjCopierException("Could not get the value of the property '" + fieldName + "' from all the provided source");
110117
}
111118
}
112119
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package io.github.thecarisma;
2+
3+
import org.junit.Assert;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
/**
8+
* Created by Adewale.Azeez on 11/25/2020
9+
*/
10+
public class CopyPropertyTest {
11+
12+
static class TestIgnoreClass {
13+
@CopyProperty(ignore = true)
14+
long id;
15+
String name;
16+
@CopyProperty(ignore = true)
17+
String permanent;
18+
String description;
19+
}
20+
21+
TestIgnoreClass testIgnoreClass1;
22+
TestIgnoreClass testIgnoreClass2;
23+
TestIgnoreClass testIgnoreClass3;
24+
25+
@Before
26+
public void setup() {
27+
testIgnoreClass1 = new TestIgnoreClass();
28+
testIgnoreClass1.id = 2;
29+
testIgnoreClass1.name = "Thecarisma";
30+
testIgnoreClass1.permanent = "Glue";
31+
testIgnoreClass1.description = "test ignore annotation class 1";
32+
33+
testIgnoreClass2 = new TestIgnoreClass();
34+
testIgnoreClass2.id = 4;
35+
testIgnoreClass2.name = "hackers Deck";
36+
testIgnoreClass2.permanent = "Marker";
37+
testIgnoreClass2.description = "test ignore annotation class 2";
38+
39+
testIgnoreClass3 = new TestIgnoreClass();
40+
testIgnoreClass3.id = 6;
41+
testIgnoreClass3.name = "Quick Utils";
42+
testIgnoreClass3.permanent = "Adhesive";
43+
testIgnoreClass3.description = "test ignore annotation class 3";
44+
}
45+
46+
@Test
47+
public void testIgnoreDuringCopy() throws FatalObjCopierException {
48+
ObjCopier.copyFields(testIgnoreClass1, testIgnoreClass2);
49+
50+
Assert.assertNotEquals(testIgnoreClass1.id, testIgnoreClass2.id);
51+
Assert.assertEquals(testIgnoreClass1.name, testIgnoreClass2.name);
52+
Assert.assertNotEquals(testIgnoreClass1.permanent, testIgnoreClass2.permanent);
53+
Assert.assertEquals(testIgnoreClass1.description, testIgnoreClass2.description);
54+
Assert.assertFalse(ObjUtils.deepCompare(testIgnoreClass1, testIgnoreClass2));
55+
}
56+
57+
}

0 commit comments

Comments
 (0)