Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/classload/ClassPathNonDelegatingClassLoader.java
41161 views
/*1* Copyright (c) 2007, 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.classload;2425import java.io.*;26import java.util.*;2728import nsk.share.FileUtils;2930/**31* Custom classloader that does not delegate to it's parent.32*33* It can load classes from classpath that have name containing34* "Class" (any package).35*/36public class ClassPathNonDelegatingClassLoader extends ClassLoader {37private String [] classPath;3839public ClassPathNonDelegatingClassLoader() {40classPath = System.getProperty("java.class.path").split(File.pathSeparator);41}4243public synchronized Class loadClass(String name, boolean resolve)44throws ClassNotFoundException {45Class c = findLoadedClass(name);46if (c != null) {47System.out.println("Found class: " + name);48return c;49}50if (name.contains("Class")) {51String newName = name.replace('.', '/');52return loadClassFromFile(name, newName + ".class", resolve);53} else {54return findSystemClass(name);55}56}5758private Class loadClassFromFile(String name, String fname, boolean resolve)59throws ClassNotFoundException {60try {61File target = new File("");6263for(int i = 0; i < classPath.length; ++i) {64target = new File(classPath[i] + File.separator + fname);65if (target.exists())66break;67}68if (!target.exists())69throw new java.io.FileNotFoundException();70byte[] buffer = FileUtils.readFile(target);71Class c = defineClass(name, buffer, 0, buffer.length);72if (resolve)73resolveClass(c);74return c;75} catch (IOException e) {76throw new ClassNotFoundException("Exception while reading classfile data", e);77}78}79}808182