Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -225,13 +224,7 @@ private static Map<String, ExcelContentProperty> doDeclaredFieldContentMap(Class
if (clazz == null) {
return null;
}
List<Field> tempFieldList = new ArrayList<>();
Class<?> tempClass = clazz;
while (tempClass != null) {
Collections.addAll(tempFieldList, tempClass.getDeclaredFields());
// Get the parent class and give it to yourself
tempClass = tempClass.getSuperclass();
}
List<Field> tempFieldList = FieldUtils.resolveFields(clazz);

ContentStyle parentContentStyle = clazz.getAnnotation(ContentStyle.class);
ContentFontStyle parentContentFontStyle = clazz.getAnnotation(ContentFontStyle.class);
Expand Down Expand Up @@ -305,20 +298,8 @@ public static FieldCache declaredFields(Class<?> clazz, ConfigurationHolder conf
}

private static FieldCache doDeclaredFields(Class<?> clazz, ConfigurationHolder configurationHolder) {
List<Field> tempFieldList = new ArrayList<>();
Map<String, Field> fieldNameToField = new HashMap<>();
Class<?> tempClass = clazz;
// Prefer subclass fields, only process the bottom-most (subclass) definition for fields with the same name
while (tempClass != null) {
for (Field field : tempClass.getDeclaredFields()) {
String fieldName = FieldUtils.resolveCglibFieldName(field);
if (!fieldNameToField.containsKey(fieldName)) {
fieldNameToField.put(fieldName, field);
tempFieldList.add(field);
}
}
tempClass = tempClass.getSuperclass();
}
List<Field> tempFieldList = FieldUtils.resolveFields(clazz);

ExcelIgnoreUnannotated excelIgnoreUnannotated = clazz.getAnnotation(ExcelIgnoreUnannotated.class);
Set<String> ignoreSet = new HashSet<>();
// First collect all field names annotated with ExcelIgnore (including subclass overrides)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,20 @@

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.fesod.common.util.MemberUtils;
import org.apache.fesod.common.util.StringUtils;
import org.apache.fesod.shaded.cglib.beans.BeanMap;
import org.apache.fesod.sheet.metadata.NullObject;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class FieldUtils {

public static Class<?> nullObjectClass = NullObject.class;
Expand Down Expand Up @@ -178,4 +186,33 @@ public static Field getField(final Class<?> cls, final String fieldName, final b
}
return match;
}

/**
* Resolves and retrieves all declared fields from the specified class and its inheritance hierarchy.
* If a field with the same name (resolved by CGLIB) is declared in both a subclass and a superclass,
* the subclass field definition takes precedence.
*
* @param cls the target {@link Class}, must not be {@code null}
* @return a {@link List} containing all resolved fields, or an empty list if no fields are found
*/
public static List<Field> resolveFields(Class<?> cls) {
Validate.isTrue(cls != null, "The class must not be null");

List<Field> result = new ArrayList<>();
Set<String> fieldNames = new HashSet<>();
Class<?> tempClass = cls;
while (tempClass != null && tempClass != Object.class) {
for (Field field : tempClass.getDeclaredFields()) {
boolean shouldSkip = Modifier.isStatic(field.getModifiers())
|| field.isSynthetic()
|| !fieldNames.add(resolveCglibFieldName(field));
if (shouldSkip) {
continue;
}
result.add(field);
}
tempClass = tempClass.getSuperclass();
}
return Collections.unmodifiableList(result);
}
}
Loading
Loading