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 autogeneratedObject = new AutogeneratedClass();
// Set field values in the autogeneratedObject
Map<String, Object> fieldMap = mapFieldsToMap(autogeneratedObject);
System.out.println(fieldMap);
}
}
Comments
Post a Comment