Example usage for java.beans PropertyDescriptor getName

List of usage examples for java.beans PropertyDescriptor getName

Introduction

In this page you can find the example usage for java.beans PropertyDescriptor getName.

Prototype

public String getName() 

Source Link

Document

Gets the programmatic name of this feature.

Usage

From source file:de.iritgo.simplelife.bean.BeanTools.java

/**
 * Return true if the given property exists in the given class
 * /*from  w  ww  . j  a  v a  2 s .c  om*/
 * @param klass The class
 * @param propertyName The property name
 * @return True if the property exists
 */
static public boolean hasProperty(Class klass, String propertyName) {
    for (PropertyDescriptor pd : PropertyUtils.getPropertyDescriptors(klass)) {
        if (pd.getName().equals(propertyName)) {
            return true;
        }
    }

    return false;
}

From source file:fr.mael.microrss.util.Tools.java

public static HashMap<String, Object> toMap(UserConfig conf)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    HashMap<String, Object> options = new HashMap<String, Object>();
    PropertyDescriptor[] descs = PropertyUtils.getPropertyDescriptors(UserConfig.class);
    for (PropertyDescriptor desc : descs) {
        if (!desc.getName().equals("class") && !desc.getName().equals("user")) {
            options.put(desc.getName(), PropertyUtils.getProperty(conf, desc.getName()));
        }//from   w ww .ja v  a 2  s  .c  om
    }
    return options;

}

From source file:de.iritgo.simplelife.bean.BeanTools.java

/**
 * Copy all attributes from the given bean to the given map
 * /*from w  ww .  j  a  v a 2  s. co m*/
 * @param object The bean
 * @param map The map
 * @param overwrite If true existing attributes in the map will be
 *            overwritten
 */
static public void copyBean2Map(Object object, Map<String, Object> map, boolean overwrite) {
    for (PropertyDescriptor pd : PropertyUtils.getPropertyDescriptors(object)) {
        String name = pd.getName();

        if (!overwrite && map.containsKey(name)) {
            continue;
        }

        try {
            map.put(name, PropertyUtils.getProperty(object, name));
        } catch (Exception ignore) {
        }
    }
}

From source file:technology.tikal.gae.pagination.PaginationModelFactory.java

private static String armaUrl(String baseUrl, final FiltroBusqueda filtro, final Object sinceId,
        final Object since, final int maxResults) {
    String r = baseUrl;/*  w  ww .jav a  2 s . c o m*/
    if (filtro != null) {
        PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(filtro);
        for (PropertyDescriptor x : descriptors) {
            if (x.getName().compareTo("class") != 0) {
                Object obj;
                try {
                    obj = PropertyUtils.getProperty(filtro, x.getName());
                } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                    throw new IllegalArgumentException(
                            "no se puede construir la pagina con el filtro proporcionado");
                }
                if (obj != null) {
                    r = addUrlParameter(r, x.getName(), obj.toString());
                }
            }
        }
    }
    if (sinceId != null) {
        r = addUrlParameter(r, "sinceId", sinceId.toString());
    }
    if (since != null) {
        r = addUrlParameter(r, "since", since.toString());
    }
    if (maxResults > 0) {
        r = addUrlParameter(r, "maxResults", maxResults + "");
    }
    return r;
}

From source file:me.yyam.beanutils.BeanUtilEx.java

/**
 * IntrospectorPropertyDescriptor Bean --> Map
 * @param obj//from w w w.  j  ava  2 s. c  o  m
 * @return
 */
public static Map transBean2Map(Object obj) throws Exception {
    if (obj == null) {
        return null;
    }
    Map<String, Object> map = new HashMap<>();
    PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(obj);
    for (PropertyDescriptor property : propertyDescriptors) {
        String key = property.getName();
        if (!key.equals("class")) {
            try {
                Object value = PropertyUtils.getProperty(obj, key);
                if (value == null) {
                    map.put(key, value);
                } else {
                    if (value instanceof List) {
                        List list = new ArrayList();
                        for (Object v : (List) value) {
                            list.add(transBean2Map(v));
                        }
                        map.put(key, list);
                    } else {
                        if (value instanceof Enum) {
                            value = value.toString();
                        }
                        if (isPrimitive(value)) {
                            map.put(key, value);
                        } else {
                            Map cmap = transBean2Map(value);
                            map.put(key, cmap);
                        }
                    }
                }
            } catch (NoSuchMethodException e) {
                System.out.println(e.toString());
            }
        }
    }
    return map;
}

From source file:io.github.moosbusch.lumpi.util.FormUtil.java

public static Map<String, Object> getPropertyValuesMap(Object bean)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    Map<String, Object> result = new HashMap<>();
    PropertyDescriptor[] propDescs = PropertyUtils.getPropertyDescriptors(bean.getClass());

    for (PropertyDescriptor propDesc : propDescs) {
        String propertyName = propDesc.getName();
        Object propertyValue = PropertyUtils.getProperty(bean, propertyName);
        result.put(propertyName, propertyValue);
    }//from  w  w  w . ja  v a2  s  .c  om

    return result;
}

From source file:org.granite.grails.integration.GrailsExternalizer.java

public static boolean isIgnored(PropertyDescriptor property) {
    if (EVENTS.contains(property.getName()))
        return true;
    if (property.getName().equals("errors") && property.getPropertyType() != null
            && property.getPropertyType().getName().equals(ERRORS))
        return true;
    return false;
}

From source file:de.unentscheidbar.validation.internal.Beans.java

public static PropertyDescriptor property(Class<?> beanClass, String propertyName) {

    try {//from  w  w  w .  j a  va  2  s.c  om
        BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
        PropertyDescriptor[] propDescriptors = beanInfo.getPropertyDescriptors();
        if (propDescriptors == null) {
            throw new IllegalArgumentException("Class " + beanClass.getName()
                    + " does not provide property descriptors in its bean info.");
        }
        for (PropertyDescriptor pd : propDescriptors) {
            if (pd.getName().equals(propertyName)) {
                return pd;
            }
        }
        return null;
    } catch (IntrospectionException e) {
        throw Throwables.propagate(e.getCause());
    }
}

From source file:org.wallride.web.support.ControllerUtils.java

public static MultiValueMap<String, String> convertBeanForQueryParams(Object target,
        ConversionService conversionService) {
    BeanWrapperImpl beanWrapper = new BeanWrapperImpl(target);
    beanWrapper.setConversionService(conversionService);
    MultiValueMap<String, String> queryParams = new LinkedMultiValueMap();
    for (PropertyDescriptor pd : beanWrapper.getPropertyDescriptors()) {
        if (beanWrapper.isWritableProperty(pd.getName())) {
            Object pv = beanWrapper.getPropertyValue(pd.getName());
            if (pv != null) {
                if (pv instanceof Collection) {
                    if (!CollectionUtils.isEmpty((Collection) pv)) {
                        for (Object element : (Collection) pv) {
                            queryParams.set(pd.getName(), convertPropertyValueForString(target, pd, element));
                        }//from  w  w w. j av a 2 s. co  m
                    }
                } else {
                    queryParams.set(pd.getName(), convertPropertyValueForString(target, pd, pv));
                }
            }
        }
    }
    return queryParams;
}

From source file:com.sf.ddao.crud.param.CRUDBeanPropsParameter.java

public static String mapPropName2Field(PropertyDescriptor descriptor) {
    final String s = descriptor.getName();
    StringBuilder sb = new StringBuilder(s.length());
    for (char ch : s.toCharArray()) {
        if (Character.isUpperCase(ch)) {
            ch = Character.toLowerCase(ch);
            sb.append("_").append(ch);
        } else {//from w  w w .jav  a  2  s .c o  m
            sb.append(ch);
        }
    }
    return sb.toString();
}