FieldAndGetterReflectionEntityInformation.java

/*
 * Copyright © 2018 spring-data-dynamodb (https://github.com/prasanna0586/spring-data-dynamodb)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.socialsignin.spring.data.dynamodb.repository.support;

import org.springframework.data.repository.core.support.AbstractEntityInformation;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * {@link org.springframework.data.repository.core.EntityInformation} implementation that inspects getters for an
 * annotation and invokes this getter's value to retrieve the id.
 *
 * @param <T> the entity type
 * @param <ID> the ID type
 * @author Prasanna Kumar Ramachandran
 */
public class FieldAndGetterReflectionEntityInformation<T, ID> extends AbstractEntityInformation<T, ID> {

    /** The getter method annotated with the ID annotation */
    protected Method method;
    /** The field annotated with the ID annotation */
    @Nullable
    private Field field;

    /**
     * Creates a new {@link FieldAndGetterReflectionEntityInformation} inspecting the given domain class for a getter
     * carrying the given annotation.
     *
     * @param domainClass must not be {@literal null}.
     * @param annotation must not be {@literal null}.
     */
    public FieldAndGetterReflectionEntityInformation(@NonNull Class<T> domainClass,
            @NonNull final Class<? extends Annotation> annotation) {

        super(domainClass);
        Assert.notNull(annotation, "annotation must not be null!");

        ReflectionUtils.doWithMethods(domainClass, (method) -> {
            if (method.getAnnotation(annotation) != null) {
                this.method = method;
            }
        });

        if (method == null) {
            field = null;
            ReflectionUtils.doWithFields(domainClass, (field) -> {
                if (field.getAnnotation(annotation) != null) {
                    this.field = field;
                }
            });
        }

        Assert.isTrue(this.method != null || this.field != null,
                String.format("No field or method annotated with %s found!", annotation));
        Assert.isTrue(this.method == null || this.field == null,
                String.format("Both field and method annotated with %s found!", annotation));

        if (method != null) {
            ReflectionUtils.makeAccessible(method);
        }
        if (field != null) {
            ReflectionUtils.makeAccessible(field);
        }
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.repository.core.EntityInformation#getId(java.lang.Object)
     */
    @Override
    @SuppressWarnings("unchecked")
    public ID getId(@NonNull T entity) {

        if (method != null) {
            return (ID) ReflectionUtils.invokeMethod(method, entity);
        } else if (field != null) {
            return (ID) ReflectionUtils.getField(field, entity);
        }
        return null;
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.repository.core.EntityInformation#getIdType()
     */
    @NonNull
    @Override
    @SuppressWarnings("unchecked")
    public Class<ID> getIdType() {
        if (method != null) {
            return (Class<ID>) method.getReturnType();
        } else if (field != null) {
            return (Class<ID>) field.getType();
        }
        throw new IllegalStateException("Neither method nor field is set for ID extraction");
    }
}