2929public class PropertyDataFetcherHelper {
3030 private static final AtomicBoolean USE_SET_ACCESSIBLE = new AtomicBoolean (true );
3131 private static final AtomicBoolean USE_NEGATIVE_CACHE = new AtomicBoolean (true );
32- private static final ConcurrentMap <String , Method > METHOD_CACHE = new ConcurrentHashMap <>();
32+ private static final ConcurrentMap <String , CachedMethod > METHOD_CACHE = new ConcurrentHashMap <>();
3333 private static final ConcurrentMap <String , Field > FIELD_CACHE = new ConcurrentHashMap <>();
3434 private static final ConcurrentMap <String , String > NEGATIVE_CACHE = new ConcurrentHashMap <>();
3535
36+ private static class CachedMethod {
37+ CachedMethod (Method method ) {
38+ this .method = method ;
39+ this .takesDataFetcherEnvironmentAsOnlyArgument = takesDataFetcherEnvironmentAsOnlyArgument (method );
40+ }
41+
42+ Method method ;
43+ boolean takesDataFetcherEnvironmentAsOnlyArgument ;
44+ }
45+
3646 public static Object getPropertyValue (String propertyName , Object object , GraphQLType graphQLType ) {
3747 return getPropertyValue (propertyName , object , graphQLType , null );
3848 }
@@ -42,49 +52,52 @@ public static Object getPropertyValue(String propertyName, Object object, GraphQ
4252 return ((Map <?, ?>) object ).get (propertyName );
4353 }
4454
45- String key = mkKey (object , propertyName );
46- //
47- // if we have tried all strategies before and they have all failed then we negatively cache
48- // the key and assume that its never going to turn up. This shortcuts the property lookup
49- // in systems where there was a `foo` graphql property but they never provided an POJO
50- // version of `foo`.
51- if (isNegativelyCached (key )) {
52- return null ;
53- }
54- // lets try positive cache mechanisms next. If we have seen the method or field before
55+ String cacheKey = mkKey (object , propertyName );
56+ // lets try positive cache mechanisms first. If we have seen the method or field before
5557 // then we invoke it directly without burning any cycles doing reflection.
56- Method cachedMethod = METHOD_CACHE .get (key );
58+ CachedMethod cachedMethod = METHOD_CACHE .get (cacheKey );
5759 if (cachedMethod != null ) {
58- MethodFinder methodFinder = (aClass , methodName ) -> cachedMethod ;
5960 try {
60- return getPropertyViaGetterMethod (object , propertyName , graphQLType , methodFinder , environment );
61+ return invokeMethod (object , environment , cachedMethod . method , cachedMethod . takesDataFetcherEnvironmentAsOnlyArgument );
6162 } catch (NoSuchMethodException ignored ) {
62- assertShouldNeverHappen ("A method cached as '%s' is no longer available??" , key );
63+ assertShouldNeverHappen ("A method cached as '%s' is no longer available??" , cacheKey );
6364 }
6465 }
65- Field cachedField = FIELD_CACHE .get (key );
66+ Field cachedField = FIELD_CACHE .get (cacheKey );
6667 if (cachedField != null ) {
67- try {
68- return getPropertyViaFieldAccess (object , propertyName );
69- } catch (FastNoSuchMethodException ignored ) {
70- assertShouldNeverHappen ("A field cached as '%s' is no longer available??" , key );
71- }
68+ return invokeField (object , cachedField );
7269 }
7370
71+ //
72+ // if we have tried all strategies before and they have all failed then we negatively cache
73+ // the cacheKey and assume that its never going to turn up. This shortcuts the property lookup
74+ // in systems where there was a `foo` graphql property but they never provided an POJO
75+ // version of `foo`.
76+ //
77+ // we do this second because we believe in the positive cached version will mostly prevail
78+ // but if we then look it up and negatively cache it then lest do that look up next
79+ //
80+ if (isNegativelyCached (cacheKey )) {
81+ return null ;
82+ }
83+ //
84+ // ok we haven't cached it and we haven't negatively cached it so we have to find the POJO method which is the most
85+ // expensive operation here
86+ //
7487 boolean dfeInUse = environment != null ;
7588 try {
76- MethodFinder methodFinder = (root , methodName ) -> findPubliclyAccessibleMethod (propertyName , root , methodName , dfeInUse );
89+ MethodFinder methodFinder = (root , methodName ) -> findPubliclyAccessibleMethod (cacheKey , propertyName , root , methodName , dfeInUse );
7790 return getPropertyViaGetterMethod (object , propertyName , graphQLType , methodFinder , environment );
7891 } catch (NoSuchMethodException ignored ) {
7992 try {
80- MethodFinder methodFinder = (aClass , methodName ) -> findViaSetAccessible (propertyName , aClass , methodName , dfeInUse );
93+ MethodFinder methodFinder = (aClass , methodName ) -> findViaSetAccessible (cacheKey , propertyName , aClass , methodName , dfeInUse );
8194 return getPropertyViaGetterMethod (object , propertyName , graphQLType , methodFinder , environment );
8295 } catch (NoSuchMethodException ignored2 ) {
8396 try {
84- return getPropertyViaFieldAccess (object , propertyName );
97+ return getPropertyViaFieldAccess (cacheKey , object , propertyName );
8598 } catch (FastNoSuchMethodException e ) {
8699 // we have nothing to ask for and we have exhausted our lookup strategies
87- putInNegativeCache (key );
100+ putInNegativeCache (cacheKey );
88101 return null ;
89102 }
90103 }
@@ -122,22 +135,10 @@ private static Object getPropertyViaGetterMethod(Object object, String propertyN
122135
123136 private static Object getPropertyViaGetterUsingPrefix (Object object , String propertyName , String prefix , MethodFinder methodFinder , DataFetchingEnvironment environment ) throws NoSuchMethodException {
124137 String getterName = prefix + propertyName .substring (0 , 1 ).toUpperCase () + propertyName .substring (1 );
125- try {
126- Method method = methodFinder .apply (object .getClass (), getterName );
127- if (takesDataFetcherEnvironmentAsOnlyArgument (method )) {
128- if (environment == null ) {
129- throw new FastNoSuchMethodException (getterName );
130- }
131- return method .invoke (object , environment );
132- } else {
133- return method .invoke (object );
134- }
135- } catch (IllegalAccessException | InvocationTargetException e ) {
136- throw new GraphQLException (e );
137- }
138+ Method method = methodFinder .apply (object .getClass (), getterName );
139+ return invokeMethod (object , environment , method , takesDataFetcherEnvironmentAsOnlyArgument (method ));
138140 }
139141
140-
141142 /**
142143 * Invoking public methods on package-protected classes via reflection
143144 * causes exceptions. This method searches a class's hierarchy for
@@ -146,18 +147,17 @@ private static Object getPropertyViaGetterUsingPrefix(Object object, String prop
146147 * which have abstract public interfaces implemented by package-protected
147148 * (generated) subclasses.
148149 */
149- private static Method findPubliclyAccessibleMethod (String propertyName , Class <?> root , String methodName , boolean dfeInUse ) throws NoSuchMethodException {
150- Class <?> currentClass = root ;
150+ private static Method findPubliclyAccessibleMethod (String cacheKey , String propertyName , Class <?> rootClass , String methodName , boolean dfeInUse ) throws NoSuchMethodException {
151+ Class <?> currentClass = rootClass ;
151152 while (currentClass != null ) {
152- String key = mkKey (currentClass , propertyName );
153153 if (Modifier .isPublic (currentClass .getModifiers ())) {
154154 if (dfeInUse ) {
155155 //
156156 // try a getter that takes DataFetchingEnvironment first (if we have one)
157157 try {
158158 Method method = currentClass .getMethod (methodName , DataFetchingEnvironment .class );
159159 if (Modifier .isPublic (method .getModifiers ())) {
160- METHOD_CACHE .putIfAbsent (key , method );
160+ METHOD_CACHE .putIfAbsent (cacheKey , new CachedMethod ( method ) );
161161 return method ;
162162 }
163163 } catch (NoSuchMethodException e ) {
@@ -166,23 +166,22 @@ private static Method findPubliclyAccessibleMethod(String propertyName, Class<?>
166166 }
167167 Method method = currentClass .getMethod (methodName );
168168 if (Modifier .isPublic (method .getModifiers ())) {
169- METHOD_CACHE .putIfAbsent (key , method );
169+ METHOD_CACHE .putIfAbsent (cacheKey , new CachedMethod ( method ) );
170170 return method ;
171171 }
172172 }
173173 currentClass = currentClass .getSuperclass ();
174174 }
175- return root .getMethod (methodName );
175+ assert rootClass != null ;
176+ return rootClass .getMethod (methodName );
176177 }
177178
178- private static Method findViaSetAccessible (String propertyName , Class <?> aClass , String methodName , boolean dfeInUse ) throws NoSuchMethodException {
179+ private static Method findViaSetAccessible (String cacheKey , String propertyName , Class <?> aClass , String methodName , boolean dfeInUse ) throws NoSuchMethodException {
179180 if (!USE_SET_ACCESSIBLE .get ()) {
180181 throw new FastNoSuchMethodException (methodName );
181182 }
182183 Class <?> currentClass = aClass ;
183184 while (currentClass != null ) {
184- String key = mkKey (currentClass , propertyName );
185-
186185 Predicate <Method > whichMethods = mth -> {
187186 if (dfeInUse ) {
188187 return hasZeroArgs (mth ) || takesDataFetcherEnvironmentAsOnlyArgument (mth );
@@ -199,7 +198,7 @@ private static Method findViaSetAccessible(String propertyName, Class<?> aClass,
199198 // few JVMs actually enforce this but it might happen
200199 Method method = m .get ();
201200 method .setAccessible (true );
202- METHOD_CACHE .putIfAbsent (key , method );
201+ METHOD_CACHE .putIfAbsent (cacheKey , new CachedMethod ( method ) );
203202 return method ;
204203 } catch (SecurityException ignored ) {
205204 }
@@ -209,25 +208,24 @@ private static Method findViaSetAccessible(String propertyName, Class<?> aClass,
209208 throw new FastNoSuchMethodException (methodName );
210209 }
211210
212- private static Object getPropertyViaFieldAccess (Object object , String propertyName ) throws FastNoSuchMethodException {
211+ private static Object getPropertyViaFieldAccess (String cacheKey , Object object , String propertyName ) throws FastNoSuchMethodException {
213212 Class <?> aClass = object .getClass ();
214- String key = mkKey (aClass , propertyName );
215213 try {
216214 Field field = aClass .getField (propertyName );
217- FIELD_CACHE .putIfAbsent (key , field );
215+ FIELD_CACHE .putIfAbsent (cacheKey , field );
218216 return field .get (object );
219217 } catch (NoSuchFieldException e ) {
220218 if (!USE_SET_ACCESSIBLE .get ()) {
221- throw new FastNoSuchMethodException (key );
219+ throw new FastNoSuchMethodException (cacheKey );
222220 }
223221 // if not public fields then try via setAccessible
224222 try {
225223 Field field = aClass .getDeclaredField (propertyName );
226224 field .setAccessible (true );
227- FIELD_CACHE .putIfAbsent (key , field );
225+ FIELD_CACHE .putIfAbsent (cacheKey , field );
228226 return field .get (object );
229227 } catch (SecurityException | NoSuchFieldException ignored2 ) {
230- throw new FastNoSuchMethodException (key );
228+ throw new FastNoSuchMethodException (cacheKey );
231229 } catch (IllegalAccessException e1 ) {
232230 throw new GraphQLException (e );
233231 }
@@ -236,6 +234,29 @@ private static Object getPropertyViaFieldAccess(Object object, String propertyNa
236234 }
237235 }
238236
237+ private static Object invokeMethod (Object object , DataFetchingEnvironment environment , Method method , boolean takesDataFetcherEnvironmentAsOnlyArgument ) throws FastNoSuchMethodException {
238+ try {
239+ if (takesDataFetcherEnvironmentAsOnlyArgument ) {
240+ if (environment == null ) {
241+ throw new FastNoSuchMethodException (method .getName ());
242+ }
243+ return method .invoke (object , environment );
244+ } else {
245+ return method .invoke (object );
246+ }
247+ } catch (IllegalAccessException | InvocationTargetException e ) {
248+ throw new GraphQLException (e );
249+ }
250+ }
251+
252+ private static Object invokeField (Object object , Field field ) {
253+ try {
254+ return field .get (object );
255+ } catch (IllegalAccessException e ) {
256+ throw new GraphQLException (e );
257+ }
258+ }
259+
239260 @ SuppressWarnings ("SimplifiableIfStatement" )
240261 private static boolean isBooleanProperty (GraphQLType graphQLType ) {
241262 if (graphQLType == GraphQLBoolean ) {
@@ -262,10 +283,7 @@ public static boolean setUseNegativeCache(boolean flag) {
262283 }
263284
264285 private static String mkKey (Object object , String propertyName ) {
265- return mkKey (object .getClass (), propertyName );
266- }
267-
268- private static String mkKey (Class <?> clazz , String propertyName ) {
286+ Class <?> clazz = object .getClass ();
269287 ClassLoader classLoader = clazz .getClassLoader ();
270288 if (classLoader != null ) {
271289 return classLoader .hashCode () + "__" + clazz .getName () + "__" + propertyName ;
0 commit comments