Commit 4b5ebd85 authored by Andy Wilkinson's avatar Andy Wilkinson

Process all non-private methods, not just public methods

Previously, the configuration processor would ignore any
@ConfigurationProperties-annotated methods that were not public. This
prevented metadata generation for package-private @Bean methods such
as those in DataSourceConfiguration's inner-classes for DBCP2, Hikari,
and Tomcat JDBC.

This commit updates the annotation processor so that it will process
any non-private method annotated with @ConfigurationProperties.

Fixes gh-18124
parent d5b289c0
......@@ -216,7 +216,8 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
}
private void processExecutableElement(String prefix, ExecutableElement element) {
if (element.getModifiers().contains(Modifier.PUBLIC) && (TypeKind.VOID != element.getReturnType().getKind())) {
if ((!element.getModifiers().contains(Modifier.PRIVATE))
&& (TypeKind.VOID != element.getReturnType().getKind())) {
Element returns = this.processingEnv.getTypeUtils().asElement(element.getReturnType());
if (returns instanceof TypeElement) {
ItemMetadata group = ItemMetadata.newGroup(prefix,
......
......@@ -24,7 +24,10 @@ import org.springframework.boot.configurationsample.method.DeprecatedMethodConfi
import org.springframework.boot.configurationsample.method.EmptyTypeMethodConfig;
import org.springframework.boot.configurationsample.method.InvalidMethodConfig;
import org.springframework.boot.configurationsample.method.MethodAndClassConfig;
import org.springframework.boot.configurationsample.method.SimpleMethodConfig;
import org.springframework.boot.configurationsample.method.PackagePrivateMethodConfig;
import org.springframework.boot.configurationsample.method.PrivateMethodConfig;
import org.springframework.boot.configurationsample.method.ProtectedMethodConfig;
import org.springframework.boot.configurationsample.method.PublicMethodConfig;
import static org.assertj.core.api.Assertions.assertThat;
......@@ -36,13 +39,32 @@ import static org.assertj.core.api.Assertions.assertThat;
class MethodBasedMetadataGenerationTests extends AbstractMetadataGenerationTests {
@Test
void simpleMethodConfig() {
ConfigurationMetadata metadata = compile(SimpleMethodConfig.class);
assertThat(metadata).has(Metadata.withGroup("foo").fromSource(SimpleMethodConfig.class));
void publicMethodConfig() {
methodConfig(PublicMethodConfig.class, PublicMethodConfig.Foo.class);
}
@Test
void protectedMethodConfig() {
methodConfig(ProtectedMethodConfig.class, ProtectedMethodConfig.Foo.class);
}
@Test
void packagePrivateMethodConfig() {
methodConfig(PackagePrivateMethodConfig.class, PackagePrivateMethodConfig.Foo.class);
}
private void methodConfig(Class<?> config, Class<?> properties) {
ConfigurationMetadata metadata = compile(config);
assertThat(metadata).has(Metadata.withGroup("foo").fromSource(config));
assertThat(metadata).has(Metadata.withProperty("foo.name", String.class).fromSource(properties));
assertThat(metadata)
.has(Metadata.withProperty("foo.name", String.class).fromSource(SimpleMethodConfig.Foo.class));
assertThat(metadata).has(Metadata.withProperty("foo.flag", Boolean.class).withDefaultValue(false)
.fromSource(SimpleMethodConfig.Foo.class));
.has(Metadata.withProperty("foo.flag", Boolean.class).withDefaultValue(false).fromSource(properties));
}
@Test
void privateMethodConfig() {
ConfigurationMetadata metadata = compile(PrivateMethodConfig.class);
assertThat(metadata).doesNotHave(Metadata.withGroup("foo"));
}
@Test
......
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.method;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Sample for testing package-private method configuration.
*
* @author Andy Wilkinson
*/
public class PackagePrivateMethodConfig {
@ConfigurationProperties(prefix = "foo")
Foo foo() {
return new Foo();
}
public static class Foo {
private String name;
private boolean flag;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public boolean isFlag() {
return this.flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
}
}
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.method;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Sample for testing private method configuration.
*
* @author Andy Wilkinson
*/
public class PrivateMethodConfig {
@ConfigurationProperties(prefix = "foo")
private Foo foo() {
return new Foo();
}
public static class Foo {
private String name;
private boolean flag;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public boolean isFlag() {
return this.flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
}
}
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.configurationsample.method;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Sample for testing protected method configuration.
*
* @author Andy Wilkinson
*/
public class ProtectedMethodConfig {
@ConfigurationProperties(prefix = "foo")
protected Foo foo() {
return new Foo();
}
public static class Foo {
private String name;
private boolean flag;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public boolean isFlag() {
return this.flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
}
}
......@@ -19,11 +19,11 @@ package org.springframework.boot.configurationsample.method;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Sample for testing simple method configuration.
* Sample for testing public method configuration.
*
* @author Stephane Nicoll
*/
public class SimpleMethodConfig {
public class PublicMethodConfig {
@ConfigurationProperties(prefix = "foo")
public Foo foo() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment