Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/shenandoah/jvmti/TestGetLoadedClasses.java
32285 views
/*1* Copyright (c) 2019, Red Hat, Inc. All rights reserved.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation.6*7* This code is distributed in the hope that it will be useful, but WITHOUT8* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or9* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License10* version 2 for more details (a copy is included in the LICENSE file that11* accompanied this code).12*13* You should have received a copy of the GNU General Public License version14* 2 along with this work; if not, write to the Free Software Foundation,15* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.16*17* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA18* or visit www.oracle.com if you need additional information or have any19* questions.20*21*/2223import java.io.*;24import java.nio.file.*;2526public class TestGetLoadedClasses {2728private static final int NUM_ITER = 1000;29private static final int NUM_CLASSES = 10000;3031static {32try {33System.loadLibrary("TestGetLoadedClasses");34} catch (UnsatisfiedLinkError ule) {35System.err.println("Could not load TestGetLoadedClasses library");36System.err.println("java.library.path: "37+ System.getProperty("java.library.path"));38throw ule;39}40}4142native static int getLoadedClasses();4344static Class[] classes = new Class[NUM_CLASSES];4546static class Dummy {47}4849static class MyClassLoader extends ClassLoader {50final String path;5152MyClassLoader(String path) {53this.path = path;54}5556public Class<?> loadClass(String name) throws ClassNotFoundException {57try {58File f = new File(path, name + ".class");59if (!f.exists()) {60return super.loadClass(name);61}6263Path path = Paths.get(f.getAbsolutePath());64byte[] cls = Files.readAllBytes(path);65return defineClass(name, cls, 0, cls.length, null);66} catch (IOException e) {67throw new ClassNotFoundException(name);68}69}70}7172static Class load(String path) throws Exception {73ClassLoader cl = new MyClassLoader(path);74Class<Dummy> c = (Class<Dummy>) Class.forName("TestGetLoadedClasses$Dummy", true, cl);75if (c.getClassLoader() != cl) {76throw new IllegalStateException("Should have loaded by target loader");77}78return c;79}8081static void loadClasses() throws Exception {82String classDir = TestGetLoadedClasses.class.getProtectionDomain().getCodeSource().getLocation().getPath();83for (int c = 0; c < NUM_CLASSES; c++) {84classes[c] = load(classDir);85}86}8788public static void main(String args[]) throws Exception {89loadClasses();90new TestGetLoadedClasses().run();91}9293volatile Object sink;94public void run() throws Exception {95for (int i = 0; i < NUM_ITER; i++) {96sink = new byte[1000000];97int count = getLoadedClasses();98}99}100}101102103