Skip to content

Commit 56c277b

Browse files
committed
Add support for inherited classes
1 parent d52ff0f commit 56c277b

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>cz.foresttech</groupId>
88
<artifactId>ForestDatabase</artifactId>
9-
<version>1.0.7</version>
9+
<version>1.0.8</version>
1010

1111
<properties>
1212
<maven.compiler.source>17</maven.compiler.source>

src/main/java/cz/foresttech/database/DatabaseEntityConvertor.java

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ public DatabaseEntityConvertor(DatabaseAPI databaseAPI) {
2323
* Converts a DBRow to an instance of the specified class.
2424
*
2525
* @param clazz the class of the object to be created.
26-
* @param row the DBRow object containing database column data.
26+
* @param row the DBRow object containing database column data.
2727
* @return an instance of T populated with data from the DBRow, or null in case of failure.
2828
*/
2929
public <T> T convertToEntity(Class<T> clazz, DBRow row) {
3030
try {
3131
T instance = clazz.getDeclaredConstructor().newInstance();
32-
Arrays.stream(clazz.getDeclaredFields())
32+
getDeclaredFields(clazz)
3333
.forEach(field -> populateFieldFromDBRow(instance, field, row));
3434
return instance;
3535
} catch (Exception e) {
@@ -42,8 +42,8 @@ public <T> T convertToEntity(Class<T> clazz, DBRow row) {
4242
* Populates a field of an instance with the corresponding value from a DBRow.
4343
*
4444
* @param instance the object instance whose field is to be populated.
45-
* @param field the field to be populated.
46-
* @param row the DBRow object containing database column data.
45+
* @param field the field to be populated.
46+
* @param row the DBRow object containing database column data.
4747
*/
4848
private <T> void populateFieldFromDBRow(T instance, Field field, DBRow row) {
4949
try {
@@ -84,8 +84,8 @@ private String getDatabaseColumnName(Field field) {
8484
/**
8585
* Gets the value for a field from a DBRow.
8686
*
87-
* @param field the field for which value is required.
88-
* @param row the DBRow containing the data.
87+
* @param field the field for which value is required.
88+
* @param row the DBRow containing the data.
8989
* @param dbName the name of the database column.
9090
* @return the value corresponding to the field from the DBRow.
9191
*/
@@ -172,7 +172,7 @@ public <T> String deleteAllScript(Class<T> clazz) {
172172
/**
173173
* Generates a SQL script to delete a specific record associated with a class instance.
174174
*
175-
* @param clazz the class for which the DELETE script is required.
175+
* @param clazz the class for which the DELETE script is required.
176176
* @param object the instance of the class to identify the record to be deleted.
177177
* @return a DELETE SQL script for a specific record of the given class.
178178
*/
@@ -189,7 +189,7 @@ public <T> String deleteScript(Class<T> clazz, T object) throws IllegalAccessExc
189189
/**
190190
* Generates an SQL script for inserting or updating a record based on a class instance.
191191
*
192-
* @param clazz the class for which the script is required.
192+
* @param clazz the class for which the script is required.
193193
* @param object the instance of the class for which the record is to be inserted or updated.
194194
* @return an INSERT or UPDATE SQL script for the given class instance.
195195
*/
@@ -229,15 +229,15 @@ public String generateCreateScript(Class<?> clazz) {
229229
/**
230230
* Processes fields of a class to generate a part of SQL script for delete operations.
231231
*
232-
* @param clazz the class whose fields are to be processed.
232+
* @param clazz the class whose fields are to be processed.
233233
* @param object the instance of the class.
234234
* @return a string representing a part of SQL script.
235235
*/
236236
private <T> String processDeleteConditionScript(Class<T> clazz, T object) throws IllegalAccessException {
237237
StringBuilder keys = new StringBuilder();
238238
StringBuilder values = new StringBuilder();
239239

240-
for (Field field : clazz.getDeclaredFields()) {
240+
for (Field field : getDeclaredFields(clazz)) {
241241
field.setAccessible(true);
242242

243243
boolean isPrimaryKey = field.isAnnotationPresent(PrimaryKey.class);
@@ -266,7 +266,7 @@ private <T> String processDeleteConditionScript(Class<T> clazz, T object) throws
266266
private <T> String getValuesFromField(Class<T> clazz, T object) throws IllegalAccessException {
267267
StringBuilder values = new StringBuilder();
268268

269-
for (Field field : clazz.getDeclaredFields()) {
269+
for (Field field : getDeclaredFields(clazz)) {
270270
field.setAccessible(true);
271271
Column column = field.getAnnotation(Column.class);
272272
if (column == null) continue;
@@ -286,7 +286,7 @@ private <T> String getValuesFromField(Class<T> clazz, T object) throws IllegalAc
286286
private <T> String getColumnsFromField(Class<T> clazz) {
287287
StringBuilder keys = new StringBuilder();
288288

289-
for (Field field : clazz.getDeclaredFields()) {
289+
for (Field field : getDeclaredFields(clazz)) {
290290
field.setAccessible(true);
291291
Column column = field.getAnnotation(Column.class);
292292
if (column == null) continue;
@@ -302,7 +302,7 @@ private <T> String getColumnsFromField(Class<T> clazz) {
302302
/**
303303
* Processes the value of a field for inclusion in an SQL script.
304304
*
305-
* @param value the value to be processed.
305+
* @param value the value to be processed.
306306
* @param processor the DatabaseValueProcessor for processing the value.
307307
* @return a string representation of the value suitable for SQL script.
308308
*/
@@ -340,7 +340,7 @@ private String getConflictPolicy(Class<?> clazz) {
340340
private String getFieldsDefinition(Class<?> clazz) {
341341
StringBuilder definition = new StringBuilder();
342342

343-
for (Field field : clazz.getDeclaredFields()) {
343+
for (Field field : getDeclaredFields(clazz)) {
344344
if (field.isAnnotationPresent(Column.class)) {
345345
Column column = field.getAnnotation(Column.class);
346346
String dbName = getDbName(field, column);
@@ -358,7 +358,7 @@ private String getFieldsDefinition(Class<?> clazz) {
358358
/**
359359
* Determines the SQL type of a field based on its annotations and type.
360360
*
361-
* @param field the field whose SQL type is needed.
361+
* @param field the field whose SQL type is needed.
362362
* @param column the Column annotation of the field.
363363
* @return a string representing the SQL type of the field.
364364
*/
@@ -431,7 +431,7 @@ private String getSqlType(Field field, Column column) {
431431
* @return a string representing the primary key constraint for SQL CREATE TABLE script.
432432
*/
433433
private String getPrimaryKeyConstraint(Class<?> clazz) {
434-
List<String> primaryKeys = Arrays.stream(clazz.getDeclaredFields())
434+
List<String> primaryKeys = getDeclaredFields(clazz).stream()
435435
.filter(f -> f.isAnnotationPresent(PrimaryKey.class))
436436
.map(f -> getDbName(f, f.getAnnotation(Column.class)))
437437
.collect(Collectors.toList());
@@ -450,7 +450,7 @@ private String getPrimaryKeyConstraint(Class<?> clazz) {
450450
* @return a string representing the primary key fields in snake case, or null if no primary keys are defined.
451451
*/
452452
private static String getPrimaryKey(Class<?> clazz) {
453-
return Arrays.stream(clazz.getDeclaredFields())
453+
return getDeclaredFields(clazz).stream()
454454
.filter(field -> field.isAnnotationPresent(PrimaryKey.class))
455455
.map(field -> {
456456
Column column = field.getAnnotation(Column.class);
@@ -487,7 +487,7 @@ private static String getTableName(Class<?> clazz) {
487487
/**
488488
* Retrieves the database column name for a given field, based on its annotations.
489489
*
490-
* @param field the field whose database column name is required.
490+
* @param field the field whose database column name is required.
491491
* @param column the Column annotation of the field.
492492
* @return the database column name in snake case.
493493
*/
@@ -519,4 +519,21 @@ private static String escapeString(String input) {
519519
return input.replace("'", "''");
520520
}
521521

522+
/**
523+
* Retrieves all declared fields of a class, including inherited fields.
524+
*
525+
* @param clazz the class whose fields are required.
526+
* @return a list of all declared fields of the class.
527+
*/
528+
private static List<Field> getDeclaredFields(Class<?> clazz) {
529+
List<Field> fieldList = new ArrayList<>();
530+
531+
if (clazz.getSuperclass() != null && clazz != Object.class) {
532+
fieldList.addAll(getDeclaredFields(clazz.getSuperclass()));
533+
}
534+
fieldList.addAll(Arrays.asList(clazz.getDeclaredFields()));
535+
536+
return fieldList;
537+
}
538+
522539
}

0 commit comments

Comments
 (0)