Skip to content

Commit

Permalink
Update SmallRye Config to 1.9.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Sep 17, 2020
1 parent 2d446a4 commit 47d4aa5
Show file tree
Hide file tree
Showing 8 changed files with 465 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bom/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<microprofile-reactive-streams-operators.version>1.0.1</microprofile-reactive-streams-operators.version>
<microprofile-rest-client.version>1.4.1</microprofile-rest-client.version>
<smallrye-common.version>1.4.0</smallrye-common.version>
<smallrye-config.version>1.8.6</smallrye-config.version>
<smallrye-config.version>1.9.0</smallrye-config.version>
<smallrye-health.version>2.2.3</smallrye-health.version>
<smallrye-metrics.version>2.4.3</smallrye-metrics.version>
<smallrye-open-api.version>2.0.9</smallrye-open-api.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.oracle.svm.core.threadlocal.FastThreadLocalFactory;
import com.oracle.svm.core.threadlocal.FastThreadLocalInt;

import io.smallrye.common.constraint.Assert;
import io.smallrye.config.Expressions;

/**
Expand Down Expand Up @@ -52,4 +53,29 @@ public static <T> T withoutExpansion(Supplier<T> supplier) {
}
}
}

@TargetClass(className = "io.smallrye.config.ConfigMappingObjectLoader")
static final class Target_ConfigMappingObjectLoader {
@Substitute
static Class<?> createMappingObjectClass(final String className, final byte[] bytes) {
return null;
}
}

@TargetClass(className = "io.smallrye.config.ConfigMappingInterface")
static final class Target_ConfigMappingInterface {
@Alias
static ClassValue<Target_ConfigMappingInterface> cv = null;

// ClassValue is substituted by a regular ConcurrentHashMap - java.lang.ClassValue.get(JavaLangSubstitutions.java:514)
@Substitute
public static Target_ConfigMappingInterface getConfigurationInterface(Class<?> interfaceType) {
Assert.checkNotNullParam("interfaceType", interfaceType);
try {
return cv.get(interfaceType);
} catch (NullPointerException e) {
return null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.quarkus.runtime.graal;

import java.lang.invoke.MethodHandles;

import com.oracle.svm.core.annotate.Delete;
import com.oracle.svm.core.annotate.TargetClass;

import io.smallrye.common.classloader.ClassDefiner;

final class ClassDefinerSubstitutions {
@TargetClass(ClassDefiner.class)
static final class Target_io_smallrye_common_classloader_ClassDefiner {
@Delete
public static Class<?> defineClass(MethodHandles.Lookup lookup, Class<?> parent, String className, byte[] classBytes) {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
package io.quarkus.arc.deployment;

import static io.quarkus.deployment.annotations.ExecutionTime.RUNTIME_INIT;
import static io.smallrye.config.ConfigMappings.ConfigMappingWithPrefix.configMappingWithPrefix;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;

import javax.enterprise.context.Dependent;
import javax.enterprise.inject.CreationException;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
Expand All @@ -30,15 +35,23 @@
import io.quarkus.arc.processor.DotNames;
import io.quarkus.arc.processor.InjectionPointInfo;
import io.quarkus.arc.runtime.ConfigBeanCreator;
import io.quarkus.arc.runtime.ConfigMappingCreator;
import io.quarkus.arc.runtime.ConfigRecorder;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.deployment.builditem.ConfigurationBuildItem;
import io.quarkus.deployment.builditem.GeneratedClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.deployment.configuration.definition.RootDefinition;
import io.quarkus.deployment.recording.RecorderContext;
import io.quarkus.gizmo.ResultHandle;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.ConfigMappingMetadata;
import io.smallrye.config.ConfigMappingObjectLoader;
import io.smallrye.config.ConfigMappings.ConfigMappingWithPrefix;
import io.smallrye.config.inject.ConfigProducer;

/**
Expand All @@ -47,6 +60,7 @@
public class ConfigBuildStep {

private static final DotName CONFIG_PROPERTY_NAME = DotName.createSimple(ConfigProperty.class.getName());
private static final DotName CONFIG_MAPPING_NAME = DotName.createSimple(ConfigMapping.class.getName());
private static final DotName SET_NAME = DotName.createSimple(Set.class.getName());
private static final DotName LIST_NAME = DotName.createSimple(List.class.getName());

Expand Down Expand Up @@ -174,6 +188,71 @@ public void register(RegistrationContext context) {
});
}

@BuildStep
void generateConfigMappings(
CombinedIndexBuildItem combinedIndex,
BeanRegistrationPhaseBuildItem beanRegistrationPhase,
BuildProducer<GeneratedClassBuildItem> generatedClasses,
BuildProducer<ReflectiveClassBuildItem> reflectiveClasses,
BuildProducer<ConfigMappingBuildItem> configMappings,
BuildProducer<BeanConfiguratorBuildItem> beanConfigurators) {

for (AnnotationInstance instance : combinedIndex.getIndex().getAnnotations(CONFIG_MAPPING_NAME)) {
AnnotationTarget target = instance.target();
if (!target.kind().equals(AnnotationTarget.Kind.CLASS)) {
continue;
}

ClassInfo classInfo = target.asClass();
String prefix = Optional.ofNullable(instance.value("prefix")).map(AnnotationValue::asString).orElse("");

ConfigMappingMetadata configMappingMetadata = ConfigMappingObjectLoader
.getConfigMappingMetadata(toClass(classInfo));

generatedClasses.produce(new GeneratedClassBuildItem(true, configMappingMetadata.getClassName(),
configMappingMetadata.getClassBytes()));
reflectiveClasses
.produce(new ReflectiveClassBuildItem(true, false, configMappingMetadata.getInterfaceType()));
reflectiveClasses
.produce(new ReflectiveClassBuildItem(true, false, false, configMappingMetadata.getClassName()));
configMappings.produce(new ConfigMappingBuildItem(toClass(classInfo), prefix));

beanConfigurators.produce(new BeanConfiguratorBuildItem(
beanRegistrationPhase.getContext()
.configure(configMappingMetadata.getInterfaceType())
.types(configMappingMetadata.getInterfaceType())
.creator(ConfigMappingCreator.class)
.param("interfaceType", configMappingMetadata.getInterfaceType())));
}
}

@BuildStep
@Record(RUNTIME_INIT)
void registerConfigMappings(
RecorderContext context,
ConfigRecorder recorder,
List<ConfigMappingBuildItem> configMappings) throws Exception {

context.registerNonDefaultConstructor(
ConfigMappingWithPrefix.class.getDeclaredConstructor(Class.class, String.class),
configMappingWithPrefix -> Stream.of(configMappingWithPrefix.getKlass(), configMappingWithPrefix.getPrefix())
.collect(toList()));

recorder.registerConfigMappings(configMappings.stream()
.map(configMapping -> configMappingWithPrefix(configMapping.getInterfaceType(), configMapping.getPrefix()))
.collect(toSet()));
}

private static Class<?> toClass(ClassInfo classInfo) {
String className = classInfo.name().toString();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
try {
return classLoader.loadClass(className);
} catch (ClassNotFoundException e) {
throw new IllegalStateException("The class (" + className + ") cannot be created during deployment.", e);
}
}

private String getPropertyName(String name, ClassInfo declaringClass) {
StringBuilder builder = new StringBuilder();
if (declaringClass.enclosingClass() == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.arc.deployment;

import io.quarkus.builder.item.MultiBuildItem;

public final class ConfigMappingBuildItem extends MultiBuildItem {
private final Class<?> interfaceType;
private final String prefix;

public ConfigMappingBuildItem(final Class<?> interfaceType, final String prefix) {
this.interfaceType = interfaceType;
this.prefix = prefix;
}

public Class<?> getInterfaceType() {
return interfaceType;
}

public String getPrefix() {
return prefix;
}
}
Loading

0 comments on commit 47d4aa5

Please sign in to comment.