Path: blob/master/test/jdk/java/lang/instrument/CustomSystemLoader/CustomLoader.java
41152 views
/*1* Copyright (c) 2016, 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.DataInputStream;24import java.io.InputStream;25import java.io.IOException;26import java.io.PrintStream;27import java.util.jar.JarFile;28import java.util.zip.ZipEntry;2930public class CustomLoader extends ClassLoader {31private static PrintStream out = System.out;32public static ClassLoader myself;33public static ClassLoader agentClassLoader;34public static boolean failed = true;3536public CustomLoader(ClassLoader classLoader) {37super(classLoader);38myself = this;39}4041@Override42public Class<?> loadClass(String name) throws ClassNotFoundException {43out.println("CustomLoader: loading class: " + name);44if (name.equals("Agent")) {45Class c = null;46try {47byte[] buf = locateBytes();48c = defineClass(name, buf, 0, buf.length);49} catch (IOException ex) {50throw new ClassNotFoundException(ex.getMessage());51}52resolveClass(c);53out.println("CustomLoader.loadClass after resolveClass: " + name +54"; Class: " + c + "; ClassLoader: " + c.getClassLoader());55return c;56}57return super.loadClass(name);58}5960private byte[] locateBytes() throws IOException {61try {62JarFile jar = new JarFile("Agent.jar");63InputStream is = jar.getInputStream(jar.getEntry("Agent.class"));64int len = is.available();65byte[] buf = new byte[len];66DataInputStream in = new DataInputStream(is);67in.readFully(buf);68return buf;69} catch (IOException ioe) {70ioe.printStackTrace();71throw new IOException("Test failed due to IOException!");72}73}7475void appendToClassPathForInstrumentation(String path) {76out.println("CustomLoader.appendToClassPathForInstrumentation: " +77this + ", jar: " + path);78failed = false;79}80}818283