Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CDI Fixes. #339

Merged
merged 3 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.smallrye.config.inject;

import static io.smallrye.config.inject.SecuritySupport.getContextClassLoader;
Expand All @@ -34,7 +33,6 @@
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.AfterDeploymentValidation;
import javax.enterprise.inject.spi.AnnotatedMember;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.BeforeBeanDiscovery;
Expand All @@ -58,24 +56,23 @@
* @author <a href="http://jmesnil.net/">Jeff Mesnil</a> (c) 2017 Red Hat inc.
*/
public class ConfigExtension implements Extension {

private Set<InjectionPoint> injectionPoints = new HashSet<>();
private final Set<InjectionPoint> injectionPoints = new HashSet<>();

public ConfigExtension() {
}

void beforeBeanDiscovery(@Observes BeforeBeanDiscovery bbd, BeanManager bm) {
protected void beforeBeanDiscovery(@Observes BeforeBeanDiscovery bbd, BeanManager bm) {
AnnotatedType<ConfigProducer> configBean = bm.createAnnotatedType(ConfigProducer.class);
bbd.addAnnotatedType(configBean, ConfigProducer.class.getName());
}

void collectConfigPropertyInjectionPoints(@Observes ProcessInjectionPoint<?, ?> pip) {
protected void collectConfigPropertyInjectionPoints(@Observes ProcessInjectionPoint<?, ?> pip) {
if (pip.getInjectionPoint().getAnnotated().isAnnotationPresent(ConfigProperty.class)) {
injectionPoints.add(pip.getInjectionPoint());
}
}

void registerCustomBeans(@Observes AfterBeanDiscovery abd, BeanManager bm) {
protected void registerCustomBeans(@Observes AfterBeanDiscovery abd, BeanManager bm) {
Set<Class<?>> customTypes = new HashSet<>();
for (InjectionPoint ip : injectionPoints) {
Type requiredType = ip.getType();
Expand All @@ -101,14 +98,17 @@ void registerCustomBeans(@Observes AfterBeanDiscovery abd, BeanManager bm) {
}
}

void validate(@Observes AfterDeploymentValidation adv) {
protected void validate(@Observes AfterDeploymentValidation adv) {
Config config = ConfigProvider.getConfig(getContextClassLoader());
Set<String> configNames = StreamSupport.stream(config.getPropertyNames().spliterator(), false).collect(toSet());
for (InjectionPoint injectionPoint : injectionPoints) {
Type type = injectionPoint.getType();

// We don't validate the Optional / Provider / Supplier / ConfigValue for defaultValue.
if (type instanceof Class && ConfigValue.class.isAssignableFrom((Class<?>) type)
|| type instanceof Class && OptionalInt.class.isAssignableFrom((Class<?>) type)
|| type instanceof Class && OptionalLong.class.isAssignableFrom((Class<?>) type)
|| type instanceof Class && OptionalDouble.class.isAssignableFrom((Class<?>) type)
|| type instanceof ParameterizedType
&& (Optional.class.isAssignableFrom((Class<?>) ((ParameterizedType) type).getRawType())
|| Provider.class.isAssignableFrom((Class<?>) ((ParameterizedType) type).getRawType())
Expand All @@ -117,7 +117,7 @@ void validate(@Observes AfterDeploymentValidation adv) {
}

ConfigProperty configProperty = injectionPoint.getAnnotated().getAnnotation(ConfigProperty.class);
String name = getConfigKey(injectionPoint, configProperty);
String name = ConfigProducerUtil.getConfigKey(injectionPoint, configProperty);

// Check if the name is part of the properties first. Since properties can be a subset, then search for the actual property for a value.
if (!configNames.contains(name) && ConfigProducerUtil.getRawValue(name, (SmallRyeConfig) config) == null) {
Expand All @@ -139,27 +139,6 @@ void validate(@Observes AfterDeploymentValidation adv) {
}
}

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

private static boolean isClassHandledByConfigProducer(Type requiredType) {
return requiredType == String.class
|| requiredType == Boolean.class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.smallrye.config.inject;

import java.lang.annotation.Annotation;
Expand Down Expand Up @@ -86,7 +85,7 @@ 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 = ConfigExtension.getConfigKey(ip, configProperty);
String key = ConfigProducerUtil.getConfigKey(ip, configProperty);
String defaultValue = configProperty.defaultValue();

if (annotated.getBaseType() instanceof ParameterizedType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.util.Set;
import java.util.function.Supplier;

import javax.enterprise.inject.spi.AnnotatedMember;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.InjectionPoint;

import org.eclipse.microprofile.config.Config;
Expand Down Expand Up @@ -120,13 +122,13 @@ public static String getName(InjectionPoint injectionPoint) {
for (Annotation qualifier : injectionPoint.getQualifiers()) {
if (qualifier.annotationType().equals(ConfigProperty.class)) {
ConfigProperty configProperty = ((ConfigProperty) qualifier);
return ConfigExtension.getConfigKey(injectionPoint, configProperty);
return getConfigKey(injectionPoint, configProperty);
}
}
return null;
}

private static String getDefaultValue(InjectionPoint injectionPoint) {
public static String getDefaultValue(InjectionPoint injectionPoint) {
for (Annotation qualifier : injectionPoint.getQualifiers()) {
if (qualifier.annotationType().equals(ConfigProperty.class)) {
String str = ((ConfigProperty) qualifier).defaultValue();
Expand All @@ -148,4 +150,25 @@ private static String getDefaultValue(InjectionPoint injectionPoint) {
}
return null;
}

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