How do I determine bean’s property type?

Using the PropertyUtils.getPropertyType() we can determine the bean property type. This method return a Class object of bean’s property type.

package org.kodejava.example.commons.beanutils;

import org.apache.commons.beanutils.PropertyUtils;

public class PropertyType {
    public static void main(String[] args) {
        Recording recording = new Recording();
        recording.setTitle("Magical Mystery Tour");
        
        try {
            /*
             * Using PropertyUtils.getPropertyType() to determine the type of
             * title property.
             */
            Class type = PropertyUtils.getPropertyType(recording, "title");
            System.out.println("type = " + type.getName());
            
            String value = (String) PropertyUtils.getProperty(recording, "title");
            System.out.println("value = " + value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

How do I read mapped bean’s property value?

In this example you’ll see how to read mapped property value of a bean. We use PropertyUtils.getMappedProperty() method the read the mapped property of the Recording object that consist of Track objects.

package org.kodejava.example.commons.beanutils;

import org.apache.commons.beanutils.PropertyUtils;

import java.util.HashMap;
import java.util.Map;

public class ReadMapProperty {
    public static void main(String[] args) {
        Recording recording = new Recording();
        recording.setTitle("Please Please Me");

        Track track1 = new Track();
        track1.setTitle("I Saw Her Standing There");

        Track track2 = new Track();
        track2.setTitle("Misery");

        Map tracks = new HashMap<>();
        tracks.put("Track One", track1);
        tracks.put("Track Two", track2);

        recording.setMapTracks(tracks);

        try {
            Track track = (Track) PropertyUtils.getMappedProperty(recording, "mapTracks(Track One)");
            System.out.println("track.getTitle() = " + track.getTitle());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
package org.kodejava.example.commons.beanutils;

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

public class Recording {
    private Long id;
    private String title;
    private List tracks = new ArrayList<>();
    private Map mapTracks = new HashMap<>();

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List getTracks() {
        return tracks;
    }

    public void setTracks(List tracks) {
        this.tracks = tracks;
    }

    public void setMapTracks(Map mapTracks) {
        this.mapTracks = mapTracks;
    }

    public Map getMapTracks() {
        return mapTracks;
    }
}

And here is the result of our program.

track.getTitle() = I Saw Her Standing There

How do I read indexed bean’s property value?

In this example you’ll see how to read an indexed property of an object such as List or array. Using the PropertyUtils.getIndexedProperty() method we can do it easily. This method take the bean and the name of indexed property including the element to be read. Let’s see the example below for more details.

package org.kodejava.example.commons.beanutils;

import org.apache.commons.beanutils.PropertyUtils;

import java.util.ArrayList;
import java.util.List;

public class ReadIndexedProperty {
    public static void main(String[] args) {
        Recording recording = new Recording();
        recording.setId(1L);
        recording.setTitle("With The Beatles");

        List tracks = new ArrayList<>();
        Track track1 = new Track();
        track1.setTitle("It Won't Be Long");

        Track track2 = new Track();
        track2.setTitle("All I've Got To Do");

        Track track3 = new Track();
        track3.setTitle("All My Loving");

        tracks.add(track1);
        tracks.add(track2);
        tracks.add(track3);

        recording.setTracks(tracks);

        try {
            Track trackOne = (Track) PropertyUtils.getIndexedProperty(recording, "tracks[0]");
            Track trackThree = (Track) PropertyUtils.getIndexedProperty(recording, "tracks[2]");

            System.out.println("trackOne.getTitle() = " + trackOne.getTitle());
            System.out.println("trackThree.getTitle() = " + trackThree.getTitle());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
package org.kodejava.example.commons.beanutils;

import java.util.ArrayList;
import java.util.List;

public class Recording {
    private Long id;
    private String title;
    private List tracks = new ArrayList<>();

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public List getTracks() {
        return tracks;
    }

    public void setTracks(List tracks) {
        this.tracks = tracks;
    }
}

Our program results are:

trackOne.getTitle() = It Won't Be Long
trackThree.getTitle() = All My Loving