Java Code Examples for javax.enterprise.inject.spi.Annotated#getAnnotation()

The following examples show how to use javax.enterprise.inject.spi.Annotated#getAnnotation() . 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: EnvironmentPropertyProducer.java    From vraptor4 with 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 2
Source File: BeanUtils.java    From deltaspike with 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 3
Source File: ConfigInjectionBean.java    From smallrye-config with 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 4
Source File: MockResourceInjectionServices.java    From weld-junit with 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 5
Source File: RouteExtension.java    From weld-vertx with 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 6
Source File: ExecutorServiceExposer.java    From porcupine with 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 7
Source File: JMS2CDIExtension.java    From tomee with 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 8
Source File: DefaultMockFilter.java    From deltaspike with 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 9
Source File: ConfigReader.java    From dashbuilder with 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;
    }