Use most specific getter when generating metadata

This commit makes sure to use the most specific getter if more than
one candidate exists.

Closes gh-24002
This commit is contained in:
Stephane Nicoll
2020-11-05 15:24:14 +01:00
parent 006d4bc36d
commit 59ea7c11f6
5 changed files with 118 additions and 26 deletions

View File

@@ -38,6 +38,7 @@ import org.springframework.boot.configurationsample.simple.SimpleTypeProperties;
import org.springframework.boot.configurationsample.specific.AnnotatedGetter;
import org.springframework.boot.configurationsample.specific.BoxingPojo;
import org.springframework.boot.configurationsample.specific.BuilderPojo;
import org.springframework.boot.configurationsample.specific.DeprecatedLessPreciseTypePojo;
import org.springframework.boot.configurationsample.specific.DeprecatedUnrelatedMethodPojo;
import org.springframework.boot.configurationsample.specific.DoubleRegistrationProperties;
import org.springframework.boot.configurationsample.specific.EmptyDefaultValueProperties;
@@ -192,12 +193,22 @@ class ConfigurationMetadataAnnotationProcessorTests extends AbstractMetadataGene
}
@Test
void boxingOnSetter() {
void deprecatedWithLessPreciseType() {
Class<?> type = DeprecatedLessPreciseTypePojo.class;
ConfigurationMetadata metadata = compile(type);
assertThat(metadata).has(Metadata.withGroup("not.deprecated").fromSource(type));
assertThat(metadata).has(Metadata.withProperty("not.deprecated.flag", Boolean.class).withDefaultValue(false)
.withNoDeprecation().fromSource(type));
}
@Test
void typBoxing() {
Class<?> type = BoxingPojo.class;
ConfigurationMetadata metadata = compile(type);
assertThat(metadata).has(Metadata.withGroup("boxing").fromSource(type));
assertThat(metadata)
.has(Metadata.withProperty("boxing.flag", Boolean.class).withDefaultValue(false).fromSource(type));
assertThat(metadata).has(Metadata.withProperty("boxing.another-flag", Boolean.class).fromSource(type));
assertThat(metadata).has(Metadata.withProperty("boxing.counter", Integer.class).fromSource(type));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@@ -29,6 +29,8 @@ public class BoxingPojo {
private boolean flag;
private Boolean anotherFlag;
private Integer counter;
public boolean isFlag() {
@@ -40,6 +42,14 @@ public class BoxingPojo {
this.flag = flag;
}
public boolean isAnotherFlag() {
return Boolean.TRUE.equals(this.anotherFlag);
}
public void setAnotherFlag(boolean anotherFlag) {
this.anotherFlag = anotherFlag;
}
public Integer getCounter() {
return this.counter;
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2012-2020 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.specific;
import org.springframework.boot.configurationsample.ConfigurationProperties;
/**
* Demonstrate that deprecating accessor with not the same type is not taken into account
* to detect the deprecated flag.
*
* @author Stephane Nicoll
*/
@ConfigurationProperties("not.deprecated")
public class DeprecatedLessPreciseTypePojo {
private boolean flag;
@Deprecated
public Boolean getFlag() {
return this.flag;
}
public boolean isFlag() {
return this.flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
@Deprecated
public void setFlag(Boolean flag) {
this.flag = flag;
}
}