< prev index next >

test/jdk/lib/testlibrary/jdk/testlibrary/OptimalCapacity.java

Print this page
rev 49518 : [mq]: 8200696-Optimal-initial-capacity-of-java-lang-Class-enumConstantDirectory

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -100,14 +100,38 @@
      * </pre>
      */
     public static void ofHashMap(Class<?> clazz, String fieldName,
             int initialCapacity)
     {
+        ofHashMap(clazz, null, fieldName, initialCapacity);
+    }
+
+    /**
+     * Checks adequacy of the initial capacity of a non-static field
+     * of type {@code HashMap}.
+     *
+     * Having
+     * <pre>
+     * class XClass {
+     *     HashMap theMap = new HashMap(N);
+     * }
+     * XClass instance = ...
+     * </pre>
+     *
+     * you should call from the test
+     *
+     * <pre>
+     * OptimalCapacity.ofHashMap(XClass.class, instance, "theMap", N);
+     * </pre>
+     */
+    public static void ofHashMap(Class<?> clazz, Object instance,
+            String fieldName, int initialCapacity)
+    {
         try {
             Field field = clazz.getDeclaredField(fieldName);
             field.setAccessible(true);
-            Object obj = field.get(null);
+            Object obj = field.get(instance);
             if (!HashMap.class.equals(obj.getClass())) {
                 throw new RuntimeException(field +
                     " expected to be of type HashMap");
             }
             HashMap<?,?> map = (HashMap<?,?>)obj;
< prev index next >