1
2
3
4
5
6
7 package net.sf.tlc.util;
8
9 /***
10 * ClassPathUtils provides a set of static utility methods used in the
11 * manipulation of paths to classes.
12 *
13 * @author aisrael
14 */
15 public final class ClassPathUtils {
16
17 private static final String CLASS_EXT = ".class";
18
19 /***
20 * ClassPathUtils instances should NOT be constructed in standard
21 * programming.
22 */
23 private ClassPathUtils() {
24
25 }
26
27 /***
28 * Returns the expected class name from a class file's absolute path or null
29 * if the path does not end with ".class".
30 *
31 * @param path
32 * the absolute path to the class
33 * @return the expected class name, or null
34 */
35 public static String toClassName(final String path) {
36 final int offset;
37 if (path.startsWith("/")) {
38 offset = 1;
39 } else {
40 offset = 0;
41 }
42 return toClassName(offset, path);
43 }
44
45 /***
46 * Returns the expected class name from a class file's absolute path and the
47 * 'base dir' to calculate it from, or null if the absolutePath does not
48 * start with the baseDir and does not end with ".class".
49 *
50 * @param baseDir
51 * the base directory
52 * @param absolutePath
53 * the absolute path to the class
54 * @return the expected class name, or null
55 */
56 public static String toClassName(final String baseDir, final String absolutePath) {
57 final String result;
58 final int offset;
59 if (baseDir.endsWith("/")) {
60 offset = 0;
61 } else {
62 offset = 1;
63 }
64 if (absolutePath.startsWith(baseDir)) {
65 result = toClassName(baseDir.length() + offset, absolutePath);
66 } else {
67 result = null;
68 }
69 return result;
70 }
71
72 /***
73 * Returns the expected class name from a class file's absolute path
74 * starting at the offset, or null if the absolutePath does not end with
75 * ".class".
76 *
77 * @param offset
78 * int
79 * @param path
80 * String
81 * @return the expected class name, or null
82 */
83 private static String toClassName(final int offset, final String path) {
84 final String result;
85 if (path.endsWith(CLASS_EXT)) {
86 result = path.substring(offset, path.lastIndexOf(CLASS_EXT)).replace('/', '.');
87 } else {
88 result = null;
89 }
90 return result;
91 }
92 }