use of java.beans.BeanInfo in project lucene-solr by apache.
the class SolrPluginUtils method findSetter.
private static Method findSetter(Class<?> clazz, String setterName, String key, Class<?> paramClazz, boolean lenient) {
BeanInfo beanInfo;
try {
beanInfo = Introspector.getBeanInfo(clazz);
} catch (IntrospectionException ie) {
if (lenient) {
return null;
}
throw new RuntimeException("Error getting bean info for class : " + clazz.getName(), ie);
}
for (final boolean matchParamClazz : new boolean[] { true, false }) {
for (final MethodDescriptor desc : beanInfo.getMethodDescriptors()) {
final Method m = desc.getMethod();
final Class<?>[] p = m.getParameterTypes();
if (m.getName().equals(setterName) && p.length == 1 && (!matchParamClazz || paramClazz.equals(p[0]))) {
return m;
}
}
}
if (lenient) {
return null;
}
throw new RuntimeException("No setter corrresponding to '" + key + "' in " + clazz.getName());
}
use of java.beans.BeanInfo in project lucene-solr by apache.
the class ParseContextConfig method extract.
private void extract(Element element, SolrResourceLoader loader) throws Exception {
final NodeList xmlEntries = element.getElementsByTagName("entry");
for (int i = 0, c1 = xmlEntries.getLength(); i < c1; i++) {
final NamedNodeMap xmlEntryAttributes = xmlEntries.item(i).getAttributes();
final String className = xmlEntryAttributes.getNamedItem("class").getNodeValue();
final String implementationName = xmlEntryAttributes.getNamedItem("impl").getNodeValue();
final NodeList xmlProperties = ((Element) xmlEntries.item(i)).getElementsByTagName("property");
final Class<?> interfaceClass = loader.findClass(className, Object.class);
final BeanInfo beanInfo = Introspector.getBeanInfo(interfaceClass, Introspector.IGNORE_ALL_BEANINFO);
final HashMap<String, PropertyDescriptor> descriptorMap = new HashMap<>();
for (final PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
descriptorMap.put(pd.getName(), pd);
}
final Object instance = loader.newInstance(implementationName, Object.class);
if (!interfaceClass.isInstance(instance)) {
throw new IllegalArgumentException("Implementation class does not extend " + interfaceClass.getName());
}
for (int j = 0, c2 = xmlProperties.getLength(); j < c2; j++) {
final Node xmlProperty = xmlProperties.item(j);
final NamedNodeMap xmlPropertyAttributes = xmlProperty.getAttributes();
final String propertyName = xmlPropertyAttributes.getNamedItem("name").getNodeValue();
final String propertyValue = xmlPropertyAttributes.getNamedItem("value").getNodeValue();
final PropertyDescriptor propertyDescriptor = descriptorMap.get(propertyName);
if (propertyDescriptor == null) {
throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Unknown bean property %s in class %s", propertyName, interfaceClass.getName()));
}
final Method method = propertyDescriptor.getWriteMethod();
if (method == null) {
throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Cannot set bean property %s in class %s (no write method available)", propertyName, interfaceClass.getName()));
}
method.invoke(instance, getValueFromString(propertyDescriptor.getPropertyType(), propertyValue));
}
entries.put(interfaceClass, instance);
}
}
use of java.beans.BeanInfo in project camel by apache.
the class SObjectTree method updateGeneralObjectId.
boolean updateGeneralObjectId(final String id, final Object object) {
final Class<? extends Object> clazz = object.getClass();
final BeanInfo beanInfo;
try {
beanInfo = Introspector.getBeanInfo(clazz);
} catch (final IntrospectionException e) {
throw new IllegalStateException(e);
}
final PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
final Optional<PropertyDescriptor> maybeIdProperty = Arrays.stream(propertyDescriptors).filter(pd -> "id".equals(pd.getName())).findFirst();
if (maybeIdProperty.isPresent()) {
final Method readMethod = maybeIdProperty.get().getReadMethod();
try {
readMethod.invoke(object, id);
return true;
} catch (IllegalAccessException | InvocationTargetException e) {
throw new IllegalStateException(e);
}
}
return false;
}
use of java.beans.BeanInfo in project hibernate-orm by hibernate.
the class Cloneable method checkListeners.
private void checkListeners() {
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(getClass(), Object.class);
internalCheckListeners(beanInfo);
} catch (IntrospectionException t) {
throw new HibernateException("Unable to validate listener config", t);
} finally {
if (beanInfo != null) {
// release the jdk internal caches everytime to ensure this
// plays nicely with destroyable class-loaders
Introspector.flushFromCaches(getClass());
}
}
}
use of java.beans.BeanInfo in project powermock by powermock.
the class ReflectUtils method getPropertiesHelper.
private static PropertyDescriptor[] getPropertiesHelper(Class type, boolean read, boolean write) {
try {
BeanInfo info = Introspector.getBeanInfo(type, Object.class);
PropertyDescriptor[] all = info.getPropertyDescriptors();
if (read && write) {
return all;
}
List properties = new ArrayList(all.length);
for (int i = 0; i < all.length; i++) {
PropertyDescriptor pd = all[i];
if ((read && pd.getReadMethod() != null) || (write && pd.getWriteMethod() != null)) {
properties.add(pd);
}
}
return (PropertyDescriptor[]) properties.toArray(new PropertyDescriptor[properties.size()]);
} catch (IntrospectionException e) {
throw new CodeGenerationException(e);
}
}
Aggregations