Search in sources :

Example 1 with ConfigPropertyModel

use of fish.payara.microprofile.config.cdi.model.ConfigPropertyModel 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

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