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

net.nemerosa.ontrack.json.ProxyAnnotation Maven / Gradle / Ivy

There is a newer version: 4.4.5
Show newest version
package net.nemerosa.ontrack.json;

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

public final class ProxyAnnotation
{
    @SuppressWarnings("unchecked")
    public static  A of(Class annotationType, Map properties)
    {
        return (A) Proxy.newProxyInstance(annotationType.getClassLoader(),
                new Class[]{annotationType}, new AnnotationInvocationHandler(annotationType,
                properties));
    }

    private static class AnnotationInvocationHandler implements
            InvocationHandler
    {
        private final Class annotationType;
        private final Map properties;

        private AnnotationInvocationHandler(Class annotationType, Map properties)
        {
            this.annotationType = annotationType;
            this.properties = properties;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
        {
            if (method.getName().equals("annotationType"))
            {
                return annotationType;
            }
            Object value = properties.get(method.getName());
            if (value != null)
            {
                return value;
            }
            return method.getDefaultValue();
        }
    }

    private ProxyAnnotation()
    {
        // prevent instantiation
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy