|
@ -0,0 +1,46 @@ |
|
|
|
|
|
package com.my.graphiteDigesterBg.diframe.util; |
|
|
|
|
|
import java.io.File; |
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
import java.net.URL; |
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
import java.util.Collections; |
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
public class ClassHelper { |
|
|
|
|
|
// get all classes in package |
|
|
|
|
|
public static List<Class<?>> getAllClassesInPackage(String packageName) { |
|
|
|
|
|
List<Class<?>> classes = new ArrayList<>(); |
|
|
|
|
|
try { |
|
|
|
|
|
String packagePath = packageName.replace(".", "/"); |
|
|
|
|
|
List<URL> resources = Collections.list(Thread.currentThread().getContextClassLoader().getResources(packagePath)); |
|
|
|
|
|
for (URL resource : resources) { |
|
|
|
|
|
if ( "file".equals(resource.getProtocol()) ) { |
|
|
|
|
|
ClassHelper.getClassListByGivenFilePath(packageName,resource.getFile(), classes); |
|
|
|
|
|
} else { |
|
|
|
|
|
throw new RuntimeException("Unsupported protocol: " + resource.getProtocol()); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
|
} |
|
|
|
|
|
return classes; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// get class list by given file path |
|
|
|
|
|
private static void getClassListByGivenFilePath( String packageName, String filePath, List<Class<?>> classes ) { |
|
|
|
|
|
File file = new File(filePath); |
|
|
|
|
|
if ( file.isDirectory() ) { |
|
|
|
|
|
for ( File subFile : file.listFiles() ) { |
|
|
|
|
|
ClassHelper.getClassListByGivenFilePath(packageName + "." + subFile.getName(), subFile.getAbsolutePath(), classes); |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
if ( file.getName().endsWith(".class") ) { |
|
|
|
|
|
try { |
|
|
|
|
|
String className = packageName.substring(0, packageName.length() - 6); |
|
|
|
|
|
classes.add(Class.forName(className)); |
|
|
|
|
|
} catch (ClassNotFoundException e) { |
|
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |