Is there an (easy) way to specify default values for LDAPField annotated variables? For example, I don't want the list myNumericAttr
to be null if no values were found but instead would like it to hold an empty list.
import com.unboundid.ldap.sdk.persist.LDAPField;
import com.unboundid.ldap.sdk.persist.LDAPObject;
@LDAPObject(structuralClass="myStructuralClass")
public class MyObject
{
@LDAPField(attribute="myStringAttr")
private String myStringAttr;
@LDAPField(attribute="myNumericAttr")
private List<Long> myNumericAttr;
}
As a workaround I could implement a postDecodeMethod
myself but that would result in a lot of code.
@SuppressWarnings("unused")
private void doPostDecode() throws LDAPPersistException, IllegalArgumentException, IllegalAccessException
{
for (Field field : this.getClass().getDeclaredFields())
{
if(field.isAnnotationPresent(LDAPField.class))
{
// check if field value is null
if (field.get(this) == null)
{
String fieldType = field.getType().getName();
log.info("field type: {}", fieldType);
if (fieldType.equals("java.lang.String"))
{
field.set(this, "");
}
else if (fieldType.equals("java.util.List"))
{
// Find out the type of list we are dealing with
ParameterizedType listGenericType = (ParameterizedType) field.getGenericType();
Class<?> listActualType = (Class<?>) listGenericType.getActualTypeArguments()[0];
log.debug("actual type of list: {}", listActualType.getName());
field.set(this, getModel(listActualType));
}
}
}
}
}
private <T> ArrayList<T> getModel(Class<T> type) {
ArrayList<T> arrayList = new ArrayList<T>();
return arrayList;
}
So my question is, did I miss some feature or is implementing your own postDecodeMethod
the only possibility at the moment?