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:info.magnolia.content2bean.impl.TypeMappingImpl.java

/**
 * Cache the already resolved types./*  w  w w . j  a  v  a2  s. co  m*/
 *
 */
@Override
public PropertyTypeDescriptor getPropertyTypeDescriptor(Class<?> beanClass, String propName) {
    PropertyTypeDescriptor dscr;
    String key = beanClass.getName() + "." + propName;

    dscr = propertyTypes.get(key);
    if (dscr != null) {
        return dscr;
    }

    //TypeMapping defaultMapping = TypeMapping.Factory.getDefaultMapping();
    // TODO - is this used - or is the comparison correct ?
    //        if (this != defaultMapping) {
    //            dscr = defaultMapping.getPropertyTypeDescriptor(beanClass, propName);
    //            if (dscr.getType()  != null) {
    //                return dscr;
    //            }
    //        }

    dscr = new PropertyTypeDescriptor();
    dscr.setName(propName);

    PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(beanClass);
    for (int i = 0; i < descriptors.length; i++) {
        PropertyDescriptor descriptor = descriptors[i];
        if (descriptor.getName().equals(propName)) {
            Class<?> propertytype = descriptor.getPropertyType(); // may be null for indexed properties
            if (propertytype != null) {
                dscr.setType(getTypeDescriptor(propertytype));
            }
            break;
        }
    }

    if (dscr.getType() != null) {
        if (dscr.isMap() || dscr.isCollection()) {
            int numberOfParameters = dscr.isMap() ? 2 : 1;
            Method method = getAddMethod(beanClass, propName, numberOfParameters);
            if (method != null) {
                dscr.setAddMethod(method);
                if (dscr.isMap()) {
                    dscr.setCollectionKeyType(getTypeDescriptor(method.getParameterTypes()[0]));
                    dscr.setCollectionEntryType(getTypeDescriptor(method.getParameterTypes()[1]));
                } else {
                    dscr.setCollectionEntryType(getTypeDescriptor(method.getParameterTypes()[0]));
                }
            }
        }
    }

    // remember me
    propertyTypes.put(key, dscr);

    return dscr;
}

From source file:com.github.erchu.beancp.PropertyBindingSide.java

/**
 *
 * Creates binding to property from property information.
 *
 * @param propertyDescriptor property information used to create binding
 *///from  www  . j  a  v a2  s . c o  m
public PropertyBindingSide(final PropertyDescriptor propertyDescriptor) {
    this._valueClass = propertyDescriptor.getPropertyType();
    this._readMethod = propertyDescriptor.getReadMethod();
    this._writeMethod = propertyDescriptor.getWriteMethod();
    this._name = propertyDescriptor.getName();
}

From source file:info.magnolia.jcr.node2bean.impl.TypeMappingImpl.java

@Override
public PropertyTypeDescriptor getPropertyTypeDescriptor(Class<?> beanClass, String propName) {
    PropertyTypeDescriptor dscr = null;/*from www  .  j  ava  2 s .c o  m*/
    String key = beanClass.getName() + "." + propName;

    dscr = propertyTypes.get(key);

    if (dscr != null) {
        return dscr;
    }

    dscr = new PropertyTypeDescriptor();
    dscr.setName(propName);

    PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(beanClass);
    Method writeMethod = null;
    for (PropertyDescriptor descriptor : descriptors) {
        if (descriptor.getName().equals(propName)) {
            // may be null for indexed properties
            Class<?> propertyType = descriptor.getPropertyType();
            writeMethod = descriptor.getWriteMethod();
            if (propertyType != null) {
                dscr.setType(getTypeDescriptor(propertyType, writeMethod));
            }
            // set write method
            dscr.setWriteMethod(writeMethod);
            // set add method
            int numberOfParameters = dscr.isMap() ? 2 : 1;
            dscr.setAddMethod(getAddMethod(beanClass, propName, numberOfParameters));

            break;
        }
    }

    if (dscr.getType() != null) {
        // we have discovered type for property
        if (dscr.isMap() || dscr.isCollection()) {
            List<Class<?>> parameterTypes = new ArrayList<Class<?>>(); // this will contain collection types (for map key/value type, for collection value type)
            if (dscr.getWriteMethod() != null) {
                parameterTypes = inferGenericTypes(dscr.getWriteMethod());
            }
            if (dscr.getAddMethod() != null && parameterTypes.size() == 0) {
                // here we know we don't have setter or setter doesn't have parameterized type
                // but we have add method so we take parameters from it
                parameterTypes = Arrays.asList(dscr.getAddMethod().getParameterTypes());
                // rather set it to null because when we are here we will use add method
                dscr.setWriteMethod(null);
            }
            if (parameterTypes.size() > 0) {
                // we resolved types
                if (dscr.isMap()) {
                    dscr.setCollectionKeyType(getTypeDescriptor(parameterTypes.get(0)));
                    dscr.setCollectionEntryType(getTypeDescriptor(parameterTypes.get(1)));
                } else {
                    // collection
                    dscr.setCollectionEntryType(getTypeDescriptor(parameterTypes.get(0)));
                }
            }
        } else if (dscr.isArray()) {
            // for arrays we don't need to discover its parameter from set/add method
            // we just take it via Class#getComponentType() method
            dscr.setCollectionEntryType(getTypeDescriptor(dscr.getType().getType().getComponentType()));
        }
    }
    propertyTypes.put(key, dscr);

    return dscr;
}

From source file:net.groupbuy.service.impl.BaseServiceImpl.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private void copyProperties(Object source, Object target, String[] ignoreProperties) throws BeansException {
    Assert.notNull(source, "Source must not be null");
    Assert.notNull(target, "Target must not be null");

    PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(target.getClass());
    List<String> ignoreList = (ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null;
    for (PropertyDescriptor targetPd : targetPds) {
        if (targetPd.getWriteMethod() != null
                && (ignoreProperties == null || (!ignoreList.contains(targetPd.getName())))) {
            PropertyDescriptor sourcePd = BeanUtils.getPropertyDescriptor(source.getClass(),
                    targetPd.getName());
            if (sourcePd != null && sourcePd.getReadMethod() != null) {
                try {
                    Method readMethod = sourcePd.getReadMethod();
                    if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                        readMethod.setAccessible(true);
                    }//from  ww  w  . java 2s .c o m
                    Object sourceValue = readMethod.invoke(source);
                    Object targetValue = readMethod.invoke(target);
                    if (sourceValue != null && targetValue != null && targetValue instanceof Collection) {
                        Collection collection = (Collection) targetValue;
                        collection.clear();
                        collection.addAll((Collection) sourceValue);
                    } else {
                        Method writeMethod = targetPd.getWriteMethod();
                        if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                            writeMethod.setAccessible(true);
                        }
                        writeMethod.invoke(target, sourceValue);
                    }
                } catch (Throwable ex) {
                    throw new FatalBeanException("Could not copy properties from source to target", ex);
                }
            }
        }
    }
}

From source file:com.expedia.seiso.domain.service.impl.ItemMerger.java

@SneakyThrows
private void mergeSimpleProperty(Item src, Item dest, PropertyDescriptor propDesc) {
    val getter = propDesc.getReadMethod();
    val setter = propDesc.getWriteMethod();

    if (getter == null || setter == null) {
        log.trace("Skipping simple property: {}", propDesc.getName());
        return;//from  www  .jav a  2 s  .co  m
    }

    val propValue = getter.invoke(src);
    setter.invoke(dest, propValue);
}

From source file:com.google.code.pathlet.web.ognl.impl.InstantiatingNullHandler.java

public Object nullPropertyValue(Map context, Object target, Object property) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Entering nullPropertyValue [target=" + target + ", property=" + property + "]");
    }/*from w ww  . j a v  a2 s  .  c om*/

    if ((target == null) || (property == null)) {
        return null;
    }

    try {
        String propName = property.toString();

        Class clazz = null;

        if (target != null) {
            PropertyDescriptor[] descs = reflectionProvider.getPropertyDescriptors(target);

            for (PropertyDescriptor desc : descs) {
                if (propName.equals(desc.getName())) {
                    clazz = desc.getPropertyType();
                }
            }
        }

        if (clazz == null) {
            // can't do much here!
            return null;
        }

        Object param = createObject(clazz, target, propName, context);
        Ognl.setValue(propName, context, target, param);

        return param;
    } catch (Exception e) {
        LOG.error("Could not create and/or set value back on to object", e);
    }

    return null;
}

From source file:io.lightlink.dao.mapping.BeanMapper.java

public BeanMapper(Class paramClass, List<String> fieldsOfLine) {

    PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(paramClass);
    descriptorMap = new CaseInsensitiveMap();
    for (PropertyDescriptor descriptor : descriptors) {
        descriptorMap.put(normalizePropertyName(descriptor.getName()), descriptor);
    }/*  w w w.ja  v a  2s .  co m*/

    List<String> ownFields = new ArrayList<String>();
    Map<String, List<String>> fieldsByChild = new CaseInsensitiveMap();

    groupFields(fieldsOfLine, ownFields, fieldsByChild);

    this.ownFields = ownFields;
    for (Map.Entry<String, List<String>> entry : fieldsByChild.entrySet()) {
        String childProperty = normalizePropertyName(entry.getKey());
        PropertyDescriptor descriptor = descriptorMap.get(childProperty);
        if (descriptor != null) {
            List<String> properties = entry.getValue();
            if (descriptor.getPropertyType().isAssignableFrom(ArrayList.class)) {
                Type[] typeArguments = ((ParameterizedType) descriptor.getReadMethod().getGenericReturnType())
                        .getActualTypeArguments();
                if (typeArguments.length == 0)
                    throw new RuntimeException("Cannot define Generic list type of " + entry.getKey()
                            + " property of " + paramClass);
                Type firstType = typeArguments[0];
                if (firstType instanceof Class) {
                    childListMappers.put(childProperty, new BeanMapper((Class) firstType, properties));
                }
            } else {
                childMappers.put(childProperty, new BeanMapper(descriptor.getPropertyType(), properties));
            }
        } else {
            throw new RuntimeException(
                    "cannot define mapping for class:" + paramClass.getName() + " property:" + childProperty);
        }
    }

    this.paramClass = paramClass;
}

From source file:com.github.mrgoro.interactivedata.spring.service.SwaggerService.java

/**
 * Get Parameters from a/*from   w  w  w  .  j ava  2s.c  om*/
 * {@link com.github.mrgoro.interactivedata.api.data.operations.OperationData OperationData}-Class.
 *
 * @param dataClass Operation Data Class
 * @param type Type of operation
 * @return Map of parameters with their names as keys
 */
private Map<String, Parameter> getParametersForClass(Class<?> dataClass, String type) {
    Map<String, Parameter> parameters = new HashMap<>();
    try {
        PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(dataClass).getPropertyDescriptors();
        for (PropertyDescriptor pd : propertyDescriptors) {
            // Class is a property descriptor => filter out
            if (pd.getReadMethod() != null && !"class".equals(pd.getName())) {
                parameters.put(pd.getName(),
                        new QueryParameter().name(pd.getName()).description(type + " param for " + pd.getName())
                                .required(false).property(getProperty(pd.getPropertyType())));
            }
        }
    } catch (IntrospectionException e) {
        log.error("Exception using introspection for generating Interactive Data API (Parameters)", e);
    }
    return parameters;
}

From source file:com.clican.pluto.common.support.spring.BeanPropertyRowMapper.java

/**
 * Initialize the mapping metadata for the given class.
 * /*  w  w w.  j  a  v  a2s  .c  o  m*/
 * @param mappedClass
 *            the mapped class.
 */
protected void initialize(Class<?> mappedClass) {
    this.mappedClass = mappedClass;
    this.mappedFields = new HashMap<String, PropertyDescriptor>();
    this.mappedProperties = new HashSet<String>();
    PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass);
    for (int i = 0; i < pds.length; i++) {
        PropertyDescriptor pd = pds[i];
        if (pd.getWriteMethod() != null) {
            this.mappedFields.put(pd.getName().toLowerCase(), pd);
            String underscoredName = underscoreName(pd.getName());
            if (!pd.getName().toLowerCase().equals(underscoredName)) {
                this.mappedFields.put(underscoredName, pd);
            }
            this.mappedProperties.add(pd.getName());
        }
    }
}

From source file:edu.harvard.med.screensaver.model.AbstractEntityInstanceTest.java

protected String fullPropName(PropertyDescriptor prop) {
    return _entityClass.getName() + "." + prop.getName();
}