diff --git a/implementation/src/main/java/io/smallrye/config/AbstractConfigInstanceBuilder.java b/implementation/src/main/java/io/smallrye/config/AbstractConfigInstanceBuilder.java new file mode 100644 index 000000000..bb823b152 --- /dev/null +++ b/implementation/src/main/java/io/smallrye/config/AbstractConfigInstanceBuilder.java @@ -0,0 +1,62 @@ +package io.smallrye.config; + +import static io.smallrye.config.ConfigMessages.msg; + +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; +import java.lang.reflect.UndeclaredThrowableException; +import java.util.function.Supplier; + +/** + * The base class for all generated configuration instance builders. + */ +public abstract class AbstractConfigInstanceBuilder implements ConfigInstanceBuilder { + private static final ClassValue>> cv = new ClassValue<>() { + @SuppressWarnings("unchecked") + protected Supplier> computeValue(final Class type) { + assert type.isInterface(); + String interfaceName = type.getName(); + MethodHandles.Lookup lookup; + try { + lookup = MethodHandles.privateLookupIn(type, MethodHandles.lookup()); + } catch (IllegalAccessException e) { + throw msg.accessDenied(getClass(), type); + } + Class> impl; + String implInternalName = interfaceName.replace('.', '/') + "$$CIBImpl"; + try { + impl = (Class>) Class + .forName(implInternalName, false, type.getClassLoader()) + .asSubclass(AbstractConfigInstanceBuilder.class); + } catch (ClassNotFoundException e) { + // generate the impl instead + throw new UnsupportedOperationException("Todo"); + } + MethodHandle mh; + try { + mh = lookup.findConstructor(impl, MethodType.methodType(void.class)); + } catch (NoSuchMethodException e) { + throw msg.noConstructor(impl); + } catch (IllegalAccessException e) { + throw msg.accessDenied(getClass(), impl); + } + // capture the constructor as a Supplier + return () -> { + try { + return (AbstractConfigInstanceBuilder) mh.invokeExact(); + } catch (RuntimeException | Error e) { + throw e; + } catch (Throwable e) { + throw new UndeclaredThrowableException(e); + } + }; + } + }; + + @SuppressWarnings("unchecked") + static AbstractConfigInstanceBuilder forInterface(Class interfaceClass) + throws IllegalArgumentException, SecurityException { + return (AbstractConfigInstanceBuilder) cv.get(interfaceClass).get(); + } +} diff --git a/implementation/src/main/java/io/smallrye/config/ConfigInstanceBuilder.java b/implementation/src/main/java/io/smallrye/config/ConfigInstanceBuilder.java new file mode 100644 index 000000000..cadaca515 --- /dev/null +++ b/implementation/src/main/java/io/smallrye/config/ConfigInstanceBuilder.java @@ -0,0 +1,320 @@ +package io.smallrye.config; + +import java.io.Serializable; +import java.util.Optional; +import java.util.OptionalDouble; +import java.util.OptionalInt; +import java.util.OptionalLong; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.function.ToIntFunction; +import java.util.function.ToLongFunction; + +/** + * A builder which can produce instances of a configuration interface. + *

+ * Objects which are produced by this API will contain values for every property found on the configuration + * interface or its supertypes. + * If no value is given for a property, its default value is used. + * If a required property has no default value, then an exception will be thrown when {@link #build} is called. + * The returned object instance is immutable and has a stable {@code equals} and {@code hashCode} method. + * If the runtime is Java 16 or later, the returned object may be a {@code Record}. + *

+ * To provide a value for a property, use a method reference to indicate which property the value should be associated + * with. + * For example, + * + *

+
+ConfigInstanceBuilder<MyProgramConfig> builder = ConfigInstanceBuilder.forInterface(MyProgramConfig.class);
+builder.with(MyProgramConfig::message, "Hello everyone!");
+builder.with(MyProgramConfig::repeatCount, 42);
+MyProgramConfig config = builder.build();
+for (int i = 0; i < config.repeatCount(); i ++) {
+    System.out.println(config.message());
+}
+
+ * 
+ * + * @param the configuration interface type + */ +public interface ConfigInstanceBuilder { + /** + * {@return the configuration interface (not null)} + */ + Class configurationInterface(); + + /** + * Set a property on the configuration object to an object value. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @return this builder (not {@code null}) + * @param the value type + * @param the accessor type + * @throws IllegalArgumentException if the getter is {@code null} + * or if the value is {@code null} + */ + & Serializable> ConfigInstanceBuilder with(F getter, T value); + + /** + * Set a property on the configuration object to an integer value. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @return this builder (not {@code null}) + * @param the accessor type + * @throws IllegalArgumentException if the getter is {@code null} + */ + & Serializable> ConfigInstanceBuilder with(F getter, int value); + + /** + * Set a property on the configuration object to an integer value. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @return this builder (not {@code null}) + * @param the accessor type + * @throws IllegalArgumentException if the getter is {@code null} + */ + & Serializable> ConfigInstanceBuilder with(F getter, long value); + + /** + * Set a property on the configuration object to a floating-point value. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @return this builder (not {@code null}) + * @param the accessor type + * @throws IllegalArgumentException if the getter is {@code null} + */ + & Serializable> ConfigInstanceBuilder with(F getter, double value); + + /** + * Set a property on the configuration object to a boolean value. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @return this builder (not {@code null}) + * @param the accessor type + * @throws IllegalArgumentException if the getter is {@code null} + */ + & Serializable> ConfigInstanceBuilder with(F getter, boolean value); + + /** + * Set an optional property on the configuration object to an object value. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @param the value type + * @param the accessor type + * @return this builder (not {@code null}) + * @throws IllegalArgumentException if the getter is {@code null} + * or the value is {@code null} + */ + default > & Serializable> ConfigInstanceBuilder withOptional(F getter, + T value) { + return with(getter, Optional.of(value)); + } + + /** + * Set an optional property on the configuration object to an integer value. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @param the accessor type + * @return this builder (not {@code null}) + * @throws IllegalArgumentException if the getter is {@code null} + */ + default & Serializable> ConfigInstanceBuilder withOptional(F getter, + int value) { + return with(getter, OptionalInt.of(value)); + } + + /** + * Set an optional property on the configuration object to an integer value. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @param the accessor type + * @return this builder (not {@code null}) + * @throws IllegalArgumentException if the getter is {@code null} + */ + default & Serializable> ConfigInstanceBuilder withOptional(F getter, + long value) { + return with(getter, OptionalLong.of(value)); + } + + /** + * Set an optional property on the configuration object to a floating-point value. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @param the accessor type + * @return this builder (not {@code null}) + * @throws IllegalArgumentException if the getter is {@code null} + */ + default & Serializable> ConfigInstanceBuilder withOptional(F getter, + double value) { + return with(getter, OptionalDouble.of(value)); + } + + /** + * Set an optional property on the configuration object to a boolean value. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @param the accessor type + * @return this builder (not {@code null}) + * @throws IllegalArgumentException if the getter is {@code null} + */ + default > & Serializable> ConfigInstanceBuilder withOptional(F getter, + boolean value) { + return with(getter, Optional.of(Boolean.valueOf(value))); + } + + /** + * Set a property to its default value (if any). + * + * @param getter the property to modify (must not be {@code null}) + * @param the value type + * @param the accessor type + * @return this builder (not {@code null}) + * @throws IllegalArgumentException if the getter is {@code null} + */ + & Serializable> ConfigInstanceBuilder withDefaultFor(F getter); + + /** + * Set a property to its default value (if any). + * + * @param getter the property to modify (must not be {@code null}) + * @param the accessor type + * @return this builder (not {@code null}) + * @throws IllegalArgumentException if the getter is {@code null} + */ + & Serializable> ConfigInstanceBuilder withDefaultFor(F getter); + + /** + * Set a property to its default value (if any). + * + * @param getter the property to modify (must not be {@code null}) + * @param the accessor type + * @return this builder (not {@code null}) + * @throws IllegalArgumentException if the getter is {@code null} + */ + & Serializable> ConfigInstanceBuilder withDefaultFor(F getter); + + /** + * Set a property to its default value (if any). + * + * @param getter the property to modify (must not be {@code null}) + * @param the accessor type + * @return this builder (not {@code null}) + * @throws IllegalArgumentException if the getter is {@code null} + */ + & Serializable> ConfigInstanceBuilder withDefaultFor(F getter); + + /** + * Set a property on the configuration object to a string value. + * The value set on the property will be the result of conversion of the string + * using the property's converter. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @return this builder (not {@code null}) + * @param the accessor type + * @throws IllegalArgumentException if the getter is {@code null}, + * or if the value is {@code null}, + * or if the value was rejected by the converter + */ + & Serializable> ConfigInstanceBuilder withString(F getter, String value); + + /** + * Set a property on the configuration object to a string value. + * The value set on the property will be the result of conversion of the string + * using the property's converter. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @return this builder (not {@code null}) + * @param the accessor type + * @throws IllegalArgumentException if the getter is {@code null}, + * or if the value is {@code null}, + * or if the value was rejected by the converter + */ + & Serializable> ConfigInstanceBuilder withString(F getter, String value); + + /** + * Set a property on the configuration object to a string value. + * The value set on the property will be the result of conversion of the string + * using the property's converter. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @return this builder (not {@code null}) + * @param the accessor type + * @throws IllegalArgumentException if the getter is {@code null}, + * or if the value is {@code null}, + * or if the value was rejected by the converter + */ + & Serializable> ConfigInstanceBuilder withString(F getter, String value); + + /** + * Set a property on the configuration object to a string value. + * The value set on the property will be the result of conversion of the string + * using the property's converter. + * + * @param getter the property accessor (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @return this builder (not {@code null}) + * @param the accessor type + * @throws IllegalArgumentException if the getter is {@code null}, + * or if the value is {@code null}, + * or if the value was rejected by the converter + */ + & Serializable> ConfigInstanceBuilder withString(F getter, String value); + + /** + * Set a property on the configuration object to a string value, using the property's + * declaring class and name to identify the property to set. + * The value set on the property will be the result of conversion of the string + * using the property's converter. + * + * @param propertyClass the declaring class of the property to set (must not be {@code null}) + * @param propertyName the name of the property to set (must not be {@code null}) + * @param value the value to set (must not be {@code null}) + * @return this builder (not {@code null}) + * @throws IllegalArgumentException if the property class or name is {@code null}, + * or if the value is {@code null}, + * or if the value was rejected by the converter, + * or if no property matches the given name and declaring class + */ + ConfigInstanceBuilder withString(Class propertyClass, String propertyName, String value); + + /** + * Build the configuration instance. + * + * @return the configuration instance (not {@code null}) + * @throws IllegalArgumentException if a required property does not have a value + */ + I build(); + + /** + * Get a builder instance for the given configuration interface. + * + * @param interfaceClass the interface class object (must not be {@code null}) + * @param the configuration interface type + * @return a new builder for the configuration interface (not {@code null}) + * @throws IllegalArgumentException if the interface class is {@code null}, + * or if the class object does not represent an interface, + * or if the interface is not a valid configuration interface, + * or if the interface has one or more required properties that were not given a value, + * or if the interface has one or more converters that could not be instantiated + * @throws SecurityException if this class does not have permission to introspect the given interface + * or one of its superinterfaces + */ + static ConfigInstanceBuilder forInterface(Class interfaceClass) + throws IllegalArgumentException, SecurityException { + return AbstractConfigInstanceBuilder.forInterface(interfaceClass); + } +} diff --git a/implementation/src/main/java/io/smallrye/config/ConfigMessages.java b/implementation/src/main/java/io/smallrye/config/ConfigMessages.java index 10b442b5c..a7719b9ee 100644 --- a/implementation/src/main/java/io/smallrye/config/ConfigMessages.java +++ b/implementation/src/main/java/io/smallrye/config/ConfigMessages.java @@ -169,4 +169,27 @@ IllegalArgumentException converterException(@Cause Throwable converterException, @Message(id = 50, value = "%s in %s does not map to any root") IllegalStateException propertyDoesNotMapToAnyRoot(String name, String location); + + default SecurityException accessDenied(Class ourClass, Class targetType) { + Module ourModule = ourClass.getModule(); + Module targetModule = targetType.getModule(); + assert targetModule.isNamed(); // otherwise we wouldn't be here + if (ourModule.isNamed()) { + return accessDenied(ourModule.getName(), targetType, targetType.getPackageName(), targetModule.getName()); + } else { + return accessDenied(targetType, targetType.getPackageName(), targetModule.getName()); + } + } + + @Message(id = 51, value = "Access to %2$s was denied in a modular environment. To avoid this error, edit " + + "`module-info.java` of %4$s to include `opens %3$s to %1$s`; or, add `--add-opens=%4$s/%3$s=%1$s` to " + + "the JVM command line.") + SecurityException accessDenied(String ourModuleName, Class targetType, String targetPackage, String targetModuleName); + + @Message(id = 51, value = "Access to %1$s was denied in a mixed-module environment. To avoid this error, " + + "add `--add-opens=%3$s/%2$s=ALL-UNNAMED` to the JVM command line.") + SecurityException accessDenied(Class targetType, String targetPackage, String targetModuleName); + + @Message(id = 52, value = "Missing a valid constructor on configuration implementation %s") + IllegalStateException noConstructor(Class implClass); }