From f001fd72d21871b9175c7beaf88bbed6e483ae45 Mon Sep 17 00:00:00 2001 From: shotishu Date: Sun, 28 Apr 2024 22:41:28 -0400 Subject: [PATCH] Update FixedLength.java --- .../vitaliy/fixedlength/FixedLength.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/java/name/velikodniy/vitaliy/fixedlength/FixedLength.java b/src/main/java/name/velikodniy/vitaliy/fixedlength/FixedLength.java index 9b9a7c2..32e598a 100644 --- a/src/main/java/name/velikodniy/vitaliy/fixedlength/FixedLength.java +++ b/src/main/java/name/velikodniy/vitaliy/fixedlength/FixedLength.java @@ -53,7 +53,7 @@ private FixedFormatLine classToLineDesc(final Class clazz) { if (annotation != null) { fixedFormatLine.startsWith = annotation.startsWith(); } - for (Field field : clazz.getDeclaredFields()) { + for (Field field : getAllFields(clazz)) { FixedField fieldAnnotation = field.getDeclaredAnnotation(FixedField.class); if (fieldAnnotation == null) { continue; @@ -71,6 +71,17 @@ private FixedFormatLine classToLineDesc(final Class clazz) { return fixedFormatLine; } + List getAllFields(final Class clazz) { + if (clazz == null) { + return Collections.emptyList(); + } + + List result = new ArrayList<>(getAllFields(clazz.getSuperclass())); + List filteredFields = Arrays.stream(clazz.getDeclaredFields()).collect(Collectors.toList()); + result.addAll(filteredFields); + return result; + } + public FixedLength registerLineType(final Class lineClass) { lineTypes.add(classToLineDesc(lineClass)); return this; @@ -306,7 +317,8 @@ public String format(List lines) { for (T line : lines) { - Arrays.stream(line.getClass().getDeclaredFields()) + getAllFields(line.getClass()) + .stream() .filter( f -> f.getAnnotation(FixedField.class) != null