Search in sources :

Example 1 with Qualifier

use of io.vertx.up.annotations.Qualifier in project vertx-zero by silentbalanceyh.

the class AffluxThread method scanQualifier.

private void scanQualifier(final Field field, final List<Class<?>> instanceCls) {
    // Field must annotated with @Qualifier
    final Annotation annotation = field.getAnnotation(Qualifier.class);
    Fn.flingUp(null == annotation, LOGGER, QualifierMissedException.class, this.getClass(), field.getName(), field.getDeclaringClass().getName());
    // All implementation class must be annotated with @Named
    final boolean match = instanceCls.stream().allMatch(item -> item.isAnnotationPresent(Named.class));
    final Set<String> names = instanceCls.stream().map(Class::getName).collect(Collectors.toSet());
    Fn.flingUp(!match, LOGGER, NamedImplementionException.class, this.getClass(), names, field.getType().getName());
    // Named value must be reflect with @Qualifier
    final String value = Instance.invoke(annotation, "value");
    final Optional<Class<?>> verified = instanceCls.stream().filter(item -> {
        final Annotation target = item.getAnnotation(Named.class);
        final String targetValue = Instance.invoke(target, "value");
        return value.equals(targetValue) && !Ut.isNil(targetValue);
    }).findAny();
    Fn.flingUp(!verified.isPresent(), LOGGER, NamedNotFoundException.class, this.getClass(), names, value);
    // Passed all specification
    this.fieldMap.put(field.getName(), verified.get());
}
Also used : Fn(io.vertx.up.func.Fn) java.util(java.util) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Ut(io.vertx.up.tool.Ut) NamedImplementionException(io.vertx.zero.exception.NamedImplementionException) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) Instance(io.vertx.up.tool.mirror.Instance) ConcurrentMap(java.util.concurrent.ConcurrentMap) Inject(javax.inject.Inject) Anno(io.vertx.up.tool.mirror.Anno) Values(io.vertx.zero.eon.Values) Annal(io.vertx.up.log.Annal) Annotation(java.lang.annotation.Annotation) MultiAnnotatedException(io.vertx.zero.exception.MultiAnnotatedException) Observable(io.reactivex.Observable) Plugins(io.vertx.up.eon.Plugins) Named(javax.inject.Named) QualifierMissedException(io.vertx.zero.exception.QualifierMissedException) Qualifier(io.vertx.up.annotations.Qualifier) NamedNotFoundException(io.vertx.zero.exception.NamedNotFoundException) Named(javax.inject.Named) Annotation(java.lang.annotation.Annotation)

Aggregations

Observable (io.reactivex.Observable)1 Qualifier (io.vertx.up.annotations.Qualifier)1 Plugins (io.vertx.up.eon.Plugins)1 Fn (io.vertx.up.func.Fn)1 Annal (io.vertx.up.log.Annal)1 Ut (io.vertx.up.tool.Ut)1 Anno (io.vertx.up.tool.mirror.Anno)1 Instance (io.vertx.up.tool.mirror.Instance)1 Values (io.vertx.zero.eon.Values)1 MultiAnnotatedException (io.vertx.zero.exception.MultiAnnotatedException)1 NamedImplementionException (io.vertx.zero.exception.NamedImplementionException)1 NamedNotFoundException (io.vertx.zero.exception.NamedNotFoundException)1 QualifierMissedException (io.vertx.zero.exception.QualifierMissedException)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 java.util (java.util)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Collectors (java.util.stream.Collectors)1 Inject (javax.inject.Inject)1