|
| 1 | +package com.baeldung.commons.beanutils; |
| 2 | + |
| 3 | +import java.lang.reflect.InvocationTargetException; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.apache.commons.beanutils.PropertyUtils; |
| 7 | + |
| 8 | +public class CourseService { |
| 9 | + |
| 10 | + public static void setValues(Course course, String name, List<String> codes) |
| 11 | + throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { |
| 12 | + // Setting the simple properties |
| 13 | + PropertyUtils.setSimpleProperty(course, "name", name); |
| 14 | + PropertyUtils.setSimpleProperty(course, "codes", codes); |
| 15 | + } |
| 16 | + |
| 17 | + public static void setIndexedValue(Course course, int codeIndex, String code) |
| 18 | + throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { |
| 19 | + // Setting the indexed properties |
| 20 | + PropertyUtils.setIndexedProperty(course, "codes[" + codeIndex + "]", code); |
| 21 | + } |
| 22 | + |
| 23 | + public static void setMappedValue(Course course, String enrollId, Student student) |
| 24 | + throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { |
| 25 | + // Setting the mapped properties |
| 26 | + PropertyUtils.setMappedProperty(course, "enrolledStudent(" + enrollId + ")", student); |
| 27 | + } |
| 28 | + |
| 29 | + public static String getNestedValue(Course course, String enrollId, String nestedPropertyName) |
| 30 | + throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { |
| 31 | + return (String) PropertyUtils.getNestedProperty( |
| 32 | + course, "enrolledStudent(" + enrollId + ")." + nestedPropertyName); |
| 33 | + } |
| 34 | +} |
0 commit comments