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); } }
protected Map<String, Object> resolveBeanMetadata(final Object bean) { final Map<String, Object> beanMetadata = new LinkedHashMap<>(); try { BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { Method readMethod = propertyDescriptor.getReadMethod(); if (readMethod != null && isSimpleType(propertyDescriptor.getPropertyType())) { String name = Introspector.decapitalize(propertyDescriptor.getName()); Object value = readMethod.invoke(bean); beanMetadata.put(name, value); } } } catch (Exception e) { throw new RuntimeException(e); } return beanMetadata; }
@Override public void actionPerformed(ActionEvent ev) { java.beans.Introspector.flushCaches(); } };
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; }
private List<Method> initMethods(Map<String, ? extends Expression<?>> args) { try { List<Method> methods = new ArrayList<Method>(args.size()); BeanInfo beanInfo = Introspector.getBeanInfo(getType()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (Map.Entry<String, ? extends Expression<?>> entry : args.entrySet()) { String property = entry.getKey(); Expression<?> expr = entry.getValue(); Method setter = null; for (PropertyDescriptor prop : propertyDescriptors) { if (prop.getName().equals(property)) { setter = prop.getWriteMethod(); if (!isAssignableFrom(prop.getPropertyType(), expr.getType())) { typeMismatch(prop.getPropertyType(), expr); } break; } } if (setter == null) { propertyNotFound(expr, property); } methods.add(setter); } return methods; } catch (IntrospectionException e) { throw new RuntimeException(e.getMessage(), e); } }
/** 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; }
/** 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 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(); } } ... }
/** * Return an {@link ExtendedBeanInfo} for the given bean class, if applicable. */ @Override @Nullable public BeanInfo getBeanInfo(Class<?> beanClass) throws IntrospectionException { return (supports(beanClass) ? new ExtendedBeanInfo(Introspector.getBeanInfo(beanClass)) : null); }
private static PropertyDescriptor[] getDescriptors(Class<?> clazz) { PropertyDescriptor[] pds; List<PropertyDescriptor> list; PropertyDescriptor[] pds2 = descriptors.get(clazz); if (null == pds2) { try { BeanInfo beanInfo = Introspector.getBeanInfo(clazz); pds = beanInfo.getPropertyDescriptors(); list = new ArrayList<PropertyDescriptor>(); for (int i = 0; i < pds.length; i++) { if (null != pds[i].getPropertyType()) { list.add(pds[i]); } } pds2 = new PropertyDescriptor[list.size()]; list.toArray(pds2); } catch (IntrospectionException ie) { ie.printStackTrace(); pds2 = new PropertyDescriptor[0]; } } descriptors.put(clazz, pds2); return (pds2); }
@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"); }
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()); }
public class SimpleBean { private final String name = "SimpleBean"; private int size; public String getName() { return this.name; } public int getSize() { return this.size; } public void setSize( int size ) { this.size = size; } public static void main( String[] args ) throws IntrospectionException { BeanInfo info = Introspector.getBeanInfo( SimpleBean.class ); for ( PropertyDescriptor pd : info.getPropertyDescriptors() ) System.out.println( pd.getName() ); } }
/** * Uses JavaBeans {@link Introspector} to computer setters of object to be * configured. */ protected void introspect() { try { BeanInfo bi = Introspector.getBeanInfo(obj.getClass()); propertyDescriptors = bi.getPropertyDescriptors(); methodDescriptors = bi.getMethodDescriptors(); } catch (IntrospectionException ex) { addError("Failed to introspect " + obj + ": " + ex.getMessage()); propertyDescriptors = new PropertyDescriptor[0]; methodDescriptors = new MethodDescriptor[0]; } }
private String propertyNameFor(Method method) { return Introspector.decapitalize(method.getName().substring(3, method.getName().length())); }
Introspector.getBeanInfo(chart.getClass()).getMethodDescriptors(); HashMap<String, Method> chartMethodMap = new HashMap<String, Method>(); for (MethodDescriptor methodDescriptor : methodDescriptors) { Introspector.getBeanInfo(chart.getStyler().getClass()).getMethodDescriptors(); HashMap<String, Method> stylerMethodMap = new HashMap<String, Method>(); for (MethodDescriptor methodDescriptor : methodDescriptors) { e1.printStackTrace();
public BeanProperties(Class<?> baseClass) { PropertyDescriptor[] descriptors; try { BeanInfo info = Introspector.getBeanInfo(baseClass); descriptors = info.getPropertyDescriptors(); } catch (IntrospectionException ie) { throw new ELException(ie); } for (PropertyDescriptor pd: descriptors) { propertyMap.put(pd.getName(), new BeanProperty(baseClass, 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); } }
/** 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]; } }
for (PropertyDescriptor descriptor : Introspector .getBeanInfo(TestBean.class, Object.class) .getPropertyDescriptors()) { System.out.println("Name: " + descriptor.getName() + ", type: " + descriptor.getPropertyType()); }