/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.lang3.builder; import static org.apache.commons.lang3.reflect.FieldUtils.readField; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.reflect.FieldUtils; /** *
* Assists in implementing {@link Diffable#diff(Object)} methods. *
** All non-static, non-transient fields (including inherited fields) * of the objects to diff are discovered using reflection and compared * for differences. *
* ** To use this class, write code as follows: *
* ** public class Person implements Diffable<Person> { * String name; * int age; * boolean smoker; * ... * * public DiffResult diff(Person obj) { * // No need for null check, as NullPointerException correct if obj is null * return new ReflectionDiffBuilder(this, obj, ToStringStyle.SHORT_PREFIX_STYLE) * .build(); * } * } ** *
* The {@code ToStringStyle} passed to the constructor is embedded in the * returned {@code DiffResult} and influences the style of the * {@code DiffResult.toString()} method. This style choice can be overridden by * calling {@link DiffResult#toString(ToStringStyle)}. *
* @param* Constructs a builder for the specified objects with the specified style. *
* ** If {@code lhs == rhs} or {@code lhs.equals(rhs)} then the builder will * not evaluate any calls to {@code append(...)} and will return an empty * {@link DiffResult} when {@link #build()} is executed. *
* @param lhs * {@code this} object * @param rhs * the object to diff against * @param style * the style will use when outputting the objects, {@code null} * uses the default * @throws IllegalArgumentException * if {@code lhs} or {@code rhs} is {@code null} */ public ReflectionDiffBuilder(final T lhs, final T rhs, final ToStringStyle style) { this.left = lhs; this.right = rhs; diffBuilder = new DiffBuilder<>(lhs, rhs, style); } @Override public DiffResult