DATACMNS-441 - Fixed potential NullPointerException in query method detection on Java 8 compiled code.

We now skip bridge methods for query method processing in order to circumvent the changed behavior of Class#getMethods() in Java 8 (when the code was compiled with target byte code level 1.8). On Java 8 this results in bridge methods being found and inspected for query method annotations. Since we currently cannot analyze generic return types of bridge methods this results in NullPointerExceptions being thrown at a later stage in AbstractRepositoryMetadata#getReturnedDomainClass(…).

Original pull request: #64.
Related issue: DATAJPA-465.
This commit is contained in:
Thomas Darimont
2014-02-18 14:55:50 +01:00
committed by Oliver Gierke
parent 931a697ec5
commit f047cce7c3
2 changed files with 25 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011-2013 the original author or authors.
* Copyright 2011-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -172,7 +172,8 @@ class DefaultRepositoryInformation implements RepositoryInformation {
* @return
*/
private boolean isQueryMethodCandidate(Method method) {
return isQueryAnnotationPresentOn(method) || !isCustomMethod(method) && !isBaseClassMethod(method);
return !method.isBridge() //
&& (isQueryAnnotationPresentOn(method) || !isCustomMethod(method) && !isBaseClassMethod(method));
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -49,6 +49,7 @@ import org.springframework.data.repository.core.support.DefaultRepositoryMetadat
* Unit tests for {@link DefaultRepositoryInformation}.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
@RunWith(MockitoJUnitRunner.class)
public class DefaultRepositoryInformationUnitTests {
@@ -208,6 +209,20 @@ public class DefaultRepositoryInformationUnitTests {
assertThat(information.getTargetClassMethod(method), is(reference));
}
/**
* @see DATACMNS-441
*/
@Test
public void getQueryShouldNotReturnAnyBridgeMethods() {
RepositoryMetadata metadata = new DefaultRepositoryMetadata(CustomDefaultRepositoryMethodsRepository.class);
RepositoryInformation information = new DefaultRepositoryInformation(metadata, CrudRepository.class, null);
for (Method method : information.getQueryMethods()) {
assertFalse(method.isBridge());
}
}
private Method getMethodFrom(Class<?> type, String name) {
for (Method method : type.getMethods()) {
if (method.getName().equals(name)) {
@@ -299,4 +314,10 @@ public class DefaultRepositoryInformationUnitTests {
}
interface BossRepository extends CrudRepository<Boss, Long> {}
interface CustomDefaultRepositoryMethodsRepository extends CrudRepository<User, Integer> {
@MyQuery
List<User> findAll();
}
}