diff --git a/querydsl-core/src/main/java/com/mysema/query/codegen/GenericExporter.java b/querydsl-core/src/main/java/com/mysema/query/codegen/GenericExporter.java index 789cb28a9..293f878bc 100644 --- a/querydsl-core/src/main/java/com/mysema/query/codegen/GenericExporter.java +++ b/querydsl-core/src/main/java/com/mysema/query/codegen/GenericExporter.java @@ -310,8 +310,6 @@ public class GenericExporter { } } catch (IOException e) { throw new QueryException(e); - } catch (ClassNotFoundException e) { - throw new QueryException(e); } } } diff --git a/querydsl-core/src/main/java/com/mysema/util/ClassPathUtils.java b/querydsl-core/src/main/java/com/mysema/util/ClassPathUtils.java index 992709e2f..f7c462b60 100644 --- a/querydsl-core/src/main/java/com/mysema/util/ClassPathUtils.java +++ b/querydsl-core/src/main/java/com/mysema/util/ClassPathUtils.java @@ -17,46 +17,16 @@ public final class ClassPathUtils { private static final Pattern JAR_URL_SEPARATOR = Pattern.compile("!"); - public static Set> scanPackage(ClassLoader classLoader, Package pkg) throws IOException, ClassNotFoundException { + public static Set> scanPackage(ClassLoader classLoader, Package pkg) throws IOException { Enumeration urls = classLoader.getResources(pkg.getName().replace('.', '/')); Set> classes = new HashSet>(); 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 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 files = new ArrayDeque(); - 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> classes, URL url) throws IOException { + Deque files = new ArrayDeque(); + 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> classes, URL url) throws IOException { + String[] fileAndPath = JAR_URL_SEPARATOR.split(url.getFile().substring(5)); + JarFile jarFile = new JarFile(fileAndPath[0]); + Enumeration 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() {} }