Skip to content

Commit

Permalink
Try loading implementation classes using the parent class' loader first
Browse files Browse the repository at this point in the history
Relates to #177
  • Loading branch information
kaqqao committed Sep 8, 2018
1 parent de66bb4 commit ee6d869
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Changed
- Upgraded to [graphql-java 10.0](https://github.com/graphql-java/graphql-java/releases)
- Introduced [`SchemaTransformer`](https://github.com/leangen/graphql-spqr/blob/master/src/main/java/io/leangen/graphql/generator/mapping/SchemaTransformer.java) to enable modifying field and argument definitions
- Introduced [`ResolverInterceptor`](https://github.com/leangen/graphql-spqr/blob/master/src/main/java/io/leangen/graphql/execution/ResolverInterceptor.java) that can perform arbitrary logic around the invocation of the underlying method/field [#180](https://github.com/leangen/graphql-spqr/issues/180)
- Introduced [`ResolverInterceptor`](https://github.com/leangen/graphql-spqr/blob/master/src/main/java/io/leangen/graphql/execution/ResolverInterceptor.java) that can perform arbitrary logic around the invocation of the underlying method/field [#180](https://github.com/leangen/graphql-spqr/issues/180) [#92](https://github.com/leangen/graphql-spqr/issues/92)
- Added a way to get all deserialized arguments `ResolutionEnvironment` [#174](https://github.com/leangen/graphql-spqr/issues/174)
- All exceptions thrown during field resolution now bubble up unchanged (using sneaky-throw)
- Try loading implementation classes using the parent class' loader first [#177](https://github.com/leangen/graphql-spqr/issues/177)
- Renamed `withTypeAliasGroup` to `withTypeSynonymGroup`

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
<version>4.1.5</version>
<version>4.1.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/io/leangen/graphql/util/ClassFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,16 @@ public List<AnnotatedType> findImplementations(AnnotatedType superType, Predicat
public List<Class<?>> findImplementations(Class superType, Predicate<ClassInfo> filter, String... packages) {
String[] scanPackages = Utils.emptyIfNull(packages);
String cacheKey = Arrays.stream(scanPackages).sorted().collect(Collectors.joining());
ScanResult scanResults = cache.computeIfAbsent(cacheKey, k -> new ClassGraph().whitelistPackages(packages).enableAllInfo().scan());
ScanResult scanResults = cache.computeIfAbsent(cacheKey, k -> new ClassGraph()
.whitelistPackages(packages)
.enableAllInfo()
.initializeLoadedClasses()
.scan());
try {
return scanResults.getAllClasses().stream()
.filter(impl -> superType.isInterface() ? impl.implementsInterface(superType.getName()) : impl.extendsSuperclass(superType.getName()))
.filter(filter == null ? info -> true : filter)
.flatMap(ClassFinder::loadClass)
.flatMap(info -> loadClass(info, superType))
.collect(Collectors.toList());
} catch (Exception e) {
log.error("Failed to auto discover the subtypes of " + superType.getName()
Expand All @@ -90,7 +94,14 @@ private static Stream<AnnotatedType> getExactSubType(AnnotatedType superType, Cl
return Stream.empty();
}

private static Stream<Class<?>> loadClass(ClassInfo classInfo) {
private static Stream<Class<?>> loadClass(ClassInfo classInfo, Class superType) {
try {
return Stream.of(Class.forName(classInfo.getName(), true, superType.getClassLoader()));
} catch (ClassNotFoundException e) {
log.warn(String.format("Implementation class %s could not be loaded using the same loader that loaded %s." +
" Trying other loaders... For details and possible solutions see %s",
classInfo.getName(), superType.getName(), Urls.Errors.IMPLEMENTATION_CLASS_LOADING_FAILED));
}
try {
return Stream.of(classInfo.loadClass());
} catch (Exception e) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/io/leangen/graphql/util/Urls.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ public static final class Errors {
public static final String AMBIGUOUS_MEMBER_TYPE = "https://github.com/leangen/graphql-spqr/wiki/Errors#ambiguous-member-type";
public static final String AMBIGUOUS_PARAMETER_TYPE = "https://github.com/leangen/graphql-spqr/wiki/Errors#ambiguous-method-parameter-type";
public static final String RELAY_CONNECTION_SPEC_VIOLATION = "https://github.com/leangen/graphql-spqr/wiki/Errors#relay-connection-spec-violation";
public static final String IMPLEMENTATION_CLASS_LOADING_FAILED = "https://github.com/leangen/graphql-spqr/wiki/Errors#class-loading-issues";
}
}

0 comments on commit ee6d869

Please sign in to comment.