Posts

Showing posts from June, 2023

Map class field to map

 import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; public class AutogeneratedClassMapper {     public static Map<String, Object> mapFieldsToMap(Object obj) throws IllegalAccessException {         Map<String, Object> fieldMap = new HashMap<>();         Class<?> clazz = obj.getClass();         Field[] fields = clazz.getDeclaredFields();         for (Field field : fields) {             field.setAccessible(true);             Object value = field.get(obj);             fieldMap.put(field.getName(), value);         }         return fieldMap;     }     // Example usage     public static void main(String[] args) throws IllegalAccessException {         AutogeneratedClass au...