View Javadoc

1   /**
2    * Copyright © 2018 spring-data-dynamodb (https://github.com/derjust/spring-data-dynamodb)
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    *     http://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.socialsignin.spring.data.dynamodb.repository.support;
17  
18  import org.springframework.data.repository.core.support.AbstractEntityInformation;
19  import org.springframework.lang.NonNull;
20  import org.springframework.util.Assert;
21  import org.springframework.util.ReflectionUtils;
22  
23  import java.lang.annotation.Annotation;
24  import java.lang.reflect.Field;
25  import java.lang.reflect.Method;
26  
27  /**
28   * {@link org.springframework.data.repository.core.EntityInformation}
29   * implementation that inspects getters for an annotation and invokes this
30   * getter's value to retrieve the id.
31   *
32   * @author Michael Lavelle
33   * @author Sebastian Just
34   */
35  public class FieldAndGetterReflectionEntityInformation<T, ID> extends AbstractEntityInformation<T, ID> {
36  
37  	protected Method method;
38  	private Field field;
39  
40  	/**
41  	 * Creates a new {@link FieldAndGetterReflectionEntityInformation} inspecting
42  	 * the given domain class for a getter carrying the given annotation.
43  	 *
44  	 * @param domainClass
45  	 *            must not be {@literal null}.
46  	 * @param annotation
47  	 *            must not be {@literal null}.
48  	 */
49  	public FieldAndGetterReflectionEntityInformation(@NonNull Class<T> domainClass,
50  			@NonNull final Class<? extends Annotation> annotation) {
51  
52  		super(domainClass);
53  		Assert.notNull(annotation, "annotation must not be null!");
54  
55  		ReflectionUtils.doWithMethods(domainClass, (method) -> {
56  			if (method.getAnnotation(annotation) != null) {
57  				this.method = method;
58  				return;
59  			}
60  		});
61  
62  		if (method == null) {
63  			field = null;
64  			ReflectionUtils.doWithFields(domainClass, (field) -> {
65  				if (field.getAnnotation(annotation) != null) {
66  					this.field = field;
67  					return;
68  				}
69  			});
70  		}
71  
72  		Assert.isTrue(this.method != null || this.field != null,
73  				String.format("No field or method annotated with %s found!", annotation.toString()));
74  		Assert.isTrue(this.method == null || this.field == null,
75  				String.format("Both field and method annotated with %s found!", annotation.toString()));
76  
77  		if (method != null) {
78  			ReflectionUtils.makeAccessible(method);
79  		}
80  	}
81  
82  	/*
83  	 * (non-Javadoc)
84  	 *
85  	 * @see org.springframework.data.repository.core.EntityInformation#getId(java
86  	 * .lang.Object)
87  	 */
88  	@Override
89  	@SuppressWarnings("unchecked")
90  	public ID getId(T entity) {
91  		if (method != null) {
92  			return entity == null ? null : (ID) ReflectionUtils.invokeMethod(method, entity);
93  		} else {
94  			return entity == null ? null : (ID) ReflectionUtils.getField(field, entity);
95  		}
96  	}
97  
98  	/*
99  	 * (non-Javadoc)
100 	 *
101 	 * @see org.springframework.data.repository.core.EntityInformation#getIdType()
102 	 */
103 	@Override
104 	@SuppressWarnings("unchecked")
105 	public Class<ID> getIdType() {
106 		return (Class<ID>) (method != null ? method.getReturnType() : field.getType());
107 	}
108 }