Skip to content

Commit 091710c

Browse files
committed
Adding in Java 9+ support for lookup across class loaders
1 parent 315b396 commit 091710c

2 files changed

Lines changed: 12 additions & 13 deletions

File tree

src/main/java/graphql/schema/fetching/LambdaFetchingSupport.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,16 @@ static Function<Object, Object> mkCallFunction(Class<?> targetClass, String targ
204204
return getterFunction;
205205
}
206206

207-
private static MethodHandles.Lookup getLookup(Class<?> targetClass) throws IllegalAccessException {
207+
private static MethodHandles.Lookup getLookup(Class<?> targetClass) {
208208
MethodHandles.Lookup lookupMe = MethodHandles.lookup();
209209
//
210-
// This is a Java 9 approach to method look up allowing private access
211-
// which we don't want to use yet until we get to Java 11
210+
// This is a Java 9+ approach to method look up allowing private access
212211
//
213-
//lookupMe = MethodHandles.privateLookupIn(targetClass, lookupMe);
214-
return lookupMe;
212+
try {
213+
return MethodHandles.privateLookupIn(targetClass, lookupMe);
214+
} catch (IllegalAccessException e) {
215+
return lookupMe;
216+
}
215217
}
216218

217219
}

src/test/groovy/graphql/schema/fetching/LambdaFetchingSupportTest.groovy

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,18 +182,15 @@ class LambdaFetchingSupportTest extends Specification {
182182
def getter = LambdaFetchingSupport.createGetter(customClass, "hello")
183183
then:
184184

185+
// with Java 9+ we can get access to methods across class loaders
185186
getter.isPresent()
186-
try {
187-
getter.get().apply(targetObject)
188-
assert false, "We expect this to fail on Java 8 without access to MethodHandles.privateLookupIn"
189-
} catch (LinkageError | ClassCastException ignored) {
190-
}
187+
def value = getter.get().apply(targetObject)
188+
value == "world"
191189

192-
// show that a DF can still be used access this because of the reflection fallback
193-
// in the future it will work via MethodHandles.privateLookupIn
190+
// show that a DF can be used
194191
when:
195192
def ageDF = PropertyDataFetcher.fetching("hello")
196-
def value = ageDF.get(fld("hello"), targetObject, { -> null })
193+
value = ageDF.get(fld("hello"), targetObject, { -> null })
197194
then:
198195
value == "world"
199196
}

0 commit comments

Comments
 (0)