Skip to content

Commit 4ccc7dc

Browse files
committed
Ignore invalid accessors
Previously, if a void method with a single argument was named "set", the annotation processor wrongly considered it was a setter candidate. This commit updates the condition to ignore it. Closes gh-5826
1 parent 5d55d57 commit 4ccc7dc

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/TypeElementMembers.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -110,14 +110,16 @@ private ExecutableElement getMatchingSetter(List<ExecutableElement> candidates,
110110

111111
private boolean isGetter(ExecutableElement method) {
112112
String name = method.getSimpleName().toString();
113-
return (name.startsWith("get") || name.startsWith("is"))
113+
return ((name.startsWith("get") && name.length() > 3)
114+
|| (name.startsWith("is") && name.length() > 2))
114115
&& method.getParameters().isEmpty()
115116
&& (TypeKind.VOID != method.getReturnType().getKind());
116117
}
117118

118119
private boolean isSetter(ExecutableElement method) {
119120
final String name = method.getSimpleName().toString();
120-
return name.startsWith("set") && method.getParameters().size() == 1
121+
return (name.startsWith("set") && name.length() > 3)
122+
&& method.getParameters().size() == 1
121123
&& (isSetterReturnType(method));
122124
}
123125

spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -60,12 +60,14 @@
6060
import org.springframework.boot.configurationsample.specific.InnerClassAnnotatedGetterConfig;
6161
import org.springframework.boot.configurationsample.specific.InnerClassProperties;
6262
import org.springframework.boot.configurationsample.specific.InnerClassRootConfig;
63+
import org.springframework.boot.configurationsample.specific.InvalidAccessorProperties;
6364
import org.springframework.boot.configurationsample.specific.SimplePojo;
6465
import org.springframework.util.FileCopyUtils;
6566

6667
import static org.hamcrest.CoreMatchers.is;
6768
import static org.hamcrest.Matchers.empty;
6869
import static org.hamcrest.Matchers.equalTo;
70+
import static org.hamcrest.Matchers.hasSize;
6971
import static org.hamcrest.Matchers.not;
7072
import static org.junit.Assert.assertFalse;
7173
import static org.junit.Assert.assertThat;
@@ -330,6 +332,13 @@ public void excludedTypesPojo() throws IOException {
330332
assertThat(metadata, not(containsProperty("excluded.writer-array")));
331333
}
332334

335+
@Test
336+
public void invalidAccessor() throws IOException {
337+
ConfigurationMetadata metadata = compile(InvalidAccessorProperties.class);
338+
assertThat(metadata, containsGroup("config"));
339+
assertThat(metadata.getItems(), hasSize(1));
340+
}
341+
333342
@Test
334343
public void lombokDataProperties() throws Exception {
335344
ConfigurationMetadata metadata = compile(LombokSimpleDataProperties.class);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2012-2016 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+
* http://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.configurationsample.specific;
18+
19+
import org.springframework.boot.configurationsample.ConfigurationProperties;
20+
21+
/**
22+
* Demonstrates that invalid accessors are ignored.
23+
*
24+
* @author Stephane Nicoll
25+
*/
26+
@ConfigurationProperties(prefix = "config")
27+
public class InvalidAccessorProperties {
28+
29+
private String name;
30+
private boolean flag;
31+
32+
public void set(String name) {
33+
this.name = name;
34+
}
35+
36+
public String get() {
37+
return this.name;
38+
}
39+
40+
public void setFlag(boolean flag) {
41+
this.flag = flag;
42+
}
43+
44+
public boolean is() {
45+
return this.flag;
46+
}
47+
48+
}

0 commit comments

Comments
 (0)