use of org.platformlayer.core.model.Generate in project platformlayer by platformlayer.
the class ServiceProviderBase method autoPopulate.
public void autoPopulate(Object item) throws OpsException {
Class<? extends Object> itemClass = item.getClass();
for (Field field : itemClass.getFields()) {
Generate defaultAnnotation = field.getAnnotation(Generate.class);
if (defaultAnnotation != null) {
Class<?> fieldType = field.getType();
Object value;
try {
value = field.get(item);
} catch (IllegalAccessException e) {
throw new IllegalStateException("Error getting field: " + field, e);
}
if (value == null) {
String defaultValue = defaultAnnotation.value();
if (!Strings.isNullOrEmpty(defaultValue)) {
value = defaultValue;
} else {
if (fieldType == Secret.class) {
Passwords passwords = new Passwords();
Secret secret = passwords.generateRandomPassword(12);
value = secret;
}
}
if (value != null) {
try {
field.set(item, value);
} catch (IllegalAccessException e) {
throw new IllegalStateException("Error setting field: " + field, e);
}
}
}
}
}
}