tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Tools and Libs > Commons Bean Utils > Get Nested Property

Get Nested Property 

Apache Commons BeanUtils is a java library useful for accessing bean properties and methods. It provides introspection capabilities to view and manipulate the properties and operations provided by the given class. This requires the libraries commons-beanutils-1.8.3.jar, commons-beanutils-bean-collections-1.8.3.jar, commons-beanutils-core-1.8.3.jar, commons-collections-3.2.1.jar, commons-logging.jar to be in classpath. The following example shows using BeanUtils.getNestedProperty() API. It returns the value of specified property.

File Name  :  
com/bethecoder/tutorials/commons_beanutils/common/Student.java 
   
package com.bethecoder.tutorials.commons_beanutils.common;

public class Student {

  private String name;
  private int age;
  private String hobby;

  public Student() {
  }

  public Student(String name, int age, String hobby) {
    super();
    this.name = name;
    this.age = age;
    this.hobby = hobby;
  }

  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  public String getHobby() {
    return hobby;
  }
  public void setHobby(String hobby) {
    this.hobby = hobby;
  }
  
  public String toString() {
    return "Student[name = " + name + ", age = " + age + ", hobby = " + hobby + "]";
  }
}
   

File Name  :  
com/bethecoder/tutorials/commons_beanutils/common/ComplexBean.java 
   
package com.bethecoder.tutorials.commons_beanutils.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ComplexBean {

  private List<Student> studentList = new ArrayList<Student>();
  private Map<String, List<Student>> subjectStudentMap = new HashMap<String, List<Student>>();
  
  public List<Student> getStudentList() {
    return studentList;
  }
  public void setStudentList(List<Student> studentList) {
    this.studentList = studentList;
  }
  public Map<String, List<Student>> getSubjectStudentMap() {
    return subjectStudentMap;
  }
  public void setSubjectStudentMap(Map<String, List<Student>> subjectStudentMap) {
    this.subjectStudentMap = subjectStudentMap;
  }
}
   

File Name  :  
com/bethecoder/tutorials/commons_beanutils/GetNestedPropertyTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.commons_beanutils;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;

import org.apache.commons.beanutils.BeanUtils;

import com.bethecoder.tutorials.commons_beanutils.common.ComplexBean;
import com.bethecoder.tutorials.commons_beanutils.common.Student;

public class GetNestedPropertyTest {

  /**
   @param args
   @throws InvocationTargetException 
   @throws IllegalAccessException 
   @throws NoSuchMethodException 
   */
  public static void main(String[] args
    throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    
    Student student = new Student("Sriram"2"Chess");
    Student student2 = new Student("Sudhakar"29"Painting");
    Student student3 = new Student("Charan"18"Reading books");
    
    ComplexBean complexBean = new ComplexBean();
    complexBean.getStudentList().addAll(Arrays.asList(student, student2, student3));
    complexBean.getSubjectStudentMap().put("MATHS"
        Arrays.asList(student, student2, student3));
    
    complexBean.getSubjectStudentMap().put("PHYSICS"
        Arrays.asList(student, student2));
    
    
    //Same as complexBean.getStudentList().get(0);
    String str = BeanUtils.getNestedProperty(complexBean, "studentList[0]");
    System.out.println(str);
    
    //Same as complexBean.getStudentList().get(0).getName();
    str = BeanUtils.getNestedProperty(complexBean, "studentList[0].name");
    System.out.println(str);
    
    //Same as complexBean.getSubjectStudentMap().get("MATHS").get(1);
    str = BeanUtils.getNestedProperty(complexBean, "subjectStudentMap(MATHS).[1]");
    System.out.println(str);
    
    //Same as complexBean.getSubjectStudentMap().get("MATHS").get(2).getName();
    str = BeanUtils.getNestedProperty(complexBean, "subjectStudentMap(MATHS).[2].name");
    System.out.println(str);
  }

}
   

It gives the following output,
Student[name = Sriram, age = 2, hobby = Chess]
Sriram
Student[name = Sudhakar, age = 29, hobby = Painting]
Charan



 
  


  
bl  br