/** 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; }
for(PropertyDescriptor propertyDescriptor : Introspector.getBeanInfo(yourClass).getPropertyDescriptors()){ // propertyEditor.getReadMethod() exposes the getter // btw, this may be null if you have a write-only property System.out.println(propertyDescriptor.getReadMethod()); }
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); } }
BeanPojoProperty(PropertyDescriptor property) { this.descriptor = property; }
private void addBeanInfoToClassIntrospectionData( Map<Object, Object> introspData, Class<?> clazz, Map<MethodSignature, List<Method>> accessibleMethods) throws IntrospectionException { BeanInfo beanInfo = Introspector.getBeanInfo(clazz); List<PropertyDescriptor> pdas = getPropertyDescriptors(beanInfo, clazz); int pdasLength = pdas.size(); if (propDesc != null && (decision.getReplaceExistingProperty() || !(introspData.get(propDesc.getName()) instanceof FastPropertyDescriptor))) { addPropertyDescriptorToClassIntrospectionData( introspData, propDesc, clazz, accessibleMethods); overloadedMethods.addMethod((Method) previous); overloadedMethods.addMethod(method); introspData.put(methodKey, overloadedMethods); || !(previous instanceof FastPropertyDescriptor)) { introspData.put(methodKey, method); Class<?>[] replaced = getArgTypesByMethod(introspData).put(method, method.getParameterTypes()); if (replaced != null) {
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);
BeanInfo info = Introspector.getBeanInfo(getMaxRateCode.getClass()); for ( PropertyDescriptor pd : info.getPropertyDescriptors() ) System.out.println(pd.getName()+": "+pd.getReadMethod().invoke(getMaxRateCode));
@Test public void standardReadMethodsAndOverloadedNonStandardWriteMethods() throws Exception { @SuppressWarnings("unused") class C { public String getFoo() { return null; } public C setFoo(String foo) { return this; } public C setFoo(Number foo) { return this; } } BeanInfo bi = Introspector.getBeanInfo(C.class); assertThat(hasReadMethodForProperty(bi, "foo"), is(true)); assertThat(hasWriteMethodForProperty(bi, "foo"), is(false)); ExtendedBeanInfo ebi = new ExtendedBeanInfo(bi); assertThat(hasReadMethodForProperty(bi, "foo"), is(true)); assertThat(hasWriteMethodForProperty(bi, "foo"), is(false)); assertThat(hasReadMethodForProperty(ebi, "foo"), is(true)); assertThat(hasWriteMethodForProperty(ebi, "foo"), is(true)); for (PropertyDescriptor pd : ebi.getPropertyDescriptors()) { if (pd.getName().equals("foo")) { assertThat(pd.getWriteMethod(), is(C.class.getMethod("setFoo", String.class))); return; } } fail("never matched write method"); }
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; }
/** * 通过方法获取属性 * * @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; } }
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); } }
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 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; } }
/** * {@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())); } }
private Class<?> getPropertyType(Class<?> clazz, String property) { BeanInfo beanInfo = Introspector.getBeanInfo(clazz); PropertyDescriptor[] propDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor propDescriptor : propDescriptors) { // String name of a property if (property.equals(propDescriptor.getName())) { // Class the getter corresponds to. return propDescriptor.getPropertyType(); } } ... }
/** Uses JavaBeans {@link Introspector} to computer setters of object to be configured. */ protected void introspect() { try { BeanInfo bi = Introspector.getBeanInfo(obj.getClass()); props = bi.getPropertyDescriptors(); } catch (IntrospectionException ex) { LogLog.error("Failed to introspect "+obj+": " + ex.getMessage()); props = new PropertyDescriptor[0]; } }
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); } }
@Test public void propertyCountsWithNonStandardWriteMethod() throws IntrospectionException { class ExtendedTestBean extends TestBean { @SuppressWarnings("unused") public ExtendedTestBean setFoo(String s) { return this; } } BeanInfo bi = Introspector.getBeanInfo(ExtendedTestBean.class); BeanInfo ebi = new ExtendedBeanInfo(bi); boolean found = false; for (PropertyDescriptor pd : ebi.getPropertyDescriptors()) { if (pd.getName().equals("foo")) { found = true; } } assertThat(found, is(true)); assertThat(ebi.getPropertyDescriptors().length, equalTo(bi.getPropertyDescriptors().length+1)); }