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);
}
}
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);
}
}
}
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);
}
}
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());
}
}
}
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;
}
Aggregations