-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClassLoader.java
More file actions
139 lines (123 loc) · 2.73 KB
/
ClassLoader.java
File metadata and controls
139 lines (123 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package api.cli;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
/**
* The Class ClassLoader.
*
* @author Gwindow
*/
public class ClassLoader {
/** The Constant classMap. */
private static final HashMap<String, Class<?>> classMap = new HashMap<String, Class<?>>();
/** The root. */
private static File root;
/**
* Sets the root path.
*
* @param path
* the new root path
*/
public static void setRootPath(String path) {
root = new File(path);
}
/**
* Load class files.
*/
public static void loadClassFiles() {
File[] list = root.listFiles();
for (int i = 0; i < list.length; i++) {
if (list[i].isDirectory()) {
recursiveDirectorySearch(list[i]);
}
if (isClassFile(list[i])) {
addFileToMap(list[i]);
}
}
}
/**
* Recursive directory search.
*
* @param dir
* the dir
*/
private static void recursiveDirectorySearch(File dir) {
File[] list = dir.listFiles();
for (int i = 0; i < list.length; i++) {
if (list[i].isDirectory()) {
recursiveDirectorySearch(list[i]);
}
if (isClassFile(list[i])) {
addFileToMap(list[i]);
}
}
}
/**
* Adds the file to map.
*
* @param file
* the file
*/
private static void addFileToMap(File file) {
FileClassLoader loader = new FileClassLoader();
Class<?> clazz;
try {
clazz = loader.createClass(file);
String className = clazz.getCanonicalName();
int levels = className.replaceAll("[^.]*", "").length();
File root = file.getParentFile();
for (int j = 0; j < levels; j++) {
root = root.getParentFile();
}
loader.addURL(root.toURI().toURL());
classMap.put(clazz.getName(), clazz);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Checks if is class file.
*
* @param file
* the file
* @return true, if is class file
*/
private static boolean isClassFile(File file) {
if (file.isFile()) {
String fileName = file.getName();
int i = fileName.lastIndexOf(".");
String ext = fileName.substring(i + 1, fileName.length());
if (ext.equalsIgnoreCase("class"))
return true;
else
return false;
} else
return false;
}
/**
* Gets the class for key.
*
* @param key
* the key
* @return the class for key
*/
public static Class<?> getClassForKey(Object key) {
if (classMap.containsKey(key))
return classMap.get(key);
else
return null;
}
/**
* Gets the class map.
*
* @return the class map
*/
public static HashMap<String, Class<?>> getClassMap() {
return classMap;
}
}