src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/utilities/SystemDictionaryHelper.java
author jjg
Mon, 20 May 2019 10:57:57 -0700
changeset 54950 46ae54c3026d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8223663: Update links for tool guides Reviewed-by: alanb, erikj, darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
46729
c62d2e8b2728 7133093: Improve system dictionary performance
coleenp
parents: 35217
diff changeset
     2
 * Copyright (c) 2002, 2017, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
package sun.jvm.hotspot.utilities;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
import java.util.*;
46729
c62d2e8b2728 7133093: Improve system dictionary performance
coleenp
parents: 35217
diff changeset
    28
import sun.jvm.hotspot.classfile.*;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
import sun.jvm.hotspot.oops.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
import sun.jvm.hotspot.memory.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
import sun.jvm.hotspot.runtime.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
public class SystemDictionaryHelper {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
   static {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
      VM.registerVMInitializedObserver(new Observer() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
         public void update(Observable o, Object data) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
            initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
      });
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
   private static synchronized void initialize() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
      klasses = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
   // Instance klass array sorted by name.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
   private static InstanceKlass[] klasses;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
   // side-effect!. caches the instance klass array.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
   public static synchronized InstanceKlass[] getAllInstanceKlasses() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
      if (klasses != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
         return klasses;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
      final Vector tmp = new Vector();
46729
c62d2e8b2728 7133093: Improve system dictionary performance
coleenp
parents: 35217
diff changeset
    56
      ClassLoaderDataGraph cldg = VM.getVM().getClassLoaderDataGraph();
c62d2e8b2728 7133093: Improve system dictionary performance
coleenp
parents: 35217
diff changeset
    57
      cldg.classesDo(new ClassLoaderDataGraph.ClassVisitor() {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
                        public void visit(Klass k) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
                           if (k instanceof InstanceKlass) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
                              InstanceKlass ik = (InstanceKlass) k;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
                              tmp.add(ik);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
                           }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
                     });
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
      Object[] tmpArray = tmp.toArray();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
      klasses = new InstanceKlass[tmpArray.length];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
      System.arraycopy(tmpArray, 0, klasses, 0, tmpArray.length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
      Arrays.sort(klasses, new Comparator() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
                          public int compare(Object o1, Object o2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
                             InstanceKlass k1 = (InstanceKlass) o1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
                             InstanceKlass k2 = (InstanceKlass) o2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
                             Symbol s1 = k1.getName();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
                             Symbol s2 = k2.getName();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
                             return s1.asString().compareTo(s2.asString());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
                          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
                      });
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
      return klasses;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
   // returns array of instance klasses whose name contains given namePart
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
   public static InstanceKlass[] findInstanceKlasses(String namePart) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
      namePart = namePart.replace('.', '/');
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
      InstanceKlass[] tmpKlasses = getAllInstanceKlasses();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
      Vector tmp = new Vector();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
      for (int i = 0; i < tmpKlasses.length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
         String name = tmpKlasses[i].getName().asString();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
         if (name.indexOf(namePart) != -1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
            tmp.add(tmpKlasses[i]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
         }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
      Object[] tmpArray = tmp.toArray();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
      InstanceKlass[] searchResult = new InstanceKlass[tmpArray.length];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
      System.arraycopy(tmpArray, 0, searchResult, 0, tmpArray.length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
      return searchResult;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
   // find first class whose name matches exactly the given argument.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
   public static InstanceKlass findInstanceKlass(String className) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
      // convert to internal name
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
      className = className.replace('.', '/');
46729
c62d2e8b2728 7133093: Improve system dictionary performance
coleenp
parents: 35217
diff changeset
   104
      ClassLoaderDataGraph cldg = VM.getVM().getClassLoaderDataGraph();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
46729
c62d2e8b2728 7133093: Improve system dictionary performance
coleenp
parents: 35217
diff changeset
   106
      // check whether we have a class of given name
c62d2e8b2728 7133093: Improve system dictionary performance
coleenp
parents: 35217
diff changeset
   107
      Klass klass = cldg.find(className);
c62d2e8b2728 7133093: Improve system dictionary performance
coleenp
parents: 35217
diff changeset
   108
      if (klass != null && klass instanceof InstanceKlass) {
c62d2e8b2728 7133093: Improve system dictionary performance
coleenp
parents: 35217
diff changeset
   109
         return (InstanceKlass) klass;
c62d2e8b2728 7133093: Improve system dictionary performance
coleenp
parents: 35217
diff changeset
   110
      } else {
c62d2e8b2728 7133093: Improve system dictionary performance
coleenp
parents: 35217
diff changeset
   111
        // no match ..
c62d2e8b2728 7133093: Improve system dictionary performance
coleenp
parents: 35217
diff changeset
   112
        return null;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
}