1 /*
2  * Copyright 2013-2020 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.data.jpa.repository.query;
17
18 import javax.persistence.Entity;
19
20 import org.springframework.core.annotation.AnnotatedElementUtils;
21 import org.springframework.util.Assert;
22 import org.springframework.util.StringUtils;
23
24 /**
25  * Default implementation for {@link JpaEntityMetadata}.
26  *
27  * @author Oliver Gierke
28  * @author Christoph Strobl
29  */

30 public class DefaultJpaEntityMetadata<T> implements JpaEntityMetadata<T> {
31
32     private final Class<T> domainType;
33
34     /**
35      * Creates a new {@link DefaultJpaEntityMetadata} for the given domain type.
36      *
37      * @param domainType must not be {@literal null}.
38      */

39     public DefaultJpaEntityMetadata(Class<T> domainType) {
40
41         Assert.notNull(domainType, "Domain type must not be null!");
42         this.domainType = domainType;
43     }
44
45     /*
46      * (non-Javadoc)
47      * @see org.springframework.data.repository.core.EntityMetadata#getJavaType()
48      */

49     @Override
50     public Class<T> getJavaType() {
51         return domainType;
52     }
53
54     /*
55      * (non-Javadoc)
56      * @see org.springframework.data.jpa.repository.support.JpaEntityMetadata#getEntityName()
57      */

58     @Override
59     public String getEntityName() {
60
61         Entity entity = AnnotatedElementUtils.findMergedAnnotation(domainType, Entity.class);
62         return null != entity && StringUtils.hasText(entity.name()) ? entity.name() : domainType.getSimpleName();
63     }
64 }
65