Skip to content

Commit

Permalink
FISH-658 Fix Programmatic @ConfigProperties injection
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Gill <[email protected]>
  • Loading branch information
MattGill98 committed Feb 3, 2021
1 parent 5bc2206 commit d65f429
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import static org.eclipse.microprofile.config.inject.ConfigProperties.UNCONFIGURED_PREFIX;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.enterprise.inject.spi.Annotated;
import javax.enterprise.inject.spi.AnnotatedField;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.BeanManager;
Expand All @@ -21,9 +24,9 @@ public class ConfigPropertiesProducer {
private static final Logger LOGGER = Logger.getLogger(ConfigPropertiesProducer.class.getName());

@ConfigProperties
public static final Object getGenericObject(InjectionPoint ip, BeanManager bm)
public static final Object getGenericObject(InjectionPoint injectionPoint, BeanManager bm)
throws InstantiationException, IllegalAccessException {
Type type = ip.getType();
Type type = injectionPoint.getType();
if (!(type instanceof Class)) {
throw new IllegalArgumentException("Unable to process injection point with @ConfigProperties of type " + type);
}
Expand All @@ -35,7 +38,7 @@ public static final Object getGenericObject(InjectionPoint ip, BeanManager bm)
final AnnotatedType<?> annotatedType = bm.createAnnotatedType((Class) type);

// Find the @ConfigProperties annotations, and calculate the property prefix
final ConfigProperties injectionAnnotation = ip.getAnnotated().getAnnotation(ConfigProperties.class);
final ConfigProperties injectionAnnotation = getQualifier(injectionPoint);
final ConfigProperties classAnnotation = annotatedType.getAnnotation(ConfigProperties.class);
final String prefix = parsePrefixes(injectionAnnotation, classAnnotation);

Expand Down Expand Up @@ -69,6 +72,25 @@ public static final Object getGenericObject(InjectionPoint ip, BeanManager bm)
return object;
}

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

private static String parsePrefixes(ConfigProperties injectionAnnotation, ConfigProperties classAnnotation) {
final String injectionPrefix = parsePrefix(injectionAnnotation);
if (injectionPrefix != null) {
Expand Down

This file was deleted.

0 comments on commit d65f429

Please sign in to comment.