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

The following examples show how to use javax.enterprise.inject.spi.InjectionPoint#getAnnotated() . 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: TemplateProducer.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@Produces
Template getDefaultTemplate(InjectionPoint injectionPoint) {
    String name = null;
    if (injectionPoint.getMember() instanceof Field) {
        // For "@Inject Template items" use "items"
        name = injectionPoint.getMember().getName();
    } else {
        AnnotatedParameter<?> parameter = (AnnotatedParameter<?>) injectionPoint.getAnnotated();
        if (parameter.getJavaParameter().isNamePresent()) {
            name = parameter.getJavaParameter().getName();
        } else {
            name = injectionPoint.getMember().getName();
            LOGGER.warnf("Parameter name not present - using the method name as the template name instead %s", name);
        }
    }
    return new InjectableTemplate(name, templateVariants, engine);
}
 
Example 2
Source File: MailTemplateProducer.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@Produces
MailTemplate getDefault(InjectionPoint injectionPoint) {

    final String name;
    if (injectionPoint.getMember() instanceof Field) {
        // For "@Inject MailTemplate test" use "test"
        name = injectionPoint.getMember().getName();
    } else {
        AnnotatedParameter<?> parameter = (AnnotatedParameter<?>) injectionPoint.getAnnotated();
        if (parameter.getJavaParameter().isNamePresent()) {
            name = parameter.getJavaParameter().getName();
        } else {
            name = injectionPoint.getMember().getName();
            LOGGER.warnf("Parameter name not present - using the method name as the template name instead %s", name);
        }
    }

    return new MailTemplate() {
        @Override
        public MailTemplateInstance instance() {
            return new MailTemplateInstanceImpl(mailer, template.select(new ResourcePath.Literal(name)).get().instance());
        }

    };
}
 
Example 3
Source File: ConfigProducerUtil.java    From smallrye-config with Apache License 2.0 6 votes vote down vote up
static String getConfigKey(InjectionPoint ip, ConfigProperty configProperty) {
    String key = configProperty.name();
    if (!key.trim().isEmpty()) {
        return key;
    }
    if (ip.getAnnotated() instanceof AnnotatedMember) {
        AnnotatedMember<?> member = (AnnotatedMember<?>) ip.getAnnotated();
        AnnotatedType<?> declaringType = member.getDeclaringType();
        if (declaringType != null) {
            String[] parts = declaringType.getJavaClass().getCanonicalName().split("\\.");
            StringBuilder sb = new StringBuilder(parts[0]);
            for (int i = 1; i < parts.length; i++) {
                sb.append(".").append(parts[i]);
            }
            sb.append(".").append(member.getJavaMember().getName());
            return sb.toString();
        }
    }
    throw InjectionMessages.msg.noConfigPropertyDefaultName(ip);
}
 
Example 4
Source File: PortletParamProducer.java    From portals-pluto with 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 5
Source File: MonetaryProducer.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
@Produces @Dependent
public static ExchangeRateProvider rateProvider(InjectionPoint ip){
    AmountConversion specAnnot = ip.getAnnotated()!=null?ip.getAnnotated().getAnnotation(AmountConversion.class):null;
    if(specAnnot!=null){
        return MonetaryConversions.getExchangeRateProvider(createConversionQuery(specAnnot));
    }
    return MonetaryConversions.getExchangeRateProvider();
}
 
Example 6
Source File: MonetaryProducer.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
@Produces @Dependent
public static Collection<CurrencyUnit> currencyUnits(InjectionPoint ip){
    AmountCurrency specAnnot = ip.getAnnotated()!=null?ip.getAnnotated().getAnnotation(AmountCurrency.class):null;
    if(specAnnot!=null){
        return Monetary.getCurrencies(createCurrencyQuery(specAnnot));
    }
    return Monetary.getCurrencies();
}
 
Example 7
Source File: MonetaryProducer.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
@Produces @Dependent
public static MonetaryAmountFactory amountFactory(InjectionPoint ip){
    Amount specAnnot = ip.getAnnotated()!=null?ip.getAnnotated().getAnnotation(Amount.class):null;
    if(specAnnot!=null){
        return Monetary.getAmountFactory(createAmountQuery(specAnnot));
    }
    return Monetary.getDefaultAmountFactory();
}
 
Example 8
Source File: MonetaryProducer.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
@Produces @Dependent
public static Collection<ExchangeRate> exchangeRates(InjectionPoint ip){
    AmountConversion specAnnot = ip.getAnnotated()!=null?ip.getAnnotated().getAnnotation(AmountConversion.class):null;
    if(specAnnot==null){
        throw new IllegalArgumentException("@RateSpec is required.");
    }
    ConversionQuery query = createConversionQuery(specAnnot);
    Collection<String> providers = query.getProviderNames();
    if(providers.isEmpty()){
        providers = MonetaryConversions.getConversionProviderNames();
    }
    List<ExchangeRate> rates = new ArrayList<>();
    for(String providerId:providers){
        ExchangeRateProvider provider = MonetaryConversions.getExchangeRateProvider(providerId);
        try {
            ExchangeRate rate = provider.getExchangeRate(query);
            if (rate != null) {
                rates.add(rate);
            }
        }catch(Exception e){
            LOG.log(Level.FINE,
                    "Could not evaluate rate from provider '" + providerId + "': "+  query,
                    e);
        }
    }
    return rates;
}
 
Example 9
Source File: PortletParamProducer.java    From portals-pluto with 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 10
Source File: MonetaryProducer.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
@Produces @Dependent
public static MonetaryAmountFormat amountFormat(InjectionPoint ip){
    AmountFormat specAnnot = ip.getAnnotated()!=null?ip.getAnnotated().getAnnotation(AmountFormat.class):null;
    if(specAnnot==null){
        throw new IllegalArgumentException("@FormatName is required.");
    }
    return MonetaryFormats.getAmountFormat(createAmountFormatQuery(
            specAnnot,
            ip.getAnnotated().getAnnotation(Amount.class)));
}
 
Example 11
Source File: PortletParamProducer.java    From portals-pluto with 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 12
Source File: MonetaryProducer.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
@Produces @Dependent
public static AmountFormatQuery amountFormatQuery(InjectionPoint ip){
    AmountFormat specAnnot = ip.getAnnotated()!=null?ip.getAnnotated().getAnnotation(AmountFormat.class):null;
    if(specAnnot==null){
        return null;
    }
    Amount amountSpec = ip.getAnnotated()!=null?ip.getAnnotated().getAnnotation(Amount.class):null;
    return createAmountFormatQuery(specAnnot, amountSpec);
}
 
Example 13
Source File: SeMetricName.java    From metrics-cdi with 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 14
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 15
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 16
Source File: MonetaryProducer.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
@Produces @Dependent
public static CurrencyQuery currencyQuery(InjectionPoint ip){
    AmountCurrency specAnnot = ip.getAnnotated()!=null?ip.getAnnotated().getAnnotation(AmountCurrency.class):null;
    if(specAnnot==null){
        return null;
    }
    return createCurrencyQuery(specAnnot);
}
 
Example 17
Source File: TransientReferenceDestroyedTest.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Dependent
@Produces
Beer newBeer(InjectionPoint injectionPoint) {
    int id;
    if (injectionPoint.getAnnotated() instanceof AnnotatedField) {
        id = 0;
    } else {
        id = COUNTER.incrementAndGet();
    }
    return new Beer(id);
}
 
Example 18
Source File: MonetaryProducer.java    From javamoney-lib with Apache License 2.0 5 votes vote down vote up
@Produces @Dependent
public static Collection<MonetaryAmountFormat> amountFormats(InjectionPoint ip){
    AmountFormat specAnnot = ip.getAnnotated()!=null?ip.getAnnotated().getAnnotation(AmountFormat.class):null;
    if(specAnnot==null){
        throw new IllegalArgumentException("@FormatName is required.");
    }
    return MonetaryFormats.getAmountFormats(createAmountFormatQuery(
            specAnnot,
            ip.getAnnotated().getAnnotation(Amount.class)));
}
 
Example 19
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 20
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;
    }