graphql.schema.fetching.LambdaFetchingSupport Maven / Gradle / Ivy
package graphql.schema.fetching;
import graphql.Internal;
import graphql.VisibleForTesting;
import java.lang.invoke.CallSite;
import java.lang.invoke.LambdaMetafactory;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import static java.util.stream.Collectors.toList;
@Internal
public class LambdaFetchingSupport {
/**
* This support class will use {@link LambdaMetafactory} and {@link MethodHandles} to create a dynamic function that allows access to a public
* getter method on the nominated class. {@link MethodHandles} is a caller senstive lookup mechanism. If the graphql-java cant lookup a class, then
* it won't be able to make dynamic lambda function to it.
*
* If one cant be made, because it doesn't exist or the calling class does not have access to the method, then it will return
* an empty result indicating that this strategy cant be used.
*
* @param sourceClass the class that has the property getter method
* @param propertyName the name of the property to get
*
* @return a function that can be used to pass in an instance of source class and returns its getter method value
*/
public static Optional> createGetter(Class> sourceClass, String propertyName) {
Method candidateMethod = getCandidateMethod(sourceClass, propertyName);
if (candidateMethod != null) {
try {
Function
© 2015 - 2024 Weber Informatics LLC | Privacy Policy