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; } }
/** Default human-presentable name of the service type. * In the default implementation, taken from the bean descriptor. * @return initial value of the human-presentable name * @see java.beans.FeatureDescriptor#getDisplayName */ protected String displayName() { try { return Introspector.getBeanInfo(getClass()).getBeanDescriptor().getDisplayName(); } catch (Exception e) { // Catching IntrospectionException, but also maybe NullPointerException...? Logger.getLogger(ServiceType.class.getName()).log(Level.WARNING, null, e); return getClass().getName(); } }
for (PropertyDescriptor pd : delegate.getPropertyDescriptors()) { try { this.propertyDescriptors.add(pd instanceof IndexedPropertyDescriptor ? logger.debug("Ignoring invalid bean property '" + pd.getName() + "': " + ex.getMessage()); MethodDescriptor[] methodDescriptors = delegate.getMethodDescriptors(); if (methodDescriptors != null) { for (Method method : findCandidateWriteMethods(methodDescriptors)) { logger.debug("Ignoring candidate write method [" + method + "]: " + ex.getMessage());
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); } }
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); } }
@Test public void shouldUseExtendedBeanInfoWhenApplicable() throws NoSuchMethodException, SecurityException { // given a class with a non-void returning setter method @SuppressWarnings("unused") class C { public Object setFoo(String s) { return this; } public String getFoo() { return null; } } // CachedIntrospectionResults should delegate to ExtendedBeanInfo CachedIntrospectionResults results = CachedIntrospectionResults.forClass(C.class); BeanInfo info = results.getBeanInfo(); PropertyDescriptor pd = null; for (PropertyDescriptor candidate : info.getPropertyDescriptors()) { if (candidate.getName().equals("foo")) { pd = candidate; } } // resulting in a property descriptor including the non-standard setFoo method assertThat(pd, notNullValue()); assertThat(pd.getReadMethod(), equalTo(C.class.getMethod("getFoo"))); assertThat( "No write method found for non-void returning 'setFoo' method. " + "Check to see if CachedIntrospectionResults is delegating to " + "ExtendedBeanInfo as expected", pd.getWriteMethod(), equalTo(C.class.getMethod("setFoo", String.class))); }
/** 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; }
BeanInfo info = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = info.getPropertyDescriptors(); MethodDescriptor[] methodDescriptors = info.getMethodDescriptors(); HashMap<String, Method> methodMap = new HashMap<String, Method>(); for (MethodDescriptor methodDescriptor : methodDescriptors) { if (skipSet.contains(pd.getName())) { continue; "Skipping property (no read method): " + prefix + pd.getName() + " " + pd.getPropertyType()); continue;
/** 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 void introspectInterfaces(Class<?> beanClass, Class<?> currClass) throws IntrospectionException { for (Class<?> ifc : currClass.getInterfaces()) { if (!ClassUtils.isJavaLanguageInterface(ifc)) { for (PropertyDescriptor pd : getBeanInfo(ifc).getPropertyDescriptors()) { PropertyDescriptor existingPd = this.propertyDescriptorCache.get(pd.getName()); if (existingPd == null || (existingPd.getReadMethod() == null && pd.getReadMethod() != null)) { // GenericTypeAwarePropertyDescriptor leniently resolves a set* write method // against a declared read method, so we prefer read method descriptors here. pd = buildGenericTypeAwarePropertyDescriptor(beanClass, pd); this.propertyDescriptorCache.put(pd.getName(), pd); } } introspectInterfaces(ifc, ifc); } } }
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 ) ); } } } }
public CompositeDataBeanAdapter(Class<?> beanClass, String defaultPropertyValue) throws IntrospectionException, OpenDataException { this.beanClass = beanClass; this.defaultPropertyValue = defaultPropertyValue; BeanInfo beanInfo = Introspector.getBeanInfo(beanClass); BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor(); CompositeTypeFactory typeFactory = new CompositeTypeFactory(beanDescriptor.getName(), beanDescriptor.getDisplayName()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor pd : propertyDescriptors) { if (isExposed(pd)) { typeFactory.defineItem(pd.getDisplayName(), SimpleType.STRING); } } compositeType = typeFactory.createCompositeType(); }
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); }
for (PropertyDescriptor descriptor : Introspector .getBeanInfo(TestBean.class, Object.class) .getPropertyDescriptors()) { System.out.println("Name: " + descriptor.getName() + ", type: " + descriptor.getPropertyType()); }
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; }
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 static PropertyDescriptor getPropertyDescriptor(Object object, String propName) throws IntrospectionException { BeanInfo info = getBeanInfo(object.getClass(), object); PropertyDescriptor[] array = info.getPropertyDescriptors(); int len = array.length; for (int i = 0; i < len; ++i) { PropertyDescriptor pd = array[i]; if (pd.getName().equals(propName)) { return pd; } } throw new IntrospectionException("No such property: " + propName); }
/** Sets/add the persistence delegates to be used for a class. * @param c The class to set the registered delegate for. * @param del The new delegate or <code>null</code> to erase to old one. */ static synchronized void setDelegate(Class<?> c, PersistenceDelegate del) { try { Introspector.getBeanInfo(c, Introspector.IGNORE_ALL_BEANINFO).getBeanDescriptor().setValue("persistenceDelegate", del); } catch (Exception ignored) { } }