Java Code Examples for javax.enterprise.inject.spi.InjectionPoint#getType()

The following examples show how to use javax.enterprise.inject.spi.InjectionPoint#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProviderExtensionSupport.java    From smallrye-jwt with Apache License 2.0 6 votes vote down vote up
/**
 * Collect the types of all {@linkplain Provider} injection points annotated with {@linkplain Claim}.
 *
 * @param pip - the injection point event information
 */
void processClaimProviderInjections(@Observes ProcessInjectionPoint<?, ? extends Provider> pip) {
    CDILogging.log.pip(pip.getInjectionPoint());
    final InjectionPoint ip = pip.getInjectionPoint();
    if (ip.getAnnotated().isAnnotationPresent(Claim.class)) {
        Claim claim = ip.getAnnotated().getAnnotation(Claim.class);
        if (claim.value().length() == 0 && claim.standard() == Claims.UNKNOWN) {
            pip.addDefinitionError(CDIMessages.msg.claimHasNoNameOrValidStandardEnumSetting(ip));
        }
        boolean usesEnum = claim.standard() != Claims.UNKNOWN;
        final String claimName = usesEnum ? claim.standard().name() : claim.value();
        CDILogging.log.checkingProviderClaim(claimName, ip);
        Type matchType = ip.getType();
        // The T from the Provider<T> injection site
        Type actualType = ((ParameterizedType) matchType).getActualTypeArguments()[0];
        // Don't add Optional or JsonValue as this is handled specially
        if (isOptional(actualType)) {
            // Validate that this is not an Optional<JsonValue>
            Type innerType = ((ParameterizedType) actualType).getActualTypeArguments()[0];
            if (!isJson(innerType)) {
                providerOptionalTypes.add(actualType);
                providerQualifiers.add(claim);
            }
        }
    }
}
 
Example 2
Source File: ClaimValueWrapper.java    From smallrye-jwt with Apache License 2.0 6 votes vote down vote up
public ClaimValueWrapper(InjectionPoint ip, CommonJwtProducer producer) {
    this.producer = producer;
    this.name = producer.getName(ip);
    Type injectedType = ip.getType();

    if (injectedType instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) injectedType;
        Type typeArgument = parameterizedType.getActualTypeArguments()[0];
        // Check if the injection point is optional, i.e. ClaimValue<<Optional<?>>
        optional = typeArgument.getTypeName().startsWith(Optional.class.getTypeName());
    } else {
        optional = false;
    }

    this.klass = unwrapType(ip.getType(), ip);
}
 
Example 3
Source File: SpringCDIExtension.java    From datawave with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unused")
<T> void processInjectionTarget(@Observes ProcessInjectionTarget<T> pit, BeanManager bm) {
    log.trace("processInjectionTarget({},{})", pit, bm);
    
    synchronized (springBeans) {
        Set<InjectionPoint> injectionPoints = pit.getInjectionTarget().getInjectionPoints();
        for (InjectionPoint ip : injectionPoints) {
            Type type = ip.getType();
            // Skip primitives
            if (!(type instanceof Class<?> || type instanceof ParameterizedType))
                continue;
            
            SpringBean sb = ip.getAnnotated().getAnnotation(SpringBean.class);
            if (sb != null) {
                String key = sb.name() + ":" + type;
                if (!springBeans.containsKey(key)) {
                    SpringCDIBean scb = sb.refreshable() ? new RefreshableSpringCDIBean(sb, type, bm) : new SpringCDIBean(sb, type, bm);
                    springBeans.put(key, scb);
                }
            }
        }
    }
}
 
Example 4
Source File: ConfigExtension.java    From smallrye-config with Apache License 2.0 6 votes vote down vote up
protected void registerCustomBeans(@Observes AfterBeanDiscovery abd, BeanManager bm) {
    Set<Class<?>> customTypes = new HashSet<>();
    for (InjectionPoint ip : injectionPoints) {
        Type requiredType = ip.getType();
        if (requiredType instanceof ParameterizedType) {
            ParameterizedType type = (ParameterizedType) requiredType;
            // TODO We should probably handle all parameterized types correctly
            if (type.getRawType().equals(Provider.class) || type.getRawType().equals(Instance.class)) {
                // These injection points are satisfied by the built-in Instance bean 
                Type typeArgument = type.getActualTypeArguments()[0];
                if (typeArgument instanceof Class && !isClassHandledByConfigProducer(typeArgument)) {
                    customTypes.add((Class<?>) typeArgument);
                }
            }
        } else if (requiredType instanceof Class
                && !isClassHandledByConfigProducer(requiredType)) {
            // type is not produced by ConfigProducer
            customTypes.add((Class<?>) requiredType);
        }
    }

    for (Class<?> customType : customTypes) {
        abd.addBean(new ConfigInjectionBean(bm, customType));
    }
}
 
Example 5
Source File: AsyncReferenceImpl.java    From weld-vertx with Apache License 2.0 6 votes vote down vote up
@Inject
public AsyncReferenceImpl(InjectionPoint injectionPoint, Vertx vertx, BeanManager beanManager, @Any WeldInstance<Object> instance) {
    this.isDone = new AtomicBoolean(false);
    this.future = new VertxCompletableFuture<>(vertx);
    this.instance = instance;

    ParameterizedType parameterizedType = (ParameterizedType) injectionPoint.getType();
    Type requiredType = parameterizedType.getActualTypeArguments()[0];
    Annotation[] qualifiers = injectionPoint.getQualifiers().toArray(new Annotation[] {});

    // First check if there is a relevant async producer method available
    WeldInstance<Object> completionStage = instance.select(new ParameterizedTypeImpl(CompletionStage.class, requiredType), qualifiers);

    if (completionStage.isAmbiguous()) {
        failure(new AmbiguousResolutionException(
                "Ambiguous async producer methods for type " + requiredType + " with qualifiers " + injectionPoint.getQualifiers()));
    } else if (!completionStage.isUnsatisfied()) {
        // Use the produced CompletionStage
        initWithCompletionStage(completionStage.getHandler());
    } else {
        // Use Vertx worker thread
        initWithWorker(requiredType, qualifiers, vertx, beanManager);
    }
}
 
Example 6
Source File: CdiEjbBean.java    From tomee with Apache License 2.0 6 votes vote down vote up
@Override
public InjectionTarget<T> createInjectionTarget(final Bean<T> bean) {
    final EjbInjectionTargetImpl<T> injectionTarget = new EjbInjectionTargetImpl<>(getAnnotatedType(), createInjectionPoints(bean), getWebBeansContext());
    final InjectionTarget<T> it = getWebBeansContext().getWebBeansUtil().fireProcessInjectionTargetEvent(injectionTarget, getAnnotatedType()).getInjectionTarget();

    for (final InjectionPoint ip : it.getInjectionPoints()) {
        if (ip.getType() != UserTransaction.class) {
            continue;
        }
        if (beanContext.getTransactionType() != TransactionType.BeanManaged) {
            throw new DefinitionException("@Inject UserTransaction is only valid for BeanManaged beans");
        }
    }

    if (!EjbInjectionTargetImpl.class.isInstance(it)) {
        return new EjbInjectionTargetImpl<>(injectionTarget, it);
    }
    return it;
}
 
Example 7
Source File: JsfMessageProducer.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
@Produces
@Dependent
public <M> JsfMessage<M> createJsfMessage(InjectionPoint injectionPoint,
                                   MessageBundleInvocationHandler invocationHandler)
{
    if (!(injectionPoint.getType() instanceof ParameterizedType))
    {
        throw new IllegalArgumentException("JsfMessage must be used as generic type");
    }
    ParameterizedType paramType = (ParameterizedType) injectionPoint.getType();
    Type[] actualTypes = paramType.getActualTypeArguments();
    if (actualTypes.length != 1)
    {
        throw new IllegalArgumentException("JsfMessage must have the MessageBundle as generic type parameter");
    }
    try
    {
        @SuppressWarnings("unchecked")
        Class<M> type = (Class<M>) actualTypes[0];
        return createJsfMessageFor(injectionPoint, type, invocationHandler);
    }
    catch (ClassCastException e)
    {
        throw new IllegalArgumentException("Incorrect class found when trying to convert to parameterized type",e);
    }
}
 
Example 8
Source File: ConfigExtension.java    From smallrye-config with Apache License 2.0 5 votes vote down vote up
protected void validate(@Observes AfterDeploymentValidation adv) {
    Config config = ConfigProvider.getConfig(getContextClassLoader());
    Set<String> configNames = StreamSupport.stream(config.getPropertyNames().spliterator(), false).collect(toSet());
    for (InjectionPoint injectionPoint : injectionPoints) {
        Type type = injectionPoint.getType();

        // We don't validate the Optional / Provider / Supplier / ConfigValue for defaultValue.
        if (type instanceof Class && ConfigValue.class.isAssignableFrom((Class<?>) type)
                || type instanceof Class && OptionalInt.class.isAssignableFrom((Class<?>) type)
                || type instanceof Class && OptionalLong.class.isAssignableFrom((Class<?>) type)
                || type instanceof Class && OptionalDouble.class.isAssignableFrom((Class<?>) type)
                || type instanceof ParameterizedType
                        && (Optional.class.isAssignableFrom((Class<?>) ((ParameterizedType) type).getRawType())
                                || Provider.class.isAssignableFrom((Class<?>) ((ParameterizedType) type).getRawType())
                                || Supplier.class.isAssignableFrom((Class<?>) ((ParameterizedType) type).getRawType()))) {
            return;
        }

        ConfigProperty configProperty = injectionPoint.getAnnotated().getAnnotation(ConfigProperty.class);
        String name = ConfigProducerUtil.getConfigKey(injectionPoint, configProperty);

        // Check if the name is part of the properties first. Since properties can be a subset, then search for the actual property for a value.
        if (!configNames.contains(name) && ConfigProducerUtil.getRawValue(name, (SmallRyeConfig) config) == null) {
            if (configProperty.defaultValue().equals(ConfigProperty.UNCONFIGURED_VALUE)) {
                adv.addDeploymentProblem(InjectionMessages.msg.noConfigValue(name));
            }
        }

        try {
            // Check if there is a Converter registed for the injected type
            Converter<?> resolvedConverter = ConfigProducerUtil.resolveConverter(injectionPoint, (SmallRyeConfig) config);

            // Check if the value can be converted. The TCK checks this, but this requires to get the value eagerly.
            // This should not be required!
            SecretKeys.doUnlocked(() -> ((SmallRyeConfig) config).getOptionalValue(name, resolvedConverter));
        } catch (IllegalArgumentException e) {
            adv.addDeploymentProblem(e);
        }
    }
}
 
Example 9
Source File: InstanceBean.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Instance<?> get(CreationalContext<Instance<?>> creationalContext) {
    // Obtain current IP to get the required type and qualifiers
    InjectionPoint ip = InjectionPointProvider.get();
    InstanceImpl<Instance<?>> instance = new InstanceImpl<Instance<?>>((InjectableBean<?>) ip.getBean(), ip.getType(),
            ip.getQualifiers(), (CreationalContextImpl<?>) creationalContext, Collections.EMPTY_SET, ip.getMember(), 0);
    CreationalContextImpl.addDependencyToParent((InjectableBean<Instance<?>>) ip.getBean(), instance, creationalContext);
    return instance;
}
 
Example 10
Source File: EventBean.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public Event<?> get(CreationalContext<Event<?>> creationalContext) {
    // Obtain current IP to get the required type and qualifiers
    InjectionPoint ip = InjectionPointProvider.get();
    return new EventImpl<>(ip.getType(), ip.getQualifiers());
}
 
Example 11
Source File: SubscriptionPublisherCdiExtension.java    From joynr with Apache License 2.0 4 votes vote down vote up
public void alterSubscriptionPublishInjectionPoints(@Observes ProcessInjectionPoint processInjectionPoint) {
    final InjectionPoint injectionPoint = processInjectionPoint.getInjectionPoint();
    logger.info("Looking at injection point: {}", injectionPoint);
    if (injectionPoint.getType() instanceof Class
            && SubscriptionPublisher.class.isAssignableFrom((Class) injectionPoint.getType())) {
        logger.info("Re-writing injection point type from {} to {} on bean {}",
                    injectionPoint.getType(),
                    SubscriptionPublisher.class,
                    injectionPoint.getBean());
        final Bean<?> bean = injectionPoint.getBean();
        InjectionPoint newInjectionPoint = new InjectionPoint() {
            @Override
            public Type getType() {
                return SubscriptionPublisher.class;
            }

            @Override
            public Set<Annotation> getQualifiers() {
                return injectionPoint.getQualifiers();
            }

            @Override
            public Bean<?> getBean() {
                return bean;
            }

            @Override
            public Member getMember() {
                return injectionPoint.getMember();
            }

            @Override
            public Annotated getAnnotated() {
                return injectionPoint.getAnnotated();
            }

            @Override
            public boolean isDelegate() {
                return injectionPoint.isDelegate();
            }

            @Override
            public boolean isTransient() {
                return injectionPoint.isTransient();
            }
        };
        processInjectionPoint.setInjectionPoint(newInjectionPoint);
    }
}
 
Example 12
Source File: ConfigurationExtension.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Produces
@ConfigProperty(name = "ignored")
public Object create(final InjectionPoint ip)
{
    return super.getUntypedPropertyValue(ip, ip.getType());
}