Search in sources :

Example 66 with BeanInfo

use of java.beans.BeanInfo in project jmeter-plugins by undera.

the class RedisDataSet method setProperty.

/**
 * Override the setProperty method in order to convert
 * the original String calcMode property.
 * This used the locale-dependent display value, so caused
 * problems when the language was changed.
 * Note that the calcMode StringProperty is replaced with an IntegerProperty
 * so the conversion only needs to happen once.
 */
@Override
public void setProperty(JMeterProperty property) {
    if (property instanceof StringProperty) {
        final String pn = property.getName();
        if (pn.equals("getMode")) {
            final Object objectValue = property.getObjectValue();
            try {
                final BeanInfo beanInfo = Introspector.getBeanInfo(this.getClass());
                final ResourceBundle rb = (ResourceBundle) beanInfo.getBeanDescriptor().getValue(GenericTestBeanCustomizer.RESOURCE_BUNDLE);
                for (Enum<GetMode> e : GetMode.values()) {
                    final String propName = e.toString();
                    if (objectValue.equals(rb.getObject(propName))) {
                        final int tmpMode = e.ordinal();
                        if (log.isDebugEnabled()) {
                            log.debug("Converted " + pn + "=" + objectValue + " to mode=" + tmpMode + " using Locale: " + rb.getLocale());
                        }
                        super.setProperty(pn, tmpMode);
                        return;
                    }
                }
                log.warn("Could not convert " + pn + "=" + objectValue + " using Locale: " + rb.getLocale());
            } catch (IntrospectionException e) {
                log.error("Could not find BeanInfo", e);
            }
        } else if (pn.equals("whenExhaustedAction")) {
            final Object objectValue = property.getObjectValue();
            try {
                final BeanInfo beanInfo = Introspector.getBeanInfo(this.getClass());
                final ResourceBundle rb = (ResourceBundle) beanInfo.getBeanDescriptor().getValue(GenericTestBeanCustomizer.RESOURCE_BUNDLE);
                for (Enum<WhenExhaustedAction> e : WhenExhaustedAction.values()) {
                    final String propName = e.toString();
                    if (objectValue.equals(rb.getObject(propName))) {
                        final int tmpMode = e.ordinal();
                        if (log.isDebugEnabled()) {
                            log.debug("Converted " + pn + "=" + objectValue + " to mode=" + tmpMode + " using Locale: " + rb.getLocale());
                        }
                        super.setProperty(pn, tmpMode);
                        return;
                    }
                }
                log.warn("Could not convert " + pn + "=" + objectValue + " using Locale: " + rb.getLocale());
            } catch (IntrospectionException e) {
                log.error("Could not find BeanInfo", e);
            }
        }
    }
    super.setProperty(property);
}
Also used : BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) StringProperty(org.apache.jmeter.testelement.property.StringProperty) ResourceBundle(java.util.ResourceBundle)

Example 67 with BeanInfo

use of java.beans.BeanInfo in project javautils by jiadongpo.

the class BeanFactory method add.

public static void add(Class clazz) throws IntrospectionException, ClassNotFoundException {
    String className = clazz.getName();
    if (!ValidUtil.isValid(BEAN_SIMPLE_PROPERTIES.get(className))) {
        BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
        PropertyDescriptor[] proDescrtptors = beanInfo.getPropertyDescriptors();
        if (proDescrtptors != null && proDescrtptors.length > 0) {
            Map<String, BeanStruct> simpleProperties = new Hashtable<>();
            Map<String, BeanStruct> simplePropertiesIgnore = new Hashtable<>();
            for (PropertyDescriptor propDesc : proDescrtptors) {
                String fieldName = propDesc.getName();
                if (!"class".equals(fieldName)) {
                    Object type = propDesc.getPropertyType();
                    Method readMethod = propDesc.getReadMethod();
                    Method writeMethod = propDesc.getWriteMethod();
                    boolean isDeclared = isDeclaredField(className, fieldName);
                    simpleProperties.put(fieldName, new BeanStruct(fieldName, type, readMethod, writeMethod, isDeclared));
                    simplePropertiesIgnore.put(fieldName.toLowerCase(), new BeanStruct(fieldName, type, readMethod, writeMethod, isDeclared));
                }
            }
            BEAN_SIMPLE_PROPERTIES.put(className, simpleProperties);
            BEAN_SIMPLE_PROPERTIESIGNORE.put(className, simplePropertiesIgnore);
        }
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) Hashtable(java.util.Hashtable) BeanInfo(java.beans.BeanInfo) Method(java.lang.reflect.Method)

Example 68 with BeanInfo

use of java.beans.BeanInfo in project dble by actiontech.

the class ParameterMapping method getDescriptors.

/**
 * @param clazz
 * @return
 */
private static PropertyDescriptor[] getDescriptors(Class<?> clazz) {
    PropertyDescriptor[] pds;
    List<PropertyDescriptor> list;
    PropertyDescriptor[] pds2 = DESCRIPTORS.get(clazz);
    // clazz init for first time?
    if (null == pds2) {
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
            pds = beanInfo.getPropertyDescriptors();
            list = new ArrayList<>();
            // add property it type is not null
            for (PropertyDescriptor pd : pds) {
                if (null != pd.getPropertyType()) {
                    list.add(pd);
                }
            }
            pds2 = new PropertyDescriptor[list.size()];
            list.toArray(pds2);
        } catch (IntrospectionException ie) {
            LOGGER.info("ParameterMappingError", ie);
            pds2 = new PropertyDescriptor[0];
        }
    }
    DESCRIPTORS.put(clazz, pds2);
    return (pds2);
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException)

Example 69 with BeanInfo

use of java.beans.BeanInfo in project dble by actiontech.

the class ObjectUtil method copyProperties.

public static void copyProperties(Object fromObj, Object toObj) {
    Class<?> fromClass = fromObj.getClass();
    Class<?> toClass = toObj.getClass();
    try {
        BeanInfo fromBean = Introspector.getBeanInfo(fromClass);
        BeanInfo toBean = Introspector.getBeanInfo(toClass);
        PropertyDescriptor[] toPd = toBean.getPropertyDescriptors();
        List<PropertyDescriptor> fromPd = Arrays.asList(fromBean.getPropertyDescriptors());
        for (PropertyDescriptor propertyDescriptor : toPd) {
            propertyDescriptor.getDisplayName();
            PropertyDescriptor pd = fromPd.get(fromPd.indexOf(propertyDescriptor));
            if (pd.getDisplayName().equals(propertyDescriptor.getDisplayName()) && !pd.getDisplayName().equals("class") && propertyDescriptor.getWriteMethod() != null) {
                propertyDescriptor.getWriteMethod().invoke(toObj, pd.getReadMethod().invoke(fromObj, null));
            }
        }
    } catch (IntrospectionException | InvocationTargetException | IllegalAccessException | IllegalArgumentException e) {
        throw new RuntimeException(e);
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 70 with BeanInfo

use of java.beans.BeanInfo in project core by weld.

the class JsonObjects method createContextualInstanceJson.

static String createContextualInstanceJson(Bean<?> bean, Object contextualInstance, Probe probe) {
    try {
        JsonObjectBuilder builder = createSimpleBeanJson(bean, probe);
        builder.add(SCOPE, simplifiedScope(bean.getScope()));
        builder.add(CONTEXT_ID, Components.getInspectableScopeId(bean.getScope()));
        builder.add(CLASS, contextualInstance.getClass().getName());
        builder.add(HASH, contextualInstance.hashCode());
        builder.add(AS_STRING, Strings.abbreviate(contextualInstance.toString(), CONTEXTUAL_INSTANCE_TO_STRING_LIMIT));
        JsonArrayBuilder propertiesBuilder = Json.arrayBuilder();
        Class<?> definingClass = contextualInstance.getClass();
        if (ProxyObject.class.isAssignableFrom(definingClass)) {
            definingClass = definingClass.getSuperclass();
        }
        BeanInfo bi = java.beans.Introspector.getBeanInfo(definingClass);
        PropertyDescriptor[] properties = bi.getPropertyDescriptors();
        for (PropertyDescriptor propertyDescriptor : properties) {
            Method readMethod = propertyDescriptor.getReadMethod();
            if (readMethod == null) {
                continue;
            }
            Object value;
            try {
                value = readMethod.invoke(contextualInstance);
            } catch (IllegalAccessException e) {
                value = toString(e);
            } catch (IllegalArgumentException e) {
                value = toString(e);
            } catch (InvocationTargetException e) {
                value = toString(e);
            }
            propertiesBuilder.add(Json.objectBuilder().add(NAME, propertyDescriptor.getDisplayName()).add(VALUE, value != null ? Strings.abbreviate(value.toString(), CONTEXTUAL_INSTANCE_PROPERTY_VALUE_LIMIT) : "null"));
        }
        builder.add(PROPERTIES, propertiesBuilder);
        return builder.build();
    } catch (IntrospectionException e) {
        ProbeLogger.LOG.introspectionProblem(bean, e);
        return null;
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) ProxyObject(org.jboss.weld.bean.proxy.ProxyObject) JsonArrayBuilder(org.jboss.weld.probe.Json.JsonArrayBuilder) ObserverMethod(javax.enterprise.inject.spi.ObserverMethod) ContainerLifecycleEventObserverMethod(org.jboss.weld.event.ContainerLifecycleEventObserverMethod) AnnotatedMethod(javax.enterprise.inject.spi.AnnotatedMethod) Method(java.lang.reflect.Method) JsonObjectBuilder(org.jboss.weld.probe.Json.JsonObjectBuilder) InvocationTargetException(java.lang.reflect.InvocationTargetException)

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