|
15 | 15 | */ |
16 | 16 | package org.springframework.data.repository.aot.generate; |
17 | 17 |
|
18 | | -import static org.assertj.core.api.Assertions.*; |
19 | | -import static org.mockito.Mockito.*; |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.mockito.Mockito.doReturn; |
| 20 | +import static org.mockito.Mockito.mock; |
20 | 21 |
|
21 | 22 | import example.UserRepository.User; |
22 | 23 |
|
23 | 24 | import java.util.List; |
| 25 | +import java.util.Map; |
24 | 26 | import java.util.TimeZone; |
25 | 27 |
|
26 | 28 | import javax.lang.model.element.Modifier; |
27 | 29 |
|
28 | 30 | import org.junit.jupiter.api.BeforeEach; |
29 | 31 | import org.junit.jupiter.api.Test; |
30 | 32 | import org.mockito.Answers; |
31 | | - |
32 | 33 | import org.springframework.aot.generate.Generated; |
33 | 34 | import org.springframework.aot.hint.TypeReference; |
34 | 35 | import org.springframework.core.ResolvableType; |
|
40 | 41 | import org.springframework.data.repository.core.RepositoryInformation; |
41 | 42 | import org.springframework.data.repository.core.support.AnnotationRepositoryMetadata; |
42 | 43 | import org.springframework.data.repository.core.support.RepositoryFragment; |
| 44 | +import org.springframework.data.repository.query.DefaultParameters; |
43 | 45 | import org.springframework.data.repository.query.QueryMethod; |
44 | 46 | import org.springframework.javapoet.ClassName; |
45 | 47 | import org.springframework.javapoet.JavaFile; |
@@ -179,8 +181,8 @@ void shouldContributeFragmentImplementationMetadata() { |
179 | 181 | AnnotationRepositoryMetadata.getMetadata(QuerydslUserRepository.class), CrudRepository.class, |
180 | 182 | List.of(RepositoryFragment.structural(QuerydslPredicateExecutor.class, DummyQuerydslPredicateExecutor.class))); |
181 | 183 |
|
182 | | - AotRepositoryCreator creator = AotRepositoryCreator |
183 | | - .forRepository(repositoryInformation, "Commons", new SpelAwareProxyProjectionFactory()); |
| 184 | + AotRepositoryCreator creator = AotRepositoryCreator.forRepository(repositoryInformation, "Commons", |
| 185 | + new SpelAwareProxyProjectionFactory()); |
184 | 186 | creator.contributeMethods(method -> null); |
185 | 187 | AotRepositoryCreator.AotBundle bundle = doCreate(creator); |
186 | 188 |
|
@@ -229,6 +231,38 @@ void usesGenericConstructorArguments() { |
229 | 231 | "public %s(List<Metric> param1, String param2, Object ctorScoped)".formatted(targetType.getSimpleName())); |
230 | 232 | } |
231 | 233 |
|
| 234 | + @Test // GH-3374 |
| 235 | + void skipsMethodWithUnresolvableGenericReturnType() { |
| 236 | + |
| 237 | + SpelAwareProxyProjectionFactory spelAwareProxyProjectionFactory = new SpelAwareProxyProjectionFactory(); |
| 238 | + AotRepositoryInformation repositoryInformation = new AotRepositoryInformation( |
| 239 | + AnnotationRepositoryMetadata.getMetadata(UserRepository.class), CrudRepository.class, |
| 240 | + List.of(RepositoryFragment.structural(QuerydslPredicateExecutor.class, DummyQuerydslPredicateExecutor.class))); |
| 241 | + |
| 242 | + AotRepositoryCreator repositoryCreator = AotRepositoryCreator.forRepository(repositoryInformation, "Commons", |
| 243 | + spelAwareProxyProjectionFactory); |
| 244 | + repositoryCreator.contributeMethods(method -> { |
| 245 | + |
| 246 | + QueryMethod queryMethod = new QueryMethod(method, repositoryInformation, spelAwareProxyProjectionFactory, |
| 247 | + DefaultParameters::new); |
| 248 | + return new MethodContributor<>(queryMethod, Map::of) { |
| 249 | + @Override |
| 250 | + public MethodSpec contribute(AotQueryMethodGenerationContext context) { |
| 251 | + return MethodSpec.methodBuilder(context.getMethod().getName()).addCode("// 1 = 1").build(); |
| 252 | + } |
| 253 | + |
| 254 | + @Override |
| 255 | + public boolean contributesMethodSpec() { |
| 256 | + return true; |
| 257 | + } |
| 258 | + }; |
| 259 | + |
| 260 | + }); |
| 261 | + |
| 262 | + // same package as source repo |
| 263 | + assertThat(generate(repositoryCreator)).contains("someMethod()").doesNotContain("findByFirstname()"); |
| 264 | + } |
| 265 | + |
232 | 266 | private AotRepositoryCreator.AotBundle doCreate(AotRepositoryCreator creator) { |
233 | 267 | return creator.create(getTypeSpecBuilder(creator)); |
234 | 268 | } |
@@ -263,6 +297,8 @@ private static TypeSpec.Builder getTypeSpecBuilder(ClassName className) { |
263 | 297 | interface UserRepository extends org.springframework.data.repository.Repository<User, String> { |
264 | 298 |
|
265 | 299 | String someMethod(); |
| 300 | + |
| 301 | + <T> List<T> findByFirstname(String firstname, Class<T> type); |
266 | 302 | } |
267 | 303 |
|
268 | 304 | interface QuerydslUserRepository |
|
0 commit comments