Skip to content

Commit 6ed550e

Browse files
authored
Merge pull request #27 from alexstaeding/master
Adds support for Id field in pojo superclass
2 parents c49654d + ca22847 commit 6ed550e

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/main/java/io/jsondb/CollectionMetaData.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,26 @@ public CollectionMetaData(String collectionName, Class<?> clazz, String schemaVe
7171
this.collectionLock = new ReentrantReadWriteLock();
7272

7373
//Populate the class metadata
74-
Field[] fs = clazz.getDeclaredFields();
74+
List<Field[]> fields = new ArrayList<>();
75+
Field[] startFields = clazz.getDeclaredFields();
76+
int totalFields = startFields.length;
77+
fields.add(startFields);
78+
while (clazz.getSuperclass() != Object.class) {
79+
clazz = clazz.getSuperclass();
80+
Field[] toAdd = clazz.getDeclaredFields();
81+
totalFields += toAdd.length;
82+
fields.add(toAdd);
83+
}
84+
85+
int x = 0;
86+
Field[] fs = new Field[totalFields];
87+
for (Field[] field : fields) {
88+
for (Field value : field) {
89+
fs[x] = value;
90+
x++;
91+
}
92+
}
93+
7594
Method[] ms = clazz.getDeclaredMethods();
7695
for (Field f : fs) {
7796
String fieldName = f.getName();
@@ -115,9 +134,11 @@ public String getCollectionName() {
115134
public String getSchemaVersion() {
116135
return schemaVersion;
117136
}
137+
118138
public String getActualSchemaVersion() {
119139
return actualSchemaVersion;
120140
}
141+
121142
public void setActualSchemaVersion(String actualSchemaVersion) {
122143
this.actualSchemaVersion = actualSchemaVersion;
123144
int compareResult = schemaComparator.compare(schemaVersion, actualSchemaVersion);
@@ -132,6 +153,7 @@ public void setActualSchemaVersion(String actualSchemaVersion) {
132153
public Class getClazz() {
133154
return clazz;
134155
}
156+
135157
public String getIdAnnotatedFieldName() {
136158
return idAnnotatedFieldName;
137159
}
@@ -206,7 +228,7 @@ public static Map<String, CollectionMetaData> builder(JsonDBConfig dbConfig) {
206228
Map<String, CollectionMetaData> collectionMetaData = new LinkedHashMap<String, CollectionMetaData>();
207229
Reflections reflections = new Reflections(dbConfig.getBaseScanPackage());
208230
Set<Class<?>> docClasses = reflections.getTypesAnnotatedWith(Document.class);
209-
for(Class<?> c : docClasses) {
231+
for (Class<?> c : docClasses) {
210232
Document d = c.getAnnotation(Document.class);
211233
String collectionName = d.collection();
212234
String version = d.schemaVersion();

0 commit comments

Comments
 (0)