Search in sources :

Example 96 with Named

use of javax.inject.Named in project core by weld.

the class ResolvableBuilder method addQualifier.

private ResolvableBuilder addQualifier(Annotation qualifier, InjectionPoint injectionPoint) {
    QualifierInstance qualifierInstance = QualifierInstance.of(qualifier, store);
    final Class<? extends Annotation> annotationType = qualifierInstance.getAnnotationClass();
    // Handle the @New qualifier special case
    if (annotationType.equals(New.class)) {
        New newQualifier = New.class.cast(qualifier);
        if (newQualifier.value().equals(New.class) && rawType == null) {
            throw new IllegalStateException("Cannot transform @New when there is no known raw type");
        } else if (newQualifier.value().equals(New.class)) {
            qualifier = new NewLiteral(rawType);
            qualifierInstance = QualifierInstance.of(qualifier, store);
        }
    } else if (injectionPoint != null && annotationType.equals(Named.class)) {
        Named named = (Named) qualifier;
        if (named.value().equals("")) {
            // WELD-1739
            // This is an injection point with an @Named qualifier, with no value specified, we need to assume the name of the field or parameter is the
            // value
            Member member = injectionPoint.getMember();
            if (member instanceof Executable) {
                // Method or constructor injection
                Executable executable = (Executable) member;
                AnnotatedParameter<?> annotatedParameter = (AnnotatedParameter<?>) injectionPoint.getAnnotated();
                Parameter parameter = executable.getParameters()[annotatedParameter.getPosition()];
                named = new NamedLiteral(parameter.getName());
            } else {
                named = new NamedLiteral(injectionPoint.getMember().getName());
            }
            qualifier = named;
            qualifierInstance = QualifierInstance.of(named, store);
        }
    }
    checkQualifier(qualifier, qualifierInstance, annotationType);
    this.qualifierInstances.add(qualifierInstance);
    return this;
}
Also used : New(javax.enterprise.inject.New) Named(javax.inject.Named) AnnotatedParameter(javax.enterprise.inject.spi.AnnotatedParameter) Parameter(java.lang.reflect.Parameter) AnnotatedParameter(javax.enterprise.inject.spi.AnnotatedParameter) NewLiteral(org.jboss.weld.literal.NewLiteral) Executable(java.lang.reflect.Executable) Member(java.lang.reflect.Member) NamedLiteral(org.jboss.weld.literal.NamedLiteral)

Example 97 with Named

use of javax.inject.Named 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)

Example 98 with Named

use of javax.inject.Named in project wicket by apache.

the class AnnotProxyFieldValueFactory method getFieldValue.

@Override
public Object getFieldValue(final Field field, final Object fieldOwner) {
    if (supportsField(field)) {
        SpringBean annot = field.getAnnotation(SpringBean.class);
        String name;
        boolean required;
        if (annot != null) {
            name = annot.name();
            required = annot.required();
        } else {
            Named named = field.getAnnotation(Named.class);
            name = named != null ? named.value() : "";
            required = true;
        }
        Class<?> generic = ResolvableType.forField(field).resolveGeneric(0);
        String beanName = getBeanName(field, name, required, generic);
        SpringBeanLocator locator = new SpringBeanLocator(beanName, field.getType(), field, contextLocator);
        // only check the cache if the bean is a singleton
        Object cachedValue = cache.get(locator);
        if (cachedValue != null) {
            return cachedValue;
        }
        Object target;
        try {
            // check whether there is a bean with the provided properties
            target = locator.locateProxyTarget();
        } catch (IllegalStateException isx) {
            if (required) {
                throw isx;
            } else {
                return null;
            }
        }
        if (wrapInProxies) {
            target = LazyInitProxyFactory.createProxy(field.getType(), locator);
        }
        // only put the proxy into the cache if the bean is a singleton
        if (locator.isSingletonBean()) {
            Object tmpTarget = cache.putIfAbsent(locator, target);
            if (tmpTarget != null) {
                target = tmpTarget;
            }
        }
        return target;
    }
    return null;
}
Also used : Named(javax.inject.Named) SpringBeanLocator(org.apache.wicket.spring.SpringBeanLocator)

Example 99 with Named

use of javax.inject.Named in project endpoints-java by cloudendpoints.

the class AnnotationApiConfigGeneratorTest method testMultipleCollectionRequests.

@Test
public void testMultipleCollectionRequests() throws Exception {
    @Api
    class MultipleCollections {

        @SuppressWarnings("unused")
        public void foo(@Named("ids") Long[] ids, @Named("authors") List<String> authors) {
        }
    }
    String apiConfigSource = g.generateConfig(MultipleCollections.class).get("myapi-v1.api");
    ObjectNode root = objectMapper.readValue(apiConfigSource, ObjectNode.class);
    JsonNode methodNode = root.path("methods").path("myapi.multipleCollections.foo");
    assertFalse(methodNode.isMissingNode());
    verifyMethodRequestParameter(methodNode.get("request"), "ids", "int64", true, true);
    verifyMethodRequestParameter(methodNode.get("request"), "authors", "string", true, true);
}
Also used : Named(javax.inject.Named) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) List(java.util.List) JsonNode(com.fasterxml.jackson.databind.JsonNode) SimpleLevelOverridingApi(com.google.api.server.spi.testing.SimpleLevelOverridingApi) Api(com.google.api.server.spi.config.Api) Test(org.junit.Test)

Example 100 with Named

use of javax.inject.Named in project endpoints-java by cloudendpoints.

the class AnnotationApiConfigGeneratorTest method testGenericCollectionRequest.

@Test
public void testGenericCollectionRequest() throws Exception {
    @Api
    abstract class GenericCollection<T> {

        @SuppressWarnings("unused")
        public void foo(@Named("collection") Collection<T> t) {
        }
    }
    // TODO: remove with JDK8, dummy to force inclusion of GenericArray to InnerClass attribute
    // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=2210448
    GenericCollection<Long> dummy = new GenericCollection<Long>() {
    };
    class Int64Collection extends GenericCollection<Long> {
    }
    String apiConfigSource = g.generateConfig(Int64Collection.class).get("myapi-v1.api");
    ObjectNode root = objectMapper.readValue(apiConfigSource, ObjectNode.class);
    JsonNode methodNode = root.path("methods").path("myapi.int64Collection.foo");
    assertFalse(methodNode.isMissingNode());
    verifyMethodRequestParameter(methodNode.get("request"), "collection", "int64", true, true);
}
Also used : Named(javax.inject.Named) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Collection(java.util.Collection) JsonNode(com.fasterxml.jackson.databind.JsonNode) SimpleLevelOverridingApi(com.google.api.server.spi.testing.SimpleLevelOverridingApi) Api(com.google.api.server.spi.config.Api) Test(org.junit.Test)

Aggregations

Named (javax.inject.Named)136 Produces (javax.enterprise.inject.Produces)40 ApplicationScoped (javax.enterprise.context.ApplicationScoped)31 Test (org.junit.Test)29 Provides (com.google.inject.Provides)23 Properties (java.util.Properties)18 Singleton (javax.inject.Singleton)18 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)15 Api (com.google.api.server.spi.config.Api)15 SimpleLevelOverridingApi (com.google.api.server.spi.testing.SimpleLevelOverridingApi)15 JsonNode (com.fasterxml.jackson.databind.JsonNode)14 PropertiesComponent (org.apache.camel.component.properties.PropertiesComponent)12 Annotation (java.lang.annotation.Annotation)11 Provides (dagger.Provides)9 ArrayList (java.util.ArrayList)8 Inject (javax.inject.Inject)8 List (java.util.List)7 HashMap (java.util.HashMap)6 Method (java.lang.reflect.Method)5 ApiConfigAnnotationReader (com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader)4