| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 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 |
|
| 23 |
import org.apache.commons.lang3.ArraySorter; |
| 24 |
import org.apache.commons.lang3.ArrayUtils; |
| 25 |
import org.apache.commons.lang3.ClassUtils; |
| 26 |
import org.apache.commons.lang3.reflect.FieldUtils; |
| 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 |
public class ReflectionDiffBuilder<T> implements Builder<DiffResult<T>> { |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public static final class Builder<T> { |
| 85 |
|
| 86 |
private String[] excludeFieldNames = ArrayUtils.EMPTY_STRING_ARRAY; |
| 87 |
private DiffBuilder<T> diffBuilder; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public Builder() { |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public ReflectionDiffBuilder<T> build() { |
| 102 |
return new ReflectionDiffBuilder<>(diffBuilder, excludeFieldNames); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public Builder<T> setDiffBuilder(final DiffBuilder<T> diffBuilder) { |
| 112 |
this.diffBuilder = diffBuilder; |
| 113 |
return this; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public Builder<T> setExcludeFieldNames(final String... excludeFieldNames) { |
| 123 |
this.excludeFieldNames = toExcludeFieldNames(excludeFieldNames); |
| 124 |
return this; |
| 125 |
} |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public static <T> Builder<T> builder() { |
| 137 |
return new Builder<>(); |
| 138 |
} |
| 139 |
|
| 140 |
private static String[] toExcludeFieldNames(final String[] excludeFieldNames) { |
| 141 |
if (excludeFieldNames == null) { |
| 142 |
return ArrayUtils.EMPTY_STRING_ARRAY; |
| 143 |
} |
| 144 |
|
| 145 |
return ArraySorter.sort(ReflectionToStringBuilder.toNoNullStringArray(excludeFieldNames)); |
| 146 |
} |
| 147 |
|
| 148 |
private final DiffBuilder<T> diffBuilder; |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
private String[] excludeFieldNames; |
| 154 |
|
| 155 |
private ReflectionDiffBuilder(final DiffBuilder<T> diffBuilder, final String[] excludeFieldNames) { |
| 156 |
this.diffBuilder = diffBuilder; |
| 157 |
this.excludeFieldNames = excludeFieldNames; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
@Deprecated |
| 175 |
public ReflectionDiffBuilder(final T left, final T right, final ToStringStyle style) { |
| 176 |
this(DiffBuilder.<T>builder().setLeft(left).setRight(right).setStyle(style).build(), null); |
| 177 |
} |
| 178 |
|
| 179 |
private boolean accept(final Field field) { |
| 180 |
if (field.getName().indexOf(ClassUtils.INNER_CLASS_SEPARATOR_CHAR) != -1) { |
| 181 |
return false; |
| 182 |
} |
| 183 |
if (Modifier.isTransient(field.getModifiers())) { |
| 184 |
return false; |
| 185 |
} |
| 186 |
if (Modifier.isStatic(field.getModifiers())) { |
| 187 |
return false; |
| 188 |
} |
| 189 |
if (this.excludeFieldNames != null && Arrays.binarySearch(this.excludeFieldNames, field.getName()) >= 0) { |
| 190 |
|
| 191 |
return false; |
| 192 |
} |
| 193 |
return !field.isAnnotationPresent(DiffExclude.class); |
| 194 |
} |
| 195 |
|
| 196 |
private void appendFields(final Class<?> clazz) { |
| 197 |
for (final Field field : FieldUtils.getAllFields(clazz)) { |
| 198 |
if (accept(field)) { |
| 199 |
try { |
| 200 |
diffBuilder.append(field.getName(), readField(field, getLeft()), readField(field, getRight())); |
| 201 |
} catch (final IllegalAccessException e) { |
| 202 |
|
| 203 |
|
| 204 |
throw new IllegalArgumentException("Unexpected IllegalAccessException: " + e.getMessage(), e); |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
@Override |
| 211 |
public DiffResult<T> build() { |
| 212 |
if (getLeft().equals(getRight())) { |
| 213 |
return diffBuilder.build(); |
| 214 |
} |
| 215 |
|
| 216 |
appendFields(getLeft().getClass()); |
| 217 |
return diffBuilder.build(); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public String[] getExcludeFieldNames() { |
| 227 |
return this.excludeFieldNames.clone(); |
| 228 |
} |
| 229 |
|
| 230 |
private T getLeft() { |
| 231 |
return diffBuilder.getLeft(); |
| 232 |
} |
| 233 |
|
| 234 |
private T getRight() { |
| 235 |
return diffBuilder.getRight(); |
| 236 |
} |
| 237 |
|
| 238 |
private Object readField(final Field field, final Object target) throws IllegalAccessException { |
| 239 |
return FieldUtils.readField(field, target, true); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
@Deprecated |
| 251 |
public ReflectionDiffBuilder<T> setExcludeFieldNames(final String... excludeFieldNames) { |
| 252 |
this.excludeFieldNames = toExcludeFieldNames(excludeFieldNames); |
| 253 |
return this; |
| 254 |
} |
| 255 |
|
| 256 |
} |