Skip to content

Commit 984d64f

Browse files
tolbertamolim7t
authored andcommitted
JAVA-2286: Improve error reporting when entity has no properties
Motivation: Currently if an Entity-annotated class lacks any properties, code generation produces code that doesn't compile because no properties are specified using QueryBuilder. To rectify this, preemptively check to see if an Entity lacks properties when generating its definition. Modifications: Update DefaultEntityFactory.getDefinition to generate an error if no properties are specified on an Entity-annotated class. Result: A more appropriate compiler error is now generated informing the user that their Entity-annotated class lacks properties.
1 parent 32460db commit 984d64f

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,15 @@ public EntityDefinition getDefinition(TypeElement classElement) {
186186
}
187187
}
188188

189+
if (encounteredPropertyNames.isEmpty()) {
190+
context
191+
.getMessager()
192+
.error(
193+
classElement,
194+
"@%s-annotated class must have at least one property defined.",
195+
Entity.class.getSimpleName());
196+
}
197+
189198
String entityName = Introspector.decapitalize(classElement.getSimpleName().toString());
190199
String defaultKeyspace = classElement.getAnnotation(Entity.class).defaultKeyspace();
191200

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,19 @@ public static Object[][] entitiesWithErrors() {
307307
.build())
308308
.build(),
309309
},
310+
{
311+
"@Entity-annotated class must have at least one property defined.",
312+
TypeSpec.classBuilder(ClassName.get("test", "Product"))
313+
.addAnnotation(Entity.class)
314+
.addField(FieldSpec.builder(UUID.class, "id", Modifier.PRIVATE).build())
315+
.addMethod(
316+
MethodSpec.methodBuilder("getId")
317+
.returns(UUID.class)
318+
.addModifiers(Modifier.PUBLIC)
319+
.addStatement("return id")
320+
.build())
321+
.build(),
322+
},
310323
};
311324
}
312325
}

0 commit comments

Comments
 (0)