1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.lang3.builder;
18
19 import java.lang.reflect.Field;
20 import java.lang.reflect.Modifier;
21 import java.util.Arrays;
22 import java.util.HashSet;
23 import java.util.Objects;
24 import java.util.Set;
25
26 import org.apache.commons.lang3.ArraySorter;
27 import org.apache.commons.lang3.ArrayUtils;
28 import org.apache.commons.lang3.ClassUtils;
29 import org.apache.commons.lang3.reflect.FieldUtils;
30 import org.apache.commons.lang3.tuple.Pair;
31
32 /**
33 * Assists in implementing {@link Diffable#diff(Object)} methods.
34 *
35 * <p>
36 * All non-static, non-transient fields (including inherited fields) of the objects to diff are discovered using reflection and compared for differences.
37 * </p>
38 *
39 * <p>
40 * To use this class, write code as follows:
41 * </p>
42 *
43 * <pre>{@code
44 * public class Person implements Diffable<Person> {
45 * String name;
46 * int age;
47 * boolean smoker;
48 * ...
49 *
50 * public DiffResult<Person> diff(Person obj) {
51 * // No need for null check, as NullPointerException correct if obj is null
52 * return ReflectionDiffBuilder.<Person>builder()
53 * .setDiffBuilder(DiffBuilder.<Person>builder()
54 * .setLeft(this)
55 * .setRight(obj)
56 * .setStyle(ToStringStyle.SHORT_PREFIX_STYLE)
57 * .build())
58 * .setExcludeFieldNames("userName", "password")
59 * .build() // -> ReflectionDiffBuilder
60 * .build(); // -> DiffResult
61 * }
62 * }
63 * }</pre>
64 *
65 * <p>
66 * The {@link ToStringStyle} passed to the constructor is embedded in the returned {@link DiffResult} and influences the style of the
67 * {@code DiffResult.toString()} method. This style choice can be overridden by calling {@link DiffResult#toString(ToStringStyle)}.
68 * </p>
69 * <p>
70 * See {@link DiffBuilder} for a non-reflection based version of this class.
71 * </p>
72 *
73 * @param <T> type of the left and right object to diff.
74 * @see Diffable
75 * @see Diff
76 * @see DiffResult
77 * @see ToStringStyle
78 * @see DiffBuilder
79 * @see AbstractBuilder#setForceAccessible(boolean)
80 * @since 3.6
81 */
82 public class ReflectionDiffBuilder<T> extends AbstractReflection implements Builder<DiffResult<T>> {
83
84 /**
85 * Constructs a new instance.
86 *
87 * @param <T> type of the left and right object.
88 * @since 3.15.0
89 */
90 public static final class Builder<T> extends AbstractBuilder<Builder<T>> {
91
92 private String[] excludeFieldNames = ArrayUtils.EMPTY_STRING_ARRAY;
93 private DiffBuilder<T> diffBuilder;
94
95 /**
96 * Constructs a new instance.
97 */
98 public Builder() {
99 // empty
100 }
101
102 /**
103 * Builds a new configured {@link ReflectionDiffBuilder}.
104 *
105 * @return A new configured {@link ReflectionDiffBuilder}.
106 */
107 public ReflectionDiffBuilder<T> build() {
108 return new ReflectionDiffBuilder<>(this);
109 }
110
111 @Override
112 public ReflectionDiffBuilder<T> get() {
113 return build();
114 }
115
116 /**
117 * Sets the DiffBuilder.
118 *
119 * @param diffBuilder The DiffBuilder.
120 * @return {@code this} instance.
121 */
122 public Builder<T> setDiffBuilder(final DiffBuilder<T> diffBuilder) {
123 this.diffBuilder = diffBuilder;
124 return this;
125 }
126
127 /**
128 * Sets field names to exclude from output. Intended for fields like {@code "password"} or {@code "lastModificationDate"}.
129 *
130 * @param excludeFieldNames field names to exclude.
131 * @return {@code this} instance.
132 */
133 public Builder<T> setExcludeFieldNames(final String... excludeFieldNames) {
134 this.excludeFieldNames = toExcludeFieldNames(excludeFieldNames);
135 return this;
136 }
137
138 }
139
140 /**
141 * A registry of objects to detect cyclical object references, avoid infinite loops, and stack overflows.
142 */
143 private static final ThreadLocal<Set<Pair<IDKey, IDKey>>> REGISTRY = ThreadLocal.withInitial(HashSet::new);
144
145 /**
146 * Constructs a new {@link Builder}.
147 *
148 * @param <T> type of the left and right object.
149 * @return A new {@link Builder}.
150 * @since 3.15.0
151 */
152 public static <T> Builder<T> builder() {
153 return new Builder<>();
154 }
155
156 /**
157 * Gets the registry of object pairs being traversed by the reflection
158 * methods in the current thread.
159 *
160 * @return Set the registry of objects being traversed
161 */
162 static Set<Pair<IDKey, IDKey>> getRegistry() {
163 return REGISTRY.get();
164 }
165
166 /**
167 * Tests whether the registry contains the given object pair.
168 * <p>
169 * Used by the reflection methods to avoid infinite loops.
170 * Objects might be swapped therefore a check is needed if the object pair
171 * is registered in the given or swapped order.
172 * </p>
173 *
174 * @param lhs {@code this} object to lookup in registry
175 * @param rhs The other object to lookup on registry
176 * @return boolean {@code true} if the registry contains the given object.
177 */
178 static boolean isRegistered(final Object lhs, final Object rhs) {
179 return isRegistered(lhs, rhs, getRegistry());
180 }
181
182 /**
183 * Registers the given object pair.
184 * Used by the reflection methods to avoid infinite loops.
185 *
186 * @param lhs {@code this} object to register
187 * @param rhs the other object to register
188 */
189 static void register(final Object lhs, final Object rhs) {
190 register(lhs, rhs, getRegistry());
191 }
192
193 private static String[] toExcludeFieldNames(final String[] excludeFieldNames) {
194 if (excludeFieldNames == null) {
195 return ArrayUtils.EMPTY_STRING_ARRAY;
196 }
197 // clone and remove nulls
198 return ArraySorter.sort(ReflectionToStringBuilder.toNoNullStringArray(excludeFieldNames));
199 }
200
201 /**
202 * Unregisters the given object pair.
203 *
204 * <p>
205 * Used by the reflection methods to avoid infinite loops.
206 * </p>
207 *
208 * @param lhs {@code this} object to unregister
209 * @param rhs the other object to unregister
210 */
211 static void unregister(final Object lhs, final Object rhs) {
212 unregister(lhs, rhs, getRegistry(), REGISTRY);
213 }
214
215 private final DiffBuilder<T> diffBuilder;
216
217 /**
218 * Field names to exclude from output. Intended for fields like {@code "password"} or {@code "lastModificationDate"}.
219 */
220 private String[] excludeFieldNames;
221
222 /**
223 * Constructs a new instance.
224 *
225 * @param builder A non-null Builder.
226 * @throws NullPointerException Thrown on null input.
227 */
228 private ReflectionDiffBuilder(final Builder<T> builder) {
229 super(Objects.requireNonNull(builder, "builder"));
230 this.diffBuilder = Objects.requireNonNull(builder.diffBuilder, "diffBuilder");
231 this.excludeFieldNames = Objects.requireNonNull(builder.excludeFieldNames, "excludeFieldNames");
232 }
233
234 /**
235 * Constructs a new instance.
236 *
237 * @param diffBuilder A non-null DiffBuilder.
238 * @param excludeFieldNames A non-null String array.
239 * @throws NullPointerException Thrown on null input.
240 */
241 private ReflectionDiffBuilder(final DiffBuilder<T> diffBuilder, final String[] excludeFieldNames) {
242 this(ReflectionDiffBuilder.<T>builder().setDiffBuilder(diffBuilder).setExcludeFieldNames(excludeFieldNames));
243 }
244
245 /**
246 * Constructs a builder for the specified objects with the specified style.
247 *
248 * <p>
249 * If {@code left == right} or {@code left.equals(right)} then the builder will not evaluate any calls to {@code append(...)} and will return an empty
250 * {@link DiffResult} when {@link #build()} is executed.
251 * </p>
252 *
253 * @param left {@code this} object.
254 * @param right The object to diff against.
255 * @param style The style will use when outputting the objects, {@code null} uses the default
256 * @throws IllegalArgumentException Thrown if {@code left} or {@code right} is {@code null}.
257 * @deprecated Use {@link Builder}.
258 */
259 @Deprecated
260 public ReflectionDiffBuilder(final T left, final T right, final ToStringStyle style) {
261 this(DiffBuilder.<T>builder().setLeft(left).setRight(right).setStyle(style).build(), ArrayUtils.EMPTY_STRING_ARRAY);
262 }
263
264 private boolean accept(final Field field) {
265 if (field.getName().indexOf(ClassUtils.INNER_CLASS_SEPARATOR_CHAR) != -1 || Modifier.isTransient(field.getModifiers())
266 || Modifier.isStatic(field.getModifiers()) || Arrays.binarySearch(excludeFieldNames, field.getName()) >= 0) {
267 // Rejected.
268 return false;
269 }
270 return !field.isAnnotationPresent(DiffExclude.class);
271 }
272
273 /**
274 * Appends fields using reflection.
275 *
276 * @throws SecurityException Thrown if an underlying accessible object's method denies the request.
277 * @see SecurityManager#checkPermission
278 */
279 private void appendFields(final Class<?> clazz) {
280 for (final Field field : FieldUtils.getAllFields(clazz)) {
281 if (accept(field)) {
282 try {
283 if (setAccessible(field)) {
284 diffBuilder.append(field.getName(), Reflection.getUnchecked(field, getLeft()), Reflection.getUnchecked(field, getRight()));
285 }
286 } catch (final RuntimeException e) {
287 // Ignored as per AccessibleObject / SecurityManager / InaccessibleObjectException
288 }
289 }
290 }
291 }
292
293 /**
294 * {@inheritDoc}
295 *
296 * @throws SecurityException Thrown if an underlying accessible object's method denies the request.
297 * @see SecurityManager#checkPermission
298 */
299 @Override
300 public DiffResult<T> build() {
301 if (getLeft() == getRight() || isRegistered(getLeft(), getRight())) {
302 return diffBuilder.build();
303 }
304 try {
305 register(getLeft(), getRight());
306 appendFields(getLeft().getClass());
307 return diffBuilder.build();
308 } finally {
309 unregister(getLeft(), getRight());
310 }
311 }
312
313 /**
314 * Gets the field names that should be excluded from the diff.
315 *
316 * @return The excludeFieldNames.
317 * @since 3.13.0
318 */
319 public String[] getExcludeFieldNames() {
320 return excludeFieldNames.clone();
321 }
322
323 private T getLeft() {
324 return diffBuilder.getLeft();
325 }
326
327 private T getRight() {
328 return diffBuilder.getRight();
329 }
330
331 /**
332 * Sets the field names to exclude.
333 *
334 * @param excludeFieldNames The field names to exclude from the diff or {@code null}.
335 * @return {@code this} instance.
336 * @since 3.13.0
337 * @deprecated Use {@link Builder#setExcludeFieldNames(String[])}.
338 */
339 @Deprecated
340 public ReflectionDiffBuilder<T> setExcludeFieldNames(final String... excludeFieldNames) {
341 this.excludeFieldNames = toExcludeFieldNames(excludeFieldNames);
342 return this;
343 }
344
345 }