A colleague noted that the answer is not in jvm, but in jdk, more precisely in the sun.reflect.Reflection class. There you will find a static initializer that does the following
static { Map<Class,String[]> map = new HashMap<Class,String[]>(); map.put(Reflection.class, new String[] {"fieldFilterMap", "methodFilterMap"}); map.put(System.class, new String[] {"security"}); fieldFilterMap = map; methodFilterMap = new HashMap<Class,String[]>(); }
If we now look a little closer to the getDeclaredFields method in java.lang.Class, we find that the fields are filtered by calling the Reflection class:
Reflection.filterFields(this, getDeclaredFields0(publicOnly));
where filterFields is implemented as
public static Field[] filterFields(Class containingClass, Field[] fields) { if (fieldFilterMap == null) { // Bootstrapping return fields; } return (Field[])filter(fields, fieldFilterMap.get(containingClass)); }
So, this solves the problem of protecting the field. However, I am still interested to know why this was implemented.
Arno mittelbach
source share