Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/classload/ClassLoadUtils.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.File;26import java.io.IOException;27import java.io.InputStream;28import java.io.FileInputStream;2930import java.util.Arrays;31import java.nio.file.Path;32import java.nio.file.Paths;33import java.nio.file.Files;3435public class ClassLoadUtils {36private ClassLoadUtils() {37}3839/**40* Get filename of class file from classpath for given class name.41*42* @param className class name43* @return filename or null if not found44*/45public static String getClassPath(String className) {46String fileName = className.replace(".", File.separator) + ".class";47String[] classPath = System.getProperty("java.class.path").split(File.pathSeparator);48File target = null;49int i;50for (i = 0; i < classPath.length; ++i) {51target = new File(classPath[i] + File.separator + fileName);52System.out.println("Try: " + target);53if (target.exists()) {54break;55}56}57if (i != classPath.length) {58return classPath[i];59}60return null;61}6263/**64* Get filename of class file from classpath for given class name.65*66* @param className class name67* @return filename or null if not found68*/69public static String getClassPathFileName(String className) {70String fileName = className.replace(".", File.separator) + ".class";71String[] classPath = System.getProperty("java.class.path").split(File.pathSeparator);72File target = null;73int i;74for (i = 0; i < classPath.length; ++i) {75target = new File(classPath[i] + File.separator + fileName);76System.out.println("Try: " + target);77if (target.exists()) {78break;79}80}81if (i != classPath.length) {82try {83return target.getCanonicalPath();84} catch (IOException e) {85return null;86}87}88return null;89}9091public static String getRedefineClassFileName(String dir, String className) {92String fileName = className.replace(".", File.separator) + ".class";93return Arrays.stream(System.getProperty("java.class.path").split(File.pathSeparator))94.map(Paths::get)95.map(p -> p.resolve(dir))96.map(p -> p.resolve(fileName))97.filter(p -> Files.exists(p))98.map(Path::toAbsolutePath)99.map(Path::toString)100.findAny()101.orElse(null);102}103104/**105* Get filename of class file which is to be redefined.106*/107public static String getRedefineClassFileName(String className) {108return getRedefineClassFileName("newclass", className);109}110}111112113