RFR (XXS) 8080535: (ch) Expected size of Character.UnicodeBlock.map is not optimal

Martin Buchholz martinrb at google.com
Wed May 20 19:57:21 UTC 2015


I'd like to contribute (the start of) a test that checks that static
HashMaps are not oversized.  I suggest using that consistently for static
HashMaps (and ArrayLists?) throughout the JDK:

import java.lang.reflect.*;
import java.util.*;

@SuppressWarnings({"unchecked", "rawtypes"})
public class StaticMapSize {

    static int tableLength(HashMap map) throws Throwable {
        Field tableField = HashMap.class.getDeclaredField("table");
        tableField.setAccessible(true);
        return ((Object[]) tableField.get(map)).length;
    }

    public static void main(String[] args) throws Throwable {
        Class<?>[] klazzes =
Class.forName("java.lang.Character").getDeclaredClasses();
        for (Class<?> klazz : klazzes) {
            for (Field field : klazz.getDeclaredFields()) {
                field.setAccessible(true);
                if (Modifier.isStatic(field.getModifiers())) {
                    Object x = field.get(null);
                    if (x instanceof HashMap) {
                        System.out.println(field);
                        HashMap map = (HashMap) x;
                        HashMap copy = new HashMap(map);
                        if (tableLength(map) != tableLength(copy)) {
                            String msg = String.format
                                ("map %s has excess capacity: need %d; use
%d%n",
                                 field.toString(), tableLength(copy),
tableLength(map));
                            throw new AssertionError(msg);
                        }
                    }
                }
            }
        }
    }
}



More information about the core-libs-dev mailing list