Skip to content

Commit a098864

Browse files
committed
Removed dependency between Instances and BeanPropertyAccessor
1 parent c46a3c1 commit a098864

File tree

2 files changed

+34
-37
lines changed

2 files changed

+34
-37
lines changed

src/main/java/de/danielbechler/diff/EqualsOnlyComparisonStrategy.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616

1717
package de.danielbechler.diff;
1818

19-
import de.danielbechler.util.*;
19+
import de.danielbechler.util.Assert;
20+
import de.danielbechler.util.Exceptions;
21+
22+
import java.lang.reflect.InvocationTargetException;
23+
import java.lang.reflect.Method;
2024

2125
import static de.danielbechler.util.Objects.isEqual;
2226

@@ -38,6 +42,32 @@ public EqualsOnlyComparisonStrategy(final String equalsValueProviderMethod)
3842
this.equalsValueProviderMethod = equalsValueProviderMethod;
3943
}
4044

45+
private static Object access(final Object target, final String methodName)
46+
{
47+
if (target == null)
48+
{
49+
return null;
50+
}
51+
try
52+
{
53+
final Method method = target.getClass().getMethod(methodName);
54+
method.setAccessible(true);
55+
return method.invoke(target);
56+
}
57+
catch (final NoSuchMethodException e)
58+
{
59+
throw Exceptions.escalate(e);
60+
}
61+
catch (final InvocationTargetException e)
62+
{
63+
throw Exceptions.escalate(e);
64+
}
65+
catch (final IllegalAccessException e)
66+
{
67+
throw Exceptions.escalate(e);
68+
}
69+
}
70+
4171
public void compare(final DiffNode node, final Instances instances)
4272
{
4373
if (hasEqual(instances))
@@ -54,7 +84,9 @@ private boolean hasEqual(final Instances instances)
5484
{
5585
if (equalsValueProviderMethod != null)
5686
{
57-
return instances.access(equalsValueProviderMethod).areEqual();
87+
final Object working = access(instances.getWorking(), equalsValueProviderMethod);
88+
final Object base = access(instances.getBase(), equalsValueProviderMethod);
89+
return isEqual(working, base);
5890
}
5991
else
6092
{

src/main/java/de/danielbechler/diff/Instances.java

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@
1616

1717
package de.danielbechler.diff;
1818

19-
import de.danielbechler.diff.bean.BeanPropertyAccessor;
2019
import de.danielbechler.util.Assert;
2120
import de.danielbechler.util.Classes;
2221
import de.danielbechler.util.Collections;
23-
import de.danielbechler.util.Exceptions;
2422

25-
import java.lang.reflect.Method;
2623
import java.util.Collection;
2724
import java.util.Map;
2825
import java.util.Set;
@@ -32,7 +29,6 @@
3229
/**
3330
* @author Daniel Bechler
3431
*/
35-
@SuppressWarnings({"UnusedDeclaration"})
3632
public class Instances
3733
{
3834
private final Accessor sourceAccessor;
@@ -80,27 +76,6 @@ public Accessor getSourceAccessor()
8076
return sourceAccessor;
8177
}
8278

83-
public Instances access(final String methodName)
84-
{
85-
Assert.hasText(methodName, "methodName");
86-
final Class<?> type = getType();
87-
if (type != null)
88-
{
89-
try
90-
{
91-
final Method method = type.getMethod(methodName);
92-
method.setAccessible(true);
93-
final BeanPropertyAccessor accessor = new BeanPropertyAccessor(methodName, method, null);
94-
return new Instances(accessor, accessor.get(working), accessor.get(base), accessor.get(fresh));
95-
}
96-
catch (NoSuchMethodException e)
97-
{
98-
throw Exceptions.escalate(type.getName(), e);
99-
}
100-
}
101-
return null;
102-
}
103-
10479
public Instances access(final Accessor accessor)
10580
{
10681
Assert.notNull(accessor, "accessor");
@@ -289,14 +264,4 @@ else if (sourceAccessorType != null)
289264
"Instances must either be null or have the exact same type.");
290265
}
291266

292-
public NodePath getPropertyPath(final DiffNode parentNode)
293-
{
294-
if (parentNode != null)
295-
{
296-
final NodePath parentPath = parentNode.getPath();
297-
final ElementSelector elementSelector = sourceAccessor.getElementSelector();
298-
return NodePath.startBuildingFrom(parentPath).element(elementSelector).build();
299-
}
300-
return NodePath.withRoot();
301-
}
302267
}

0 commit comments

Comments
 (0)