|
| 1 | +package org.baeldung.customannotation; |
| 2 | + |
| 3 | +import java.lang.reflect.Constructor; |
| 4 | +import java.lang.reflect.Field; |
| 5 | +import java.lang.reflect.ParameterizedType; |
| 6 | +import java.lang.reflect.Type; |
| 7 | + |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | +import org.springframework.beans.factory.config.AutowireCapableBeanFactory; |
| 11 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 12 | +import org.springframework.util.ReflectionUtils; |
| 13 | +import org.springframework.util.ReflectionUtils.FieldCallback; |
| 14 | + |
| 15 | + |
| 16 | +public final class DataAccessFieldCallback implements FieldCallback { |
| 17 | + |
| 18 | + private static Logger logger = LoggerFactory.getLogger(DataAccessFieldCallback.class); |
| 19 | + private static int AUTOWIRE_MODE = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME; |
| 20 | + |
| 21 | + private static String ERROR_ENTITY_VALUE_NOT_SAME = "@DataAccess(entity) " |
| 22 | + + "value should have same type with injected generic type."; |
| 23 | + private static String WARN_NON_GENERIC_VALUE = "@DataAccess annotation assigned " |
| 24 | + + "to raw (non-generic) declaration. This will make your code less type-safe."; |
| 25 | + private static String ERROR_CREATE_INSTANCE = "Cannot create instance of " |
| 26 | + + "type '{}' or instance creation is failed because: {}"; |
| 27 | + |
| 28 | + private ConfigurableListableBeanFactory configurableListableBeanFactory; |
| 29 | + private Object bean; |
| 30 | + |
| 31 | + public DataAccessFieldCallback(final ConfigurableListableBeanFactory bf, final Object bean) { |
| 32 | + configurableListableBeanFactory = bf; |
| 33 | + this.bean = bean; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void doWith(final Field field) |
| 38 | + throws IllegalArgumentException, IllegalAccessException { |
| 39 | + if (!field.isAnnotationPresent(DataAccess.class)) { |
| 40 | + return; |
| 41 | + } |
| 42 | + ReflectionUtils.makeAccessible(field); |
| 43 | + final Type fieldGenericType = field.getGenericType(); |
| 44 | + // In this example, get actual "GenericDAO' type. |
| 45 | + final Class<?> generic = field.getType(); |
| 46 | + final Class<?> classValue = field.getDeclaredAnnotation(DataAccess.class).entity(); |
| 47 | + |
| 48 | + if (genericTypeIsValid(classValue, fieldGenericType)) { |
| 49 | + final String beanName = classValue.getSimpleName() + generic.getSimpleName(); |
| 50 | + final Object beanInstance = getBeanInstance(beanName, generic, classValue); |
| 51 | + field.set(bean, beanInstance); |
| 52 | + } else { |
| 53 | + throw new IllegalArgumentException(ERROR_ENTITY_VALUE_NOT_SAME); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + /** |
| 59 | + * For example, if user write: |
| 60 | + * <pre> |
| 61 | + * @DataAccess(entity=Person.class) |
| 62 | + * private GenericDAO<Account> personGenericDAO; |
| 63 | + * </pre> |
| 64 | + * then this is should be failed. |
| 65 | + */ |
| 66 | + public boolean genericTypeIsValid(final Class<?> clazz, final Type field) { |
| 67 | + if (field instanceof ParameterizedType) { |
| 68 | + final ParameterizedType parameterizedType = (ParameterizedType) field; |
| 69 | + final Type type = parameterizedType.getActualTypeArguments()[0]; |
| 70 | + |
| 71 | + return type.equals(clazz); |
| 72 | + } else { |
| 73 | + logger.warn(WARN_NON_GENERIC_VALUE); |
| 74 | + return true; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + public final Object getBeanInstance(final String beanName, final Class<?> genericClass, final Class<?> paramClass) { |
| 81 | + Object daoInstance = null; |
| 82 | + if (!configurableListableBeanFactory.containsBean(beanName)) { |
| 83 | + logger.info("Creating new DataAccess bean named '{}'.", beanName); |
| 84 | + |
| 85 | + Object toRegister = null; |
| 86 | + try { |
| 87 | + final Constructor<?> ctr = genericClass.getConstructor(Class.class); |
| 88 | + toRegister = ctr.newInstance(paramClass); |
| 89 | + } catch (final Exception e) { |
| 90 | + logger.error(ERROR_CREATE_INSTANCE, genericClass.getTypeName(), e); |
| 91 | + throw new RuntimeException(e); |
| 92 | + } |
| 93 | + |
| 94 | + daoInstance = configurableListableBeanFactory.initializeBean(toRegister, beanName); |
| 95 | + configurableListableBeanFactory.autowireBeanProperties(daoInstance, AUTOWIRE_MODE, true); |
| 96 | + configurableListableBeanFactory.registerSingleton(beanName, daoInstance); |
| 97 | + logger.info("Bean named '{}' created successfully.", beanName); |
| 98 | + } else { |
| 99 | + daoInstance = configurableListableBeanFactory.getBean(beanName); |
| 100 | + logger.info("Bean named '{}' already exist used as current bean reference.", beanName); |
| 101 | + } |
| 102 | + return daoInstance; |
| 103 | + } |
| 104 | +} |
0 commit comments