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.opentele.server.core.util.CustomGroovyBeanJSONMarshaller.java

public void marshalObject(Object o, JSON json) throws ConverterException {
    JSONWriter writer = json.getWriter();
    try {//  ww  w .  j  a  va2s  . c  o m
        writer.object();
        for (PropertyDescriptor property : BeanUtils.getPropertyDescriptors(o.getClass())) {
            String name = property.getName();
            Method readMethod = property.getReadMethod();
            if (readMethod != null && !(name.equals("metaClass")) && !(name.equals("class"))) {
                Object value = readMethod.invoke(o, (Object[]) null);
                writer.key(name);
                json.convertAnother(value);
            }
        }
        for (Field field : o.getClass().getDeclaredFields()) {
            int modifiers = field.getModifiers();
            if (Modifier.isPublic(modifiers)
                    && !(Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers))) {
                writer.key(field.getName());
                json.convertAnother(field.get(o));
            }
        }
        writer.endObject();
    } catch (ConverterException ce) {
        throw ce;
    } catch (Exception e) {
        throw new ConverterException("Error converting Bean with class " + o.getClass().getName(), e);
    }
}

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

/**
 * Instantiates a new property attr setter getter.
 *
 * @param propDescriptor the prop descriptor
 *//*from  w  ww .  j  av a  2  s.c  om*/
public PropertyAttrSetterGetter(PropertyDescriptor propDescriptor) {
    this.propertyName = propDescriptor.getName();
    this.propDescriptor = propDescriptor;
}

From source file:com.subakva.formicid.options.ParameterHandler.java

protected HashMap<String, PropertyDescriptor> getWritableProperties(Class taskClass) {
    HashMap<String, PropertyDescriptor> writableProperties = new HashMap<String, PropertyDescriptor>();
    PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(taskClass);
    for (PropertyDescriptor descriptor : descriptors) {
        if (descriptor.getWriteMethod() != null && !excluded.contains(descriptor.getName())) {
            writableProperties.put(descriptor.getName().toLowerCase(), descriptor);
        }/*from  w  w  w .  ja v a 2 s  .c o m*/
    }
    return writableProperties;
}

From source file:ru.ilb.common.jpa.tools.DescriptorUtils.java

public void fixInverseLinks(Object entity) {
    final BeanWrapper src = new BeanWrapperImpl(entity);
    java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
    ClassDescriptor cd = entityManager.unwrap(Session.class).getDescriptor(entity);
    if (cd == null) {
        return;/*from   www  .  j a va2s.c o  m*/
    }
    for (java.beans.PropertyDescriptor pd : pds) {
        DatabaseMapping dm = cd.getMappingForAttributeName(pd.getName());
        if (dm != null) {
            if (dm instanceof OneToManyMapping) {
                OneToManyMapping dmOtM = (OneToManyMapping) dm;
                if (dmOtM.getMappedBy() != null) {
                    List srcValue = (List) src.getPropertyValue(pd.getName());
                    if (srcValue != null) {
                        for (Object v : srcValue) {
                            final BeanWrapper srcv = new BeanWrapperImpl(v);
                            srcv.setPropertyValue(dmOtM.getMappedBy(), entity);
                            fixInverseLinks(v);
                        }
                    }

                }
            }
            if (dm instanceof OneToOneMapping) {
                OneToOneMapping dmOtO = (OneToOneMapping) dm;
                if (dmOtO.getMappedBy() != null) {
                    Object srcValue = src.getPropertyValue(pd.getName());
                    if (srcValue != null) {
                        final BeanWrapper srcv = new BeanWrapperImpl(srcValue);
                        srcv.setPropertyValue(dmOtO.getMappedBy(), entity);
                        fixInverseLinks(srcValue);
                    }

                }
            }
        }

    }
}

From source file:com.erinors.hpb.server.handler.HibernateEmbeddedObjectHandler.java

@Override
public Object merge(MergingContext context, Object object) {
    if (object == null || !object.getClass().isAnnotationPresent(Embeddable.class)) {
        return null;
    }/*from  www  . ja v  a2s.co m*/

    boolean empty = true;
    try {
        for (PropertyDescriptor pd : PropertyUtils.getPropertyDescriptors(object)) {
            String propertyName = pd.getName();

            if (!"class".equals(propertyName) && !pd.getReadMethod().isAnnotationPresent(Transient.class)) {
                if (PropertyUtils.getSimpleProperty(object, propertyName) != null) {
                    empty = false;
                    break;
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Error while checking embedded object: " + object, e);
    }

    if (empty) {
        context.addProcessedObject(object, null);
        return ProcessedToNull;
    } else {
        return null;
    }
}

From source file:com.mycollab.db.persistence.service.DefaultService.java

@Override
public void removeByCriteria(S criteria, Integer accountId) {
    boolean isValid = false;
    try {//w  w  w. j a  v a  2s . c om
        PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(criteria);

        for (PropertyDescriptor descriptor : propertyDescriptors) {
            String propName = descriptor.getName();
            if ((descriptor.getPropertyType().getGenericSuperclass() == SearchField.class)
                    && (PropertyUtils.getProperty(criteria, propName) != null)) {
                isValid = true;
                break;
            }

        }
    } catch (Exception e) {
        LOG.debug("Error while validating criteria", e);
    }
    if (isValid) {
        getSearchMapper().removeByCriteria(criteria);
    }

}

From source file:com.impetus.kundera.metadata.processor.AbstractEntityFieldProcessor.java

/**
* Populates @Id accesser methods like, getId and setId of clazz to
* metadata./* w w w . j a  va 2  s.  c  om*/
* 
* @param metadata
*            the metadata
* @param clazz
*            the clazz
* @param f
*            the f
*/
protected final void populateIdAccessorMethods(EntityMetadata metadata, Class<?> clazz, Field f) {
    try {
        BeanInfo info = Introspector.getBeanInfo(clazz);

        for (PropertyDescriptor descriptor : info.getPropertyDescriptors()) {
            if (descriptor.getName().equals(f.getName())) {
                metadata.setReadIdentifierMethod(descriptor.getReadMethod());
                metadata.setWriteIdentifierMethod(descriptor.getWriteMethod());
                return;
            }
        }
    } catch (IntrospectionException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.esofthead.mycollab.core.persistence.service.DefaultService.java

@Override
public void removeByCriteria(S criteria, int accountId) {
    boolean isValid = false;
    try {/*from ww w. j av a 2 s.c o m*/
        PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(criteria);

        for (PropertyDescriptor descriptor : propertyDescriptors) {
            String propName = descriptor.getName();
            if ((descriptor.getPropertyType().getGenericSuperclass() == SearchField.class)
                    && (PropertyUtils.getProperty(criteria, propName) != null)) {
                isValid = true;
                break;
            }

        }
    } catch (Exception e) {
        LOG.debug("Error while validating criteria", e);
    }
    if (isValid) {
        getSearchMapper().removeByCriteria(criteria);
    }

}

From source file:com.unboundid.scim2.common.utils.JsonRefBeanSerializer.java

/**
 * {@inheritDoc}//  w  w w .ja v  a 2 s  .  c o  m
 */
@Override
public void serialize(final Object value, final JsonGenerator gen, final SerializerProvider serializers)
        throws IOException {
    Class<?> clazz = value.getClass();
    try {
        gen.writeStartObject();
        Collection<PropertyDescriptor> propertyDescriptors = SchemaUtils.getPropertyDescriptors(clazz);
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
            Field field = SchemaUtils.findField(clazz, propertyDescriptor.getName());
            if (field == null) {
                continue;
            }
            field.setAccessible(true);
            Object obj = field.get(value);
            if (obj instanceof JsonReference) {
                JsonReference<?> reference = (JsonReference<?>) obj;
                if (reference.isSet()) {
                    gen.writeFieldName(field.getName());
                    serializers.defaultSerializeValue(reference.getObj(), gen);
                }
            }
        }
        gen.writeEndObject();
    } catch (IntrospectionException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

}

From source file:org.grails.datastore.mapping.engine.EntityAccess.java

/**
 * Refreshes the object from entity state.
 *///www  .  j  a v a  2 s  .  c o m
public void refresh() {
    final PropertyDescriptor[] descriptors = beanWrapper.getPropertyDescriptors();
    for (PropertyDescriptor descriptor : descriptors) {
        final String name = descriptor.getName();
        if (EXCLUDED_PROPERTIES.contains(name)) {
            continue;
        }

        if (!beanWrapper.isReadableProperty(name) || !beanWrapper.isWritableProperty(name)) {
            continue;
        }

        Object newValue = getProperty(name);
        setProperty(name, newValue);
    }
}