From 745a5e97585e6ebd009d4057cf32ec8136e82cff Mon Sep 17 00:00:00 2001 From: "David M. Lloyd" Date: Tue, 3 Oct 2023 14:48:52 -0500 Subject: [PATCH] [WIP] API and implementation for config instance builder Fixes #1001 --- .../config/ConfigInstanceBuilder.java | 320 ++++++++++++++ .../config/ConfigInstanceBuilderImpl.java | 418 ++++++++++++++++++ .../io/smallrye/config/ConfigMessages.java | 26 ++ 3 files changed, 764 insertions(+) create mode 100644 implementation/src/main/java/io/smallrye/config/ConfigInstanceBuilder.java create mode 100644 implementation/src/main/java/io/smallrye/config/ConfigInstanceBuilderImpl.java 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..2ec0e593b --- /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 ConfigInstanceBuilderImpl.forInterface(interfaceClass); + } +} diff --git a/implementation/src/main/java/io/smallrye/config/ConfigInstanceBuilderImpl.java b/implementation/src/main/java/io/smallrye/config/ConfigInstanceBuilderImpl.java new file mode 100644 index 000000000..c0ae84adc --- /dev/null +++ b/implementation/src/main/java/io/smallrye/config/ConfigInstanceBuilderImpl.java @@ -0,0 +1,418 @@ +package io.smallrye.config; + +import static io.smallrye.config.ConfigMessages.msg; + +import java.io.Serializable; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; +import java.lang.invoke.SerializedLambda; +import java.lang.reflect.UndeclaredThrowableException; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.BiConsumer; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.function.Supplier; +import java.util.function.ToIntFunction; +import java.util.function.ToLongFunction; + +import org.eclipse.microprofile.config.spi.Converter; + +import io.smallrye.common.constraint.Assert; +import sun.reflect.ReflectionFactory; + +/** + * The implementation for configuration instance builders. + */ +final class ConfigInstanceBuilderImpl implements ConfigInstanceBuilder { + + /** + * Reflection factory, used for getting the serialized lambda information out of a getter reference. + */ + private static final ReflectionFactory rf = ReflectionFactory.getReflectionFactory(); + /** + * Stack walker for getting caller class, used for setter caching. + */ + private static final StackWalker sw = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE); + /** + * Our cached lookup object. + */ + private static final MethodHandles.Lookup myLookup = MethodHandles.lookup(); + /** + * Class value which holds the cached builder class instance. + */ + private static final ClassValue> builderFactories = new ClassValue<>() { + protected Supplier computeValue(final Class type) { + assert type.isInterface(); + String interfaceName = type.getName(); + MethodHandles.Lookup lookup; + try { + lookup = MethodHandles.privateLookupIn(type, myLookup); + } catch (IllegalAccessException e) { + throw msg.accessDenied(getClass(), type); + } + String implInternalName = interfaceName.replace('.', '/') + "$$SC_BuilderImpl"; + Class impl; + try { + impl = lookup.findClass(implInternalName); + } catch (ClassNotFoundException e) { + // generate the impl instead + throw new UnsupportedOperationException("Todo"); + } catch (IllegalAccessException e) { + throw msg.accessDenied(getClass(), type); + } + 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 (ConfigInstanceBuilderImpl) mh.invokeExact(); + } catch (RuntimeException | Error e) { + throw e; + } catch (Throwable e) { + throw new UndeclaredThrowableException(e); + } + }; + } + }; + /** + * Class value which holds the cached config class instance constructors. + */ + private static final ClassValue> configFactories = new ClassValue<>() { + protected Function computeValue(final Class type) { + assert type.isInterface(); + String interfaceName = type.getName(); + MethodHandles.Lookup lookup; + try { + lookup = MethodHandles.privateLookupIn(type, myLookup); + } catch (IllegalAccessException e) { + throw msg.accessDenied(getClass(), type); + } + String implInternalName = interfaceName.replace('.', '/') + "$$SC_BuilderImpl"; + Class impl; + try { + impl = lookup.findClass(implInternalName); + } catch (ClassNotFoundException e) { + // generate the impl instead + throw new UnsupportedOperationException("Todo"); + } catch (IllegalAccessException e) { + throw msg.accessDenied(getClass(), type); + } + MethodHandle mh; + Class builderClass = null; + if (true) + throw new UnsupportedOperationException("Not finished yet..."); + try { + mh = lookup.findConstructor(impl, MethodType.methodType(void.class, builderClass)); + } catch (NoSuchMethodException e) { + throw msg.noConstructor(impl); + } catch (IllegalAccessException e) { + throw msg.accessDenied(getClass(), impl); + } + // capture the constructor as a Function + return builder -> { + try { + return type.cast(mh.invokeExact(builder)); + } catch (RuntimeException | Error e) { + throw e; + } catch (Throwable e) { + throw new UndeclaredThrowableException(e); + } + }; + } + }; + + /** + * Class value that holds the cache of maps of method reference lambdas to their corresponding setter. + */ + private static final ClassValue>> setterMapsByCallingClass = new ClassValue<>() { + protected Map> computeValue(final Class type) { + return new ConcurrentHashMap<>(); + } + }; + + // ===================================== + + static ConfigInstanceBuilderImpl forInterface(Class configurationInterface) + throws IllegalArgumentException, SecurityException { + return new ConfigInstanceBuilderImpl(configurationInterface, builderFactories.get(configurationInterface).get()); + } + + // ===================================== + + private final Class configurationInterface; + private final MethodHandles.Lookup lookup; + private final Object builderObject; + + ConfigInstanceBuilderImpl(final Class configurationInterface, final Object builderObject) { + this.configurationInterface = configurationInterface; + try { + lookup = MethodHandles.privateLookupIn(builderObject.getClass(), myLookup); + } catch (IllegalAccessException e) { + throw msg.accessDenied(builderObject.getClass(), getClass()); + } + this.builderObject = builderObject; + } + + // ===================================== + + public Class configurationInterface() { + return configurationInterface; + } + + // ------------------------------------- + + public & Serializable> ConfigInstanceBuilder with(final F getter, final T value) { + Assert.checkNotNullParam("getter", getter); + Assert.checkNotNullParam("value", value); + Class callerClass = sw.getCallerClass(); + BiConsumer setter = getSetter(getter, callerClass); + setter.accept(builderObject, value); + return this; + } + + public & Serializable> ConfigInstanceBuilder with(final F getter, final int value) { + Assert.checkNotNullParam("getter", getter); + Class callerClass = sw.getCallerClass(); + BiConsumer setter = getSetter(getter, callerClass); + setter.accept(builderObject, Integer.valueOf(value)); + return this; + } + + public & Serializable> ConfigInstanceBuilder with(final F getter, + final long value) { + Assert.checkNotNullParam("getter", getter); + Class callerClass = sw.getCallerClass(); + BiConsumer setter = getSetter(getter, callerClass); + setter.accept(builderObject, Long.valueOf(value)); + return this; + } + + public & Serializable> ConfigInstanceBuilder with(final F getter, + final double value) { + Assert.checkNotNullParam("getter", getter); + Class callerClass = sw.getCallerClass(); + BiConsumer setter = getSetter(getter, callerClass); + setter.accept(builderObject, Double.valueOf(value)); + return this; + } + + public & Serializable> ConfigInstanceBuilder with(final F getter, final boolean value) { + Assert.checkNotNullParam("getter", getter); + Class callerClass = sw.getCallerClass(); + BiConsumer setter = getSetter(getter, callerClass); + setter.accept(builderObject, Boolean.valueOf(value)); + return this; + } + + // ------------------------------------- + + public & Serializable> ConfigInstanceBuilder withDefaultFor(final F getter) { + Assert.checkNotNullParam("getter", getter); + Class callerClass = sw.getCallerClass(); + Consumer resetter = getResetter(getter, callerClass); + resetter.accept(builderObject); + return this; + } + + public & Serializable> ConfigInstanceBuilder withDefaultFor(final F getter) { + Assert.checkNotNullParam("getter", getter); + Class callerClass = sw.getCallerClass(); + Consumer resetter = getResetter(getter, callerClass); + resetter.accept(builderObject); + return this; + } + + public & Serializable> ConfigInstanceBuilder withDefaultFor(final F getter) { + Assert.checkNotNullParam("getter", getter); + Class callerClass = sw.getCallerClass(); + Consumer resetter = getResetter(getter, callerClass); + resetter.accept(builderObject); + return this; + } + + public & Serializable> ConfigInstanceBuilder withDefaultFor(final F getter) { + Assert.checkNotNullParam("getter", getter); + Class callerClass = sw.getCallerClass(); + Consumer resetter = getResetter(getter, callerClass); + resetter.accept(builderObject); + return this; + } + + // ------------------------------------- + + public & Serializable> ConfigInstanceBuilder withString(final F getter, + final String value) { + return withString(getter, value, sw.getCallerClass()); + } + + public & Serializable> ConfigInstanceBuilder withString(final F getter, + final String value) { + return withString(getter, value, sw.getCallerClass()); + } + + public & Serializable> ConfigInstanceBuilder withString(final F getter, + final String value) { + return withString(getter, value, sw.getCallerClass()); + } + + public & Serializable> ConfigInstanceBuilder withString(final F getter, + final String value) { + return withString(getter, value, sw.getCallerClass()); + } + + private ConfigInstanceBuilderImpl withString(final Object getter, final String value, final Class callerClass) { + Assert.checkNotNullParam("getter", getter); + Assert.checkNotNullParam("value", value); + Converter converter = getConverter(getter, callerClass); + BiConsumer setter = getSetter(getter, callerClass); + setter.accept(builderObject, converter.convert(value)); + return this; + } + + // ------------------------------------- + + public ConfigInstanceBuilder withString(final Class propertyClass, final String propertyName, + final String value) { + throw new UnsupportedOperationException("Need class info registry"); + } + + // ------------------------------------- + + public I build() { + return configurationInterface.cast(configFactories.get(configurationInterface).apply(builderObject)); + } + + // ===================================== + + private Converter getConverter(final Object getter, final Class callerClass) { + throw new UnsupportedOperationException("Need class info registry"); + } + + private BiConsumer getSetter(final Object getter, final Class callerClass) { + Map> setterMap = setterMapsByCallingClass.get(callerClass); + BiConsumer setter = setterMap.get(getter); + if (setter == null) { + setter = setterMap.computeIfAbsent(getter, this::createSetter); + } + return setter; + } + + private BiConsumer createSetter(Object lambda) { + MethodHandle writeReplace = rf.writeReplaceForSerialization(lambda.getClass()); + if (writeReplace == null) { + throw msg.invalidGetter(); + } + Object replaced; + try { + replaced = writeReplace.invokeExact(lambda); + } catch (RuntimeException | Error e) { + throw e; + } catch (Throwable e) { + throw new UndeclaredThrowableException(e); + } + if (!(replaced instanceof SerializedLambda)) { + throw msg.invalidGetter(); + } + SerializedLambda sl = (SerializedLambda) replaced; + if (sl.getCapturedArgCount() != 0) { + throw msg.invalidGetter(); + } + String implClassName = sl.getImplClass(); + // TODO: check implClassName against the supertype hierarchy of the config interface using shared info mapping + String setterName = sl.getImplMethodName(); + Class type = parseReturnType(sl.getImplMethodSignature()); + return createSetterByName(setterName, type); + } + + private BiConsumer createSetterByName(final String setterName, final Class type) { + Class builderClass = builderObject.getClass(); + MethodHandle setter; + try { + setter = lookup.findVirtual(builderClass, setterName, MethodType.methodType(void.class, builderClass, type)); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw msg.accessDenied(getClass(), builderClass); + } + // adapt it to be an object consumer + MethodHandle castSetter = setter.asType(MethodType.methodType(void.class, builderClass, Object.class)); + return (builder, val) -> { + try { + castSetter.invokeExact(builderObject, builder, val); + } catch (RuntimeException | Error e) { + throw e; + } catch (Throwable e) { + throw new UndeclaredThrowableException(e); + } + }; + } + + private Consumer getResetter(final Object getter, final Class callerClass) { + throw new UnsupportedOperationException("Unsupported for now"); + } + + private Class parseReturnType(final String signature) { + int idx = signature.lastIndexOf(')'); + if (idx == -1) { + throw new IllegalStateException("Unexpected invalid signature"); + } + return parseType(signature, idx + 1, signature.length()); + } + + private Class parseType(String desc, int start, int end) { + switch (desc.charAt(start)) { + case 'L': { + return parseClassName(desc, start + 1, end - 1); + } + case '[': { + return parseType(desc, start + 1, end).arrayType(); + } + case 'B': { + return byte.class; + } + case 'C': { + return char.class; + } + case 'D': { + return double.class; + } + case 'F': { + return float.class; + } + case 'I': { + return int.class; + } + case 'J': { + return long.class; + } + case 'S': { + return short.class; + } + case 'Z': { + return boolean.class; + } + default: { + throw msg.invalidGetter(); + } + } + } + + private Class parseClassName(final String signature, final int start, final int end) { + try { + return lookup.findClass(signature.substring(start, end)); + } catch (ClassNotFoundException e) { + throw msg.invalidGetter(); + } catch (IllegalAccessException e) { + throw msg.accessDenied(getClass(), builderObject.getClass()); + } + } +} diff --git a/implementation/src/main/java/io/smallrye/config/ConfigMessages.java b/implementation/src/main/java/io/smallrye/config/ConfigMessages.java index 10b442b5c..a038be2a6 100644 --- a/implementation/src/main/java/io/smallrye/config/ConfigMessages.java +++ b/implementation/src/main/java/io/smallrye/config/ConfigMessages.java @@ -169,4 +169,30 @@ 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); + + @Message(id = 53, value = "The accessor for a configuration property is not valid") + IllegalArgumentException invalidGetter(); }