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.easyget.commons.csv.bean.HeaderColumnNameMappingStrategy.java

/**
 * builds a map of property descriptors for the Bean.
 * @return - map of property descriptors
 * @throws IntrospectionException - thrown on error getting information about the bean.
 *///  w  w  w  .j  a v  a2  s.  c o m
protected Map<String, PropertyDescriptor> loadDescriptorMap() throws IntrospectionException {
    Map<String, PropertyDescriptor> map = new HashMap<>();

    PropertyDescriptor[] descriptors;
    descriptors = loadDescriptors(getType()); //TODO refactor this class to use T instead of getType.
    for (PropertyDescriptor descriptor : descriptors) {
        map.put(descriptor.getName().toUpperCase().trim(), descriptor);
    }

    return map;
}

From source file:com.opencsv.bean.HeaderColumnNameMappingStrategy.java

/**
 * builds a map of property descriptors for the Bean.
 * @return - map of property descriptors
 * @throws IntrospectionException - thrown on error getting information about the bean.
 *///from  ww w .j  a  v a 2  s.  c  om
protected Map<String, PropertyDescriptor> loadDescriptorMap() throws IntrospectionException {
    Map<String, PropertyDescriptor> map = new HashMap<String, PropertyDescriptor>();

    PropertyDescriptor[] descriptors;
    descriptors = loadDescriptors(getType()); //TODO refactor this class to use T instead of getType.
    for (PropertyDescriptor descriptor : descriptors) {
        map.put(descriptor.getName().toUpperCase().trim(), descriptor);
    }

    return map;
}

From source file:com.agimatec.validation.util.PropertyAccess.java

public Type getJavaType() {
    /*if(Map.class.isAssignableFrom(beanClass)) {
    return beanClass. //w  ww .  j  a  v a 2  s .  co  m
    }*/
    if (rememberField != null) { // use cached field of previous access
        return rememberField.getGenericType();
    }
    for (PropertyDescriptor each : PropertyUtils.getPropertyDescriptors(beanClass)) {
        if (each.getName().equals(propertyName) && each.getReadMethod() != null) {
            return each.getReadMethod().getGenericReturnType();
        }
    }
    try { // try public field
        return beanClass.getField(propertyName).getGenericType();
    } catch (NoSuchFieldException ex2) {
        // search for private/protected field up the hierarchy
        Class theClass = beanClass;
        while (theClass != null) {
            try {
                return theClass.getDeclaredField(propertyName).getGenericType();
            } catch (NoSuchFieldException ex3) {
                // do nothing
            }
            theClass = theClass.getSuperclass();
        }
    }
    return Object.class; // unknown type: allow any type?? 
}

From source file:es.caib.zkib.jxpath.util.ValueUtils.java

/**
 * Modifies the index'th element of the bean's property represented by
 * the supplied property descriptor. Converts the value to the required
 * type if necessary./*from  w ww  .ja  v  a  2s.c o  m*/
 * @param bean to edit
 * @param propertyDescriptor indicating what to set
 * @param index int
 * @param value to set
 */
public static void setValue(Object bean, PropertyDescriptor propertyDescriptor, int index, Object value) {
    if (propertyDescriptor instanceof IndexedPropertyDescriptor) {
        try {
            IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) propertyDescriptor;
            Method method = ipd.getIndexedWriteMethod();
            if (method != null) {
                method.invoke(bean,
                        new Object[] { new Integer(index), convert(value, ipd.getIndexedPropertyType()) });
                return;
            }
        } catch (Exception ex) {
            throw new RuntimeException(
                    "Cannot access property: " + propertyDescriptor.getName() + ", " + ex.getMessage());
        }
    }
    // We will fall through if there is no indexed read
    Object collection = getValue(bean, propertyDescriptor);
    if (isCollection(collection)) {
        setValue(collection, index, value);
    } else if (index == 0) {
        setValue(bean, propertyDescriptor, value);
    } else {
        throw new RuntimeException("Not a collection: " + propertyDescriptor.getName());
    }
}

From source file:gr.abiss.calipso.jpasearch.json.serializer.FormSchemaSerializer.java

@Override
public void serialize(FormSchema schema, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonGenerationException {
    try {/*from   w w w.  j a v a  2  s  .  co m*/
        Class domainClass = schema.getDomainClass();

        if (null == domainClass) {
            throw new RuntimeException("formSchema has no domain class set");
        } else {
            jgen.writeStartObject();

            PropertyDescriptor[] descriptors = new PropertyUtilsBean().getPropertyDescriptors(domainClass);

            for (int i = 0; i < descriptors.length; i++) {

                PropertyDescriptor descriptor = descriptors[i];
                String name = descriptor.getName();
                if (!ignoredFieldNames.contains(name)) {
                    jgen.writeFieldName(name);
                    jgen.writeStartObject();

                    //                  
                    String fieldValue = getFormFieldConfig(domainClass, name);
                    if (StringUtils.isNotBlank(fieldValue)) {
                        jgen.writeRaw(fieldValue);
                    }

                    jgen.writeEndObject();
                }

            }

            jgen.writeEndObject();

        }

    } catch (Exception e) {
        LOGGER.error("Failed serializing form schema", e);
    }
}

From source file:org.onebusaway.nyc.presentation.impl.NycConfigurationServiceImpl.java

private ConfigurationBean loadSettings() {

    if (_path == null || !_path.exists())
        return new ConfigurationBean();

    try {/*  www. j  av  a 2  s  .c  o  m*/

        Properties properties = new Properties();
        properties.load(new FileReader(_path));

        ConfigurationBean bean = new ConfigurationBean();
        BeanInfo beanInfo = Introspector.getBeanInfo(ConfigurationBean.class);

        for (PropertyDescriptor desc : beanInfo.getPropertyDescriptors()) {
            String name = desc.getName();
            Object value = properties.getProperty(name);
            if (value != null) {
                Converter converter = ConvertUtils.lookup(desc.getPropertyType());
                value = converter.convert(desc.getPropertyType(), value);
                Method m = desc.getWriteMethod();
                m.invoke(bean, value);
            }
        }

        return bean;
    } catch (Exception ex) {
        throw new IllegalStateException("error loading configuration from properties file " + _path, ex);
    }
}

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

private synchronized void init(Context ctx) {
    if (descriptors != null) {
        return;//from   w  w w. j av  a2 s  .c o  m
    }
    Class<?> beanClass = CRUDParameterService.getCRUDDaoBean(ctx, argNum);
    final PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(beanClass);
    List<PropertyDescriptor> res = new ArrayList<PropertyDescriptor>(descriptors.length);
    for (PropertyDescriptor descriptor : descriptors) {
        if (CRUDDao.IGNORED_PROPS.contains(descriptor.getName())) {
            continue;
        }
        final Method readMethod = descriptor.getReadMethod();
        if (readMethod == null) {
            continue;
        }
        if (readMethod.getAnnotation(CRUDIgnore.class) != null) {
            continue;
        }
        res.add(descriptor);
    }
    this.descriptors = res;
}

From source file:net.sf.dsig.Environment.java

/**
 * <p>Initialize an object's public properties, using any environmental
 * values that have been declared.//from  w  w w  . j a  va2  s.  co m
 * 
 * @param obj the object to initialize.
 * @param prefix the prefix to add while looking at the environmental values
 */
public void init(Object obj, String prefix) {
    // Iterate through all the properties declared for the applet class
    PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(obj);
    for (PropertyDescriptor descriptor : descriptors) {
        String propertyName = descriptor.getName();
        logger.debug("Checking" + ": obj.class=" + obj.getClass() + ", propertyName=" + propertyName);

        if (descriptor.getWriteMethod() == null) {
            continue;
        }

        // Check if an environment parameter has been specified; if so, 
        // override the value with the one supplied
        String key = (prefix != null) ? prefix + propertyName : propertyName;

        Object value;
        if (descriptor.getPropertyType().isArray()) {
            value = getValues(key);
        } else {
            value = getValue(key);
        }

        if (value != null) {
            logger.debug("Setting" + ": obj.class=" + obj.getClass() + ", propertyName=" + propertyName
                    + ", value=" + value);

            try {
                BeanUtils.setProperty(obj, propertyName, value);
            } catch (Exception e) {
                logger.warn("Object initialization error", e);
            }
        }
    }
}

From source file:org.onebusaway.nyc.presentation.impl.NycConfigurationServiceImpl.java

private void saveSettings(ConfigurationBean bean) {

    if (_path == null)
        return;/*  w w w  . j  a  va2  s.co m*/

    try {

        Properties properties = new Properties();
        BeanInfo beanInfo = Introspector.getBeanInfo(ConfigurationBean.class);

        for (PropertyDescriptor desc : beanInfo.getPropertyDescriptors()) {

            String name = desc.getName();

            if (name.equals("class"))
                continue;

            Method m = desc.getReadMethod();
            Object value = m.invoke(bean);
            if (value != null) {
                properties.setProperty(name, value.toString());
            }
        }

        properties.store(new FileWriter(_path), "onebusaway-nyc configuration");
    } catch (Exception ex) {
        throw new IllegalStateException("error saving configuration to properties file " + _path, ex);
    }
}

From source file:com.espertech.esperio.SendableBeanEvent.java

/**
 * Converts mapToSend to an instance of beanClass
 * @param mapToSend - the map containing data to send into the runtime
 * @param beanClass  - type of the bean to create from mapToSend
 * @param eventTypeName - the event type name for the map event
 * @param timestamp - the timestamp for this event
 * @param scheduleSlot - the schedule slot for the entity that created this event
 *///from  w  ww .j av a  2  s .  c o  m
public SendableBeanEvent(Map<String, Object> mapToSend, Class beanClass, String eventTypeName, long timestamp,
        ScheduleSlot scheduleSlot) {
    super(timestamp, scheduleSlot);

    try {
        beanToSend = beanClass.newInstance();
        // pre-create nested properties if any, as BeanUtils does not otherwise populate 'null' objects from their respective properties
        PropertyDescriptor[] pds = ReflectUtils.getBeanSetters(beanClass);
        for (PropertyDescriptor pd : pds) {
            if (!pd.getPropertyType().isPrimitive() && !pd.getPropertyType().getName().startsWith("java")) {
                BeanUtils.setProperty(beanToSend, pd.getName(), pd.getPropertyType().newInstance());
            }
        }
        // this method silently ignores read only properties on the dest bean but we should
        // have caught them in CSVInputAdapter.constructPropertyTypes.
        BeanUtils.copyProperties(beanToSend, mapToSend);
    } catch (Exception e) {
        throw new EPException("Cannot populate bean instance", e);
    }
}