Merging BAEL-968: Apache Commons BeanUtils (#2149)

This commit is contained in:
Syed Ali Raza
2017-06-25 19:12:51 +05:00
committed by Grzegorz Piwowarek
parent 526fd057ce
commit ed7c1694a7
5 changed files with 127 additions and 1 deletions
@@ -0,0 +1,35 @@
package com.baeldung.commons.beanutils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Course {
private String name;
private List<String> codes;
private Map<String, Student> enrolledStudent = new HashMap<String, Student>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getCodes() {
return codes;
}
public void setCodes(List<String> codes) {
this.codes = codes;
}
public void setEnrolledStudent(String enrolledId, Student student) {
enrolledStudent.put(enrolledId, student);
}
public Student getEnrolledStudent(String enrolledId) {
return enrolledStudent.get(enrolledId);
}
}
@@ -0,0 +1,34 @@
package com.baeldung.commons.beanutils;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import org.apache.commons.beanutils.PropertyUtils;
public class CourseService {
public static void setValues(Course course, String name, List<String> codes)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
// Setting the simple properties
PropertyUtils.setSimpleProperty(course, "name", name);
PropertyUtils.setSimpleProperty(course, "codes", codes);
}
public static void setIndexedValue(Course course, int codeIndex, String code)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
// Setting the indexed properties
PropertyUtils.setIndexedProperty(course, "codes[" + codeIndex + "]", code);
}
public static void setMappedValue(Course course, String enrollId, Student student)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
// Setting the mapped properties
PropertyUtils.setMappedProperty(course, "enrolledStudent(" + enrollId + ")", student);
}
public static String getNestedValue(Course course, String enrollId, String nestedPropertyName)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
return (String) PropertyUtils.getNestedProperty(
course, "enrolledStudent(" + enrollId + ")." + nestedPropertyName);
}
}
@@ -0,0 +1,13 @@
package com.baeldung.commons.beanutils;
public class Student {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}