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
18 changes: 18 additions & 0 deletions src/main/java/graphql/schema/PropertyFetchingImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ private interface MethodFinder {
}

private Object getPropertyViaGetterMethod(Object object, String propertyName, GraphQLType graphQLType, MethodFinder methodFinder, Object singleArgumentValue) throws NoSuchMethodException {
if (isRecord(object.getClass())) {
try {
return getPropertyViaGetterWithoutPrefix(object, propertyName, methodFinder, singleArgumentValue);
} catch (NoSuchMethodException ignored) {
// Records can still have getX and isX methods...
}
}
if (isBooleanProperty(graphQLType)) {
try {
return getPropertyViaGetterUsingPrefix(object, propertyName, "is", methodFinder, singleArgumentValue);
Expand All @@ -142,6 +149,11 @@ private Object getPropertyViaGetterUsingPrefix(Object object, String propertyNam
return invokeMethod(object, singleArgumentValue, method, takesSingleArgumentTypeAsOnlyArgument(method));
}

private Object getPropertyViaGetterWithoutPrefix(Object object, String propertyName, MethodFinder methodFinder, Object singleArgumentValue) throws NoSuchMethodException {
Method method = methodFinder.apply(object.getClass(), propertyName);
return invokeMethod(object, singleArgumentValue, method, takesSingleArgumentTypeAsOnlyArgument(method));
}

/**
* Invoking public methods on package-protected classes via reflection
* causes exceptions. This method searches a class's hierarchy for
Expand Down Expand Up @@ -260,6 +272,12 @@ private Object invokeField(Object object, Field field) {
}
}

private boolean isRecord(Class<?> clazz) {
// https://github.com/openjdk/jdk/blob/3f26d84f6a03030080328e36a1fd1a08c982838c/src/java.base/share/classes/java/lang/Class.java#L3829-L3835
return "java.lang.Record".equals(clazz.getSuperclass().getName())
&& (clazz.getModifiers() & Modifier.FINAL) != 0;
}

@SuppressWarnings("SimplifiableIfStatement")
private boolean isBooleanProperty(GraphQLType graphQLType) {
if (graphQLType == GraphQLBoolean) {
Expand Down