Skip to content

Commit 1da9a2e

Browse files
jonpereiradevJonathan Pereira
authored andcommitted
Refactory.
1 parent 62d5b9a commit 1da9a2e

9 files changed

Lines changed: 83 additions & 76 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.github.jonpereiradev.diffobjects;
2+
3+
public class DiffException extends RuntimeException {
4+
5+
public DiffException(String message) {
6+
super(message);
7+
}
8+
}

src/main/java/com/github/jonpereiradev/diffobjects/DiffResults.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/main/java/com/github/jonpereiradev/diffobjects/builder/DiffBuilder.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.github.jonpereiradev.diffobjects.builder;
22

3+
import com.github.jonpereiradev.diffobjects.DiffException;
34
import com.github.jonpereiradev.diffobjects.strategy.DiffMetadata;
45
import com.github.jonpereiradev.diffobjects.strategy.DiffReflections;
56
import com.github.jonpereiradev.diffobjects.strategy.DiffStrategyType;
67
import org.apache.commons.lang.StringUtils;
78

89
import java.lang.reflect.Field;
910
import java.lang.reflect.Method;
11+
import java.lang.reflect.Modifier;
1012
import java.util.*;
1113

1214
/**
@@ -89,14 +91,19 @@ public DiffMappingBuilder mapping(String field) {
8991
* @param field name of the field that will me used to find the getter method.
9092
* @param value the nested property of the object to make the diff.
9193
* @return the instance of this mapping instance.
94+
* @throws DiffException throw if the field doesn't have a public no args method for the field.
9295
*/
9396
@Override
9497
public DiffMappingBuilder mapping(String field, String value) {
9598
Method method = DiffReflections.discoverGetter(classMap, field);
9699
DiffStrategyType diffStrategyType = DiffStrategyType.SINGLE;
97100

98101
if (method == null) {
99-
throw new IllegalArgumentException("Method " + field + " not found in class " + classMap.getName());
102+
throw new DiffException("Method " + field + " not found in class " + classMap.getName());
103+
}
104+
105+
if (!Modifier.isPublic(method.getModifiers()) || method.getParameterTypes().length > 0) {
106+
throw new DiffException("Method " + method.getName() + " must be public and no-args.");
100107
}
101108

102109
if (value != null && !value.isEmpty()) {
@@ -110,7 +117,7 @@ public DiffMappingBuilder mapping(String field, String value) {
110117
DiffMetadata diffMetadata = new DiffMetadata(value, method, diffStrategyType);
111118

112119
if (metadatas.contains(diffMetadata)) {
113-
throw new IllegalStateException("Field \"" + field + "\" already mapped in this builder.");
120+
throw new DiffException("Field \"" + field + "\" already mapped in this builder.");
114121
}
115122

116123
metadatas.add(diffMetadata);

src/main/java/com/github/jonpereiradev/diffobjects/strategy/DiffCollectionStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private <T> Collection<T> initializeCollection(Object object, Method method) {
7070
Collection<T> collection = null;
7171

7272
if (object != null) {
73-
collection = DiffReflections.invoke(method, object);
73+
collection = DiffReflections.invoke(object, method);
7474
}
7575

7676
if (collection != null && collection.isEmpty()) {

src/main/java/com/github/jonpereiradev/diffobjects/strategy/DiffDeepStrategy.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,22 @@ public <T> DiffResult<T> diff(Object before, Object after, DiffMetadata diffMeta
3131
Object afterObject = null;
3232

3333
if (before != null) {
34-
beforeObject = DiffReflections.invoke(beforeMethod, before);
34+
beforeObject = DiffReflections.invoke(before, beforeMethod);
3535
}
3636
if (after != null) {
37-
afterObject = DiffReflections.invoke(afterMethod, after);
37+
afterObject = DiffReflections.invoke(after, afterMethod);
3838
}
3939

4040
if (beforeObject != null || afterObject != null) {
4141
for (String property : diffMetadata.getValue().split(REGEX_PROPERTY_SEPARATOR)) {
4242
if (beforeObject != null) {
43-
beforeMethod = DiffReflections.discoverGetter(beforeObject, property);
44-
beforeObject = DiffReflections.invoke(beforeMethod, beforeObject);
43+
beforeMethod = DiffReflections.discoverGetter(beforeObject.getClass(), property);
44+
beforeObject = DiffReflections.invoke(beforeObject, beforeMethod);
4545
}
4646

4747
if (afterObject != null) {
48-
afterMethod = DiffReflections.discoverGetter(afterObject, property);
49-
afterObject = DiffReflections.invoke(afterMethod, afterObject);
48+
afterMethod = DiffReflections.discoverGetter(afterObject.getClass(), property);
49+
afterObject = DiffReflections.invoke(afterObject, afterMethod);
5050
}
5151
}
5252
}
Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.github.jonpereiradev.diffobjects.strategy;
22

3-
import com.github.jonpereiradev.diffobjects.DiffObjects;
3+
import com.github.jonpereiradev.diffobjects.DiffException;
44
import com.github.jonpereiradev.diffobjects.annotation.DiffMapping;
55
import com.github.jonpereiradev.diffobjects.annotation.DiffMappings;
66
import com.github.jonpereiradev.diffobjects.builder.DiffBuilder;
@@ -13,70 +13,73 @@
1313
import java.util.List;
1414
import java.util.Map;
1515
import java.util.concurrent.ConcurrentHashMap;
16-
import java.util.logging.Level;
17-
import java.util.logging.Logger;
1816

1917
/**
18+
* A class for common reflections operations.
19+
*
2020
* @author jonpereiradev@gmail.com
2121
*/
2222
public final class DiffReflections {
2323

2424
private static final Map<String, List<DiffMetadata>> CACHE_MAP = new ConcurrentHashMap<>();
2525

2626
/**
27-
* Descobre os mapeamentos de instance na classe.
27+
* Map the methods of the object that has the annotations for diff and stores in cache.
2828
*
29-
* @param diffClass classe que será analisada.
30-
* @return metadata para realizar o instance.
29+
* @param diffClass class that have the diff annotations.
30+
* @return the diff mappings of the class.
3131
*/
3232
public static List<DiffMetadata> mapAnnotations(Class<?> diffClass) {
3333
if (!CACHE_MAP.containsKey(diffClass.getName())) {
3434
DiffInstanceBuilder builder = DiffBuilder.map(diffClass);
3535

36-
try {
37-
for (Method method : diffClass.getMethods()) {
38-
if (method.isAnnotationPresent(DiffMapping.class)) {
39-
DiffMapping property = method.getAnnotation(DiffMapping.class);
40-
builder.mapper().mapping(method.getName(), property.value());
41-
} else if (method.isAnnotationPresent(DiffMappings.class)) {
42-
for (DiffMapping property : method.getAnnotation(DiffMappings.class).value()) {
43-
builder.mapper().mapping(method.getName(), property.value());
44-
}
36+
for (Method method : diffClass.getMethods()) {
37+
if (method.isAnnotationPresent(DiffMapping.class)) {
38+
DiffMapping diffMapping = method.getAnnotation(DiffMapping.class);
39+
builder.mapper().mapping(method.getName(), diffMapping.value());
40+
} else if (method.isAnnotationPresent(DiffMappings.class)) {
41+
for (DiffMapping diffMapping : method.getAnnotation(DiffMappings.class).value()) {
42+
builder.mapper().mapping(method.getName(), diffMapping.value());
4543
}
4644
}
47-
} catch (SecurityException ex) {
48-
Logger.getLogger(DiffObjects.class.getName()).log(Level.SEVERE, null, ex);
4945
}
5046

5147
CACHE_MAP.put(diffClass.getName(), builder.configuration().build());
5248
}
5349

54-
5550
return CACHE_MAP.get(diffClass.getName());
5651
}
5752

58-
public static Method discoverGetter(Object object, String property) {
59-
return discoverGetter(object.getClass(), property);
60-
}
53+
/**
54+
* Discover the public non-args method for access a field value.
55+
*
56+
* @param diffClass the class that has the getter method.
57+
* @param fieldOrMethodName the name of the field for discover or the name of the getter method.
58+
* @return the getter method to get the value.
59+
*/
60+
public static Method discoverGetter(Class<?> diffClass, String fieldOrMethodName) {
61+
String possibleAccessMethodName = fieldOrMethodName;
6162

62-
public static <T> T invoke(Method method, Object instance) {
63-
try {
64-
return (T) method.invoke(instance);
65-
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
66-
Logger.getLogger(DiffReflections.class.getName()).log(Level.SEVERE, null, ex);
63+
if (!fieldOrMethodName.startsWith("get")) {
64+
possibleAccessMethodName = "get" + StringUtils.capitalize(fieldOrMethodName);
6765
}
6866

69-
return null; // fail safe dummy
67+
return MethodUtils.getMatchingAccessibleMethod(diffClass, possibleAccessMethodName, null);
7068
}
7169

72-
public static Method discoverGetter(Class<?> diffClass, String property) {
73-
String possibleAccessMethodName = property;
74-
75-
if (!property.startsWith("get")) {
76-
possibleAccessMethodName = "get" + StringUtils.capitalize(property);
70+
/**
71+
* Calls the method for the object and returns the value.
72+
*
73+
* @param instance the object instance that have the method.
74+
* @param method the getter method to get the value.
75+
* @param <T> the type of value returned by the method.
76+
* @return the value returned by the getter method.
77+
*/
78+
public static <T> T invoke(Object instance, Method method) {
79+
try {
80+
return (T) method.invoke(instance);
81+
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
82+
throw new DiffException("Method " + method.getName() + " must be public and no-args.");
7783
}
78-
79-
return MethodUtils.getMatchingAccessibleMethod(diffClass, possibleAccessMethodName, null);
8084
}
81-
8285
}

src/main/java/com/github/jonpereiradev/diffobjects/strategy/DiffSingleStrategy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ public <T> DiffResult<T> diff(Object before, Object after, DiffMetadata diffMeta
2626
T afterValue = null;
2727

2828
if (before != null) {
29-
beforeValue = DiffReflections.invoke(diffMetadata.getMethod(), before);
29+
beforeValue = DiffReflections.invoke(before, diffMetadata.getMethod());
3030
}
3131

3232
if (after != null) {
33-
afterValue = DiffReflections.invoke(diffMetadata.getMethod(), after);
33+
afterValue = DiffReflections.invoke(after, diffMetadata.getMethod());
3434
}
3535

3636
return new DiffResult<>(beforeValue, afterValue, Objects.deepEquals(beforeValue, afterValue));

src/test/java/com/github/jonpereiradev/diffobjects/DiffObjectsWithBuilderTest.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
package com.github.jonpereiradev.diffobjects;
22

3+
import com.github.jonpereiradev.diffobjects.builder.DiffBuilder;
4+
import com.github.jonpereiradev.diffobjects.builder.DiffConfigurationBuilder;
35
import org.junit.Assert;
4-
import org.junit.Ignore;
6+
import org.junit.Before;
57
import org.junit.Test;
68

79
import java.util.List;
810

911
public class DiffObjectsWithBuilderTest {
1012

13+
private DiffConfigurationBuilder configuration;
14+
15+
@Before
16+
public void beforeTest() {
17+
configuration = DiffBuilder.map(ObjectElement.class).mapper().mapping("name").instance().configuration();
18+
}
19+
1120
@Test(expected = NullPointerException.class)
1221
public void testDiffObjectsWithNullBeforeStateMustThrowNullPointerException() {
1322
DiffObjects.diff(null, new ObjectElement(null), null);
@@ -45,11 +54,10 @@ public void testDiffObjectsWithNullConfigurationEqualsStateMustThrowNullPointerE
4554
}
4655

4756
@Test
48-
@Ignore
49-
public void testDiffObjectsWithEqualsObjectElementMustReturnDiffWithEquals() {
57+
public void testDiffObjectsWithEqualsObjectElementMustReturnResultWithEquals() {
5058
ObjectElement objectA = new ObjectElement("Object");
5159
ObjectElement objectB = new ObjectElement("Object");
52-
List<DiffResult<?>> results = DiffObjects.diff(objectA, objectB, null);
60+
List<DiffResult<?>> results = DiffObjects.diff(objectA, objectB, configuration);
5361

5462
Assert.assertFalse(results.isEmpty());
5563
Assert.assertEquals(1, results.size());
@@ -59,11 +67,10 @@ public void testDiffObjectsWithEqualsObjectElementMustReturnDiffWithEquals() {
5967
}
6068

6169
@Test
62-
@Ignore
63-
public void testDiffObjectsWithDifferentObjectElementMustReturnDiffWithDifference() {
70+
public void testDiffObjectsWithDifferentObjectElementMustReturnResultWithDifference() {
6471
ObjectElement objectA = new ObjectElement("Object A");
6572
ObjectElement objectB = new ObjectElement("Object B");
66-
List<DiffResult<?>> results = DiffObjects.diff(objectA, objectB, null);
73+
List<DiffResult<?>> results = DiffObjects.diff(objectA, objectB, configuration);
6774

6875
Assert.assertFalse(results.isEmpty());
6976
Assert.assertEquals(1, results.size());
@@ -73,20 +80,18 @@ public void testDiffObjectsWithDifferentObjectElementMustReturnDiffWithDifferenc
7380
}
7481

7582
@Test
76-
@Ignore
7783
public void testDiffObjectsWithEqualsObjectElementMustReturnEqualsTrue() {
7884
ObjectElement objectA = new ObjectElement("Object");
7985
ObjectElement objectB = new ObjectElement("Object");
8086

81-
Assert.assertTrue(DiffObjects.isEquals(objectA, objectB, null));
87+
Assert.assertTrue(DiffObjects.isEquals(objectA, objectB, configuration));
8288
}
8389

8490
@Test
85-
@Ignore
8691
public void testDiffObjectsWithEqualsObjectElementMustReturnEqualsFalse() {
8792
ObjectElement objectA = new ObjectElement("Object A");
8893
ObjectElement objectB = new ObjectElement("Object B");
8994

90-
Assert.assertFalse(DiffObjects.isEquals(objectA, objectB, null));
95+
Assert.assertFalse(DiffObjects.isEquals(objectA, objectB, configuration));
9196
}
9297
}

src/test/java/com/github/jonpereiradev/diffobjects/builder/DiffBuilderTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.jonpereiradev.diffobjects.builder;
22

33
import com.github.jonpereiradev.diffobjects.ComplexElement;
4+
import com.github.jonpereiradev.diffobjects.DiffException;
45
import com.github.jonpereiradev.diffobjects.ObjectElement;
56
import com.github.jonpereiradev.diffobjects.strategy.DiffMetadata;
67
import com.github.jonpereiradev.diffobjects.strategy.DiffStrategyType;
@@ -75,12 +76,12 @@ public void testDiffBuilderMappingFieldParent() {
7576
Assert.assertEquals("getParent", metadata.get(0).getMethod().getName());
7677
}
7778

78-
@Test(expected = IllegalStateException.class)
79+
@Test(expected = DiffException.class)
7980
public void testDiffBuilderMappingDuplicated() {
8081
DiffBuilder.map(ObjectElement.class).mapper().mapping("name").mapping("name");
8182
}
8283

83-
@Test(expected = IllegalArgumentException.class)
84+
@Test(expected = DiffException.class)
8485
public void testDiffBuilderMappingNotFound() {
8586
DiffBuilder.map(ObjectElement.class).mapper().mapping("notExists");
8687
}

0 commit comments

Comments
 (0)