List of usage examples for java.beans Introspector getBeanInfo
public static BeanInfo getBeanInfo(Class<?> beanClass) throws IntrospectionException
From source file:org.soybeanMilk.core.bean.PropertyInfo.java
private static PropertyInfo getPropertyInfoAnatomized(Class<?> beanClass, Map<Class<?>, PropertyInfo> localExists, int depth) { PropertyInfo cached = propertyInfoCache.get(beanClass); if (cached != null) { if (log.isDebugEnabled()) log.debug(getSpace(depth) + "got " + SbmUtils.toString(beanClass) + " property information from cache"); return cached; }/*from ww w .j a v a 2s.co m*/ if (log.isDebugEnabled()) log.debug(getSpace(depth) + "start anatomizing " + SbmUtils.toString(beanClass) + " property information"); PropertyInfo beanInfo = new PropertyInfo(beanClass); localExists.put(beanInfo.getPropType(), beanInfo); PropertyDescriptor[] pds = null; try { pds = Introspector.getBeanInfo(beanInfo.getPropType()).getPropertyDescriptors(); } catch (IntrospectionException e) { throw new RuntimeException(e); } if (pds == null || pds.length == 0) ; else { for (PropertyDescriptor pd : pds) { String name = pd.getName(); Method wm = pd.getWriteMethod(); Method rm = pd.getReadMethod(); Class<?> propertyClazz = pd.getPropertyType(); //? if (wm == null || rm == null || !Modifier.isPublic(wm.getModifiers()) || !Modifier.isPublic(rm.getModifiers())) continue; //localExists?PropertyInfo PropertyInfo exist = localExists.get(propertyClazz); if (exist == null) exist = getPropertyInfoAnatomized(propertyClazz, localExists, depth + 1); //??? PropertyInfo copied = new PropertyInfo(beanClass, propertyClazz, name, rm, wm); copied.setSubPropertyInfos(exist.getSubPropertyInfos()); beanInfo.addSubPropertyInfo(copied); if (log.isDebugEnabled()) log.debug(getSpace(depth) + " add " + SbmUtils.toString(copied)); } } if (log.isDebugEnabled()) log.debug(getSpace(depth) + "finish anatomizing " + SbmUtils.toString(beanClass) + " property information"); propertyInfoCache.putIfAbsent(beanClass, beanInfo); return beanInfo; }
From source file:kg.apc.jmeter.config.redis.RedisDataSet.java
/** * 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. *//*from w w w. ja v a 2 s.c o m*/ @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); }
From source file:com.krm.dbaudit.common.utils.Collections3.java
/** * Map JavaBean //from ww w . jav a 2s. co m * @param type ? * @param map ? map * @return ? JavaBean * @throws IntrospectionException ? * @throws IllegalAccessException JavaBean * @throws InstantiationException JavaBean * @throws InvocationTargetException setter */ public static Object convertMap(Class type, Map map) throws IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException { BeanInfo beanInfo = Introspector.getBeanInfo(type); // ? Object obj = type.newInstance(); // JavaBean // JavaBean PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (int i = 0; i < propertyDescriptors.length; i++) { PropertyDescriptor descriptor = propertyDescriptors[i]; String propertyName = descriptor.getName(); if (map.containsKey(propertyName)) { // ??? try ??? Object value = map.get(propertyName); if (propertyName.equals("id") || propertyName.equals("parentId")) { value = Long.valueOf(value.toString()); } if (value instanceof Number) { value = Long.valueOf(value.toString()); } else if (value instanceof Date) { value = (Date) value; } Object[] args = new Object[1]; args[0] = value; descriptor.getWriteMethod().invoke(obj, args); } } return obj; }
From source file:org.zht.framework.util.ZBeanUtil.java
@SuppressWarnings({ "unchecked", "rawtypes" }) public static Map convertBeanToMap(Object bean) { Map returnMap = null;/* ww w . j a v a2 s. c o m*/ try { Class<?> type = bean.getClass(); BeanInfo beanInfo = Introspector.getBeanInfo(type); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); // MethodAccess access = MethodAccess.get(type.getClass()); returnMap = new HashMap(); for (PropertyDescriptor property : propertyDescriptors) { String properName = property.getName(); Method getter = property.getReadMethod(); Object value = getter.invoke(bean); returnMap.put(properName, value); // // Object value=access.invoke(bean,"get" + ZStrUtil.toUpCaseFirst(properName)); // if (value != null){ // returnMap.put(properName, value); // }else{ // returnMap.put(properName, null); // } } } catch (Exception e) { e.printStackTrace(); return returnMap; } return returnMap; }
From source file:org.hawkular.inventory.impl.tinkerpop.sql.impl.SqlGraph.java
private void setupDataSource(DataSource dataSource, Configuration configuration) throws Exception { BeanInfo beanInfo = Introspector.getBeanInfo(dataSource.getClass()); PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors(); Map<String, PropertyDescriptor> propsByName = new HashMap<>(); for (PropertyDescriptor p : properties) { propsByName.put(p.getName().toLowerCase(), p); }/*from ww w .j a v a 2 s . co m*/ Iterator it = configuration.getKeys("sql.datasource"); while (it.hasNext()) { String key = (String) it.next(); String property = key.substring("sql.datasource.".length()).toLowerCase(); PropertyDescriptor d = propsByName.get(property); if (d == null) { continue; } Method write = d.getWriteMethod(); if (write != null) { write.invoke(dataSource, configuration.getProperty(key)); } } }
From source file:org.apache.jmeter.testbeans.gui.TestBeanGUI.java
public TestBeanGUI(Class<?> testBeanClass) { super();//w w w. j a v a 2 s. c o m log.debug("testing class: " + testBeanClass.getName()); // A quick verification, just in case: if (!TestBean.class.isAssignableFrom(testBeanClass)) { Error e = new Error(); log.error("This should never happen!", e); throw e; // Programming error: bail out. } this.testBeanClass = testBeanClass; // Get the beanInfo: try { beanInfo = Introspector.getBeanInfo(testBeanClass); } catch (IntrospectionException e) { log.error("Can't get beanInfo for " + testBeanClass.getName(), e); throw new Error(e.toString()); // Programming error. Don't // continue. } customizerClass = beanInfo.getBeanDescriptor().getCustomizerClass(); // Creation of the customizer and GUI initialization is delayed until // the // first // configure call. We don't need all that just to find out the static // label, menu // categories, etc! initialized = false; JMeterUtils.addLocaleChangeListener(this); }
From source file:org.opendaylight.controller.cluster.datastore.DatastoreContextIntrospector.java
/** * Finds the getter method on a yang-generated type for the specified property name. */// w w w . java 2s . com private static void findYangTypeGetter(Class<?> type, String propertyName) throws Exception { for (PropertyDescriptor desc : Introspector.getBeanInfo(type).getPropertyDescriptors()) { if (desc.getName().equals(propertyName)) { yangTypeGetters.put(type, desc.getReadMethod()); return; } } throw new IllegalArgumentException(String.format( "Getter method for constructor property %s not found for YANG type %s", propertyName, type)); }
From source file:org.androidtransfuse.processor.ManifestManager.java
private <T extends Mergeable> void updateMergeTags(Class<T> clazz, T mergeable) throws MergerException { try {/*from www . j av a 2 s . com*/ mergeable.setGenerated(true); BeanInfo beanInfo = Introspector.getBeanInfo(clazz); for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) { Method readMethod = propertyDescriptor.getReadMethod(); Method writeMethod = propertyDescriptor.getWriteMethod(); Merge mergeAnnotation = findAnnotation(Merge.class, writeMethod, readMethod); Object property = PropertyUtils.getProperty(mergeable, propertyDescriptor.getName()); if (mergeAnnotation != null && property != null) { mergeable.addMergeTag(mergeAnnotation.value()); } } } catch (IntrospectionException e) { throw new MergerException(e); } catch (InvocationTargetException e) { throw new MergerException(e); } catch (NoSuchMethodException e) { throw new MergerException(e); } catch (IllegalAccessException e) { throw new MergerException(e); } }
From source file:com.siberhus.tdfl.mapping.BeanWrapLineMapper.java
@SuppressWarnings("unchecked") protected Properties getBeanProperties(Object bean, Properties properties) throws IntrospectionException { Class<?> cls = bean.getClass(); String[] namesCache = propertyNamesCache.get(cls); if (namesCache == null) { List<String> setterNames = new ArrayList<String>(); BeanInfo beanInfo = Introspector.getBeanInfo(cls); PropertyDescriptor propDescs[] = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor propDesc : propDescs) { if (propDesc.getWriteMethod() != null) { setterNames.add(propDesc.getName()); }// w w w . j av a 2 s .c o m } propertyNamesCache.put(cls, setterNames.toArray(new String[0])); } // Map from field names to property names Map<String, String> matches = propertiesMatched.get(cls); if (matches == null) { matches = new HashMap<String, String>(); propertiesMatched.put(cls, matches); } @SuppressWarnings("rawtypes") Set<String> keys = new HashSet(properties.keySet()); for (String key : keys) { if (matches.containsKey(key)) { switchPropertyNames(properties, key, matches.get(key)); continue; } String name = findPropertyName(bean, key); if (name != null) { matches.put(key, name); switchPropertyNames(properties, key, name); } } return properties; }
From source file:io.fabric8.devops.ProjectConfigs.java
/** * Configures the given {@link ProjectConfig} with a map of key value pairs from * something like a JBoss Forge command//from www.jav a 2s.co m */ public static void configureProperties(ProjectConfig config, Map map) { Class<? extends ProjectConfig> clazz = config.getClass(); BeanInfo beanInfo = null; try { beanInfo = Introspector.getBeanInfo(clazz); } catch (IntrospectionException e) { LOG.warn("Could not introspect " + clazz.getName() + ". " + e, e); } if (beanInfo != null) { PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor descriptor : propertyDescriptors) { Method writeMethod = descriptor.getWriteMethod(); if (writeMethod != null) { String name = descriptor.getName(); Object value = map.get(name); if (value != null) { Object safeValue = null; Class<?> propertyType = descriptor.getPropertyType(); if (propertyType.isInstance(value)) { safeValue = value; } else { PropertyEditor editor = descriptor.createPropertyEditor(config); if (editor == null) { editor = PropertyEditorManager.findEditor(propertyType); } if (editor != null) { String text = value.toString(); editor.setAsText(text); safeValue = editor.getValue(); } else { LOG.warn("Cannot update property " + name + " with value " + value + " of type " + propertyType.getName() + " on " + clazz.getName()); } } if (safeValue != null) { try { writeMethod.invoke(config, safeValue); } catch (Exception e) { LOG.warn("Failed to set property " + name + " with value " + value + " on " + clazz.getName() + " " + config + ". " + e, e); } } } } } } String flow = null; Object flowValue = map.get("pipeline"); if (flowValue == null) { flowValue = map.get("flow"); } if (flowValue != null) { flow = flowValue.toString(); } config.setPipeline(flow); }