Skip to content

Commit 573fcb1

Browse files
Alexandre Dutraolim7t
authored andcommitted
JAVA-2325: Allow "is" prefix for boolean getters in mapped entities
1 parent 57d42a3 commit 573fcb1

3 files changed

Lines changed: 58 additions & 4 deletions

File tree

changelog/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### 4.2.0 (in progress)
66

7+
- [bug] JAVA-2325: Allow "is" prefix for boolean getters in mapped entities
78
- [improvement] JAVA-2308: Add customWhereClause to `@Delete`
89
- [improvement] JAVA-2247: PagingIterable implementations should implement spliterator()
910
- [bug] JAVA-2312: Handle UDTs with names that clash with collection types

mapper-processor/src/main/java/com/datastax/oss/driver/internal/mapper/processor/entity/DefaultEntityFactory.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,21 +102,38 @@ public EntityDefinition getDefinition(TypeElement classElement) {
102102
continue;
103103
}
104104
ExecutableElement getMethod = (ExecutableElement) child;
105-
String getMethodName = getMethod.getSimpleName().toString();
106-
if (!getMethodName.startsWith("get") || !getMethod.getParameters().isEmpty()) {
105+
if (!getMethod.getParameters().isEmpty()) {
107106
continue;
108107
}
109108
TypeMirror typeMirror = getMethod.getReturnType();
110109
if (typeMirror.getKind() == TypeKind.VOID) {
111110
continue;
112111
}
113-
String propertyName = Introspector.decapitalize(getMethodName.substring(3));
112+
113+
String getMethodName = getMethod.getSimpleName().toString();
114+
boolean regularGetterName = getMethodName.startsWith("get");
115+
boolean booleanGetterName =
116+
getMethodName.startsWith("is")
117+
&& (typeMirror.getKind() == TypeKind.BOOLEAN
118+
|| context.getClassUtils().isSame(typeMirror, Boolean.class));
119+
if (!regularGetterName && !booleanGetterName) {
120+
continue;
121+
}
122+
123+
String propertyName;
124+
String setMethodName;
125+
if (regularGetterName) {
126+
propertyName = Introspector.decapitalize(getMethodName.substring(3));
127+
setMethodName = getMethodName.replaceFirst("get", "set");
128+
} else {
129+
propertyName = Introspector.decapitalize(getMethodName.substring(2));
130+
setMethodName = getMethodName.replaceFirst("is", "set");
131+
}
114132
// skip properties we've already encountered.
115133
if (encounteredPropertyNames.contains(propertyName)) {
116134
continue;
117135
}
118136

119-
String setMethodName = getMethodName.replaceFirst("get", "set");
120137
ExecutableElement setMethod = findSetMethod(typeHierarchy, setMethodName, typeMirror);
121138
if (setMethod == null) {
122139
continue; // must have both

mapper-processor/src/test/java/com/datastax/oss/driver/internal/mapper/processor/entity/EntityAnnotationTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.datastax.oss.driver.api.mapper.annotations.Entity;
2121
import com.datastax.oss.driver.internal.mapper.processor.MapperProcessorTest;
22+
import com.google.common.truth.StringSubject;
2223
import com.google.testing.compile.Compilation;
2324
import com.squareup.javapoet.ClassName;
2425
import com.squareup.javapoet.MethodSpec;
@@ -64,6 +65,41 @@ public void should_work_on_nested_class() {
6465
.contains("class Foo_BarHelper__MapperGenerated extends EntityHelperBase<Foo.Bar>");
6566
}
6667

68+
@Test
69+
public void should_detect_boolean_getter() {
70+
Compilation compilation =
71+
compileWithMapperProcessor(
72+
"test",
73+
TypeSpec.classBuilder(ClassName.get("test", "Foo"))
74+
.addModifiers(Modifier.PUBLIC)
75+
.addType(
76+
TypeSpec.classBuilder("Bar")
77+
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
78+
.addAnnotation(Entity.class)
79+
// Dummy getter and setter to have at least one mapped property
80+
.addMethod(
81+
MethodSpec.methodBuilder("setBool")
82+
.addParameter(TypeName.BOOLEAN, "bool")
83+
.addModifiers(Modifier.PUBLIC)
84+
.build())
85+
.addMethod(
86+
MethodSpec.methodBuilder("isBool")
87+
.returns(TypeName.BOOLEAN)
88+
.addModifiers(Modifier.PUBLIC)
89+
.addStatement("return true")
90+
.build())
91+
.build())
92+
.build());
93+
assertThat(compilation).succeededWithoutWarnings();
94+
StringSubject contents =
95+
assertThat(compilation)
96+
.generatedFile(
97+
StandardLocation.SOURCE_OUTPUT, "test", "Foo_BarHelper__MapperGenerated.java")
98+
.contentsAsUtf8String();
99+
contents.contains("target = target.setBoolean(\"bool\", entity.isBool())");
100+
contents.contains("returnValue.setBool(source.getBoolean(\"bool\"))");
101+
}
102+
67103
@Test
68104
public void should_fail_on_interface() {
69105
should_fail_with_expected_error(

0 commit comments

Comments
 (0)