co.com.bancolombia.commons.jms.mq.config.utils.AnnotationParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-jms-mq Show documentation
Show all versions of commons-jms-mq Show documentation
A performant abstraction on top of JMS
The 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());
}
}
}