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

io.vertx.up.uca.web.anima.InfixPlugin Maven / Gradle / Ivy

There is a newer version: 0.9.0
Show newest version
package io.vertx.up.uca.web.anima;

import io.reactivex.Observable;
import io.vertx.up.annotations.Plugin;
import io.vertx.up.eon.Info;
import io.vertx.up.fn.Fn;
import io.vertx.up.log.Annal;
import io.vertx.up.plugin.Infix;
import io.vertx.up.runtime.ZeroAmbient;
import io.vertx.up.util.Ut;

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

class InfixPlugin {

    private transient final Class clazz;
    private transient final Annal logger;

    private InfixPlugin(final Class clazz) {
        this.clazz = clazz;
        logger = Annal.get(clazz);
    }

    static InfixPlugin create(final Class clazz) {
        return Fn.pool(Pool.PLUGINS, clazz, () -> new InfixPlugin(clazz));
    }

    void inject(final Object proxy) {
        final ConcurrentMap, Class> binds = getBind();
        final Class type = proxy.getClass();
        Observable.fromArray(type.getDeclaredFields())
                .filter(field -> field.isAnnotationPresent(Plugin.class))
                .subscribe(field -> {
                    final Class fieldType = field.getType();
                    final Class infixCls = binds.get(fieldType);
                    if (null != infixCls) {
                        if (Ut.isImplement(infixCls, Infix.class)) {
                            final Infix reference = Ut.singleton(infixCls);
                            final Object tpRef = Ut.invoke(reference, "get");
                            final String fieldName = field.getName();
                            Ut.field(proxy, fieldName, tpRef);
                        } else {
                            logger.warn(Info.INFIX_IMPL, infixCls.getName(), Infix.class.getName());
                        }
                    } else {
                        logger.warn(Info.INFIX_NULL, "tp", field.getName(), type.getName());
                    }
                })
                .dispose();
    }

    private ConcurrentMap, Class> getBind() {
        // Extract all infixes
        final Set> infixes = new HashSet<>(ZeroAmbient.getInjections().values());
        final ConcurrentMap, Class> binds = new ConcurrentHashMap<>();
        Observable.fromIterable(infixes)
                .filter(Infix.class::isAssignableFrom)
                .subscribe(item -> {
                    final Method method = Fn.getJvm(() -> item.getDeclaredMethod("get"), item);
                    final Class type = method.getReturnType();
                    binds.put(type, item);
                })
                .dispose();
        return binds;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy