package nl.tudelft.simulation.introspection.beans;
import java.beans.BeanInfo;
import java.beans.PropertyDescriptor;
import java.util.HashSet;
import java.util.Set;
import nl.tudelft.simulation.introspection.Introspector;
import nl.tudelft.simulation.introspection.Property;
/**
* The Bean introspector provides a simplified JavaBean TM implementation of the introspection interfaces. Its behavior
* adhers to the following:
*
* - Properties are discovered by searching for 'getter' and / or 'setter' methods
* - Property value are manipulated via a property's 'setter' method. If no such method is found, the property cannot
* be altered
* - Indexed properties are probably not correctly supported.
*
*
* (c) copyright 2002-2014 Delft University of Technology.
* BSD-style license. See DSOL License.
* @author Peter Jacobs.
* @author Alexander Verbraeck.
* @author Niels Lang.
* @since 1.5
*/
public class BeanIntrospector implements Introspector
{
/** {@inheritDoc} */
@Override
public Property[] getProperties(final Object introspected)
{
Set props = new HashSet();
try
{
BeanInfo info = java.beans.Introspector.getBeanInfo(introspected.getClass());
PropertyDescriptor[] descrips = info.getPropertyDescriptors();
for (int i = 0; i < descrips.length; i++)
{
props.add(new BeanProperty(introspected, descrips[i]));
}
}
catch (Exception e)
{
throw new IllegalArgumentException(this + " - getProperties", e);
}
return props.toArray(new Property[props.size()]);
}
/** {@inheritDoc} */
@Override
public Property getProperty(final Object introspected, final String property)
{
try
{
BeanInfo info = java.beans.Introspector.getBeanInfo(introspected.getClass());
PropertyDescriptor[] descrips = info.getPropertyDescriptors();
for (int i = 0; i < descrips.length; i++)
{
if (descrips[i].getName().equals(property))
{
return new BeanProperty(introspected, descrips[i]);
}
}
}
catch (Exception e)
{
throw new IllegalArgumentException(this + " - getProperty", e);
}
throw new IllegalArgumentException("Property '" + property + "' not found for " + introspected);
}
/** {@inheritDoc} */
@Override
public String[] getPropertyNames(final Object introspected)
{
Set props = new HashSet();
try
{
BeanInfo info = java.beans.Introspector.getBeanInfo(introspected.getClass());
PropertyDescriptor[] descrips = info.getPropertyDescriptors();
for (int i = 0; i < descrips.length; i++)
{
props.add(descrips[i].getName());
}
}
catch (Exception e)
{
throw new IllegalArgumentException(this + " - getPropertyNames", e);
}
return props.toArray(new String[props.size()]);
}
}