/** * Returns a {@link PropertyDescriptor} for a property matching the given name in the given object. * * @param propertyName the given property name. * @param target the given object. * @return a {@code PropertyDescriptor} for a property matching the given name in the given object. * @throws NullPointerException if the given property name is {@code null}. * @throws IllegalArgumentException if the given property name is empty. * @throws NullPointerException if the given object is {@code null}. * @throws IntrospectionError if a matching property cannot be found or accessed. */ public static PropertyDescriptor getProperty(String propertyName, Object target) { checkNotNullOrEmpty(propertyName); checkNotNull(target); BeanInfo beanInfo = null; Class<?> type = target.getClass(); try { beanInfo = Introspector.getBeanInfo(type); } catch (Throwable t) { throw new IntrospectionError(format("Unable to get BeanInfo for type %s", type.getName()), t); } for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) { if (propertyName.equals(descriptor.getName())) { return descriptor; } } throw new IntrospectionError(propertyNotFoundErrorMessage(propertyName, target)); }