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; } }
public MethodProperty(PropertyDescriptor property) { super(property.getName(), property.getPropertyType(), MethodProperty.discoverGenericType(property)); this.property = property; this.readable = property.getReadMethod() != null; this.writable = property.getWriteMethod() != null; }
public static <T> void findBooleanIsMethods(Class<T> clazz, PropertyDescriptor descriptor) throws IntrospectionException { if (descriptor.getReadMethod() == null && descriptor.getPropertyType() == Boolean.class) { try { PropertyDescriptor pd = new PropertyDescriptor(descriptor.getName(), clazz); descriptor.setReadMethod(pd.getReadMethod()); } catch (IntrospectionException e) { } } }
public BeanPropertyHandler(PropertyDescriptor pd) { super(pd.getPropertyType(), pd.getReadMethod() != null, pd.getWriteMethod() != null); this.pd = pd; }
/** * Compare the given {@code PropertyDescriptors} and return {@code true} if * they are equivalent, i.e. their read method, write method, property type, * property editor and flags are equivalent. * @see java.beans.PropertyDescriptor#equals(Object) */ public static boolean equals(PropertyDescriptor pd, PropertyDescriptor otherPd) { return (ObjectUtils.nullSafeEquals(pd.getReadMethod(), otherPd.getReadMethod()) && ObjectUtils.nullSafeEquals(pd.getWriteMethod(), otherPd.getWriteMethod()) && ObjectUtils.nullSafeEquals(pd.getPropertyType(), otherPd.getPropertyType()) && ObjectUtils.nullSafeEquals(pd.getPropertyEditorClass(), otherPd.getPropertyEditorClass()) && pd.isBound() == otherPd.isBound() && pd.isConstrained() == otherPd.isConstrained()); }
public SimplePropertyDescriptor(PropertyDescriptor original) throws IntrospectionException { this(original.getName(), original.getReadMethod(), original.getWriteMethod()); PropertyDescriptorUtils.copyNonMethodProperties(original, this); }
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); } }
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 ) ); } } } }
private PropertyDescriptor buildGenericTypeAwarePropertyDescriptor(Class<?> beanClass, PropertyDescriptor pd) { try { return new GenericTypeAwarePropertyDescriptor(beanClass, pd.getName(), pd.getReadMethod(), pd.getWriteMethod(), pd.getPropertyEditorClass()); } catch (IntrospectionException ex) { throw new FatalBeanException("Failed to re-introspect class [" + beanClass.getName() + "]", ex); } }
for (PropertyDescriptor descriptor : Introspector .getBeanInfo(TestBean.class, Object.class) .getPropertyDescriptors()) { System.out.println("Name: " + descriptor.getName() + ", type: " + descriptor.getPropertyType()); }
private PropertyDescriptor getPropertyDescriptor(String propertyName) { BeanInfo beanInfo = null; try { beanInfo = Introspector.getBeanInfo(beanClass); } catch (IntrospectionException e) { throw new FacesException(e.getMessage(), e); } finally { Introspector.flushFromCaches(beanClass); } if (beanInfo == null) { return null; } PropertyDescriptor[] propertyDescriptorsArray = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor pd : propertyDescriptorsArray) { if (propertyName.equals(pd.getName())) { return pd; } } return null; }
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());
@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"); }
/** Finds property descriptor for a bean. * @param bean the bean * @param name name of the property to find * @return the descriptor * @exception IllegalArgumentException if the method is not found */ private static PropertyDescriptor findInfo(Object bean, String name) throws IllegalArgumentException { try { BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass()); PropertyDescriptor[] descr = beanInfo.getPropertyDescriptors(); for (int i = 0; i < descr.length; i++) { if (descr[i].getName().equals(name)) { return descr[i]; } } throw new IllegalArgumentException("No property named " + name + " in class " + bean.getClass() // NOI18N ); } catch (IntrospectionException e) { throw (IllegalArgumentException) new IllegalArgumentException(e.toString()).initCause(e); } }
/** * Return an array of non-simple bean properties that are unsatisfied. * These are probably unsatisfied references to other beans in the * factory. Does not include simple properties like primitives or Strings. * @param mbd the merged bean definition the bean was created with * @param bw the BeanWrapper the bean was created with * @return an array of bean property names * @see org.springframework.beans.BeanUtils#isSimpleProperty */ protected String[] unsatisfiedNonSimpleProperties(AbstractBeanDefinition mbd, BeanWrapper bw) { Set<String> result = new TreeSet<>(); PropertyValues pvs = mbd.getPropertyValues(); PropertyDescriptor[] pds = bw.getPropertyDescriptors(); for (PropertyDescriptor pd : pds) { if (pd.getWriteMethod() != null && !isExcludedFromDependencyCheck(pd) && !pvs.contains(pd.getName()) && !BeanUtils.isSimpleProperty(pd.getPropertyType())) { result.add(pd.getName()); } } return StringUtils.toStringArray(result); }
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); }
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; }
/** * Generate possible property alternatives for the given property and class. * Internally uses the {@code getStringDistance} method, which in turn uses * the Levenshtein algorithm to determine the distance between two Strings. * @param descriptors the JavaBeans property descriptors to search * @param maxDistance the maximum distance to accept */ private static String[] calculateMatches(String name, PropertyDescriptor[] descriptors, int maxDistance) { List<String> candidates = new ArrayList<>(); for (PropertyDescriptor pd : descriptors) { if (pd.getWriteMethod() != null) { String possibleAlternative = pd.getName(); if (calculateStringDistance(name, possibleAlternative) <= maxDistance) { candidates.add(possibleAlternative); } } } Collections.sort(candidates); return StringUtils.toStringArray(candidates); }