|
|
@@ -37,31 +37,29 @@ public final class PackageScanner {
|
|
|
private final ClassLoader loader;
|
|
|
private final String packageName;
|
|
|
private final String packagePath;
|
|
|
- private Set<Class<?>> target;
|
|
|
- private Set<Class<?>> result;
|
|
|
+ private final Set<Class<?>> target;
|
|
|
|
|
|
- public PackageScanner(String packageName) {
|
|
|
- this(packageName, Thread.currentThread().getContextClassLoader());
|
|
|
- }
|
|
|
-
|
|
|
- public PackageScanner(String packageName, ClassLoader loader) {
|
|
|
+ private PackageScanner(String packageName, ClassLoader loader) {
|
|
|
this.loader = loader;
|
|
|
this.packageName = packageName;
|
|
|
this.packagePath = packageName.replace('.', '/');
|
|
|
+ this.target = new HashSet<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Set<Class<?>> list(String packageName) throws IOException, ClassNotFoundException, URISyntaxException {
|
|
|
+ return list(packageName, Thread.currentThread().getContextClassLoader());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Set<Class<?>> list(String packageName, ClassLoader loader) throws IOException, ClassNotFoundException, URISyntaxException {
|
|
|
+ return new PackageScanner(packageName, loader).list();
|
|
|
}
|
|
|
|
|
|
- public Set<Class<?>> list() throws IOException, ClassNotFoundException, URISyntaxException {
|
|
|
- if( null != result ) {
|
|
|
- return result;
|
|
|
- }
|
|
|
+ private Set<Class<?>> list() throws IOException, ClassNotFoundException, URISyntaxException {
|
|
|
Enumeration<URL> resources = loader.getResources(packagePath);
|
|
|
if (resources == null) {
|
|
|
- result = Collections.emptySet();
|
|
|
- return result;
|
|
|
+ return target;
|
|
|
}
|
|
|
|
|
|
- target = new HashSet<>();
|
|
|
-
|
|
|
while (resources.hasMoreElements()) {
|
|
|
String path = URLDecoder.decode(resources.nextElement().getPath(), TextEncoding.IO_DEFAULT);
|
|
|
File file = new File(path);
|
|
|
@@ -72,11 +70,10 @@ public final class PackageScanner {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- result = Collections.unmodifiableSet(target);
|
|
|
- return result;
|
|
|
+ return target;
|
|
|
}
|
|
|
|
|
|
- void getFromDirectory(File directory) {
|
|
|
+ private void getFromDirectory(File directory) {
|
|
|
for(File file : DirectoryHelper.find(directory, "*.class")) {
|
|
|
try {
|
|
|
String cname = packageName + '.' + path2name(PathHelper.asRelative(directory, file));
|
|
|
@@ -90,7 +87,7 @@ public final class PackageScanner {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- void getFromJARFile(String jar) {
|
|
|
+ private void getFromJARFile(String jar) {
|
|
|
try {
|
|
|
JarInputStream jarFile = new JarInputStream(new FileInputStream(jar));
|
|
|
while(true) {
|