|
1 /* |
|
2 * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 package nsk.share.classload; |
|
25 |
|
26 import java.io.File; |
|
27 import java.io.IOException; |
|
28 import java.io.InputStream; |
|
29 import java.io.FileInputStream; |
|
30 |
|
31 import java.util.Arrays; |
|
32 import java.nio.file.Path; |
|
33 import java.nio.file.Paths; |
|
34 import java.nio.file.Files; |
|
35 |
|
36 public class ClassLoadUtils { |
|
37 private ClassLoadUtils() { |
|
38 } |
|
39 |
|
40 /** |
|
41 * Get filename of class file from classpath for given class name. |
|
42 * |
|
43 * @param className class name |
|
44 * @return filename or null if not found |
|
45 */ |
|
46 public static String getClassPath(String className) { |
|
47 String fileName = className.replace(".", File.separator) + ".class"; |
|
48 String[] classPath = System.getProperty("java.class.path").split(File.pathSeparator); |
|
49 File target = null; |
|
50 int i; |
|
51 for (i = 0; i < classPath.length; ++i) { |
|
52 target = new File(classPath[i] + File.separator + fileName); |
|
53 System.out.println("Try: " + target); |
|
54 if (target.exists()) { |
|
55 break; |
|
56 } |
|
57 } |
|
58 if (i != classPath.length) { |
|
59 return classPath[i]; |
|
60 } |
|
61 return null; |
|
62 } |
|
63 |
|
64 /** |
|
65 * Get filename of class file from classpath for given class name. |
|
66 * |
|
67 * @param className class name |
|
68 * @return filename or null if not found |
|
69 */ |
|
70 public static String getClassPathFileName(String className) { |
|
71 String fileName = className.replace(".", File.separator) + ".class"; |
|
72 String[] classPath = System.getProperty("java.class.path").split(File.pathSeparator); |
|
73 File target = null; |
|
74 int i; |
|
75 for (i = 0; i < classPath.length; ++i) { |
|
76 target = new File(classPath[i] + File.separator + fileName); |
|
77 System.out.println("Try: " + target); |
|
78 if (target.exists()) { |
|
79 break; |
|
80 } |
|
81 } |
|
82 if (i != classPath.length) { |
|
83 try { |
|
84 return target.getCanonicalPath(); |
|
85 } catch (IOException e) { |
|
86 return null; |
|
87 } |
|
88 } |
|
89 return null; |
|
90 } |
|
91 |
|
92 public static String getRedefineClassFileName(String dir, String className) { |
|
93 String fileName = className.replace(".", File.separator) + ".class"; |
|
94 return Arrays.stream(System.getProperty("java.class.path").split(File.pathSeparator)) |
|
95 .map(Paths::get) |
|
96 .map(p -> p.resolve(dir)) |
|
97 .map(p -> p.resolve(fileName)) |
|
98 .filter(p -> Files.exists(p)) |
|
99 .map(Path::toAbsolutePath) |
|
100 .map(Path::toString) |
|
101 .findAny() |
|
102 .orElse(null); |
|
103 } |
|
104 |
|
105 /** |
|
106 * Get filename of class file which is to be redefined. |
|
107 */ |
|
108 public static String getRedefineClassFileName(String className) { |
|
109 return getRedefineClassFileName("newclass", className); |
|
110 } |
|
111 } |