From 8683de127e1c175fbf1cbe8eb0109e5ec4164633 Mon Sep 17 00:00:00 2001 From: ChaseHsbrk Date: Thu, 26 Nov 2020 14:53:24 +0200 Subject: [PATCH] Allowing for better mapping of accessor methods Allowing for mapping the getter and setter methods through the Java object hierarchy in a recursive way that can cater for inherited class attributes and methods --- .../java/io/jsondb/CollectionMetaData.java | 27 +++++-------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/src/main/java/io/jsondb/CollectionMetaData.java b/src/main/java/io/jsondb/CollectionMetaData.java index 2223851..e2c82cd 100644 --- a/src/main/java/io/jsondb/CollectionMetaData.java +++ b/src/main/java/io/jsondb/CollectionMetaData.java @@ -71,26 +71,14 @@ public CollectionMetaData(String collectionName, Class clazz, String schemaVe this.collectionLock = new ReentrantReadWriteLock(); //Populate the class metadata - List fields = new ArrayList<>(); - Field[] startFields = clazz.getDeclaredFields(); - int totalFields = startFields.length; - fields.add(startFields); - while (clazz.getSuperclass() != Object.class) { - clazz = clazz.getSuperclass(); - Field[] toAdd = clazz.getDeclaredFields(); - totalFields += toAdd.length; - fields.add(toAdd); - } + setupClassMetadata(clazz); - int x = 0; - Field[] fs = new Field[totalFields]; - for (Field[] field : fields) { - for (Field value : field) { - fs[x] = value; - x++; - } - } + this.idAnnotatedFieldGetterMethod = getterMethodMap.get(idAnnotatedFieldName); + this.idAnnotatedFieldSetterMethod = setterMethodMap.get(idAnnotatedFieldName); + } + private void setupClassMetadata(final Class clazz) { + Field[] fs = clazz.getDeclaredFields(); Method[] ms = clazz.getDeclaredMethods(); for (Field f : fs) { String fieldName = f.getName(); @@ -119,8 +107,7 @@ public CollectionMetaData(String collectionName, Class clazz, String schemaVe } } } - this.idAnnotatedFieldGetterMethod = getterMethodMap.get(idAnnotatedFieldName); - this.idAnnotatedFieldSetterMethod = setterMethodMap.get(idAnnotatedFieldName); + if (clazz.getSuperclass() != Object.class) setupClassMetadata(clazz.getSuperclass()); } protected ReentrantReadWriteLock getCollectionLock() {