Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/CustomClassLoader.java
40948 views
/*1* Copyright (c) 2003, 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*/2223package nsk.share;2425import java.io.*;2627/**28* The <code>CustomClassLoader</code> class is used in <code>ClassUnloader</code>.29*30* <p>This class loader can load classes and notify <code>ClassUnloader</code>31* about own finalization to make sure that all loaded classes have been unloaded.32*33* <p>By default this class loader loads class from .class file located in directory34* specified with <code>setClassPath()</code> method. To use any other technique35* of class loading one should implement derived class, which would override36* <code>findClass</code> method.37*38* @see nsk.share.ClassUnloader39*40* @see #setClassPath(String)41* @see #findClass(String)42*/43public class CustomClassLoader extends ClassLoader {4445private ClassUnloader classUnloader;46protected String classPath;4748/**49* Initializes a newly created <code>CustomClassloader</code> object50* not yet linked with any <code>ClassUnloader</code> object.51*52*/53public CustomClassLoader() {54super(CustomClassLoader.class.getClassLoader());55this.classUnloader = null;56}5758/**59* Initializes a newly created <code>CustomClassloader</code> object60* linked with specified <code>ClassUnloader</code> object.61*62* @param classUnloader an instance of <code>ClassUnloader</code>63*/64public CustomClassLoader(ClassUnloader classUnloader) {65super(CustomClassLoader.class.getClassLoader());66this.classUnloader = classUnloader;67}6869/**70* Links this class loader with specified <code>ClassUnloader</code> object.71*72* @param classUnloader an instance of <code>ClassUnloader</code>73*/74public void setClassUnloader(ClassUnloader classUnloader) {75this.classUnloader = classUnloader;76}7778/**79* Specifies path to .class file location.80*81* @param classPath a path to .class file location82*/83public void setClassPath(String classPath) {84this.classPath = classPath;85}8687/**88* Finds and loads class for specified class name.89* This method loads class from .class file located in a directory90* previously specified by <code>setClassPath()</code>.91*92* @param name The name of the class.93*94* @throws ClassNotFoundException if no .class file found95* for specified class name96*97* @see #setClassPath(String)98*/99protected synchronized Class findClass(String name) throws ClassNotFoundException {100java.nio.file.Path path = ClassFileFinder.findClassFile(name, classPath);101if (path == null) {102throw new ClassNotFoundException(name);103}104String classFileName = path.toString();105106FileInputStream in;107try {108in = new FileInputStream(classFileName);109if (in == null) {110throw new ClassNotFoundException(classFileName);111}112} catch (FileNotFoundException e) {113throw new ClassNotFoundException(classFileName, e);114}115116int len;117byte data[];118try {119len = in.available();120data = new byte[len];121for (int total = 0; total < data.length; ) {122total += in.read(data, total, data.length - total);123}124} catch (IOException e) {125throw new ClassNotFoundException(classFileName, e);126} finally {127try {128in.close();129} catch (IOException e) {130throw new ClassNotFoundException(classFileName, e);131}132}133134return defineClass(name, data, 0, data.length);135}136137/**138* Notifies <code>ClassUnloader</code> about finalization.139*/140protected void finalize() throws Throwable {141142// notify ClassUnloader about finalization143if (classUnloader != null) {144classUnloader.finalized = true;145}146147super.finalize();148}149}150151152