Provides extensions to Apache Commons Lang for obfuscating objects.
Currently it has one extension: an obfuscating ToStringStyle. This can obfuscate fields when used with ToStringBuilder or similar classes.
To create an obfuscating ToStringStyle
, use one of the factory methods of class
ObfuscatingToStringStyle to create a builder, add fields, and build the result. For instance, using the default style:
ToStringStyle style = ObfuscatingToStringStyle.defaultStyle()
.withField("password", Obfuscator.fixedLength(3))
.build();
Most of the styles available in Apache Commons Lang 3 are available, including the recursive and multi-line recursive style. The only style that has been omitted is the JSON toString style.
Most of the styles available in Apache Commons Lang 3 are all immutable. The same cannot be said for the obfuscating styles, they are not immutable and not thread-safe. Reusing the same instance should not be done concurrently (reusing it in the same thread should be possible).
However, it is possible to create immutable suppliers instead:
Supplier<? extends ToStringStyle> styleSupplier = ObfuscatingToStringStyle.defaultStyle()
.withField("password", Obfuscator.fixedLength(3))
.supplier();
The difference between using build()
and using supplier()
is that supplier()
is more light-weight if you need to create multiple styles with the same settings. Such a supplier can be used directly or using ThreadLocal
. For instance, in a class:
private static final Supplier<? extends ToStringStyle> TO_STRING_STYLE = ObfuscatingToStringStyle.defaultStyle()
...
.supplier();
...
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, TO_STRING_STYLE.get());
}
Obfuscating ToStringStyle
instances are serializable if the obfuscators they use are. This most often means that they are not serializable, even though most ToStringStyle
implementations are.
See examples for some examples. These include both an example that simply reuses ObfuscatingToStringStyle.Builder, and one that provides a builder with more properties.