Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 31 additions & 12 deletions java/src/org/openqa/selenium/json/SimplePropertyDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<SimplePropertyDescriptor[]> DESCRIPTORS = new ClassValue<>() {
@Override
protected SimplePropertyDescriptor[] computeValue(Class<?> type) {
return getPropertyDescriptorsUncached(type);
}
};

private static final Function<Object, @Nullable Object> GET_CLASS_NAME = new GetClassName();
private final Class<?> clazz;
private final String name;
Expand Down Expand Up @@ -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();
}
Comment thread
rajat315315 marked this conversation as resolved.
Resolved

private static SimplePropertyDescriptor[] getPropertyDescriptorsUncached(Class<?> clazz) {
Map<String, SimplePropertyDescriptor> properties = new HashMap<>();

properties.put("class", new SimplePropertyDescriptor(clazz, "class", GET_CLASS_NAME, null));
Expand Down Expand Up @@ -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,
Expand Down