Skip to content

Commit

Permalink
Fixed the package-based filter in the classpath scanner.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Jul 28, 2015
1 parent d0eba9f commit 96f64ed
Showing 1 changed file with 36 additions and 30 deletions.
66 changes: 36 additions & 30 deletions rapidoid-scan/src/main/java/org/rapidoid/scan/ClasspathUtil.java
Expand Up @@ -156,32 +156,39 @@ private static List<Class<?>> retrieveClasses(String packageName, Predicate<Clas


List<Class<?>> classes = new ArrayList<Class<?>>(); List<Class<?>> classes = new ArrayList<Class<?>>();


String pkgName = U.or(packageName, ""); String pkgName = U.safe(packageName);
String pkgPath = pkgName.replace('.', File.separatorChar);


Set<String> classpath = getClasspath(); Set<String> classpath = getClasspath();


Log.info("Scanning classpath", "classpath", classpath);

Set<String> jars = U.set(); Set<String> jars = U.set();


for (String cpe : classpath) { for (String cpe : classpath) {
File file = new File(cpe); File cpEntry = new File(cpe);


String path = file.getAbsolutePath(); if (cpEntry.exists()) {

if (cpEntry.isDirectory()) {
String pkgPath = pkgName.replace('.', File.separatorChar); if (shouldScanDir(cpEntry.getAbsolutePath())) {
String rootPath = pkgPath.isEmpty() ? path : path.replace(File.separatorChar + pkgPath, ""); Log.debug("Scanning directory", "root", cpEntry.getAbsolutePath());


File root = new File(rootPath); File startingDir;
if (pkgPath.isEmpty()) {
startingDir = cpEntry;
} else {
startingDir = new File(cpEntry.getAbsolutePath(), pkgPath);
}


if (root.exists()) { if (startingDir.exists()) {
if (root.isDirectory()) { getClassesFromDir(classes, cpEntry, startingDir, pkgName, regex, filter, annotated,
if (shouldScanDir(root.getAbsolutePath())) { classLoader);
Log.debug("Scanning directory", "name", root.getAbsolutePath()); }
getClassesFromDir(classes, root, file, regex, filter, annotated, classLoader);
} else { } else {
Log.debug("Skipping directory", "name", root.getAbsolutePath()); Log.debug("Skipping directory", "root", cpEntry.getAbsolutePath());
} }
} else if (root.isFile() && root.getAbsolutePath().toLowerCase().endsWith(".jar")) { } else if (cpEntry.isFile() && cpEntry.getAbsolutePath().toLowerCase().endsWith(".jar")) {
jars.add(root.getAbsolutePath()); jars.add(cpEntry.getAbsolutePath());
} else { } else {
Log.warn("Invalid classpath entry: " + cpe); Log.warn("Invalid classpath entry: " + cpe);
} }
Expand Down Expand Up @@ -212,22 +219,21 @@ private static boolean shouldScanJAR(String jar) {
return simpleName.startsWith("app.") || simpleName.startsWith("app-"); return simpleName.startsWith("app.") || simpleName.startsWith("app-");
} }


private static void getClassesFromDir(Collection<Class<?>> classes, File root, File parent, Pattern regex, private static void getClassesFromDir(Collection<Class<?>> classes, File root, File dir, String pkg, Pattern regex,
Predicate<Class<?>> filter, Class<? extends Annotation> annotated, ClassLoader classLoader) { Predicate<Class<?>> filter, Class<? extends Annotation> annotated, ClassLoader classLoader) {
U.must(dir.isDirectory());
Log.debug("Traversing directory", "root", root, "dir", dir);


if (parent.isDirectory()) { for (File file : dir.listFiles()) {
Log.debug("scanning directory", "dir", parent); if (file.isDirectory()) {
for (File file : parent.listFiles()) { getClassesFromDir(classes, root, file, pkg, regex, filter, annotated, classLoader);
if (file.isDirectory()) { } else {
getClassesFromDir(classes, root, file, regex, filter, annotated, classLoader); String rootPath = U.trimr(root.getAbsolutePath(), File.separatorChar);
} else { int from = rootPath.length() + 1;
String rootPath = U.trimr(root.getAbsolutePath(), File.separatorChar); String relName = file.getAbsolutePath().substring(from);
int from = rootPath.length() + 1;
String relName = file.getAbsolutePath().substring(from);


if (!ignore(relName)) { if (!ignore(relName)) {
scanFile(classes, regex, filter, annotated, classLoader, relName); scanFile(classes, regex, filter, annotated, classLoader, relName);
}
} }
} }
} }
Expand Down

0 comments on commit 96f64ed

Please sign in to comment.