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;
}
Aggregations