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.avanza.ymer.MongoQueryFactory.java

private boolean isNotTemplatableMethod(PropertyDescriptor pd) {
    return pd.getReadMethod() == null || pd.getReadMethod().getDeclaringClass() == Object.class
            || pd.getWriteMethod() == null || pd.getName().equals("versionID");
}

From source file:be.fgov.kszbcss.rhq.websphere.component.jdbc.db2.pool.ConnectionContextImpl.java

ConnectionContextImpl(Map<String, Object> properties) {
    dataSource = new DB2SimpleDataSource();
    BeanInfo beanInfo;/*ww w .  j  a v  a2  s . c o  m*/
    try {
        beanInfo = Introspector.getBeanInfo(DB2SimpleDataSource.class);
    } catch (IntrospectionException ex) {
        throw new Error(ex);
    }
    for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {
        String name = descriptor.getName();
        if (properties.containsKey(name)) {
            Object value = properties.get(name);
            Class<?> propertyType = descriptor.getPropertyType();
            if (log.isDebugEnabled()) {
                log.debug("Setting property " + name + ": propertyType=" + propertyType.getName() + ", value="
                        + value + " (class=" + (value == null ? "<N/A>" : value.getClass().getName()) + ")");
            }
            if (propertyType != String.class && value instanceof String) {
                // Need to convert value to correct type
                if (propertyType == Integer.class || propertyType == Integer.TYPE) {
                    value = Integer.valueOf((String) value);
                }
                if (log.isDebugEnabled()) {
                    log.debug("Converted value to " + value + " (class=" + value.getClass().getName() + ")");
                }
            }
            try {
                descriptor.getWriteMethod().invoke(dataSource, value);
            } catch (IllegalArgumentException ex) {
                throw new RuntimeException("Failed to set '" + name + "' property", ex);
            } catch (IllegalAccessException ex) {
                throw new IllegalAccessError(ex.getMessage());
            } catch (InvocationTargetException ex) {
                Throwable cause = ex.getCause();
                if (cause instanceof RuntimeException) {
                    throw (RuntimeException) cause;
                } else if (cause instanceof Error) {
                    throw (Error) cause;
                } else {
                    throw new RuntimeException(ex);
                }
            }
        }
    }
}

From source file:com.cloudbees.plugins.credentials.matchers.BeanPropertyMatcher.java

/**
 * {@inheritDoc}//from  w  w  w .  j a v a  2s .c om
 */
@Override
public boolean matches(@NonNull Credentials item) {
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(item.getClass());
        for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
            if (name.equals(pd.getName())) {
                Method readMethod = pd.getReadMethod();
                if (readMethod == null) {
                    return false; // we cannot read it therefore it cannot be a match
                }
                try {
                    Object actual = readMethod.invoke(item);
                    return expected == null ? actual == null : expected.equals(actual);
                } catch (IllegalAccessException e) {
                    return false; // if we cannot access it then it's not a match
                } catch (InvocationTargetException e) {
                    return false; // if we cannot access it then it's not a match
                }
            }
        }
        return false; // if there is no corresponding property then it cannot be a match
    } catch (IntrospectionException e) {
        return false; // if we cannot introspect it then it cannot be a match
    }
}

From source file:net.sf.json.util.DynaBeanToBeanMorpher.java

/**
 * DOCUMENT ME!/*from   w  ww.j a  va2 s  .  c  o  m*/
 *
 * @param value DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 */
public Object morph(Object value) {
    if (value == null) {
        return null;
    }

    if (!supports(value.getClass())) {
        throw new MorphException("value is not a DynaBean");
    }

    Object bean = null;

    try {
        bean = beanClass.newInstance();

        DynaBean dynaBean = (DynaBean) value;
        DynaClass dynaClass = dynaBean.getDynaClass();
        PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(beanClass);

        for (int i = 0; i < pds.length; i++) {
            PropertyDescriptor pd = pds[i];
            String name = pd.getName();
            DynaProperty dynaProperty = dynaClass.getDynaProperty(name);

            if (dynaProperty != null) {
                Class dynaType = dynaProperty.getType();
                Class type = pd.getPropertyType();

                if (type.isAssignableFrom(dynaType)) {
                    PropertyUtils.setProperty(bean, name, dynaBean.get(name));
                } else {
                    if (IdentityObjectMorpher.getInstance() == morpherRegistry.getMorpherFor(type)) {
                        throw new MorphException("Can't find a morpher for target class " + type);
                    } else {
                        PropertyUtils.setProperty(bean, name, morpherRegistry.morph(type, dynaBean.get(name)));
                    }
                }
            }
        }
    } catch (InstantiationException e) {
        throw new MorphException(e);
    } catch (IllegalAccessException e) {
        throw new MorphException(e);
    } catch (InvocationTargetException e) {
        throw new MorphException(e);
    } catch (NoSuchMethodException e) {
        throw new MorphException(e);
    }

    return bean;
}

From source file:com.heren.turtle.server.utils.TransUtils.java

public Map<String, Object> beanToMap(Object obj) {
    Map<String, Object> resultMap = new HashMap<>();
    if (obj == null) {
        return null;
    }/*  www.j  a  v  a 2 s  .c o m*/
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            String key = property.getName();
            if (!key.equals("class")) {
                Method getter = property.getReadMethod();
                Object value = getter.invoke(obj);
                resultMap.put(key, value);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("transBean2Map Error " + e);
    }
    return resultMap;
}

From source file:com.heren.turtle.server.utils.TransUtils.java

public Object toTransObject(Object obj) {
    if (obj == null)
        return null;
    try {/*from ww  w  .j av  a2  s  . co m*/
        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            String key = property.getName();
            if (!key.equals("class")) {
                Method getter = property.getReadMethod();
                Object value = getter.invoke(obj);
                if (value instanceof String) {
                    Method setter = property.getWriteMethod();
                    Object newValue = toTrans(value);
                    setter.invoke(obj, newValue);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return obj;
}

From source file:com.sunsprinter.diffunit.core.translators.ToXmlTranslator.java

@Override
protected String doTranslate(final T object) throws TranslationException {
    String currentPropertyName = "DIFFUNIT UNKNOWN";
    try {//w w  w  .  j  a va2  s  .  c om
        final StringBuilder sb = new StringBuilder();

        if (getIncludeEnclosingTags()) {
            sb.append(createStartTag(object.getClass().getSimpleName(), object,
                    getIncludeOuterElementInstanceNumber()));
        }

        final Collection<PropertyDescriptor> eligibleProperties = determinePropertiesEligibleForTranslation(
                object);
        for (final PropertyDescriptor propertyDescriptor : eligibleProperties) {
            currentPropertyName = propertyDescriptor.getName();
            final Object propertyValue = propertyDescriptor.getReadMethod().invoke(object);
            String propertyValueAsString = getDelegateTranslator().translate(propertyValue);
            if (getEscapePropertyValues()) {
                propertyValueAsString = StringEscapeUtils.escapeXml(propertyValueAsString);
            }
            sb.append(String.format("%s%s%s",
                    createStartTag(propertyDescriptor.getName(), propertyValue,
                            getIncludeInnerElementInstanceNumber()),
                    propertyValueAsString, createEndTag(propertyDescriptor.getName())));
        }

        if (getIncludeEnclosingTags()) {
            sb.append(createEndTag(object.getClass().getSimpleName()));
        }

        return sb.toString();
    } catch (final Exception e) {
        throw new TranslationException(object, String.format("Unable to translate property '%s' of object '%s'",
                currentPropertyName, getInstanceTracker().getObjectId(object)), e);
    }
}

From source file:com.heren.turtle.server.utils.TransUtils.java

public Object U2IObject(Object obj) {
    Object newObj = new Object();
    if (obj == null) {
        return null;
    }//from  w  w  w  .  j  a  v a2s .  c  o  m
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            String key = property.getName();
            if (!key.equals("class")) {
                Method getter = property.getReadMethod();
                Object value = getter.invoke(obj);
                if (value instanceof String) {
                    Method setter = property.getWriteMethod();
                    Object newValue = U2I((String) value);
                    setter.invoke(obj, newValue);
                }
            }
        }
        newObj = obj;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return newObj;
}

From source file:jp.co.opentone.bsol.framework.test.dataset.bean.BeanTable.java

private ITableMetaData createTableMetaData() throws DataSetException {
    if (beans.isEmpty()) {
        return new DefaultTableMetaData(tableName, new Column[0]);
    } else {/*from w w w  . j  ava 2  s  .  c o  m*/
        try {
            List<Column> columns = new ArrayList<Column>();
            BeanInfo info = Introspector.getBeanInfo(beans.get(0).getClass());
            PropertyDescriptor[] descs = info.getPropertyDescriptors();
            for (PropertyDescriptor desc : descs) {
                if (!isIgnoreProperty(desc)) {
                    System.out.println(desc.getName());
                    columns.add(new Column(desc.getName(), DataType.UNKNOWN));
                }
            }

            Column[] c = (Column[]) columns.toArray(new Column[columns.size()]);
            return new DefaultTableMetaData(tableName, c);
        } catch (IntrospectionException e) {
            throw new DataSetException(e);
        }
    }
}

From source file:net.sf.beanlib.provider.BeanChecker.java

/** 
 * @param fBean from bean/* ww w . j a  v  a  2s.  c om*/
 * @param tBean to bean
 * @return true if the two beans are equal in a JavaBean sense.
 * ie. if they have the same number of properties, 
 * and the properties that can be read contain the same values.
 * 
 * TODO: unit test me
 */
public boolean beanEquals(Object fBean, Object tBean) {
    if (fBean == tBean)
        return true;
    if (fBean == null || tBean == null)
        return false;
    try {
        BeanInfo bi_f = Introspector.getBeanInfo(fBean.getClass());
        PropertyDescriptor[] pda_f = bi_f.getPropertyDescriptors();

        Map<?, ?> tMap = beanGetter.getPropertyName2DescriptorMap(tBean.getClass());

        if (pda_f.length != tMap.size())
            return false;

        for (int i = pda_f.length - 1; i > -1; i--) {
            PropertyDescriptor pd_f = pda_f[i];
            PropertyDescriptor pd_t = (PropertyDescriptor) tMap.get(pd_f.getName());
            Method m_f = pd_f.getReadMethod();
            Method m_t = pd_t.getReadMethod();

            if (m_f == null) {
                if (m_t == null)
                    continue;
                return false;
            }
            if (m_t == null)
                return false;
            Object v_f = m_f.invoke(fBean);
            Object v_t = m_t.invoke(tBean);

            if (!new EqualsBuilder().append(v_f, v_t).isEquals())
                return false;
        }
        return true;
    } catch (IntrospectionException e) {
        log.error("", e);
        throw new BeanlibException(e);
    } catch (IllegalAccessException e) {
        log.error("", e);
        throw new BeanlibException(e);
    } catch (InvocationTargetException e) {
        log.error("", e.getTargetException());
        throw new BeanlibException(e.getTargetException());
    }
}