public SimplePropertyDescriptor(PropertyDescriptor original) throws IntrospectionException { this(original.getName(), original.getReadMethod(), original.getWriteMethod()); PropertyDescriptorUtils.copyNonMethodProperties(original, this); }
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; }
/** * 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); }
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; } }
PropertyDescriptor[] pds = this.beanInfo.getPropertyDescriptors(); for (PropertyDescriptor pd : pds) { if (Class.class == beanClass && ("classLoader".equals(pd.getName()) || "protectionDomain".equals(pd.getName()))) { logger.trace("Found bean property '" + pd.getName() + "'" + (pd.getPropertyType() != null ? " of type [" + pd.getPropertyType().getName() + "]" : "") + (pd.getPropertyEditorClass() != null ? "; editor [" + pd.getPropertyEditorClass().getName() + "]" : "")); this.propertyDescriptorCache.put(pd.getName(), pd);
/** * Initialize the mapping meta-data for the given class. * @param mappedClass the mapped class */ protected void initialize(Class<T> mappedClass) { this.mappedClass = mappedClass; this.mappedFields = new HashMap<>(); this.mappedProperties = new HashSet<>(); PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass); for (PropertyDescriptor pd : pds) { if (pd.getWriteMethod() != null) { this.mappedFields.put(lowerCaseName(pd.getName()), pd); String underscoredName = underscoreName(pd.getName()); if (!lowerCaseName(pd.getName()).equals(underscoredName)) { this.mappedFields.put(underscoredName, pd); } this.mappedProperties.add(pd.getName()); } } }
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; }
@Nullable private PropertyDescriptor findExistingPropertyDescriptor(String propertyName, Class<?> propertyType) { for (PropertyDescriptor pd : this.propertyDescriptors) { final Class<?> candidateType; final String candidateName = pd.getName(); if (pd instanceof IndexedPropertyDescriptor) { IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd; candidateType = ipd.getIndexedPropertyType(); if (candidateName.equals(propertyName) && (candidateType.equals(propertyType) || candidateType.equals(propertyType.getComponentType()))) { return pd; } } else { candidateType = pd.getPropertyType(); if (candidateName.equals(propertyName) && (candidateType.equals(propertyType) || propertyType.equals(candidateType.getComponentType()))) { return pd; } } } return null; }
/** * 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 boolean hasReadMethodForProperty(BeanInfo beanInfo, String propertyName) { for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) { if (pd.getName().equals(propertyName)) { return pd.getReadMethod() != null; } } return false; }
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 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 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) { } } }
@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"); }
@Override public Object get(Object object) { try { property.getReadMethod().setAccessible(true);// issue 50 return property.getReadMethod().invoke(object); } catch (Exception e) { throw new YAMLException("Unable to find getter for property '" + property.getName() + "' on object " + object + ":" + 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); } } }
PropertyDescriptor[] pds = this.beanInfo.getPropertyDescriptors(); for (PropertyDescriptor pd : pds) { if (Class.class == beanClass && ("classLoader".equals(pd.getName()) || "protectionDomain".equals(pd.getName()))) { logger.trace("Found bean property '" + pd.getName() + "'" + (pd.getPropertyType() != null ? " of type [" + pd.getPropertyType().getName() + "]" : "") + (pd.getPropertyEditorClass() != null ? "; editor [" + pd.getPropertyEditorClass().getName() + "]" : "")); this.propertyDescriptorCache.put(pd.getName(), pd);
public BeanClassProperty(PropertyDescriptor descriptor) { type = descriptor.getPropertyType(); readMethodName = descriptor.getReadMethod().getName(); writeMethodName = descriptor.getWriteMethod().getName(); getter = createGetterFunction(); setter = createSetterFunction(paramGetter -> writeMethodName + "(" + paramGetter + ")"); name = descriptor.getName(); beanType = descriptor.getReadMethod().getDeclaringClass(); } }
private static Map<String, ClassProperty> createProperty(Class type) { List<String> fieldNames = Arrays.stream(type.getDeclaredFields()) .map(Field::getName).collect(Collectors.toList()); return Stream.of(propertyUtils.getPropertyDescriptors(type)) .filter(property -> !property.getName().equals("class") && property.getReadMethod() != null && property.getWriteMethod() != null) .map(BeanClassProperty::new) //让字段有序 .sorted(Comparator.comparing(property -> fieldNames.indexOf(property.name))) .collect(Collectors.toMap(ClassProperty::getName, Function.identity(), (k, k2) -> k, LinkedHashMap::new)); }