|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2019 the original author or authors. |
| 2 | + * Copyright 2002-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
17 | 17 | package org.springframework.beans;
|
18 | 18 |
|
19 | 19 | import java.beans.IntrospectionException;
|
| 20 | +import java.beans.Introspector; |
20 | 21 | import java.beans.PropertyDescriptor;
|
21 | 22 | import java.lang.reflect.Method;
|
| 23 | +import java.util.Collection; |
22 | 24 | import java.util.Enumeration;
|
| 25 | +import java.util.Map; |
| 26 | +import java.util.TreeMap; |
23 | 27 |
|
24 | 28 | import org.springframework.lang.Nullable;
|
25 | 29 | import org.springframework.util.ObjectUtils;
|
|
32 | 36 | */
|
33 | 37 | abstract class PropertyDescriptorUtils {
|
34 | 38 |
|
| 39 | + public static final PropertyDescriptor[] EMPTY_PROPERTY_DESCRIPTOR_ARRAY = {}; |
| 40 | + |
| 41 | + |
| 42 | + /** |
| 43 | + * Simple introspection algorithm for basic set/get/is accessor methods, |
| 44 | + * building corresponding JavaBeans property descriptors for them. |
| 45 | + * <p>This just supports the basic JavaBeans conventions, without indexed |
| 46 | + * properties or any customizers, and without other BeanInfo metadata. |
| 47 | + * For standard JavaBeans introspection, use the JavaBeans Introspector. |
| 48 | + * @param beanClass the target class to introspect |
| 49 | + * @return a collection of property descriptors |
| 50 | + * @throws IntrospectionException from introspecting the given bean class |
| 51 | + * @since 5.3.24 |
| 52 | + * @see SimpleBeanInfoFactory |
| 53 | + * @see java.beans.Introspector#getBeanInfo(Class) |
| 54 | + */ |
| 55 | + public static Collection<PropertyDescriptor> determineBasicProperties(Class<?> beanClass) throws IntrospectionException { |
| 56 | + Map<String, PropertyDescriptor> pdMap = new TreeMap<>(); |
| 57 | + |
| 58 | + for (Method method : beanClass.getMethods()) { |
| 59 | + String methodName = method.getName(); |
| 60 | + |
| 61 | + boolean setter; |
| 62 | + int nameIndex; |
| 63 | + if (methodName.startsWith("set") && method.getParameterCount() == 1) { |
| 64 | + setter = true; |
| 65 | + nameIndex = 3; |
| 66 | + } |
| 67 | + else if (methodName.startsWith("get") && method.getParameterCount() == 0 && method.getReturnType() != Void.TYPE) { |
| 68 | + setter = false; |
| 69 | + nameIndex = 3; |
| 70 | + } |
| 71 | + else if (methodName.startsWith("is") && method.getParameterCount() == 0 && method.getReturnType() == boolean.class) { |
| 72 | + setter = false; |
| 73 | + nameIndex = 2; |
| 74 | + } |
| 75 | + else { |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + String propertyName = Introspector.decapitalize(methodName.substring(nameIndex)); |
| 80 | + if (propertyName.isEmpty()) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + PropertyDescriptor pd = pdMap.get(propertyName); |
| 85 | + if (pd != null) { |
| 86 | + if (setter) { |
| 87 | + if (pd.getWriteMethod() == null || |
| 88 | + pd.getWriteMethod().getParameterTypes()[0].isAssignableFrom(method.getParameterTypes()[0])) { |
| 89 | + try { |
| 90 | + pd.setWriteMethod(method); |
| 91 | + } |
| 92 | + catch (IntrospectionException ex) { |
| 93 | + // typically a type mismatch -> ignore |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + else { |
| 98 | + if (pd.getReadMethod() == null || |
| 99 | + (pd.getReadMethod().getReturnType() == method.getReturnType() && method.getName().startsWith("is"))) { |
| 100 | + try { |
| 101 | + pd.setReadMethod(method); |
| 102 | + } |
| 103 | + catch (IntrospectionException ex) { |
| 104 | + // typically a type mismatch -> ignore |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + else { |
| 110 | + pd = new BasicPropertyDescriptor(propertyName, beanClass); |
| 111 | + if (setter) { |
| 112 | + pd.setWriteMethod(method); |
| 113 | + } |
| 114 | + else { |
| 115 | + pd.setReadMethod(method); |
| 116 | + } |
| 117 | + pdMap.put(propertyName, pd); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return pdMap.values(); |
| 122 | + } |
| 123 | + |
35 | 124 | /**
|
36 | 125 | * See {@link java.beans.FeatureDescriptor}.
|
37 | 126 | */
|
@@ -173,4 +262,46 @@ public static boolean equals(PropertyDescriptor pd, PropertyDescriptor otherPd)
|
173 | 262 | pd.isBound() == otherPd.isBound() && pd.isConstrained() == otherPd.isConstrained());
|
174 | 263 | }
|
175 | 264 |
|
| 265 | + |
| 266 | + /** |
| 267 | + * PropertyDescriptor for {@link #determineBasicProperties(Class)}, |
| 268 | + * not performing any early type determination for |
| 269 | + * {@link #setReadMethod}/{@link #setWriteMethod}. |
| 270 | + * @since 5.3.24 |
| 271 | + */ |
| 272 | + private static class BasicPropertyDescriptor extends PropertyDescriptor { |
| 273 | + |
| 274 | + @Nullable |
| 275 | + private Method readMethod; |
| 276 | + |
| 277 | + @Nullable |
| 278 | + private Method writeMethod; |
| 279 | + |
| 280 | + public BasicPropertyDescriptor(String propertyName, Class<?> beanClass) throws IntrospectionException { |
| 281 | + super(propertyName, beanClass, null, null); |
| 282 | + } |
| 283 | + |
| 284 | + @Override |
| 285 | + public void setReadMethod(@Nullable Method readMethod) { |
| 286 | + this.readMethod = readMethod; |
| 287 | + } |
| 288 | + |
| 289 | + @Override |
| 290 | + @Nullable |
| 291 | + public Method getReadMethod() { |
| 292 | + return this.readMethod; |
| 293 | + } |
| 294 | + |
| 295 | + @Override |
| 296 | + public void setWriteMethod(@Nullable Method writeMethod) { |
| 297 | + this.writeMethod = writeMethod; |
| 298 | + } |
| 299 | + |
| 300 | + @Override |
| 301 | + @Nullable |
| 302 | + public Method getWriteMethod() { |
| 303 | + return this.writeMethod; |
| 304 | + } |
| 305 | + } |
| 306 | + |
176 | 307 | }
|
0 commit comments