-
Notifications
You must be signed in to change notification settings - Fork 590
/
ClassComparer.java
212 lines (187 loc) · 8.16 KB
/
ClassComparer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright (c) 2021 The Bank of New York Mellon.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.impl.test;
import java.io.IOException;
import java.lang.reflect.Method;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.set.sorted.MutableSortedSet;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.api.tuple.Triplet;
import org.eclipse.collections.api.tuple.Twin;
import org.eclipse.collections.impl.factory.SortedSets;
import org.eclipse.collections.impl.tuple.Tuples;
import org.eclipse.collections.impl.utility.ArrayIterate;
import org.eclipse.collections.impl.utility.StringIterate;
/**
* This class will compare the method signatures between two classes. The comparison
* can be based on the following method information:
* <p><ul>
* <li> Method Name
* <li> Method Name + Parameter Types
* <li> Method Name + Return Type
* <li> Method Name + Parameter Types + Return Type
* </ul>
* <p>
* The operations that can be used to compare the method signatures of two classes are:
* <p><ul>
* <li> Intersection
* <li> Difference
* <li> Symmetric difference
* </ul>
*/
public class ClassComparer
{
private static final String SYMMETRIC_DIFFERENCE = "Symmetric Difference";
private static final String INTERSECTION = "Intersection";
private static final String DIFFERENCE = "Difference";
private final boolean includeParameterTypesInMethods;
private final boolean includeReturnTypes;
private final boolean includePackageNames;
private final Appendable appendable;
public ClassComparer()
{
this(false, false, false);
}
public ClassComparer(Appendable out)
{
this(out, false, false, false);
}
public ClassComparer(boolean includeParameterTypesInMethods, boolean includeReturnTypes, boolean includePackageNames)
{
this(System.out, includeParameterTypesInMethods, includeReturnTypes, includePackageNames);
}
public ClassComparer(
Appendable out,
boolean includeParameterTypesInMethods,
boolean includeReturnTypes,
boolean includePackageNames)
{
this.includeParameterTypesInMethods = includeParameterTypesInMethods;
this.includeReturnTypes = includeReturnTypes;
this.includePackageNames = includePackageNames;
this.appendable = out;
}
public Triplet<MutableSortedSet<String>> compare(Class<?> leftClass, Class<?> rightClass)
{
MutableSortedSet<String> intersection = this.intersect(leftClass, rightClass);
MutableSortedSet<String> differenceLeft = this.difference(leftClass, rightClass);
MutableSortedSet<String> differenceRight = this.difference(rightClass, leftClass);
return Tuples.triplet(intersection, differenceLeft, differenceRight);
}
public MutableSortedSet<String> difference(Class<?> leftClass, Class<?> rightClass)
{
MutableSortedSet<String> leftMethods = this.getMethodNames(leftClass);
MutableSortedSet<String> rightMethods = this.getMethodNames(rightClass);
return leftMethods.difference(rightMethods);
}
public MutableSortedSet<String> printDifference(Class<?> leftClass, Class<?> rightClass)
{
MutableSortedSet<String> difference = this.difference(leftClass, rightClass);
this.output(Tuples.twin(leftClass, rightClass), DIFFERENCE, difference);
return difference;
}
public MutableSortedSet<String> symmetricDifference(Class<?> leftClass, Class<?> rightClass)
{
MutableSortedSet<String> leftMethods = this.getMethodNames(leftClass);
MutableSortedSet<String> rightMethods = this.getMethodNames(rightClass);
return leftMethods.symmetricDifference(rightMethods);
}
public MutableSortedSet<String> printSymmetricDifference(Class<?> leftClass, Class<?> rightClass)
{
MutableSortedSet<String> symmetricDifference = this.symmetricDifference(leftClass, rightClass);
this.output(Tuples.twin(leftClass, rightClass), SYMMETRIC_DIFFERENCE, symmetricDifference);
return symmetricDifference;
}
public MutableSortedSet<String> intersect(Class<?> leftClass, Class<?> rightClass)
{
MutableSortedSet<String> leftMethods = this.getMethodNames(leftClass);
MutableSortedSet<String> rightMethods = this.getMethodNames(rightClass);
return leftMethods.intersect(rightMethods);
}
public MutableSortedSet<String> printIntersection(Class<?> leftClass, Class<?> rightClass)
{
MutableSortedSet<String> intersect = this.intersect(leftClass, rightClass);
this.output(Tuples.twin(leftClass, rightClass), INTERSECTION, intersect);
return intersect;
}
public MutableSortedSet<String> printClass(Class<?> clazz)
{
MutableSortedSet<String> methodNames = this.getMethodNames(clazz);
this.outputTitle("Class: " + (this.includePackageNames ? clazz.getName() : clazz.getSimpleName()));
this.outputGroupByToString(methodNames);
return methodNames;
}
public Triplet<MutableSortedSet<String>> compareAndPrint(Class<?> leftClass, Class<?> rightClass)
{
Triplet<MutableSortedSet<String>> compare = this.compare(leftClass, rightClass);
Twin<Class<?>> classPair = Tuples.twin(leftClass, rightClass);
this.output(classPair, INTERSECTION, compare.getOne());
this.output(classPair, DIFFERENCE, compare.getTwo());
this.output(Tuples.twin(rightClass, leftClass), DIFFERENCE, compare.getThree());
return compare;
}
private void output(Twin<Class<?>> classPair, String operation, RichIterable<String> strings)
{
this.outputTitle(operation + " (" + this.classNames(classPair) + ")");
this.outputGroupByToString(strings);
}
private String classNames(Twin<Class<?>> classPair)
{
Class<?> left = classPair.getOne();
Class<?> right = classPair.getTwo();
return (this.includePackageNames ? left.getName() : left.getSimpleName())
+ ", "
+ (this.includePackageNames ? right.getName() : right.getSimpleName());
}
public MutableSortedSet<String> getMethodNames(Class<?> classOne)
{
return ArrayIterate.collect(classOne.getMethods(), this::methodName, SortedSets.mutable.empty());
}
private String methodName(Method method)
{
String methodNamePlusParams = this.includeParameterTypesInMethods
? method.getName() + "(" + this.parameterNames(method) + ")"
: method.getName();
return methodNamePlusParams + (this.includeReturnTypes ? ":" + method.getReturnType().getSimpleName() : "");
}
private String parameterNames(Method method)
{
return ArrayIterate.collect(method.getParameters(), parm -> parm.getType().getSimpleName())
.makeString();
}
private void outputTitle(String title)
{
try
{
this.appendable.append(title).append(System.lineSeparator());
this.appendable.append(StringIterate.repeat('-', title.length())).append(System.lineSeparator());
}
catch (IOException e)
{
throw new RuntimeException("MethodComparer error in outputTitle", e);
}
}
private void outputGroupByToString(RichIterable<String> methods)
{
String output = methods.groupBy(string -> string.charAt(0))
.keyMultiValuePairsView()
.toSortedListBy(Pair::getOne)
.makeString(System.lineSeparator());
try
{
this.appendable.append(output).append(System.lineSeparator());
this.appendable.append(System.lineSeparator());
}
catch (IOException e)
{
throw new RuntimeException("MethodComparer error in outputGroupByToString", e);
}
}
}