diff --git a/java/src/org/openqa/selenium/json/SimplePropertyDescriptor.java b/java/src/org/openqa/selenium/json/SimplePropertyDescriptor.java index 301e33eed8742..799caf8a3b19e 100644 --- a/java/src/org/openqa/selenium/json/SimplePropertyDescriptor.java +++ b/java/src/org/openqa/selenium/json/SimplePropertyDescriptor.java @@ -24,8 +24,19 @@ import java.util.function.Function; import org.jspecify.annotations.Nullable; +/** + * A lightweight descriptor representing a bean property (getter, setter, or boolean prefix property) + * used for JSON serialization and deserialization coercion. + */ public class SimplePropertyDescriptor { + private static final ClassValue DESCRIPTORS = new ClassValue<>() { + @Override + protected SimplePropertyDescriptor[] computeValue(Class type) { + return getPropertyDescriptorsUncached(type); + } + }; + private static final Function GET_CLASS_NAME = new GetClassName(); private final Class clazz; private final String name; @@ -60,7 +71,17 @@ public String toString() { return String.format("%s.%s", clazz.getSimpleName(), name); } + /** + * Retrieves the cached simple property descriptors for the specified class. + * + * @param clazz the target class to inspect + * @return a cloned array of {@link SimplePropertyDescriptor}s representing the class's properties + */ public static SimplePropertyDescriptor[] getPropertyDescriptors(Class clazz) { + return DESCRIPTORS.get(clazz).clone(); + } + + private static SimplePropertyDescriptor[] getPropertyDescriptorsUncached(Class clazz) { Map properties = new HashMap<>(); properties.put("class", new SimplePropertyDescriptor(clazz, "class", GET_CLASS_NAME, null)); @@ -98,21 +119,19 @@ public static SimplePropertyDescriptor[] getPropertyDescriptors(Class clazz) if (readMethod != null) { final Method finalReadMethod = readMethod; - read = - obj -> { - try { - finalReadMethod.setAccessible(true); - return finalReadMethod.invoke(obj); - } catch (ReflectiveOperationException e) { - throw new JsonException(e); - } - }; + read = obj -> { + try { + finalReadMethod.setAccessible(true); + return finalReadMethod.invoke(obj); + } catch (ReflectiveOperationException e) { + throw new JsonException(e); + } + }; } if (propertyName != null && (readMethod != null || writeMethod != null)) { - SimplePropertyDescriptor descriptor = - properties.getOrDefault( - propertyName, new SimplePropertyDescriptor(clazz, propertyName, null, null)); + SimplePropertyDescriptor descriptor = properties.getOrDefault( + propertyName, new SimplePropertyDescriptor(clazz, propertyName, null, null)); properties.put( propertyName,