javax.enterprise.inject.spi.Annotated Java Examples

The following examples show how to use javax.enterprise.inject.spi.Annotated. 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 Project: cdi   Author: AxonFramework   File: MessageHandlingBeanDefinition.java    License: Apache License 2.0 6 votes vote down vote up
static Optional<MessageHandlingBeanDefinition> inspect(Bean<?> bean, Annotated annotated) {
    if (!(annotated instanceof AnnotatedType)) {
        return Optional.empty();
    }
    AnnotatedType at = (AnnotatedType) annotated;
    boolean isEventHandler = CdiUtilities.hasAnnotatedMethod(at, EventHandler.class);
    boolean isQueryHandler = CdiUtilities.hasAnnotatedMethod(at, QueryHandler.class);
    boolean isCommandHandler = CdiUtilities.hasAnnotatedMethod(at, CommandHandler.class);

    if (isEventHandler || isQueryHandler || isCommandHandler) {
        return Optional.of(new MessageHandlingBeanDefinition(bean,
                isEventHandler, isQueryHandler, isCommandHandler));
    }

    return Optional.empty();
}
 
Example #2
Source Project: vraptor4   Author: caelum   File: EnvironmentPropertyProducer.java    License: Apache License 2.0 6 votes vote down vote up
@Produces @Property
public String get(InjectionPoint ip) {
	Annotated annotated = ip.getAnnotated();
	Property property = annotated.getAnnotation(Property.class);
	String key = property.value();
	if (isNullOrEmpty(key)) {
		key = ip.getMember().getName();
	}
	
	String defaultValue = property.defaultValue();
	if(!isNullOrEmpty(defaultValue)){
		return environment.get(key, defaultValue);
	}
	
	return environment.get(key);
}
 
Example #3
Source Project: deltaspike   Author: apache   File: AnnotatedTypeBuilder.java    License: Apache License 2.0 6 votes vote down vote up
protected void mergeAnnotationsOnElement(Annotated annotated,
                                         boolean overwriteExisting,
                                         AnnotationBuilder typeAnnotations)
{
    for (Annotation annotation : annotated.getAnnotations())
    {
        if (typeAnnotations.getAnnotation(annotation.annotationType()) != null)
        {
            if (overwriteExisting)
            {
                typeAnnotations.remove(annotation.annotationType());
                typeAnnotations.add(annotation);
            }
        }
        else
        {
            typeAnnotations.add(annotation);
        }
    }
}
 
Example #4
Source Project: deltaspike   Author: apache   File: BeanUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @param annotated element to search in
 * @param targetType target type to search for
 * @param <T> type of the Annotation which get searched
 * @return annotation instance extracted from the annotated member
 */
public static <T extends Annotation> T extractAnnotation(Annotated annotated, Class<T> targetType)
{
    T result = annotated.getAnnotation(targetType);

    if (result == null)
    {
        for (Annotation annotation : annotated.getAnnotations())
        {
            result = annotation.annotationType().getAnnotation(targetType);

            if (result != null)
            {
                break;
            }
        }
    }

    return result;
}
 
Example #5
Source Project: smallrye-config   Author: smallrye   File: ConfigInjectionBean.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public T create(CreationalContext<T> context) {
    InjectionPoint ip = (InjectionPoint) bm.getInjectableReference(new InjectionPointMetadataInjectionPoint(), context);
    Annotated annotated = ip.getAnnotated();
    ConfigProperty configProperty = annotated.getAnnotation(ConfigProperty.class);
    String key = ConfigProducerUtil.getConfigKey(ip, configProperty);
    String defaultValue = configProperty.defaultValue();

    if (annotated.getBaseType() instanceof ParameterizedType) {
        ParameterizedType paramType = (ParameterizedType) annotated.getBaseType();
        Type rawType = paramType.getRawType();

        // handle Provider<T> and Instance<T>
        if (rawType instanceof Class
                && (((Class<?>) rawType).isAssignableFrom(Provider.class)
                        || ((Class<?>) rawType).isAssignableFrom(Instance.class))
                && paramType.getActualTypeArguments().length == 1) {
            Class<?> paramTypeClass = (Class<?>) paramType.getActualTypeArguments()[0];
            return (T) getConfig().getValue(key, paramTypeClass);
        }
    } else {
        Class annotatedTypeClass = (Class) annotated.getBaseType();
        if (defaultValue == null || defaultValue.length() == 0) {
            return (T) getConfig().getValue(key, annotatedTypeClass);
        } else {
            Config config = getConfig();
            Optional<T> optionalValue = config.getOptionalValue(key, annotatedTypeClass);
            if (optionalValue.isPresent()) {
                return optionalValue.get();
            } else {
                return (T) ((SmallRyeConfig) config).convert(defaultValue, annotatedTypeClass);
            }
        }
    }

    throw InjectionMessages.msg.unhandledConfigProperty();
}
 
Example #6
Source Project: smallrye-metrics   Author: smallrye   File: SeMetricName.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public String of(InjectionPoint ip) {
    Annotated annotated = ip.getAnnotated();
    if (annotated instanceof AnnotatedMember) {
        return of((AnnotatedMember<?>) annotated);
    } else if (annotated instanceof AnnotatedParameter) {
        return of((AnnotatedParameter<?>) annotated);
    } else {
        throw SmallRyeMetricsMessages.msg.unableToRetrieveMetricNameForInjectionPoint(ip);
    }
}
 
Example #7
Source Project: weld-junit   Author: weld   File: MockResourceInjectionServices.java    License: Apache License 2.0 5 votes vote down vote up
private Resource getResourceAnnotation(InjectionPoint injectionPoint) {
    Annotated annotated = injectionPoint.getAnnotated();
    if (annotated instanceof AnnotatedParameter<?>) {
        annotated = ((AnnotatedParameter<?>) annotated).getDeclaringCallable();
    }
    return annotated.getAnnotation(Resource.class);
}
 
Example #8
Source Project: weld-vertx   Author: weld   File: RouteExtension.java    License: Apache License 2.0 5 votes vote down vote up
private WebRoute[] getWebRoutes(Annotated annotated) {
    WebRoute webRoute = annotated.getAnnotation(WebRoute.class);
    if (webRoute != null) {
        return new WebRoute[] { webRoute };
    }
    Annotation container = annotated.getAnnotation(WebRoutes.class);
    if (container != null) {
        WebRoutes webRoutes = (WebRoutes) container;
        return webRoutes.value();
    }
    return new WebRoute[] {};
}
 
Example #9
Source Project: porcupine   Author: AdamBien   File: ExecutorServiceExposer.java    License: Apache License 2.0 5 votes vote down vote up
String getPipelineName(InjectionPoint ip) {
    Annotated annotated = ip.getAnnotated();
    Dedicated dedicated = annotated.getAnnotation(Dedicated.class);
    String name;
    if (dedicated != null && !Dedicated.DEFAULT.equals(dedicated.value())) {
        name = dedicated.value();
    } else {
        name = ip.getMember().getName();
    }
    return name;
}
 
Example #10
Source Project: hawkular-metrics   Author: hawkular   File: ConfigurableProducerTest.java    License: Apache License 2.0 5 votes vote down vote up
private InjectionPoint configureInjectionPoint(ConfigurationKey configurationKey) {
    Annotated annotated = mock(Annotated.class);
    ConfigurationProperty configurationProperty = mock(ConfigurationProperty.class);
    when(configurationProperty.value()).thenReturn(configurationKey);
    when(annotated.getAnnotation(eq(ConfigurationProperty.class))).thenReturn(configurationProperty);
    InjectionPoint injectionPoint = mock(InjectionPoint.class);
    when(injectionPoint.getAnnotated()).thenReturn(annotated);
    return injectionPoint;
}
 
Example #11
Source Project: hawkular-metrics   Author: hawkular   File: ConfigurableProducerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldThrowIllegalArgumentExceptionIfAnnotationIsMissing() throws Exception {
    // Override default: simulate a missing config property annotation
    Annotated annotated = mock(Annotated.class);
    when(annotated.getAnnotation(eq(ConfigurationProperty.class))).thenReturn(null);
    InjectionPoint injectionPoint = mock(InjectionPoint.class);
    when(injectionPoint.getAnnotated()).thenReturn(annotated);

    expectedException.expect(IllegalArgumentException.class);
    String message = "Any field or parameter annotated with @Configurable "
                     + "must also be annotated with @ConfigurationProperty";
    expectedException.expectMessage(message);
    configurableProducer.getConfigurationPropertyAsString(injectionPoint);
}
 
Example #12
Source Project: portals-pluto   Author: apache   File: PortletParamProducer.java    License: Apache License 2.0 5 votes vote down vote up
@Dependent
@PortletParam
@Produces
public Boolean getBooleanParam(ClientDataRequest clientDataRequest, PortletResponse portletResponse,
	InjectionPoint injectionPoint) {

	String value = getStringParam(clientDataRequest, portletResponse, injectionPoint);

	if (value == null) {
		return null;
	}

	Annotated field = injectionPoint.getAnnotated();

	Annotation[] fieldAnnotations = _getFieldAnnotations(field);

	ParamConverter<Boolean> paramConverter = _getParamConverter(Boolean.class, field.getBaseType(),
			fieldAnnotations);

	if (paramConverter != null) {

		try {
			return paramConverter.fromString(value);
		}
		catch (IllegalArgumentException iae) {
			_addBindingError(fieldAnnotations, iae.getMessage(), value);

			return null;
		}
	}

	if (LOG.isWarnEnabled()) {
		LOG.warn("Unable to find a ParamConverterProvider for type Boolean");
	}

	return null;
}
 
Example #13
Source Project: portals-pluto   Author: apache   File: PortletParamProducer.java    License: Apache License 2.0 5 votes vote down vote up
@Dependent
@PortletParam
@Produces
public Date getDateParam(ClientDataRequest clientDataRequest, PortletResponse portletResponse,
	InjectionPoint injectionPoint) {

	String value = getStringParam(clientDataRequest, portletResponse, injectionPoint);

	if (value == null) {
		return null;
	}

	Annotated field = injectionPoint.getAnnotated();

	Annotation[] fieldAnnotations = _getFieldAnnotations(field);

	ParamConverter<Date> paramConverter = _getParamConverter(Date.class, field.getBaseType(), fieldAnnotations);

	if (paramConverter != null) {

		try {
			return paramConverter.fromString(value);
		}
		catch (IllegalArgumentException iae) {
			_addBindingError(fieldAnnotations, iae.getMessage(), value);

			return null;
		}
	}

	if (LOG.isWarnEnabled()) {
		LOG.warn("Unable to find a ParamConverterProvider for type Date");
	}

	return null;
}
 
Example #14
Source Project: portals-pluto   Author: apache   File: PortletParamProducer.java    License: Apache License 2.0 5 votes vote down vote up
@Dependent
@PortletParam
@Produces
public Double getDoubleParam(ClientDataRequest clientDataRequest, PortletResponse portletResponse,
	InjectionPoint injectionPoint) {

	String value = getStringParam(clientDataRequest, portletResponse, injectionPoint);

	if (value == null) {
		return null;
	}

	Annotated field = injectionPoint.getAnnotated();

	Annotation[] fieldAnnotations = _getFieldAnnotations(field);

	ParamConverter<Double> paramConverter = _getParamConverter(Double.class, field.getBaseType(), fieldAnnotations);

	if (paramConverter != null) {

		try {
			return paramConverter.fromString(value);
		}
		catch (IllegalArgumentException iae) {
			_addBindingError(fieldAnnotations, iae.getMessage(), value);

			return null;
		}
	}

	if (LOG.isWarnEnabled()) {
		LOG.warn("Unable to find a ParamConverterProvider for type Double");
	}

	return null;
}
 
Example #15
Source Project: portals-pluto   Author: apache   File: PortletParamProducer.java    License: Apache License 2.0 5 votes vote down vote up
@Dependent
@PortletParam
@Produces
public Float getFloatParam(ClientDataRequest clientDataRequest, PortletResponse portletResponse,
	InjectionPoint injectionPoint) {

	String value = getStringParam(clientDataRequest, portletResponse, injectionPoint);

	if (value == null) {
		return null;
	}

	Annotated field = injectionPoint.getAnnotated();

	Annotation[] fieldAnnotations = _getFieldAnnotations(field);

	ParamConverter<Float> paramConverter = _getParamConverter(Float.class, field.getBaseType(), fieldAnnotations);

	if (paramConverter != null) {

		try {
			return paramConverter.fromString(value);
		}
		catch (IllegalArgumentException iae) {
			_addBindingError(fieldAnnotations, iae.getMessage(), value);

			return null;
		}
	}

	if (LOG.isWarnEnabled()) {
		LOG.warn("Unable to find a ParamConverterProvider for type Float");
	}

	return null;
}
 
Example #16
Source Project: portals-pluto   Author: apache   File: PortletParamProducer.java    License: Apache License 2.0 5 votes vote down vote up
@Dependent
@PortletParam
@Produces
public Integer getIntegerParam(ClientDataRequest clientDataRequest, PortletResponse portletResponse,
	InjectionPoint injectionPoint) {

	String value = getStringParam(clientDataRequest, portletResponse, injectionPoint);

	if (value == null) {
		return null;
	}

	Annotated field = injectionPoint.getAnnotated();

	Annotation[] fieldAnnotations = _getFieldAnnotations(field);

	ParamConverter<Integer> paramConverter = _getParamConverter(Integer.class, field.getBaseType(),
			fieldAnnotations);

	if (paramConverter != null) {

		try {
			return paramConverter.fromString(value);
		}
		catch (IllegalArgumentException iae) {
			_addBindingError(fieldAnnotations, iae.getMessage(), value);

			return null;
		}
	}

	if (LOG.isWarnEnabled()) {
		LOG.warn("Unable to find a ParamConverterProvider for type Integer");
	}

	return null;
}
 
Example #17
Source Project: portals-pluto   Author: apache   File: PortletParamProducer.java    License: Apache License 2.0 5 votes vote down vote up
@Dependent
@PortletParam
@Produces
public Long getLongParam(ClientDataRequest clientDataRequest, PortletResponse portletResponse,
	InjectionPoint injectionPoint) {

	String value = getStringParam(clientDataRequest, portletResponse, injectionPoint);

	if (value == null) {
		return null;
	}

	Annotated field = injectionPoint.getAnnotated();

	Annotation[] fieldAnnotations = _getFieldAnnotations(field);

	ParamConverter<Long> paramConverter = _getParamConverter(Long.class, field.getBaseType(), fieldAnnotations);

	if (paramConverter != null) {

		try {
			return paramConverter.fromString(value);
		}
		catch (IllegalArgumentException iae) {
			_addBindingError(fieldAnnotations, iae.getMessage(), value);

			return null;
		}
	}

	if (LOG.isWarnEnabled()) {
		LOG.warn("Unable to find a ParamConverterProvider for type Long");
	}

	return Long.valueOf(value);
}
 
Example #18
Source Project: tomee   Author: apache   File: JMS2CDIExtension.java    License: Apache License 2.0 5 votes vote down vote up
private Key newKey(final InjectionPoint ip) {
    final Annotated annotated = ip.getAnnotated();
    final JMSConnectionFactory jmsConnectionFactory = annotated.getAnnotation(JMSConnectionFactory.class);
    final JMSSessionMode sessionMode = annotated.getAnnotation(JMSSessionMode.class);
    final JMSPasswordCredential credential = annotated.getAnnotation(JMSPasswordCredential.class);

    final String jndi = "openejb:Resource/" +
        (jmsConnectionFactory == null ? findAnyConnectionFactory() : findMatchingConnectionFactory(jmsConnectionFactory.value()));
    return new Key(
        jndi,
        credential != null ? credential.userName() : null,
        credential != null ? credential.password() : null,
        sessionMode != null ? sessionMode.value() : null);
}
 
Example #19
Source Project: metrics-cdi   Author: astefanutti   File: SeMetricName.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public String of(InjectionPoint ip) {
    Annotated annotated = ip.getAnnotated();
    if (annotated instanceof AnnotatedMember)
        return of((AnnotatedMember<?>) annotated);
    else if (annotated instanceof AnnotatedParameter)
        return of((AnnotatedParameter<?>) annotated);
    else
        throw new IllegalArgumentException("Unable to retrieve metric name for injection point [" + ip + "], only members and parameters are supported");
}
 
Example #20
Source Project: deltaspike   Author: apache   File: DefaultMockFilter.java    License: Apache License 2.0 5 votes vote down vote up
protected boolean isMockSupportEnabled(Annotated annotated)
{
    if ((annotated instanceof AnnotatedMethod || annotated instanceof AnnotatedField) &&
            annotated.getAnnotation(Produces.class) != null)
    {
        return TestBaseConfig.MockIntegration.ALLOW_MOCKED_PRODUCERS;
    }
    else
    {
        return TestBaseConfig.MockIntegration.ALLOW_MOCKED_BEANS;
    }
}
 
Example #21
Source Project: smallrye-jwt   Author: smallrye   File: CDILogging.java    License: Apache License 2.0 4 votes vote down vote up
@LogMessage(level = Logger.Level.DEBUG)
@Message(id = 12008, value = "addTypeToClaimProducer(%s)")
void addTypeToClaimProducer(Annotated annotated);
 
Example #22
Source Project: smallrye-config   Author: smallrye   File: ConfigInjectionBean.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Annotated getAnnotated() {
    return null;
}
 
Example #23
Source Project: smallrye-metrics   Author: smallrye   File: AnnotatedDecorator.java    License: Apache License 2.0 4 votes vote down vote up
AnnotatedDecorator(Annotated decorated, Set<Annotation> annotations) {
    this.decorated = decorated;
    this.annotations = annotations;
}
 
Example #24
Source Project: quarkus   Author: quarkusio   File: CurrentInjectionPointProvider.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Annotated getAnnotated() {
    return annotated;
}
 
Example #25
Source Project: quarkus   Author: quarkusio   File: BeanManagerTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testGetInjectableReference() {
    BeanManager beanManager = Arc.container().beanManager();
    Set<Bean<?>> beans = beanManager.getBeans(WithInjectionPointMetadata.class);
    assertEquals(1, beans.size());
    @SuppressWarnings("unchecked")
    Bean<WithInjectionPointMetadata> bean = (Bean<WithInjectionPointMetadata>) beans.iterator().next();
    WithInjectionPointMetadata injectableReference = (WithInjectionPointMetadata) beanManager
            .getInjectableReference(new InjectionPoint() {

                @Override
                public boolean isTransient() {
                    return false;
                }

                @Override
                public boolean isDelegate() {
                    return false;
                }

                @Override
                public Type getType() {
                    return WithInjectionPointMetadata.class;
                }

                @Override
                public Set<Annotation> getQualifiers() {
                    return Collections.singleton(Any.Literal.INSTANCE);
                }

                @Override
                public Member getMember() {
                    return null;
                }

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

                @Override
                public Annotated getAnnotated() {
                    return null;
                }
            }, beanManager.createCreationalContext(bean));
    assertNotNull(injectableReference.injectionPoint);
    assertEquals(WithInjectionPointMetadata.class, injectableReference.injectionPoint.getType());
    assertNull(injectableReference.injectionPoint.getBean());
}
 
Example #26
Source Project: quarkus   Author: quarkusio   File: MethodParameterInjectionPoint.java    License: Apache License 2.0 4 votes vote down vote up
public Annotated getAnnotated() {
    return new ArgumentAnnotated<T>();
}
 
Example #27
Source Project: weld-vertx   Author: weld   File: RouteExtension.java    License: Apache License 2.0 4 votes vote down vote up
private boolean isWebRoute(Annotated annotated) {
    return annotated.isAnnotationPresent(WebRoute.class) || annotated.isAnnotationPresent(WebRoutes.class);
}
 
Example #28
Source Project: weld-vertx   Author: weld   File: ServiceProxyExtension.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Annotated getAnnotated() {
    return null;
}
 
Example #29
Source Project: dashbuilder   Author: dashbuilder   File: ConfigReader.java    License: Apache License 2.0 4 votes vote down vote up
public @Produces @Config String readConfig(InjectionPoint p) {

        // Read from specific bean
        String beanKey = p.getMember().getDeclaringClass().getName();
        Properties beanProperties = beanPropertyMap.get(beanKey);
        if (beanProperties == null) {
            beanPropertyMap.put(beanKey, beanProperties = new Properties());
            try {
                InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("META-INF/" + beanKey + ".config");
                if (is != null)  beanProperties.load(is);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }

        // Read from the bean config
        String configKey = p.getMember().getName();
        String configValue = beanProperties.getProperty(configKey);
        if (configValue != null) return configValue;

        // Read from global - by the fully qualified class name and field name
        for (Type type : p.getBean().getTypes()) {
            configKey = ((Class)type).getName() + "." + p.getMember().getName();
            configValue = globalProperties.getProperty(configKey);
            if (configValue != null) return configValue;

            // Try class name from System.properties
            configValue = System.getProperty(configKey);
            if (configValue != null) {
                log.info(String.format("System property: %s=%s", configKey, configValue));
                return configValue;
            }
            // Try class simple name from System.properties
            configKey = ((Class)type).getSimpleName() + "." + p.getMember().getName();
            configValue = System.getProperty(configKey);
            if (configValue != null) {
                log.info(String.format("System property: %s=%s", configKey, configValue));
                return configValue;
            }
        }

        // Read from global - only by the field name
        configKey = p.getMember().getName();
        configValue = globalProperties.getProperty(configKey);
        if (configValue != null) return configValue;

        // Return the default value if any.
        Annotated annotated = p.getAnnotated();
        Config config = annotated.getAnnotation(Config.class);
        if (config != null) return config.value();
        return null;
    }
 
Example #30
Source Project: portals-pluto   Author: apache   File: PortletParamProducer.java    License: Apache License 2.0 4 votes vote down vote up
private Annotation[] _getFieldAnnotations(Annotated field) {
	Set<Annotation> annotations = field.getAnnotations();

	return annotations.toArray(new Annotation[annotations.size()]);
}