1
16 package org.springframework.data.repository.core.support;
17
18 import lombok.Getter;
19
20 import java.util.List;
21 import java.util.function.Supplier;
22
23 import org.springframework.data.repository.Repository;
24 import org.springframework.data.repository.core.RepositoryMetadata;
25 import org.springframework.data.util.ClassTypeInformation;
26 import org.springframework.data.util.TypeInformation;
27 import org.springframework.util.Assert;
28
29
36 @Getter
37 public class DefaultRepositoryMetadata extends AbstractRepositoryMetadata {
38
39 private static final String MUST_BE_A_REPOSITORY = String.format("Given type must be assignable to %s!",
40 Repository.class);
41
42 private final Class<?> idType;
43 private final Class<?> domainType;
44
45
50 public DefaultRepositoryMetadata(Class<?> repositoryInterface) {
51
52 super(repositoryInterface);
53 Assert.isTrue(Repository.class.isAssignableFrom(repositoryInterface), MUST_BE_A_REPOSITORY);
54
55 List<TypeInformation<?>> arguments = ClassTypeInformation.from(repositoryInterface)
56 .getRequiredSuperTypeInformation(Repository.class)
57 .getTypeArguments();
58
59 this.domainType = resolveTypeParameter(arguments, 0,
60 () -> String.format("Could not resolve domain type of %s!", repositoryInterface));
61 this.idType = resolveTypeParameter(arguments, 1,
62 () -> String.format("Could not resolve id type of %s!", repositoryInterface));
63 }
64
65 private static Class<?> resolveTypeParameter(List<TypeInformation<?>> arguments, int index,
66 Supplier<String> exceptionMessage) {
67
68 if (arguments.size() <= index || arguments.get(index) == null) {
69 throw new IllegalArgumentException(exceptionMessage.get());
70 }
71
72 return arguments.get(index).getType();
73 }
74 }
75