Skip to content

Commit 6205e14

Browse files
committed
Replace all references to PropertyUtil
1 parent 6919275 commit 6205e14

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

hamcrest/src/main/java/org/hamcrest/beans/HasPropertyWithValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import static org.hamcrest.Condition.matched;
1616
import static org.hamcrest.Condition.notMatched;
17-
import static org.hamcrest.beans.PropertyUtil.NO_ARGUMENTS;
17+
import static org.hamcrest.beans.PropertyAccessor.NO_ARGUMENTS;
1818

1919
/**
2020
* <p>A matcher that checks if an object has a JavaBean property with the

hamcrest/src/main/java/org/hamcrest/beans/PropertyAccessor.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
* JavaBean specification and APIs, or it will fall back to finding
1919
* fields with corresponding methods, enabling the property matchers
2020
* to work with newer classes like Records.
21+
* <p>
22+
* See <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/beans/index.html">https://docs.oracle.com/javase/8/docs/technotes/guides/beans/index.html</a> for
23+
* more information on JavaBeans.
2124
*/
2225
public class PropertyAccessor {
2326
private final Object beanLikeObject;
@@ -33,7 +36,7 @@ public PropertyAccessor(Object beanLikeObject) {
3336
}
3437

3538
private Map<String, PropertyReadLens> makeLensesFor(Object bean) {
36-
PropertyDescriptor[] properties = PropertyUtil.propertyDescriptorsFor(bean, Object.class);
39+
PropertyDescriptor[] properties = propertyDescriptorsFor(bean, Object.class);
3740
if (properties != null && properties.length > 0) {
3841
return makePropertyLensesFrom(properties);
3942
}
@@ -185,10 +188,32 @@ public Method getReadMethod() {
185188
public Object getValue() {
186189
Object bean = PropertyAccessor.this.beanLikeObject;
187190
try {
188-
return readMethod.invoke(bean, PropertyUtil.NO_ARGUMENTS);
191+
return readMethod.invoke(bean, NO_ARGUMENTS);
189192
} catch (Exception e) {
190193
throw new IllegalArgumentException("Could not invoke " + readMethod + " on " + bean, e);
191194
}
192195
}
193196
}
197+
198+
/**
199+
* Returns all the property descriptors for the class associated with the given object
200+
*
201+
* @param fromObj Use the class of this object
202+
* @param stopClass Don't include any properties from this ancestor class upwards.
203+
* @return Property descriptors
204+
* @throws IllegalArgumentException if there's a introspection failure
205+
*/
206+
public static PropertyDescriptor[] propertyDescriptorsFor(Object fromObj, Class<Object> stopClass) throws IllegalArgumentException {
207+
try {
208+
return Introspector.getBeanInfo(fromObj.getClass(), stopClass).getPropertyDescriptors();
209+
} catch (IntrospectionException e) {
210+
throw new IllegalArgumentException("Could not get property descriptors for " + fromObj.getClass(), e);
211+
}
212+
}
213+
214+
/**
215+
* Empty object array, used for documenting that we are deliberately passing no arguments to a method.
216+
*/
217+
public static final Object[] NO_ARGUMENTS = new Object[0];
218+
194219
}

hamcrest/src/main/java/org/hamcrest/beans/PropertyUtil.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* @author Iain McGinniss
1313
* @author Steve Freeman
1414
* @since 1.1.0
15+
* @deprecated Replaced by {@link PropertyAccessor}
1516
*/
1617
public class PropertyUtil {
1718

0 commit comments

Comments
 (0)