Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/runtime/ClassUnload/MyDiffClassLoader.java
32284 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 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 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.*;24import com.oracle.java.testlibrary.InMemoryJavaCompiler;2526public class MyDiffClassLoader extends ClassLoader {2728public String loaderName;29public static boolean switchClassData = false;3031MyDiffClassLoader(String name) {32this.loaderName = name;33}3435public Class loadClass(String name) throws ClassNotFoundException {36if (!name.contains("c1r") &&37!name.contains("c1c") &&38!name.contains("c1s") &&39!name.equals("p2.c2")) {40return super.loadClass(name);41}4243// new loader loads p2.c244if (name.equals("p2.c2") && !loaderName.equals("C2Loader")) {45Class<?> c = new MyDiffClassLoader("C2Loader").loadClass(name);46switchClassData = true;47return c;48}4950byte[] data = switchClassData ? getNewClassData(name) : getClassData(name);51System.out.println("name is " + name);52return defineClass(name, data, 0, data.length);53}54byte[] getClassData(String name) {55try {56String TempName = name.replaceAll("\\.", "/");57String currentDir = System.getProperty("test.classes");58String filename = currentDir + File.separator + TempName + ".class";59FileInputStream fis = new FileInputStream(filename);60byte[] b = new byte[5000];61int cnt = fis.read(b, 0, 5000);62byte[] c = new byte[cnt];63for (int i=0; i<cnt; i++) c[i] = b[i];64return c;65} catch (IOException e) {66return null;67}68}6970// Return p2.c2 with everything removed71byte[] getNewClassData(String name) {72return InMemoryJavaCompiler.compile("p2.c2", "package p2; public class c2 { }");73}74}757677