All Downloads are FREE. Search and download functionalities are using the official Maven repository.

co.com.bancolombia.commons.jms.mq.config.utils.AnnotationParser Maven / Gradle / Ivy

There is a newer version: 2.3.5
Show newest version
package co.com.bancolombia.commons.jms.mq.config.utils;

import lombok.experimental.UtilityClass;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotation.Adapt;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;

@UtilityClass
public class AnnotationParser {

    public static  T parseMergedAnnotation(MergedAnnotation mergedAnnotation) {
        Map attributes = mergedAnnotation.asMap(Adapt.ANNOTATION_TO_MAP);
        return createAnnotationProxy(mergedAnnotation.getType(), attributes);
    }

    @SuppressWarnings("unchecked")
    private static  T createAnnotationProxy(Class annotationType, Map attributes) {
        return (T) Proxy.newProxyInstance(
                annotationType.getClassLoader(),
                new Class[]{annotationType},
                new AnnotationInvocationHandler(attributes)
        );
    }

    private static class AnnotationInvocationHandler implements InvocationHandler {
        private final Map attributes;

        AnnotationInvocationHandler(Map attributes) {
            this.attributes = attributes;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            return attributes.get(method.getName());
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy