Search in sources :

Example 51 with IntrospectionException

use of java.beans.IntrospectionException in project CzechIdMng by bcvsolutions.

the class CzechIdMIcConfigurationService method initDefaultConfiguration.

/**
 * Create instance of default connector configuration
 *
 * @param configurationClass
 * @return
 */
private IcConnectorConfiguration initDefaultConfiguration(Class<? extends ConfigurationClass> configurationClass) {
    try {
        ConfigurationClass configurationClassInstance = configurationClass.newInstance();
        List<IcConfigurationProperty> properties = new ArrayList<>();
        PropertyDescriptor[] descriptors = Introspector.getBeanInfo(configurationClass).getPropertyDescriptors();
        Lists.newArrayList(descriptors).stream().forEach(descriptor -> {
            Method readMethod = descriptor.getReadMethod();
            String propertyName = descriptor.getName();
            ConfigurationClassProperty property = readMethod.getAnnotation(ConfigurationClassProperty.class);
            if (property != null) {
                IcConfigurationPropertyImpl icProperty = (IcConfigurationPropertyImpl) CzechIdMIcConvertUtil.convertConfigurationProperty(property);
                icProperty.setName(propertyName);
                icProperty.setType(readMethod.getGenericReturnType().getTypeName());
                try {
                    icProperty.setValue(readMethod.invoke(configurationClassInstance));
                } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                    throw new CoreException("Cannot read value of connector configuration property!", e);
                }
                properties.add(icProperty);
            }
        });
        // Sort by order
        properties.sort(Comparator.comparing(IcConfigurationProperty::getOrder));
        IcConfigurationPropertiesImpl icProperties = new IcConfigurationPropertiesImpl();
        icProperties.setProperties(properties);
        IcConnectorConfigurationImpl configuration = new IcConnectorConfigurationImpl();
        configuration.setConnectorPoolingSupported(false);
        configuration.setConfigurationProperties(icProperties);
        return configuration;
    } catch (IntrospectionException | InstantiationException | IllegalAccessException e) {
        throw new CoreException("Cannot read connector configuration property!", e);
    }
}
Also used : ConfigurationClass(eu.bcvsolutions.idm.core.api.domain.ConfigurationClass) IcConnectorConfigurationImpl(eu.bcvsolutions.idm.ic.impl.IcConnectorConfigurationImpl) PropertyDescriptor(java.beans.PropertyDescriptor) ArrayList(java.util.ArrayList) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method) IcConfigurationPropertiesImpl(eu.bcvsolutions.idm.ic.impl.IcConfigurationPropertiesImpl) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConfigurationClassProperty(eu.bcvsolutions.idm.core.api.domain.ConfigurationClassProperty) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) IcConfigurationProperty(eu.bcvsolutions.idm.ic.api.IcConfigurationProperty) IcConfigurationPropertyImpl(eu.bcvsolutions.idm.ic.impl.IcConfigurationPropertyImpl)

Example 52 with IntrospectionException

use of java.beans.IntrospectionException in project tomee by apache.

the class HeavyweightTypeInfoBuilder method mapFields.

/**
 * Map the (nested) fields of a XML Schema Type to Java Beans properties or public fields of the specified Java Class.
 *
 * @param javaClass          the java class to map
 * @param xmlTypeInfo        the xml schema for the type
 * @param javaXmlTypeMapping the java to xml type mapping metadata
 * @param typeInfo           the JaxRpcTypeInfo for this type
 * @throws OpenEJBException if the XML Schema Type can not be mapped to the Java Class
 */
private void mapFields(Class javaClass, XmlTypeInfo xmlTypeInfo, JavaXmlTypeMapping javaXmlTypeMapping, JaxRpcTypeInfo typeInfo) throws OpenEJBException {
    // Skip arrays since they can't define a variable-mapping element
    if (!javaClass.isArray()) {
        // if there is a variable-mapping, log a warning
        if (!javaXmlTypeMapping.getVariableMapping().isEmpty()) {
            log.warn("Ignoring variable-mapping defined for class " + javaClass + " which is an array.");
        }
        return;
    }
    // Index Java bean properties by name
    Map<String, Class> properties = new HashMap<String, Class>();
    try {
        PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(javaClass).getPropertyDescriptors();
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
            properties.put(propertyDescriptor.getName(), propertyDescriptor.getPropertyType());
        }
    } catch (IntrospectionException e) {
        throw new OpenEJBException("Class " + javaClass + " is not a valid javabean", e);
    }
    for (VariableMapping variableMapping : javaXmlTypeMapping.getVariableMapping()) {
        String fieldName = variableMapping.getJavaVariableName();
        if (variableMapping.getXmlAttributeName() != null) {
            JaxRpcFieldInfo fieldInfo = new JaxRpcFieldInfo();
            fieldInfo.name = fieldName;
            // verify that the property exists on the java class
            Class javaType = properties.get(fieldName);
            if (javaType == null) {
                throw new OpenEJBException("field name " + fieldName + " not found in " + properties);
            }
            String attributeLocalName = variableMapping.getXmlAttributeName();
            QName xmlName = new QName("", attributeLocalName);
            fieldInfo.xmlName = xmlName;
            fieldInfo.xmlType = xmlTypeInfo.attributes.get(attributeLocalName);
            if (fieldInfo.xmlType == null) {
                throw new OpenEJBException("attribute " + xmlName + " not found in schema " + xmlTypeInfo.qname);
            }
            typeInfo.fields.add(fieldInfo);
        } else {
            JaxRpcFieldInfo fieldInfo = new JaxRpcFieldInfo();
            fieldInfo.isElement = true;
            fieldInfo.name = fieldName;
            // verify that the property exists on the java class or there is a public field
            Class javaType = properties.get(fieldName);
            if (javaType == null) {
                // see if it is a public field
                try {
                    Field field = javaClass.getField(fieldName);
                    javaType = field.getType();
                } catch (NoSuchFieldException e) {
                    throw new OpenEJBException("field name " + fieldName + " not found in " + properties, e);
                }
            }
            QName xmlName = new QName("", variableMapping.getXmlElementName());
            XmlElementInfo nestedElement = xmlTypeInfo.elements.get(xmlName);
            if (nestedElement == null) {
                String ns = xmlTypeInfo.qname.getNamespaceURI();
                xmlName = new QName(ns, variableMapping.getXmlElementName());
                nestedElement = xmlTypeInfo.elements.get(xmlName);
                if (nestedElement == null) {
                    throw new OpenEJBException("element " + xmlName + " not found in schema " + xmlTypeInfo.qname);
                }
            }
            fieldInfo.isNillable = nestedElement.nillable || hasEncoded;
            fieldInfo.xmlName = xmlName;
            // xml type
            if (nestedElement.xmlType != null) {
                fieldInfo.xmlType = nestedElement.xmlType;
            } else {
                QName anonymousName;
                if (xmlTypeInfo.anonymous) {
                    anonymousName = new QName(xmlTypeInfo.qname.getNamespaceURI(), xmlTypeInfo.qname.getLocalPart() + ">" + nestedElement.qname.getLocalPart());
                } else {
                    anonymousName = new QName(xmlTypeInfo.qname.getNamespaceURI(), ">" + xmlTypeInfo.qname.getLocalPart() + ">" + nestedElement.qname.getLocalPart());
                }
                fieldInfo.xmlType = anonymousName;
            }
            if (javaType.isArray()) {
                fieldInfo.minOccurs = nestedElement.minOccurs;
                fieldInfo.maxOccurs = nestedElement.maxOccurs;
            }
            typeInfo.fields.add(fieldInfo);
        }
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) PropertyDescriptor(java.beans.PropertyDescriptor) HashMap(java.util.HashMap) VariableMapping(org.apache.openejb.jee.VariableMapping) QName(javax.xml.namespace.QName) IntrospectionException(java.beans.IntrospectionException) Field(java.lang.reflect.Field)

Example 53 with IntrospectionException

use of java.beans.IntrospectionException in project tomee by apache.

the class LightweightTypeInfoBuilder method mapFields.

private void mapFields(Class javaClass, XmlTypeInfo xmlTypeInfo, JaxRpcTypeInfo typeInfo) throws OpenEJBException {
    // Map JavaBean property name to propertyType
    Map<String, Class> propertyToClass = new HashMap<String, Class>();
    try {
        for (PropertyDescriptor descriptor : Introspector.getBeanInfo(javaClass).getPropertyDescriptors()) {
            propertyToClass.put(descriptor.getName(), descriptor.getPropertyType());
        }
    } catch (IntrospectionException e) {
        throw new OpenEJBException("Class " + javaClass + " is not a valid javabean", e);
    }
    // Map the elements nexted in the XML Schema Type
    for (XmlElementInfo nestedElement : xmlTypeInfo.elements.values()) {
        String fieldName = nestedElement.qname.getLocalPart();
        Class javaType = propertyToClass.get(fieldName);
        if (javaType == null) {
            throw new OpenEJBException("Field " + fieldName + " is not defined by class " + javaClass.getName());
        }
        JaxRpcFieldInfo fieldInfo = new JaxRpcFieldInfo();
        fieldInfo.name = fieldName;
        fieldInfo.isNillable = nestedElement.nillable;
        fieldInfo.xmlName = nestedElement.qname;
        fieldInfo.xmlType = nestedElement.xmlType;
        if (javaType.isArray()) {
            fieldInfo.minOccurs = nestedElement.minOccurs;
            fieldInfo.maxOccurs = nestedElement.maxOccurs;
        }
        typeInfo.fields.add(fieldInfo);
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) PropertyDescriptor(java.beans.PropertyDescriptor) HashMap(java.util.HashMap) IntrospectionException(java.beans.IntrospectionException)

Example 54 with IntrospectionException

use of java.beans.IntrospectionException in project opennms by OpenNMS.

the class BeanInfoManager method initialize.

// -------------------------------------
/**
 * Initializes by mapping property names to BeanInfoProperties
 */
void initialize(Logger pLogger) throws ELException {
    try {
        mBeanInfo = Introspector.getBeanInfo(mBeanClass);
        mPropertyByName = new HashMap();
        mIndexedPropertyByName = new HashMap();
        PropertyDescriptor[] pds = mBeanInfo.getPropertyDescriptors();
        for (int i = 0; pds != null && i < pds.length; i++) {
            // Treat as both an indexed property and a normal property
            PropertyDescriptor pd = pds[i];
            if (pd instanceof IndexedPropertyDescriptor) {
                IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
                Method readMethod = getPublicMethod(ipd.getIndexedReadMethod());
                Method writeMethod = getPublicMethod(ipd.getIndexedWriteMethod());
                BeanInfoIndexedProperty property = new BeanInfoIndexedProperty(readMethod, writeMethod, ipd);
                mIndexedPropertyByName.put(ipd.getName(), property);
            }
            Method readMethod = getPublicMethod(pd.getReadMethod());
            Method writeMethod = getPublicMethod(pd.getWriteMethod());
            BeanInfoProperty property = new BeanInfoProperty(readMethod, writeMethod, pd);
            mPropertyByName.put(pd.getName(), property);
        }
        mEventSetByName = new HashMap();
        EventSetDescriptor[] esds = mBeanInfo.getEventSetDescriptors();
        for (int i = 0; esds != null && i < esds.length; i++) {
            EventSetDescriptor esd = esds[i];
            mEventSetByName.put(esd.getName(), esd);
        }
    } catch (IntrospectionException exc) {
        if (pLogger.isLoggingWarning()) {
            pLogger.logWarning(Constants.EXCEPTION_GETTING_BEANINFO, exc, mBeanClass.getName());
        }
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor) HashMap(java.util.HashMap) IntrospectionException(java.beans.IntrospectionException) IndexedPropertyDescriptor(java.beans.IndexedPropertyDescriptor) Method(java.lang.reflect.Method) EventSetDescriptor(java.beans.EventSetDescriptor)

Example 55 with IntrospectionException

use of java.beans.IntrospectionException in project opennms by OpenNMS.

the class SelectableItem method getPropertyDescriptors.

/**
 * <b>Note:</b>This method is simmilar to
 * {@link com.vaadin.data.util.BeanItem#getPropertyDescriptors(java.lang.Class)
 * }
 * but adds an additional <code>VaadinPropertyDescriptor</codE> to support a
 * "is selectable" feature. Because the earlier mentioned method is static,
 * we cannot overwrite it. Therefore I decided to implement it in my own
 * way.<br/>
 * <br/>
 *
 * <b>In short:</b> It lookups all methods which fullfill the java bean
 * convention of <code>clazz</code> and builds accessors for it (read
 * method, write method, etc. for a field, etc. Have a look at
 * {@link java.beans.Introspector}). And in addition we a new "accessor" for
 * a selectable item is added.<br/>
 * <br/>
 *
 * <b>In detail:</b><br/>
 * {@link java.beans.Introspector} is used to get all PropertyDescriptors
 * from the given class <code>clazz</code>. Each PropertyDescriptor is
 * converted to a vaadin <code>MethodPropertyDescriptor</code>(
 * {@link com.vaadin.data.util.MethodPropertyDescriptor} is created. In
 * addition a VaadinPropertyDescriptor is added to provide the "select"
 * feature. For this we use the <code>ObjectProperty</code> of vaadin. <br/>
 * <br/>
 *
 * The build map is a mapping between property names and the
 * <code>VaadinPropertyDescriptor</code>s, whereby
 * {@link com.vaadin.data.util.VaadinPropertyDescriptor#getName()} is
 * identically with the key of the returned map (and therefore represents
 * the property name).
 *
 * @param <T>
 * @param clazz
 * @return
 */
protected static <T> Map<String, VaadinPropertyDescriptor> getPropertyDescriptors(Class<? super T> clazz) {
    Map<String, VaadinPropertyDescriptor> mpdMap = new HashMap<String, VaadinPropertyDescriptor>();
    try {
        // add all available method property descriptors
        for (PropertyDescriptor pd : Introspector.getBeanInfo(clazz).getPropertyDescriptors()) {
            MethodPropertyDescriptor mpd = new MethodPropertyDescriptor<T>(pd.getName(), pd.getPropertyType(), pd.getReadMethod(), pd.getWriteMethod());
            mpdMap.put(pd.getName(), mpd);
        }
        // add selected property descriptor
        mpdMap.put("selected", new VaadinPropertyDescriptor<T>() {

            @Override
            public String getName() {
                return "selected";
            }

            @Override
            public Class<?> getPropertyType() {
                return Boolean.class;
            }

            @Override
            public com.vaadin.data.Property createProperty(T value) {
                return new ObjectProperty(false, Boolean.class, false);
            }
        });
    } catch (IntrospectionException ex) {
        LOG.warn("Error while introspecting class '{}'. Will continue anyway, result may not be complete.", clazz);
    }
    return mpdMap;
}
Also used : ObjectProperty(com.vaadin.data.util.ObjectProperty) VaadinPropertyDescriptor(com.vaadin.data.util.VaadinPropertyDescriptor) VaadinPropertyDescriptor(com.vaadin.data.util.VaadinPropertyDescriptor) MethodPropertyDescriptor(com.vaadin.data.util.MethodPropertyDescriptor) PropertyDescriptor(java.beans.PropertyDescriptor) HashMap(java.util.HashMap) MethodPropertyDescriptor(com.vaadin.data.util.MethodPropertyDescriptor) IntrospectionException(java.beans.IntrospectionException) ObjectProperty(com.vaadin.data.util.ObjectProperty)

Aggregations

IntrospectionException (java.beans.IntrospectionException)111 PropertyDescriptor (java.beans.PropertyDescriptor)65 Method (java.lang.reflect.Method)46 BeanInfo (java.beans.BeanInfo)44 MockJavaBean (org.apache.harmony.beans.tests.support.mock.MockJavaBean)24 InvocationTargetException (java.lang.reflect.InvocationTargetException)18 IndexedPropertyDescriptor (java.beans.IndexedPropertyDescriptor)17 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)10 Field (java.lang.reflect.Field)9 EventSetDescriptor (java.beans.EventSetDescriptor)7 List (java.util.List)6 Map (java.util.Map)6 MessageFormat (java.text.MessageFormat)5 UUID (java.util.UUID)5 Assert (org.springframework.util.Assert)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 Optional (java.util.Optional)4 RecursiveChildrenListManager (cern.gp.explorer.test.helpers.RecursiveChildrenListManager)3 SimpleDemoBean (cern.gp.explorer.test.helpers.SimpleDemoBean)3