mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 14:54:52 +02:00
8186961: Class.getFields() does not return fields of previously visited super interfaces/classes
Reviewed-by: mchung, redestad
This commit is contained in:
parent
da3e01d1ce
commit
3e26f1114d
2 changed files with 110 additions and 24 deletions
|
@ -53,12 +53,11 @@ import java.util.ArrayList;
|
|||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
import jdk.internal.HotSpotIntrinsicCandidate;
|
||||
|
@ -1771,7 +1770,7 @@ public final class Class<T> implements java.io.Serializable,
|
|||
if (sm != null) {
|
||||
checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true);
|
||||
}
|
||||
return copyFields(privateGetPublicFields(null));
|
||||
return copyFields(privateGetPublicFields());
|
||||
}
|
||||
|
||||
|
||||
|
@ -3026,7 +3025,7 @@ public final class Class<T> implements java.io.Serializable,
|
|||
// Returns an array of "root" fields. These Field objects must NOT
|
||||
// be propagated to the outside world, but must instead be copied
|
||||
// via ReflectionFactory.copyField.
|
||||
private Field[] privateGetPublicFields(Set<Class<?>> traversedInterfaces) {
|
||||
private Field[] privateGetPublicFields() {
|
||||
Field[] res;
|
||||
ReflectionData<T> rd = reflectionData();
|
||||
if (rd != null) {
|
||||
|
@ -3034,35 +3033,25 @@ public final class Class<T> implements java.io.Serializable,
|
|||
if (res != null) return res;
|
||||
}
|
||||
|
||||
// No cached value available; compute value recursively.
|
||||
// Traverse in correct order for getField().
|
||||
List<Field> fields = new ArrayList<>();
|
||||
if (traversedInterfaces == null) {
|
||||
traversedInterfaces = new HashSet<>();
|
||||
}
|
||||
// Use a linked hash set to ensure order is preserved and
|
||||
// fields from common super interfaces are not duplicated
|
||||
LinkedHashSet<Field> fields = new LinkedHashSet<>();
|
||||
|
||||
// Local fields
|
||||
Field[] tmp = privateGetDeclaredFields(true);
|
||||
addAll(fields, tmp);
|
||||
addAll(fields, privateGetDeclaredFields(true));
|
||||
|
||||
// Direct superinterfaces, recursively
|
||||
for (Class<?> c : getInterfaces()) {
|
||||
if (!traversedInterfaces.contains(c)) {
|
||||
traversedInterfaces.add(c);
|
||||
addAll(fields, c.privateGetPublicFields(traversedInterfaces));
|
||||
}
|
||||
for (Class<?> si : getInterfaces()) {
|
||||
addAll(fields, si.privateGetPublicFields());
|
||||
}
|
||||
|
||||
// Direct superclass, recursively
|
||||
if (!isInterface()) {
|
||||
Class<?> c = getSuperclass();
|
||||
if (c != null) {
|
||||
addAll(fields, c.privateGetPublicFields(traversedInterfaces));
|
||||
}
|
||||
Class<?> sc = getSuperclass();
|
||||
if (sc != null) {
|
||||
addAll(fields, sc.privateGetPublicFields());
|
||||
}
|
||||
|
||||
res = new Field[fields.size()];
|
||||
fields.toArray(res);
|
||||
res = fields.toArray(new Field[0]);
|
||||
if (rd != null) {
|
||||
rd.publicFields = res;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue