1 /*
   2  * Copyright (c) 2019, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 import java.io.*;
  25 import java.nio.file.*;
  26 
  27 public class TestGetLoadedClasses {
  28 
  29     private static final int NUM_ITER = 1000;
  30     private static final int NUM_CLASSES = 10000;
  31 
  32     static {
  33         try {
  34             System.loadLibrary("TestGetLoadedClasses");
  35         } catch (UnsatisfiedLinkError ule) {
  36             System.err.println("Could not load TestGetLoadedClasses library");
  37             System.err.println("java.library.path: "
  38                     + System.getProperty("java.library.path"));
  39             throw ule;
  40         }
  41     }
  42 
  43     native static int getLoadedClasses();
  44 
  45     static Class[] classes = new Class[NUM_CLASSES];
  46 
  47     static class Dummy {
  48     }
  49 
  50     static class MyClassLoader extends ClassLoader {
  51         final String path;
  52 
  53         MyClassLoader(String path) {
  54             this.path = path;
  55         }
  56 
  57         public Class<?> loadClass(String name) throws ClassNotFoundException {
  58             try {
  59                 File f = new File(path, name + ".class");
  60                 if (!f.exists()) {
  61                     return super.loadClass(name);
  62                 }
  63 
  64                 Path path = Paths.get(f.getAbsolutePath());
  65                 byte[] cls = Files.readAllBytes(path);
  66                 return defineClass(name, cls, 0, cls.length, null);
  67             } catch (IOException e) {
  68                 throw new ClassNotFoundException(name);
  69             }
  70         }
  71     }
  72 
  73     static Class load(String path) throws Exception {
  74         ClassLoader cl = new MyClassLoader(path);
  75         Class<Dummy> c = (Class<Dummy>) Class.forName("TestGetLoadedClasses$Dummy", true, cl);
  76         if (c.getClassLoader() != cl) {
  77             throw new IllegalStateException("Should have loaded by target loader");
  78         }
  79         return c;
  80     }
  81 
  82     static void loadClasses() throws Exception {
  83         String classDir = TestGetLoadedClasses.class.getProtectionDomain().getCodeSource().getLocation().getPath();
  84         for (int c = 0; c < NUM_CLASSES; c++) {
  85             classes[c] = load(classDir);
  86         }
  87     }
  88 
  89     public static void main(String args[]) throws Exception {
  90         loadClasses();
  91         new TestGetLoadedClasses().run();
  92     }
  93 
  94     volatile Object sink;
  95     public void run() throws Exception {
  96         for (int i = 0; i < NUM_ITER; i++) {
  97             sink = new byte[1000000];
  98             int count = getLoadedClasses();
  99         }
 100     }
 101 }