Skip to content

Commit 421f2fa

Browse files
committed
Fail fast when constructor bound and not compiled with -parameters
Closes gh-33182
1 parent e2dc359 commit 421f2fa

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/external-config.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,9 @@ include::code:nonnull/MyProperties[tag=*]
740740
NOTE: To use constructor binding the class must be enabled using `@EnableConfigurationProperties` or configuration property scanning.
741741
You cannot use constructor binding with beans that are created by the regular Spring mechanisms (for example `@Component` beans, beans created by using `@Bean` methods or beans loaded by using `@Import`)
742742

743+
NOTE: To use constructor binding in a native image the class must be compiled with `-parameters`.
744+
This will happen automatically if you use Spring Boot's Gradle plugin or if you use Maven and `spring-boot-starter-parent`.
745+
743746
NOTE: The use of `java.util.Optional` with `@ConfigurationProperties` is not recommended as it is primarily intended for use as a return type.
744747
As such, it is not well-suited to configuration property injection.
745748
For consistency with properties of other types, if you do declare an `Optional` property and it has no value, `null` rather than an empty `Optional` will be bound.

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/BindableRuntimeHintsRegistrar.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
import org.springframework.beans.BeanInfoFactory;
3838
import org.springframework.beans.ExtendedBeanInfoFactory;
3939
import org.springframework.boot.context.properties.NestedConfigurationProperty;
40+
import org.springframework.core.ParameterNameDiscoverer;
4041
import org.springframework.core.ResolvableType;
42+
import org.springframework.core.StandardReflectionParameterNameDiscoverer;
4143
import org.springframework.core.annotation.MergedAnnotations;
4244
import org.springframework.util.Assert;
4345
import org.springframework.util.ReflectionUtils;
@@ -71,8 +73,12 @@ public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
7173
}
7274

7375
public void registerHints(RuntimeHints hints) {
76+
Set<Class<?>> compiledWithoutParameters = new HashSet<>();
7477
for (Class<?> type : this.types) {
75-
new Processor(type).process(hints.reflection());
78+
new Processor(type, compiledWithoutParameters).process(hints.reflection());
79+
}
80+
if (!compiledWithoutParameters.isEmpty()) {
81+
throw new MissingParametersCompilerArgumentException(compiledWithoutParameters);
7682
}
7783
}
7884

@@ -87,6 +93,8 @@ public static BindableRuntimeHintsRegistrar forTypes(Class<?>... types) {
8793

8894
private final class Processor {
8995

96+
private static final ParameterNameDiscoverer PARAMETER_NAME_DISCOVERER = new StandardReflectionParameterNameDiscoverer();
97+
9098
private final Class<?> type;
9199

92100
private final Constructor<?> bindConstructor;
@@ -95,15 +103,19 @@ private final class Processor {
95103

96104
private final Set<Class<?>> seen;
97105

98-
Processor(Class<?> type) {
99-
this(type, false, new HashSet<>());
106+
private final Set<Class<?>> compiledWithoutParameters;
107+
108+
Processor(Class<?> type, Set<Class<?>> compiledWithoutParameters) {
109+
this(type, false, new HashSet<>(), compiledWithoutParameters);
100110
}
101111

102-
private Processor(Class<?> type, boolean nestedType, Set<Class<?>> seen) {
112+
private Processor(Class<?> type, boolean nestedType, Set<Class<?>> seen,
113+
Set<Class<?>> compiledWithoutParameters) {
103114
this.type = type;
104115
this.bindConstructor = BindConstructorProvider.DEFAULT.getBindConstructor(Bindable.of(type), nestedType);
105116
this.beanInfo = getBeanInfo(type);
106117
this.seen = seen;
118+
this.compiledWithoutParameters = compiledWithoutParameters;
107119
}
108120

109121
private static BeanInfo getBeanInfo(Class<?> beanType) {
@@ -135,13 +147,21 @@ else if (this.beanInfo != null) {
135147

136148
private void handleConstructor(ReflectionHints hints) {
137149
if (this.bindConstructor != null) {
150+
verifyParameterNamesAreAvailable();
138151
hints.registerConstructor(this.bindConstructor, ExecutableMode.INVOKE);
139152
return;
140153
}
141154
Arrays.stream(this.type.getDeclaredConstructors()).filter(this::hasNoParameters).findFirst()
142155
.ifPresent((constructor) -> hints.registerConstructor(constructor, ExecutableMode.INVOKE));
143156
}
144157

158+
private void verifyParameterNamesAreAvailable() {
159+
String[] parameterNames = PARAMETER_NAME_DISCOVERER.getParameterNames(this.bindConstructor);
160+
if (parameterNames == null) {
161+
this.compiledWithoutParameters.add(this.bindConstructor.getDeclaringClass());
162+
}
163+
}
164+
145165
private boolean hasNoParameters(Constructor<?> candidate) {
146166
return candidate.getParameterCount() == 0;
147167
}
@@ -205,7 +225,7 @@ else if (isNestedType(propertyName, propertyClass)) {
205225
}
206226

207227
private void processNested(Class<?> type, ReflectionHints hints) {
208-
new Processor(type, true, this.seen).process(hints);
228+
new Processor(type, true, this.seen, this.compiledWithoutParameters).process(hints);
209229
}
210230

211231
private Class<?> getComponentClass(ResolvableType type) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.context.properties.bind;
18+
19+
import java.util.Set;
20+
21+
/**
22+
* Exception thrown to indicate that a class has not been compiled with
23+
* {@code -parameters}.
24+
*
25+
* @author Andy Wilkinson
26+
*/
27+
class MissingParametersCompilerArgumentException extends RuntimeException {
28+
29+
MissingParametersCompilerArgumentException(Set<Class<?>> faultyClasses) {
30+
super(message(faultyClasses));
31+
}
32+
33+
private static String message(Set<Class<?>> faultyClasses) {
34+
StringBuilder message = new StringBuilder(String.format(
35+
"Constructor binding in a native image requires compilation with -parameters but the following classes were compiled without it:%n"));
36+
for (Class<?> faultyClass : faultyClasses) {
37+
message.append(String.format("\t%s%n", faultyClass.getName()));
38+
}
39+
return message.toString();
40+
}
41+
42+
}

0 commit comments

Comments
 (0)