-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestUtil.java
More file actions
53 lines (42 loc) · 1.46 KB
/
Copy pathTestUtil.java
File metadata and controls
53 lines (42 loc) · 1.46 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
package SupportData;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
public class TestUtil {
public static List<Class<?>> getAllClassesByPackageName(String packageName){
List<Class<?>> classes = new ArrayList<Class<?>>();
Enumeration<URL> dirs;
try {
dirs = Thread.currentThread().getContextClassLoader().getResources(packageName);
while(dirs.hasMoreElements()){
URL url = dirs.nextElement();
String filePath = URLDecoder.decode(url.getFile(),"UTF-8");
classes.addAll(getAllClassesByFilePath(filePath));
}
} catch (IOException e) {
e.printStackTrace();
}
return classes;
}
private static List<Class<?>> getAllClassesByFilePath(String filePath){
List<Class<?>> result = new ArrayList<Class<?>>();
File file = new File(filePath);
File [] files = file.listFiles();
for(File childFile:files){
if(childFile.isDirectory()){
continue;
}
try {
String fileName = childFile.getName();
result.add(Class.forName(fileName.substring(0,fileName.length()-6)));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
return result;
}
}