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:org.psnively.scala.beans.ScalaBeanInfo.java

private static PropertyDescriptor[] initPropertyDescriptors(BeanInfo beanInfo) {
    Map<String, PropertyDescriptor> propertyDescriptors = new TreeMap<String, PropertyDescriptor>(
            new PropertyNameComparator());
    for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
        propertyDescriptors.put(pd.getName(), pd);
    }//from  www. ja v a 2 s . co  m

    for (MethodDescriptor md : beanInfo.getMethodDescriptors()) {
        Method method = md.getMethod();

        if (ReflectionUtils.isObjectMethod(method)) {
            continue;
        }
        if (isScalaSetter(method)) {
            addScalaSetter(propertyDescriptors, method);
        } else if (isScalaGetter(method)) {
            addScalaGetter(propertyDescriptors, method);
        }
    }
    return propertyDescriptors.values().toArray(new PropertyDescriptor[propertyDescriptors.size()]);
}

From source file:com.gisgraphy.rest.BeanToRestParameter.java

public static Map<String, String> ToMap(Object object) {
    if (object == null) {
        throw new RestClientException("Can not get queryString for null object");
    }// w  w w . j  a  v  a  2  s .c  o  m
    Map<String, String> result = new HashMap<String, String>();
    try {
        for (PropertyDescriptor thisPropertyDescriptor : Introspector
                .getBeanInfo(object.getClass(), Object.class).getPropertyDescriptors()) {
            Object property = PropertyUtils.getProperty(object, thisPropertyDescriptor.getName());
            if (property != null) {
                result.put(thisPropertyDescriptor.getName(), property.toString());
            }
        }
    } catch (Exception e) {
        throw new RestClientException("can not generate Map for bean : " + e.getMessage(), e);
    }
    return result;
}

From source file:com.allinfinance.system.util.BeanUtils.java

/**
 * null??//from w  ww  . j a  v a 2  s.  c  om
* @param source
* @return
*/
public static String[] getNullPropertyNames(Object source) {
    final BeanWrapper src = new BeanWrapperImpl(source);
    java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

    Set<String> emptyNames = new HashSet<String>();
    for (java.beans.PropertyDescriptor pd : pds) {
        Object srcValue = src.getPropertyValue(pd.getName());
        if (srcValue == null)
            emptyNames.add(pd.getName());
    }
    String[] result = new String[emptyNames.size()];
    return emptyNames.toArray(result);
}

From source file:com.gisgraphy.rest.BeanToRestParameter.java

public static String toQueryString(Object object) {
    if (object == null) {
        throw new RestClientException("Can not get queryString for null object");
    }/*from   www  .java2  s  . com*/
    StringBuilder sb = new StringBuilder(128);
    try {
        boolean first = true;
        String andValue = "&";
        for (PropertyDescriptor thisPropertyDescriptor : Introspector
                .getBeanInfo(object.getClass(), Object.class).getPropertyDescriptors()) {
            Object property = PropertyUtils.getProperty(object, thisPropertyDescriptor.getName());
            if (property != null) {
                sb.append(first ? "?" : andValue);
                sb.append(thisPropertyDescriptor.getName());
                sb.append("=");
                sb.append(URLEncoder.encode(property.toString(), Constants.CHARSET));
                first = false;
            }
        }
    } catch (Exception e) {
        throw new RestClientException("can not generate url for bean: " + e.getMessage(), e);
    }
    return sb.toString();
}

From source file:com.glaf.core.util.Tools.java

@SuppressWarnings("unchecked")
public static void populate(Object model, Map<String, Object> dataMap) {
    if (model instanceof Map) {
        Map<String, Object> map = (Map<String, Object>) model;
        Set<Entry<String, Object>> entrySet = map.entrySet();
        for (Entry<String, Object> entry : entrySet) {
            String key = entry.getKey();
            if (dataMap.containsKey(key)) {
                map.put(key, dataMap.get(key));
            }/*from  w  ww  . ja v a2 s.c  o m*/
        }
    } else {
        PropertyDescriptor[] propertyDescriptor = BeanUtils.getPropertyDescriptors(model.getClass());
        for (int i = 0; i < propertyDescriptor.length; i++) {
            PropertyDescriptor descriptor = propertyDescriptor[i];
            String propertyName = descriptor.getName();
            if (propertyName.equalsIgnoreCase("class")) {
                continue;
            }
            String value = null;
            Object x = null;
            Object object = dataMap.get(propertyName);
            if (object != null && object instanceof String) {
                value = (String) object;
            }
            try {
                Class<?> clazz = descriptor.getPropertyType();
                if (value != null) {
                    x = getValue(clazz, value);
                } else {
                    x = object;
                }
                if (x != null) {
                    // PropertyUtils.setProperty(model, propertyName, x);
                    ReflectUtils.setFieldValue(model, propertyName, x);
                }
            } catch (Exception ex) {
                logger.debug(ex);
            }
        }
    }
}

From source file:org.openlmis.fulfillment.testutils.DtoGenerator.java

private static <T> void generate(Class<T> clazz) {
    Object instance;/*from  www  .  j a va  2 s  . com*/

    try {
        instance = clazz.newInstance();
    } catch (Exception exp) {
        throw new IllegalStateException("Missing no args constructor", exp);
    }

    for (PropertyDescriptor descriptor : PropertyUtils.getPropertyDescriptors(clazz)) {
        if ("class".equals(descriptor.getName())) {
            continue;
        }

        if (null == descriptor.getReadMethod() || null == descriptor.getWriteMethod()) {
            // we support only full property (it has to have a getter and setter)
            continue;
        }

        try {
            Object value = generateValue(clazz, descriptor.getPropertyType());
            PropertyUtils.setProperty(instance, descriptor.getName(), value);
        } catch (Exception exp) {
            throw new IllegalStateException("Can't set value for property: " + descriptor.getName(), exp);
        }
    }

    REFERENCES.put(clazz, instance);
}

From source file:corner.util.ognl.OgnlUtil.java

/**
 * Copies the properties in the object "from" and sets them in the object "to"
 * using specified type converter, or {@link com.opensymphony.xwork.util.CornerTypeConverter} if none is specified.
 *
 * @param from the source object//w  w w .ja v a  2  s .  c  om
 * @param to the target object
 * @param context the action context we're running under
 */
public static void copy(Object from, Object to, Map context) {
    Map contextFrom = Ognl.createDefaultContext(from);

    Map contextTo = Ognl.createDefaultContext(to);

    BeanInfo beanInfoFrom = null;

    try {
        beanInfoFrom = Introspector.getBeanInfo(from.getClass(), Object.class);
    } catch (IntrospectionException e) {
        log.error("An error occured", e);

        return;
    }

    PropertyDescriptor[] pds = beanInfoFrom.getPropertyDescriptors();

    for (int i = 0; i < pds.length; i++) {
        PropertyDescriptor pd = pds[i];

        try {
            Object expr = compile(pd.getName());
            Object value = Ognl.getValue(expr, contextFrom, from);
            Ognl.setValue(expr, contextTo, to, value);
        } catch (OgnlException e) {
            // ignore, this is OK
        }
    }
}

From source file:com.handpay.ibenefit.framework.util.WebUtils.java

public static Map<String, Object> getSearchParameterMap(HttpServletRequest request, Class DomainObjecClass) {
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Map<String, Object> parameterMap = new HashMap();
    Map requestMap = getParametersStartingWith(request, DEFAULT_SEARCH_PREFIX);
    boolean matchAll = true;
    PropertyDescriptor[] beandesc = PropertyUtils.getPropertyDescriptors(DomainObjecClass);
    for (int i = 0; i < beandesc.length; i++) {
        PropertyDescriptor descriptor = beandesc[i];
        String propertyName = descriptor.getName();
        Class propertyClass = descriptor.getPropertyType();
        if ("class".equals(propertyName)) {
            continue;
        }//from www .  j  a  va2s . c o m
        if (propertyClass == Date.class) {
            String after = (String) requestMap.get(propertyName + "After");
            if (after != null && after.trim().length() > 0) {
                try {
                    Date afterDate = dateFormat.parse(after);
                    parameterMap.put(propertyName + "After", afterDate);
                } catch (Exception e) {
                }
            }
            String befor = (String) requestMap.get(propertyName + "Befor");
            if (befor != null && befor.trim().length() > 0) {
                try {
                    Date beforDate = dateFormat.parse(befor);
                    // to the end of the day
                    long dayTail = beforDate.getTime() + 24 * 3600 * 1000 - 1;
                    parameterMap.put(propertyName + "Befor", new Date(dayTail));
                } catch (Exception e) {
                }
            }
        } else if (propertyClass == String.class) {
            String value = (String) requestMap.get(propertyName);
            if (value != null && value.trim().length() > 0) {
                if (matchAll) {
                    parameterMap.put(propertyName, "%" + value + "%");
                } else {
                    parameterMap.put(propertyName, value + "%");
                }
            }
        } else if (propertyClass == Long.class) {
            String value = (String) requestMap.get(propertyName);
            if (value != null && value.trim().length() > 0) {
                parameterMap.put(propertyName, Long.parseLong(value));
            }
        } else if (propertyClass == Integer.class) {
            String value = (String) requestMap.get(propertyName);
            if (value != null && value.trim().length() > 0) {
                parameterMap.put(propertyName, Integer.parseInt(value));
            }
        } else if (propertyClass == Double.class) {
            String value = (String) requestMap.get(propertyName);
            if (value != null && value.trim().length() > 0) {
                parameterMap.put(propertyName, Double.parseDouble(value));
            }
        } else if (propertyClass == Float.class) {
            String value = (String) requestMap.get(propertyName);
            if (value != null && value.trim().length() > 0) {
                parameterMap.put(propertyName, Float.parseFloat(value));
            }
        } else if (propertyClass == Boolean.class) {
            String value = (String) requestMap.get(propertyName);
            if (value != null && value.trim().length() > 0) {
                parameterMap.put(propertyName, Boolean.parseBoolean(value));
            }
        }
    }
    return parameterMap;
}

From source file:de.micromata.genome.util.bean.PropertyDescriptorUtils.java

/**
 * Write property./*w  w  w.j av  a 2  s  .c  om*/
 *
 * @param bean the bean
 * @param pd the pd
 * @param value the value
 * @throws PropertyAccessException the property access exception
 */
public static void writeProperty(Object bean, PropertyDescriptor pd, Object value)
        throws PropertyAccessException {
    PropertyUtilsBean proputils = BeanUtilsBean.getInstance().getPropertyUtils();
    try {
        proputils.setSimpleProperty(bean, pd.getName(), value);
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
        throw new PropertyAccessException("setSimpleProperty", bean, pd, ex);
    }
}

From source file:de.micromata.genome.util.bean.PropertyDescriptorUtils.java

/**
 * Read property./*from w w w.  j av a 2s  .  co  m*/
 *
 * @param bean the bean
 * @param pd the pd
 * @return the object
 * @throws PropertyAccessException the property access exception
 */
public static Object readProperty(Object bean, PropertyDescriptor pd) throws PropertyAccessException {
    PropertyUtilsBean proputils = BeanUtilsBean.getInstance().getPropertyUtils();
    Object value;
    try {
        value = proputils.getSimpleProperty(bean, pd.getName());
        return value;
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
        throw new PropertyAccessException("getSimpleProperty", bean, pd, ex);
    }
}