Search in sources :

Example 1 with ConfigProperties

use of org.eclipse.microprofile.config.inject.ConfigProperties in project Payara by payara.

the class ConfigPropertiesProducer method getQualifier.

private static ConfigProperties getQualifier(InjectionPoint injectionPoint) {
    // If it's an @Inject point
    final Annotated annotated = injectionPoint.getAnnotated();
    if (annotated != null) {
        return annotated.getAnnotation(ConfigProperties.class);
    }
    // If it's a programmatic lookup
    final Set<Annotation> qualifiers = injectionPoint.getQualifiers();
    for (Annotation qualifier : qualifiers) {
        if (qualifier instanceof ConfigProperties) {
            return (ConfigProperties) qualifier;
        }
    }
    return null;
}
Also used : Annotated(javax.enterprise.inject.spi.Annotated) ConfigProperties(org.eclipse.microprofile.config.inject.ConfigProperties) Annotation(java.lang.annotation.Annotation)

Example 2 with ConfigProperties

use of org.eclipse.microprofile.config.inject.ConfigProperties in project Payara by payara.

the class ConfigPropertiesProducer method getGenericObject.

@ConfigProperties
public static final Object getGenericObject(InjectionPoint injectionPoint, BeanManager bm) throws InstantiationException, IllegalAccessException {
    Type type = injectionPoint.getType();
    if (!(type instanceof Class)) {
        throw new IllegalArgumentException("Unable to process injection point with @ConfigProperties of type " + type);
    }
    // Initialise the object. This may throw exceptions
    final Object object = ((Class) type).newInstance();
    // Model the class
    final AnnotatedType<?> annotatedType = bm.createAnnotatedType((Class) type);
    // Find the @ConfigProperties annotations, and calculate the property prefix
    final ConfigProperties injectionAnnotation = getQualifier(injectionPoint);
    final ConfigProperties classAnnotation = annotatedType.getAnnotation(ConfigProperties.class);
    final String prefix = parsePrefixes(injectionAnnotation, classAnnotation);
    for (AnnotatedField<?> field : annotatedType.getFields()) {
        // Find the java field and field name
        final Field javaField = field.getJavaMember();
        // Make sure the field is accessible
        javaField.setAccessible(true);
        // Model the field
        final InjectionPoint fieldInjectionPoint = bm.createInjectionPoint(field);
        final ConfigPropertyModel model = new ConfigPropertyModel(fieldInjectionPoint, prefix);
        try {
            final Object value = ConfigPropertyProducer.getGenericPropertyFromModel(model);
            if (value != null) {
                javaField.set(object, value);
            }
        } catch (Exception ex) {
            if (javaField.get(object) == null) {
                LOGGER.log(Level.WARNING, String.format("Unable to inject property with name %s into type %s.", model.getName(), type.getTypeName()), ex);
                throw ex;
            }
        }
    }
    return object;
}
Also used : Field(java.lang.reflect.Field) AnnotatedField(javax.enterprise.inject.spi.AnnotatedField) AnnotatedType(javax.enterprise.inject.spi.AnnotatedType) Type(java.lang.reflect.Type) ConfigProperties(org.eclipse.microprofile.config.inject.ConfigProperties) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) ConfigPropertyModel(fish.payara.microprofile.config.cdi.model.ConfigPropertyModel) ConfigProperties(org.eclipse.microprofile.config.inject.ConfigProperties)

Aggregations

ConfigProperties (org.eclipse.microprofile.config.inject.ConfigProperties)2 ConfigPropertyModel (fish.payara.microprofile.config.cdi.model.ConfigPropertyModel)1 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Type (java.lang.reflect.Type)1 Annotated (javax.enterprise.inject.spi.Annotated)1 AnnotatedField (javax.enterprise.inject.spi.AnnotatedField)1 AnnotatedType (javax.enterprise.inject.spi.AnnotatedType)1 InjectionPoint (javax.enterprise.inject.spi.InjectionPoint)1