Newer
Older
object-utils / src / main / java / io / ljunggren / object / utils / ObjectUtils.java
@Alex Ljunggren Alex Ljunggren on 26 Sep 2024 920 bytes Package renamed
package io.ljunggren.object.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class ObjectUtils {

    @SuppressWarnings("unchecked")
    public static <T> T trimProperties(T object) throws IllegalArgumentException, IllegalAccessException {
        if (object == null) {
            return null;
        }
        if (object instanceof String) {
            String string = ((String) object).trim();
            return (T) string;
        }
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field: fields) {
            if (field != null && field.getType() == String.class && !Modifier.isFinal(field.getModifiers())) {
                field.setAccessible(true);
                String value = (String) field.get(object);
                field.set(object, value == null ? value : value.trim());
            }
        }
        return object;
    }
    
}