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:com.reachlocal.grails.plugins.cassandra.utils.DataMapper.java

public static Map<String, Object> dataProperties(GroovyObject data, List<String> transients,
        Map<String, Class> hasMany, String expandoMapName, Collection mappedProperties) throws IOException {
    Map<String, Object> map = new LinkedHashMap<String, Object>();
    Class clazz = data.getClass();
    if (expandoMapName != null) {
        transients.add(expandoMapName);/*from w  ww .jav a  2 s .  c o m*/
    }

    // Unneeded since we now get the class from the method signatures
    // Might be needed again if we ever support subclasses
    //map.put(CLASS_NAME_KEY, clazz.getName());

    for (PropertyDescriptor pd : PropertyUtils.getPropertyDescriptors(data)) {
        String name = pd.getName();
        if (!transients.contains(name) && !GLOBAL_TRANSIENTS.contains(name) && hasMany.get(name) == null) {
            Object prop = data.getProperty(name);
            if (prop == null) {
                if (mappedProperties.contains(name)) {
                    map.put(name + "Id", null);
                } else {
                    map.put(name, null);
                }
            } else {
                //if (OrmHelper.isMappedObject(prop)) {   // TODO new is mapped
                if (mappedProperties.contains(name)) {
                    GroovyObject g = (GroovyObject) prop;
                    String idName = name + "Id";
                    map.put(idName, g.getProperty("id"));
                } else {
                    Object value = dataProperty(prop);
                    map.put(name, value);
                }
            }
        }
    }

    if (expandoMapName != null) {
        Map<String, Object> expandoMap = (Map<String, Object>) data.getProperty(expandoMapName);
        if (expandoMap != null) {
            for (Map.Entry<String, Object> entry : expandoMap.entrySet()) {
                map.put(entry.getKey(), entry.getValue());
            }
        }
    }

    return map;
}

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

private static String convertPropertyValueForString(Object target, PropertyDescriptor descriptor,
        Object propertyValue) {// www. j a  v a2s  .co m
    DateTimeFormat dateTimeFormat;
    try {
        dateTimeFormat = target.getClass().getDeclaredField(descriptor.getName())
                .getAnnotation(DateTimeFormat.class);
    } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    }
    if (dateTimeFormat != null) {
        JodaDateTimeFormatAnnotationFormatterFactory factory = new JodaDateTimeFormatAnnotationFormatterFactory();
        Printer printer = factory.getPrinter(dateTimeFormat, descriptor.getPropertyType());
        return printer.print(propertyValue, LocaleContextHolder.getLocale());
    }
    return propertyValue.toString();
}

From source file:com.github.pfmiles.minvelocity.TemplateUtil.java

private static void putAllPojoVals(Object ctxPojo, Context ctx) {
    ctx.put("ParseUtil", ParseUtil.class);
    if (ctxPojo == null)
        return;/*from  w ww  .  ja v  a  2s  . c  om*/
    if (ctxPojo instanceof Map) {
        for (Map.Entry<?, ?> e : ((Map<?, ?>) ctxPojo).entrySet()) {
            ctx.put(e.getKey().toString(), e.getValue());
        }
    } else {
        BeanInfo bi;
        try {
            bi = Introspector.getBeanInfo(ctxPojo.getClass());
            for (PropertyDescriptor pd : bi.getPropertyDescriptors()) {
                if ("class".equals(pd.getName()))
                    continue;
                Method rm = pd.getReadMethod();
                if (rm != null)
                    ctx.put(pd.getName(), rm.invoke(ctxPojo));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:com.mawujun.utils.bean.BeanUtils.java

private static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String propertyName)
        throws IntrospectionException {
    PropertyDescriptor[] pds = getPropertyDescriptors(clazz);
    for (PropertyDescriptor pd : pds) {
        if (pd.getName().equals(propertyName)) {
            return pd;
        }// w  ww  .  j av  a  2s  .c o  m
    }
    return null;
}

From source file:com.ponysdk.ui.server.list.form.AddCustomColumnDescriptorForm.java

private static boolean hasProperty(final Class<?> clas, final String property) {
    final String propertyPath = property;
    boolean isValid = true;
    Class<?> propertyClass = null;
    try {/*from  w w  w  . j  a  va  2s .  co  m*/
        if (propertyPath != null) {
            final String[] tokens = propertyPath.split("\\.");
            final PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(clas);
            for (final PropertyDescriptor propertyDescriptor : propertyDescriptors) {
                if (propertyDescriptor.getName().equals(tokens[0])) {
                    propertyClass = propertyDescriptor.getPropertyType();
                    break;
                }
            }

            if (propertyClass == null)
                throw new Exception("unknown property#" + tokens[0]);
            for (int i = 1; i < tokens.length; i++) {
                final PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(propertyClass);
                boolean found = false;
                for (final PropertyDescriptor propertyDescriptor : descriptors) {
                    if (propertyDescriptor.getName().equals(tokens[i])) {
                        propertyClass = propertyDescriptor.getPropertyType();
                        found = true;
                    }
                }
                if (!found)
                    throw new Exception("unknown property#" + tokens[i] + " for class#" + propertyClass);
            }
        }
    } catch (final Exception e) {
        final String errorMessage = "Error occured when finding property '" + propertyPath + "'";
        log.error(errorMessage, e);
        isValid = false;
    }
    return isValid;
}

From source file:kelly.util.BeanUtils.java

/**
 * Retrieve the JavaBeans {@code PropertyDescriptors} for the given property.
 * @param clazz the Class to retrieve the PropertyDescriptor for
 * @param propertyName the name of the property
 * @return the corresponding PropertyDescriptor, or {@code null} if none
 * @throws BeansException if PropertyDescriptor lookup fails
 *///from  w  w  w.j  a v  a 2s  .c om
public static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String propertyName) {
    PropertyDescriptor[] pds = getPropertyDescriptors(clazz);
    for (PropertyDescriptor pd : pds) {
        if (propertyName.equals(pd.getName())) {
            return pd;
        }
    }
    return null;
}

From source file:org.zht.framework.util.ZBeanUtil.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static Map convertBeanToMap(Object bean) {
    Map returnMap = null;/* ww  w .  ja  v a2 s . c  o  m*/
    try {
        Class<?> type = bean.getClass();
        BeanInfo beanInfo = Introspector.getBeanInfo(type);
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        //           MethodAccess access = MethodAccess.get(type.getClass());
        returnMap = new HashMap();
        for (PropertyDescriptor property : propertyDescriptors) {
            String properName = property.getName();
            Method getter = property.getReadMethod();
            Object value = getter.invoke(bean);
            returnMap.put(properName, value);
            //
            //            Object value=access.invoke(bean,"get" + ZStrUtil.toUpCaseFirst(properName));
            //            if (value != null){
            //               returnMap.put(properName, value);
            //            }else{
            //               returnMap.put(properName, null);
            //            }

        }
    } catch (Exception e) {
        e.printStackTrace();
        return returnMap;
    }
    return returnMap;
}

From source file:ar.com.fdvs.dj.domain.builders.ReflectiveReportBuilder.java

/**
 * Calculates a column title using camel humps to separate words.
 * @param _property the property descriptor.
 * @return the column title for the given property.
 *///from   ww w.jav  a  2  s.  co  m
private static String getColumnTitle(final PropertyDescriptor _property) {
    final StringBuffer buffer = new StringBuffer();
    final String name = _property.getName();
    buffer.append(Character.toUpperCase(name.charAt(0)));
    for (int i = 1; i < name.length(); i++) {
        final char c = name.charAt(i);
        if (Character.isUpperCase(c)) {
            buffer.append(' ');
        }
        buffer.append(c);
    }
    return buffer.toString();
}

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

@SuppressWarnings("unchecked")
public static Map<String, Object> getDataMap(Object target) {
    Map<String, Object> dataMap = new TreeMap<String, Object>();
    if (Map.class.isAssignableFrom(target.getClass())) {
        Map<String, Object> map = (Map<String, Object>) target;
        Set<Entry<String, Object>> entrySet = map.entrySet();
        for (Entry<String, Object> entry : entrySet) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (key != null && value != null) {
                dataMap.put(key.toString(), value);
            }//from   ww w .java2s  .  co  m
        }
    } else {
        PropertyDescriptor[] propertyDescriptor = BeanUtils.getPropertyDescriptors(target.getClass());
        for (int i = 0; i < propertyDescriptor.length; i++) {
            PropertyDescriptor descriptor = propertyDescriptor[i];
            String propertyName = descriptor.getName();
            if (propertyName.equalsIgnoreCase("class")) {
                continue;
            }
            try {
                Object value = PropertyUtils.getProperty(target, propertyName);
                dataMap.put(propertyName, value);
            } catch (Exception ex) {

            }
        }
    }
    return dataMap;
}

From source file:org.zht.framework.util.ZBeanUtil.java

@SuppressWarnings("rawtypes")
public static Object convertMapToBean(Map map, Class<?> type) {
    Object obj = null;/*from  w w w.  j  a  v a 2 s  .  c  o m*/
    try {
        BeanInfo beanInfo = null;

        beanInfo = Introspector.getBeanInfo(type);
        obj = type.newInstance();
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            String properName = property.getName();
            if (map.containsKey(property.getName())) {
                try {// ??

                    Object value = map.get(properName);
                    Method setter = property.getWriteMethod();
                    setter.invoke(obj, value);
                    //Unable to find non-private method
                    //               access.invoke(obj,"set" + ZStrUtil.toUpCaseFirst(properName),value);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return obj;
    }
    return obj;
}