Skip to content

Commit

Permalink
Replace all references to PropertyUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
tumbarumba committed Dec 1, 2024
1 parent 6919275 commit 6205e14
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import static org.hamcrest.Condition.matched;
import static org.hamcrest.Condition.notMatched;
import static org.hamcrest.beans.PropertyUtil.NO_ARGUMENTS;
import static org.hamcrest.beans.PropertyAccessor.NO_ARGUMENTS;

/**
* <p>A matcher that checks if an object has a JavaBean property with the
Expand Down
29 changes: 27 additions & 2 deletions hamcrest/src/main/java/org/hamcrest/beans/PropertyAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
* JavaBean specification and APIs, or it will fall back to finding
* fields with corresponding methods, enabling the property matchers
* to work with newer classes like Records.
* <p>
* 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
* more information on JavaBeans.
*/
public class PropertyAccessor {
private final Object beanLikeObject;
Expand All @@ -33,7 +36,7 @@ public PropertyAccessor(Object beanLikeObject) {
}

private Map<String, PropertyReadLens> makeLensesFor(Object bean) {
PropertyDescriptor[] properties = PropertyUtil.propertyDescriptorsFor(bean, Object.class);
PropertyDescriptor[] properties = propertyDescriptorsFor(bean, Object.class);
if (properties != null && properties.length > 0) {
return makePropertyLensesFrom(properties);
}
Expand Down Expand Up @@ -185,10 +188,32 @@ public Method getReadMethod() {
public Object getValue() {
Object bean = PropertyAccessor.this.beanLikeObject;
try {
return readMethod.invoke(bean, PropertyUtil.NO_ARGUMENTS);
return readMethod.invoke(bean, NO_ARGUMENTS);
} catch (Exception e) {
throw new IllegalArgumentException("Could not invoke " + readMethod + " on " + bean, e);
}
}
}

/**
* Returns all the property descriptors for the class associated with the given object
*
* @param fromObj Use the class of this object
* @param stopClass Don't include any properties from this ancestor class upwards.
* @return Property descriptors
* @throws IllegalArgumentException if there's a introspection failure
*/
public static PropertyDescriptor[] propertyDescriptorsFor(Object fromObj, Class<Object> stopClass) throws IllegalArgumentException {
try {
return Introspector.getBeanInfo(fromObj.getClass(), stopClass).getPropertyDescriptors();
} catch (IntrospectionException e) {
throw new IllegalArgumentException("Could not get property descriptors for " + fromObj.getClass(), e);
}
}

/**
* Empty object array, used for documenting that we are deliberately passing no arguments to a method.
*/
public static final Object[] NO_ARGUMENTS = new Object[0];

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* @author Iain McGinniss
* @author Steve Freeman
* @since 1.1.0
* @deprecated Replaced by {@link PropertyAccessor}
*/
public class PropertyUtil {

Expand Down

0 comments on commit 6205e14

Please sign in to comment.