mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-13 21:01:01 +08:00
improved Class loading in GenericExporter
This commit is contained in:
parent
76ee0b4d54
commit
2118178623
@ -310,8 +310,6 @@ public class GenericExporter {
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new QueryException(e);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new QueryException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,46 +17,16 @@ public final class ClassPathUtils {
|
||||
|
||||
private static final Pattern JAR_URL_SEPARATOR = Pattern.compile("!");
|
||||
|
||||
public static Set<Class<?>> scanPackage(ClassLoader classLoader, Package pkg) throws IOException, ClassNotFoundException {
|
||||
public static Set<Class<?>> scanPackage(ClassLoader classLoader, Package pkg) throws IOException {
|
||||
Enumeration<URL> urls = classLoader.getResources(pkg.getName().replace('.', '/'));
|
||||
Set<Class<?>> classes = new HashSet<Class<?>>();
|
||||
while (urls.hasMoreElements()){
|
||||
URL url = urls.nextElement();
|
||||
if (url.getProtocol().equals("jar")){
|
||||
String[] fileAndPath = JAR_URL_SEPARATOR.split(url.getFile().substring(5));
|
||||
JarFile jarFile = new JarFile(fileAndPath[0]);
|
||||
Enumeration<JarEntry> entries = jarFile.entries();
|
||||
while (entries.hasMoreElements()){
|
||||
JarEntry entry = entries.nextElement();
|
||||
if (entry.getName().endsWith(".class") && entry.getName().startsWith(fileAndPath[1].substring(1))){
|
||||
String className = entry.getName().substring(0, entry.getName().length()-6).replace('/', '.');
|
||||
classes.add(Class.forName(className));
|
||||
|
||||
}
|
||||
}
|
||||
scanJar(classes, url);
|
||||
|
||||
}else if (url.getProtocol().equals("file")){
|
||||
Deque<File> files = new ArrayDeque<File>();
|
||||
String packagePath;
|
||||
try {
|
||||
File packageAsFile = new File(url.toURI());
|
||||
packagePath = packageAsFile.getPath();
|
||||
files.add(packageAsFile);
|
||||
} catch (URISyntaxException e) {
|
||||
throw new IOException(e);
|
||||
}
|
||||
while (!files.isEmpty()){
|
||||
File file = files.pop();
|
||||
for (File child : file.listFiles()){
|
||||
if (child.getName().endsWith(".class")){
|
||||
String fileName = child.getPath().substring(packagePath.length()+1).replace(File.separatorChar, '.');
|
||||
String className = pkg.getName() + "." + fileName.substring(0, fileName.length()-6);
|
||||
classes.add(Class.forName(className));
|
||||
}else if (child.isDirectory()){
|
||||
files.add(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
scanDirectory(pkg, classes, url);
|
||||
|
||||
}else{
|
||||
throw new IllegalArgumentException("Illegal url : " + url);
|
||||
@ -65,6 +35,57 @@ public final class ClassPathUtils {
|
||||
return classes;
|
||||
}
|
||||
|
||||
private static void scanDirectory(Package pkg, Set<Class<?>> classes, URL url) throws IOException {
|
||||
Deque<File> files = new ArrayDeque<File>();
|
||||
String packagePath;
|
||||
try {
|
||||
File packageAsFile = new File(url.toURI());
|
||||
packagePath = packageAsFile.getPath();
|
||||
files.add(packageAsFile);
|
||||
} catch (URISyntaxException e) {
|
||||
throw new IOException(e);
|
||||
}
|
||||
while (!files.isEmpty()){
|
||||
File file = files.pop();
|
||||
for (File child : file.listFiles()){
|
||||
if (child.getName().endsWith(".class")){
|
||||
String fileName = child.getPath().substring(packagePath.length()+1).replace(File.separatorChar, '.');
|
||||
String className = pkg.getName() + "." + fileName.substring(0, fileName.length()-6);
|
||||
Class<?> cl = safeClassForName(className);
|
||||
if (cl != null) {
|
||||
classes.add(cl);
|
||||
}
|
||||
}else if (child.isDirectory()){
|
||||
files.add(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void scanJar(Set<Class<?>> classes, URL url) throws IOException {
|
||||
String[] fileAndPath = JAR_URL_SEPARATOR.split(url.getFile().substring(5));
|
||||
JarFile jarFile = new JarFile(fileAndPath[0]);
|
||||
Enumeration<JarEntry> entries = jarFile.entries();
|
||||
while (entries.hasMoreElements()){
|
||||
JarEntry entry = entries.nextElement();
|
||||
if (entry.getName().endsWith(".class") && entry.getName().startsWith(fileAndPath[1].substring(1))){
|
||||
String className = entry.getName().substring(0, entry.getName().length()-6).replace('/', '.');
|
||||
Class<?> cl = safeClassForName(className);
|
||||
if (cl != null) {
|
||||
classes.add(cl);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Class<?> safeClassForName(String className){
|
||||
try {
|
||||
return Class.forName(className);
|
||||
} catch (ClassNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private ClassPathUtils() {}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user