/** Create a new PropertyGetter for the specified Object. This is done in prepartion for invoking {@link #getProperties(PropertyGetter.PropertyCallback, String)} one or more times. @param obj the object for which to set properties */ public PropertyGetter(Object obj) throws IntrospectionException { BeanInfo bi = Introspector.getBeanInfo(obj.getClass()); props = bi.getPropertyDescriptors(); this.obj = obj; }
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); } }
public void processBeanInfo(BeanInfo beanInfo) throws Exception { final PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors(); for ( PropertyDescriptor descriptor : descriptors ) { final String propertyName = descriptor.getName(); if ( injectionData.containsKey( propertyName ) ) { final Method method = descriptor.getWriteMethod(); method.invoke( theConnectionProvider, injectionData.get( propertyName ) ); } } } }
BeanPojoProperty(PropertyDescriptor property) { this.descriptor = property; }
protected static Map propertyMap(BeanInfo info) { PropertyDescriptor[] properties = info.getPropertyDescriptors(); Map lookup = new HashMap(properties.length); for (int i = 0; i < properties.length; i++) { lookup.put(properties[i].getName(), properties[i]); } return lookup; }
private static PropertyDescriptor getPropertyDescriptor(Class targetClass, String propertyName) { PropertyDescriptor result = null; if (targetClass == null) { LOGGER.warn("Cannot retrieve property " + propertyName + ". Class is null"); } else { try { BeanInfo beanInfo = Introspector.getBeanInfo(targetClass); PropertyDescriptor[] propDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor propDesc : propDescriptors) { if (propDesc.getName().equals(propertyName)) { result = propDesc; break; } } } catch (IntrospectionException ie) { LOGGER.warn("Cannot retrieve property " + propertyName + ". Cause is: " + ie); } } return result; }
info = Introspector.getBeanInfo(base.getClass()); } catch (Exception ex) { info.getPropertyDescriptors().length); for (PropertyDescriptor pd: info.getPropertyDescriptors()) { pd.setValue("type", pd.getPropertyType()); pd.setValue("resolvableAtDesignTime", Boolean.TRUE); list.add(pd);
private boolean hasReadMethodForProperty(BeanInfo beanInfo, String propertyName) { for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) { if (pd.getName().equals(propertyName)) { return pd.getReadMethod() != null; } } return false; }
BeanInfo info = Introspector.getBeanInfo(getMaxRateCode.getClass()); for ( PropertyDescriptor pd : info.getPropertyDescriptors() ) System.out.println(pd.getName()+": "+pd.getReadMethod().invoke(getMaxRateCode));
private boolean hasIndexedWriteMethodForProperty(BeanInfo beanInfo, String propertyName) { for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) { if (pd.getName().equals(propertyName)) { if (!(pd instanceof IndexedPropertyDescriptor)) { return false; } return ((IndexedPropertyDescriptor)pd).getIndexedWriteMethod() != null; } } return false; }
private static boolean hasAtLeastOneBeanProperty(Class<?> clazz) { BeanInfo beanInfo = null; try { beanInfo = Introspector.getBeanInfo(clazz); } catch (IntrospectionException e) { return false; } for (PropertyDescriptor beanProp : beanInfo.getPropertyDescriptors()) { if (beanProp.getReadMethod() != null && beanProp.getWriteMethod() != null) { return true; } } return false; }
/** * {@link BeanInfo#getPropertyDescriptors()} returns alphanumerically sorted. * Test that {@link ExtendedBeanInfo#getPropertyDescriptors()} does the same. */ @Test public void propertyDescriptorOrderIsEqual() throws IntrospectionException { BeanInfo bi = Introspector.getBeanInfo(TestBean.class); BeanInfo ebi = new ExtendedBeanInfo(bi); for (int i = 0; i < bi.getPropertyDescriptors().length; i++) { assertThat("element " + i + " in BeanInfo and ExtendedBeanInfo propertyDescriptor arrays do not match", ebi.getPropertyDescriptors()[i].getName(), equalTo(bi.getPropertyDescriptors()[i].getName())); } }
info = Introspector.getBeanInfo(base.getClass()); } catch (Exception ex) { info.getPropertyDescriptors().length); for (PropertyDescriptor pd: info.getPropertyDescriptors()) { pd.setValue("type", pd.getPropertyType()); pd.setValue("resolvableAtDesignTime", Boolean.TRUE); list.add(pd);
/** * Returns all the property descriptors for the class associated with the given object * * @param fromObj Use the class of this object * @param stopClass Don't include any properties from this ancestor class upwards. * @return Property descriptors * @throws IllegalArgumentException if there's a introspection failure */ public static PropertyDescriptor[] propertyDescriptorsFor(Object fromObj, Class<Object> stopClass) throws IllegalArgumentException { try { return Introspector.getBeanInfo(fromObj.getClass(), stopClass).getPropertyDescriptors(); } catch (IntrospectionException e) { throw new IllegalArgumentException("Could not get property descriptors for " + fromObj.getClass(), e); } }
private void internalCheckListeners(BeanInfo beanInfo) { PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); try { for ( PropertyDescriptor pd : pds ) { final Object listener = pd.getReadMethod().invoke( this, READER_METHOD_ARGS ); if ( listener == null ) { throw new HibernateException( "Listener [" + pd.getName() + "] was null" ); } if ( listener.getClass().isArray() ) { Object[] listenerArray = (Object[]) listener; for ( Object aListenerArray : listenerArray ) { if ( aListenerArray == null ) { throw new HibernateException( "Listener in [" + pd.getName() + "] was null" ); } } } } } catch (HibernateException e) { throw e; } catch (Throwable t) { throw new HibernateException( "Unable to validate listener config" ); } }
private boolean hasWriteMethodForProperty(BeanInfo beanInfo, String propertyName) { for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) { if (pd.getName().equals(propertyName)) { return pd.getWriteMethod() != null; } } return false; }
MyBean bean = new MyBean(); BeanInfo beanInfo = Introspector.getBeanInfo(MyBean.class); for (PropertyDescriptor propertyDesc : beanInfo.getPropertyDescriptors()) { String propertyName = propertyDesc.getName(); Object value = propertyDesc.getReadMethod().invoke(bean); }
private boolean hasIndexedReadMethodForProperty(BeanInfo beanInfo, String propertyName) { for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) { if (pd.getName().equals(propertyName)) { if (!(pd instanceof IndexedPropertyDescriptor)) { return false; } return ((IndexedPropertyDescriptor)pd).getIndexedReadMethod() != null; } } return false; }
private Method getAccessor(Class<?> owner, String property) { try { BeanInfo beanInfo = Introspector.getBeanInfo(owner); PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor pd : descriptors) { if (pd.getName().equals(property)) { return pd.getReadMethod(); } } return null; } catch (IntrospectionException e) { return null; } }
/** * 通过方法获取属性 * * @param entityClass * @return */ @Override public List<EntityField> getProperties(Class<?> entityClass) { List<EntityField> entityFields = new ArrayList<EntityField>(); BeanInfo beanInfo = null; try { beanInfo = Introspector.getBeanInfo(entityClass); } catch (IntrospectionException e) { throw new MapperException(e); } PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor desc : descriptors) { if (!"class".equals(desc.getName())) { entityFields.add(new EntityField(null, desc)); } } return entityFields; } }