Skip to content

Commit

Permalink
Merge pull request #846 from paulcwarren/feature/classwalker-subclass…
Browse files Browse the repository at this point in the history
…-support

ClassWalker can now walk subclasses
  • Loading branch information
paulcwarren authored Apr 1, 2022
2 parents e3a4527 + 38f576e commit 2e6a8ec
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.springframework.content.commons.mappingcontext;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -24,7 +27,8 @@ public void accept(ContentPropertyBuilderVisitor visitor) {
return;
}

Field[] fields = this.klazz.getDeclaredFields();
List<Field>fields = new ArrayList<>();
fields = getAllFields(fields, klazz);
for (Field field : fields) {
fContinue &= visitor.visitField(field);
}
Expand All @@ -34,4 +38,14 @@ public void accept(ContentPropertyBuilderVisitor visitor) {

visitor.visitClassEnd(klazz);
}

private List<Field> getAllFields(List<Field> fields, Class<?> type) {
fields.addAll(Arrays.asList(type.getDeclaredFields()));

if (type.getSuperclass() != null) {
getAllFields(fields, type.getSuperclass());
}

return fields;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,22 @@ public class ClassWalkerTest {
assertThat(visitor.getProperties(), aMapWithSize(0));
});
});

Context("given a class with content properties in its super class", () -> {
It("should visit them", () -> {
ContentPropertyBuilderVisitor visitor = new ContentPropertyBuilderVisitor("/", ".", new ContentPropertyBuilderVisitor.CanonicalName());
ClassWalker walker = new ClassWalker(TestClass4.class);
walker.accept(visitor);

ContentProperty expectedProperty = new ContentProperty();
expectedProperty.setContentPropertyPath("content");
expectedProperty.setContentIdPropertyPath("contentId");
expectedProperty.setContentLengthPropertyPath("contentLength");
expectedProperty.setMimeTypePropertyPath("contentMimeType");
expectedProperty.setOriginalFileNamePropertyPath("contentOriginalFileName");
assertThat(visitor.getProperties(), hasEntry("content", expectedProperty));
});
});
}

public static class TestClass {
Expand Down Expand Up @@ -199,4 +215,7 @@ public static class TestClass3 {
@DBRef
private TestSubClass docRef;
}

public static class TestClass4 extends TestSubSubClass {
}
}

0 comments on commit 2e6a8ec

Please sign in to comment.