List of usage examples for java.beans Introspector getBeanInfo
public static BeanInfo getBeanInfo(Class<?> beanClass) throws IntrospectionException
From source file:org.springframework.richclient.table.renderer.BeanTableCellRenderer.java
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (value != null) { if (beanWrapper == null) { beanWrapper = new BeanWrapperImpl(value); } else {// www. ja va2 s. co m beanWrapper.setWrappedInstance(value); } try { BeanInfo info = Introspector.getBeanInfo(value.getClass()); int index = info.getDefaultPropertyIndex(); if (index != -1) { String defaultPropName = beanWrapper.getPropertyDescriptors()[index].getName(); Object val = beanWrapper.getPropertyValue(defaultPropName); TableCellRenderer r = table.getDefaultRenderer(val.getClass()); return r.getTableCellRendererComponent(table, val, isSelected, hasFocus, row, column); } } catch (IntrospectionException e) { log.debug("Error during introspection of bean: " + e.getMessage(), e); } } return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); }
From source file:com.mawujun.utils.bean.BeanUtils.java
private static PropertyDescriptor[] getPropertyDescriptors(Class<?> clazz) throws IntrospectionException { PropertyDescriptor[] pds = beanPropertyCache.get(clazz.getName()); if (pds != null) { //return pds; } else {//from w w w . j a v a 2 s . c om BeanInfo beanInfo = Introspector.getBeanInfo(clazz); pds = beanInfo.getPropertyDescriptors(); beanPropertyCache.put(clazz.getName(), pds); } return pds; }
From source file:egovframework.com.ext.ldapumt.service.impl.ObjectMapper.java
/** * ContextAdapter? ? vo ./*from ww w. j a va 2 s.com*/ */ public Object mapFromContext(Object arg0) throws NamingException { DirContextAdapter adapter = (DirContextAdapter) arg0; Attributes attrs = adapter.getAttributes(); LdapObject vo = null; try { vo = (LdapObject) type.newInstance(); } catch (Exception e2) { throw new RuntimeException(e2); } vo.setDn(adapter.getDn().toString()); BeanInfo beanInfo; try { beanInfo = Introspector.getBeanInfo(type); } catch (IntrospectionException e1) { throw new RuntimeException(e1); } PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor descriptor : propertyDescriptors) { if (attrs.get(descriptor.getName()) != null) try { Class<?> o = descriptor.getPropertyType(); if (o == int.class) PropertyUtils.setProperty(vo, descriptor.getName(), Integer.valueOf((String) attrs.get(descriptor.getName()).get())); if (o == String.class) PropertyUtils.setProperty(vo, descriptor.getName(), (String) attrs.get(descriptor.getName()).get()); if (o == Boolean.class) PropertyUtils.setProperty(vo, descriptor.getName(), ((String) attrs.get(descriptor.getName()).get()).equals("Y")); } catch (Exception e) { throw new RuntimeException(e); } } return vo; }
From source file:org.eclipse.scada.da.core.VariantBeanHelper.java
public static Map<String, Variant> extractSafe(final Object source) { final Map<String, Variant> result = new HashMap<String, Variant>(); BeanInfo bi;//from www . j ava 2 s. com try { bi = Introspector.getBeanInfo(source.getClass()); } catch (final IntrospectionException e) { return result; } for (final PropertyDescriptor pd : bi.getPropertyDescriptors()) { final Method m = pd.getReadMethod(); if (m != null) { try { result.put(pd.getName(), Variant.valueOf(m.invoke(source))); } catch (final Exception e) { // ignore } } } return result; }
From source file:org.bibsonomy.database.validation.DatabaseModelValidator.java
/** * checks if the string attributes of the model respect the field lengths of * the database//w w w . j a v a 2 s . co m * * @param model the model to validate * @param id the id of the model (used for the errormessage) * @param session the session */ public void validateFieldLength(final T model, final String id, final DBSession session) { final Class<? extends Object> clazz = model.getClass(); final FieldLengthErrorMessage fieldLengthError = new FieldLengthErrorMessage(); try { final BeanInfo bi = Introspector.getBeanInfo(clazz); /* * loop through all properties * if there are any performance issues, their cause might be here */ for (final PropertyDescriptor d : bi.getPropertyDescriptors()) { final Method getter = d.getReadMethod(); if (present(getter)) { final Object value = getter.invoke(model, (Object[]) null); /* * check max length */ if (value instanceof String) { final String stringValue = (String) value; final int length = stringValue.length(); final String propertyName = d.getName(); final int maxLength = DatabaseSchemaInformation.getInstance() .getMaxColumnLengthForProperty(clazz, propertyName); if ((maxLength > 0) && (length > maxLength)) { fieldLengthError.addToFields(propertyName, maxLength); } } } } if (fieldLengthError.hasErrors()) { session.addError(id, fieldLengthError); log.warn("Added fieldlengthError"); } } catch (final Exception ex) { log.error("could not introspect object of class 'user'", ex); } }
From source file:jp.co.opentone.bsol.framework.test.dataset.bean.BeanTable.java
private ITableMetaData createTableMetaData() throws DataSetException { if (beans.isEmpty()) { return new DefaultTableMetaData(tableName, new Column[0]); } else {/*from w ww . j a v a 2 s .c o m*/ try { List<Column> columns = new ArrayList<Column>(); BeanInfo info = Introspector.getBeanInfo(beans.get(0).getClass()); PropertyDescriptor[] descs = info.getPropertyDescriptors(); for (PropertyDescriptor desc : descs) { if (!isIgnoreProperty(desc)) { System.out.println(desc.getName()); columns.add(new Column(desc.getName(), DataType.UNKNOWN)); } } Column[] c = (Column[]) columns.toArray(new Column[columns.size()]); return new DefaultTableMetaData(tableName, c); } catch (IntrospectionException e) { throw new DataSetException(e); } } }
From source file:BeanUtil.java
/** * Retreives a property descriptor object for a given property. * <p>//from w w w . j av a2 s . co m * Uses the classes in <code>java.beans</code> to get back * a descriptor for a property. Read-only and write-only * properties will have a slower return time. * </p> * * @param propertyName The programmatic name of the property * @param beanClass The Class object for the target bean. * For example sun.beans.OurButton.class. * @return a PropertyDescriptor for a property that follows the * standard Java naming conventions. * @throws PropertyNotFoundException indicates that the property * could not be found on the bean class. */ private static final PropertyDescriptor getPropertyDescriptor(String propertyName, Class beanClass) { PropertyDescriptor resultPropertyDescriptor = null; char[] pNameArray = propertyName.toCharArray(); pNameArray[0] = Character.toLowerCase(pNameArray[0]); propertyName = new String(pNameArray); try { resultPropertyDescriptor = new PropertyDescriptor(propertyName, beanClass); } catch (IntrospectionException e1) { // Read-only and write-only properties will throw this // exception. The properties must be looked up using // brute force. // This will get the list of all properties and iterate // through looking for one that matches the property // name passed into the method. try { BeanInfo beanInfo = Introspector.getBeanInfo(beanClass); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (int i = 0; i < propertyDescriptors.length; i++) { if (propertyDescriptors[i].getName().equals(propertyName)) { // If the names match, this this describes the // property being searched for. Break out of // the iteration. resultPropertyDescriptor = propertyDescriptors[i]; break; } } } catch (IntrospectionException e2) { e2.printStackTrace(); } } // If no property descriptor was found, then this bean does not // have a property matching the name passed in. if (resultPropertyDescriptor == null) { System.out.println("resultPropertyDescriptor == null"); } return resultPropertyDescriptor; }
From source file:net.zcarioca.zcommons.config.data.BeanPropertySetterFactory.java
/** * Gets a collection of {@link BeanPropertySetter} to configure the bean. * * @param bean The bean to configure.// ww w.j av a 2s .c o m * @return Returns a collection of {@link BeanPropertySetter}. * @throws ConfigurationException */ public Collection<BeanPropertySetter> getPropertySettersForBean(Object bean) throws ConfigurationException { List<BeanPropertySetter> setters = new ArrayList<BeanPropertySetter>(); Map<String, PropertyDescriptor> descriptors = new HashMap<String, PropertyDescriptor>(); Class<?> beanClass = bean.getClass(); try { BeanInfo beanInfo = Introspector.getBeanInfo(beanClass); for (PropertyDescriptor desc : beanInfo.getPropertyDescriptors()) { Method reader = desc.getReadMethod(); Method writer = desc.getWriteMethod(); Field field = getField(beanClass, desc); if (reader != null) { descriptors.put(desc.getDisplayName(), desc); } if (writer != null) { if (writer.isAnnotationPresent(ConfigurableAttribute.class)) { setters.add(new WriterBeanPropertySetter(bean, desc, field, writer.getAnnotation(ConfigurableAttribute.class))); descriptors.remove(desc.getDisplayName()); } if (reader != null && reader.isAnnotationPresent(ConfigurableAttribute.class)) { setters.add(new WriterBeanPropertySetter(bean, desc, field, reader.getAnnotation(ConfigurableAttribute.class))); descriptors.remove(desc.getDisplayName()); } } } } catch (Throwable t) { throw new ConfigurationException("Could not introspect bean class", t); } do { Field[] fields = beanClass.getDeclaredFields(); for (Field field : fields) { if (field.isAnnotationPresent(ConfigurableAttribute.class)) { if (descriptors.containsKey(field.getName()) && descriptors.get(field.getName()).getWriteMethod() != null) { PropertyDescriptor desc = descriptors.get(field.getName()); setters.add(new WriterBeanPropertySetter(bean, desc, field, field.getAnnotation(ConfigurableAttribute.class))); } else { setters.add(new FieldBeanPropertySetter(bean, null, field, field.getAnnotation(ConfigurableAttribute.class))); } } else if (descriptors.containsKey(field.getName())) { // the annotation may have been set on the getter, not the field PropertyDescriptor desc = descriptors.get(field.getName()); if (desc.getReadMethod().isAnnotationPresent(ConfigurableAttribute.class)) { setters.add(new FieldBeanPropertySetter(bean, desc, field, desc.getReadMethod().getAnnotation(ConfigurableAttribute.class))); } } } } while ((beanClass = beanClass.getSuperclass()) != null); return setters; }
From source file:no.sesat.search.datamodel.DataModelFactoryImpl.java
@SuppressWarnings("unchecked") public DataModel instantiate() { try {/*from www .ja va2 s . c o m*/ final Class<DataModel> cls = DataModel.class; final PropertyDescriptor[] propDescriptors = Introspector.getBeanInfo(DataModel.class) .getPropertyDescriptors(); final Property[] properties = new Property[propDescriptors.length]; for (int i = 0; i < properties.length; ++i) { properties[i] = new Property(propDescriptors[i].getName(), propDescriptors[i] instanceof MappedPropertyDescriptor ? new MapDataObjectSupport(Collections.EMPTY_MAP) : null); } final InvocationHandler handler = new BeanDataModelInvocationHandler( new BeanDataModelInvocationHandler.PropertyInitialisor(DataModel.class, properties)); return (DataModel) ConcurrentProxy.newProxyInstance(cls.getClassLoader(), new Class[] { cls }, handler); } catch (IntrospectionException ie) { throw new IllegalStateException("Need to introspect DataModel properties before instantiation"); } }
From source file:BeanUtility.java
/** This method takes 2 JavaBeans of the same type and copies the properties of one bean to the other. * Any attempts that have an IllegalAccessException will be ignored. This will also NOT recurse into nested bean * results. References to existing beanage will be includes. Try using .clone() for that stuff. * @param from Source Bean/* ww w . j a v a2 s . co m*/ * @param to Desitnation Bean */ public static void copyBeanToBean(Object from, Object to) throws InvocationTargetException, IntrospectionException { PropertyDescriptor[] pds = Introspector.getBeanInfo(from.getClass()).getPropertyDescriptors(); for (int i = 0; i < pds.length; i++) { try { if (pds[i].getName().equals("class")) continue; Object[] value = { pds[i].getReadMethod().invoke(from) }; pds[i].getWriteMethod().invoke(to, value); } catch (IllegalAccessException iae) { //Im just going to ignore any properties I don't have access too. } } }