Search in sources :

Example 16 with BeanInfo

use of java.beans.BeanInfo in project coprhd-controller by CoprHD.

the class DbClientGeoVersionTest method setup.

@Before
public void setup() {
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(DummyGeoObject.class);
        PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor pd : pds) {
            if (pd.getDisplayName().equals("dummyField1")) {
                this.dummyPd1 = pd;
            }
            if (pd.getDisplayName().equals("dummyField2")) {
                this.dummyPd2 = pd;
            }
        }
        maxVersionMethod = DbClientImpl.class.getDeclaredMethod("getMaxGeoAllowedVersion", Class.class, PropertyDescriptor.class);
        this.maxVersionMethod.setAccessible(true);
        hasGeoVersionMethod = DbClientImpl.class.getDeclaredMethod("hasGeoVersionAnnotation", Class.class, PropertyDescriptor.class);
        this.hasGeoVersionMethod.setAccessible(true);
        client1 = new DbClientImpl();
        client2 = new DbClientImpl();
    } catch (Exception e) {
        loger.error("setup failed ", e);
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) Before(org.junit.Before)

Example 17 with BeanInfo

use of java.beans.BeanInfo in project coprhd-controller by CoprHD.

the class DataObjectType method init.

/**
 * Initializes cf & all column metadata for this data type
 */
private void init() {
    String cfName = _clazz.getAnnotation(Cf.class).value();
    _cf = new ColumnFamily<String, CompositeColumnName>(cfName, StringSerializer.get(), CompositeColumnNameSerializer.get());
    BeanInfo bInfo;
    try {
        bInfo = Introspector.getBeanInfo(_clazz);
    } catch (IntrospectionException ex) {
        throw DatabaseException.fatals.serializationFailedInitializingBeanInfo(_clazz, ex);
    }
    PropertyDescriptor[] pds = bInfo.getPropertyDescriptors();
    for (int i = 0; i < pds.length; i++) {
        PropertyDescriptor pd = pds[i];
        // skip class property
        if (!isColumnField(bInfo.getBeanDescriptor().getBeanClass().getName(), pd)) {
            _log.info("Not column field, skip {}.{}", bInfo.getBeanDescriptor().getBeanClass().getName(), pd.getName());
            continue;
        }
        ColumnField col = new ColumnField(this, pd);
        if (col.getType() == ColumnField.ColumnType.Id) {
            _idField = col;
            continue;
        }
        _columnFieldMap.put(col.getName(), col);
        if (col.isLazyLoaded()) {
            _lazyLoadedFields.add(col);
        }
    }
    // Need to resolve field cross references here....
    Collection<ColumnField> fields = _columnFieldMap.values();
    for (ColumnField field : fields) {
        DbIndex index = field.getIndex();
        if (index instanceof AggregateDbIndex) {
            String[] groupByArr = ((AggregateDbIndex) index).getGroupBy();
            for (String groupByName : groupByArr) {
                ColumnField groupField = _columnFieldMap.get(groupByName);
                // The index for this field will be cleared together with the index of the referenced field
                if (groupField == null || groupField.getIndex() == null) {
                    DatabaseException.fatals.invalidAnnotation("AggregateIndex", "property " + groupByName + " does not have a valid value or referenced another indexed field");
                }
                ((AggregateDbIndex) index).addGroupByField(_columnFieldMap.get(groupByName));
                if (groupField != null && groupField.getDependentFields() != null) {
                    groupField.getDependentFields().add(field);
                }
                field.getRefFields().add(groupField);
            }
            if (!field.getRefFields().isEmpty()) {
                _preprocessedFields.add(field);
            }
        }
    }
    // initialization for lazy loading
    lazyLoadInit();
}
Also used : Cf(com.emc.storageos.db.client.model.Cf) PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException)

Example 18 with BeanInfo

use of java.beans.BeanInfo in project coprhd-controller by CoprHD.

the class PropertyListDataObjectMapper method map.

public static <T> PropertyListDataObject map(T from, String type) {
    if (from == null) {
        return null;
    }
    PropertyListDataObject to = new PropertyListDataObject();
    to.setResourceType(type);
    try {
        BeanInfo bInfo = Introspector.getBeanInfo(from.getClass());
        PropertyDescriptor[] pds = bInfo.getPropertyDescriptors();
        for (int i = 0; i < pds.length; i++) {
            PropertyDescriptor pd = pds[i];
            // skip class property
            String pdName = pd.getName();
            if (pd.getName().equals("class")) {
                continue;
            }
            if (pdName.equals("Id")) {
                if (URI.class.isAssignableFrom(pd.getPropertyType())) {
                    URI value = (URI) pd.getReadMethod().invoke(from);
                    to.setId(value);
                }
            }
            if (pdName.equals("creationTime")) {
                if (Calendar.class.isAssignableFrom(pd.getPropertyType())) {
                    Calendar value = (Calendar) pd.getReadMethod().invoke(from);
                    to.setCreationTime(value);
                }
            } else {
                Object value = pd.getReadMethod().invoke(from);
                to.getResourceData().put(pdName, value.toString());
            }
        }
        return to;
    } catch (Exception ex) {
        throw DatabaseException.fatals.deserializationFailed(from.getClass(), ex);
    }
}
Also used : PropertyListDataObject(com.emc.storageos.db.client.model.PropertyListDataObject) PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) Calendar(java.util.Calendar) PropertyListDataObject(com.emc.storageos.db.client.model.PropertyListDataObject) URI(java.net.URI) DatabaseException(com.emc.storageos.db.exceptions.DatabaseException)

Example 19 with BeanInfo

use of java.beans.BeanInfo in project entando-core by entando.

the class AbstractObjectInfoTag method getPropertyValue.

protected Object getPropertyValue(Object masterObject, String propertyValue) {
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(masterObject.getClass());
        PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
        for (int i = 0; i < descriptors.length; i++) {
            PropertyDescriptor descriptor = descriptors[i];
            if (!descriptor.getName().equals(propertyValue)) {
                continue;
            }
            Method method = descriptor.getReadMethod();
            Object[] args = null;
            return method.invoke(masterObject, args);
        }
        _logger.debug("Invalid required object property : Master Object '{}' - property '{}'", masterObject.getClass().getName(), propertyValue);
    } catch (Throwable t) {
        _logger.error("Error extracting property value : Master Object {} - property: '{}'", masterObject.getClass().getName(), propertyValue, t);
    }
    return null;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) Method(java.lang.reflect.Method)

Example 20 with BeanInfo

use of java.beans.BeanInfo in project coprhd-controller by CoprHD.

the class BeanTestUtils method testGettersAndSetters.

public static void testGettersAndSetters(Object bean) {
    Assert.assertNotNull(bean);
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
        PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
        Assert.assertNotNull("No properties for " + bean.getClass(), properties);
        for (PropertyDescriptor property : properties) {
            Class<?> type = property.getPropertyType();
            if (isSupportedType(type)) {
                boolean canRead = property.getReadMethod() != null;
                boolean canWrite = property.getWriteMethod() != null;
                if (canRead && canWrite) {
                    testGetterAndSetter(bean, property.getName(), type);
                }
            }
        }
    } catch (IntrospectionException e) {
        throw new RuntimeException(e);
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException)

Aggregations

BeanInfo (java.beans.BeanInfo)420 PropertyDescriptor (java.beans.PropertyDescriptor)328 Method (java.lang.reflect.Method)217 SimpleBeanInfo (java.beans.SimpleBeanInfo)167 FakeFox01BeanInfo (org.apache.harmony.beans.tests.support.mock.FakeFox01BeanInfo)161 IndexedPropertyDescriptor (java.beans.IndexedPropertyDescriptor)148 IntrospectionException (java.beans.IntrospectionException)115 InvocationTargetException (java.lang.reflect.InvocationTargetException)38 HashMap (java.util.HashMap)33 Test (org.junit.jupiter.api.Test)33 ArrayList (java.util.ArrayList)30 Field (java.lang.reflect.Field)17 Map (java.util.Map)17 Test (org.junit.Test)13 EventSetDescriptor (java.beans.EventSetDescriptor)12 MethodDescriptor (java.beans.MethodDescriptor)12 List (java.util.List)11 BeanDescriptor (java.beans.BeanDescriptor)10 HashSet (java.util.HashSet)8 Introspector (java.beans.Introspector)7