From 413441a26a57b2bdd5d2552b4b3d012aa90816e8 Mon Sep 17 00:00:00 2001 From: Zhen Tang Date: Thu, 6 Jun 2013 17:31:58 +0800 Subject: [PATCH] Class Helper updated. It can search for classes correctly. --- src/main/java/haflow/utility/ClassHelper.java | 204 +++++++++--------- .../java/haflow/test/ClassHelperTest.java | 20 ++ 2 files changed, 116 insertions(+), 108 deletions(-) create mode 100644 src/test/java/haflow/test/ClassHelperTest.java diff --git a/src/main/java/haflow/utility/ClassHelper.java b/src/main/java/haflow/utility/ClassHelper.java index 1506ded..4ee9163 100644 --- a/src/main/java/haflow/utility/ClassHelper.java +++ b/src/main/java/haflow/utility/ClassHelper.java @@ -1,9 +1,9 @@ package haflow.utility; import java.io.File; +import java.io.IOException; import java.net.URL; import java.net.URLClassLoader; -import java.net.URLDecoder; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; @@ -14,117 +14,26 @@ import org.springframework.stereotype.Component; @Component public class ClassHelper { - private List getClassNameByFile(String packageName, - String filePath, List className, - boolean searchInChildPackage) throws Exception { - List classNames = new ArrayList(); - String decodedFilePath = URLDecoder.decode(filePath, "utf-8"); - File file = new File(decodedFilePath); - File[] classFiles = file.listFiles(); - for (File classFile : classFiles) { - if (classFile.isDirectory()) { - if (searchInChildPackage) { - classNames.addAll(getClassNameByFile(packageName, - classFile.getPath(), classNames, - searchInChildPackage)); - } - } else { - String classFilePath = classFile.getPath(); - if (classFilePath.endsWith(".class")) { - classFilePath = classFilePath.replace("\\", "."); - classFilePath = classFilePath.replace("/", "."); - classFilePath = classFilePath.substring( - classFilePath.indexOf("classes") + 8, - classFilePath.lastIndexOf(".")); - - classNames.add(classFilePath); - } - } - } - return classNames; - } - - private List getClassNameByJar(String jarPath, - boolean searchInChildPackage) throws Exception { - List classNames = new ArrayList(); - String[] jarInfo = jarPath.split("!"); - String jarFilePath = jarInfo[0].substring(jarInfo[0].indexOf("/")); - String decodedJarFilePath = URLDecoder.decode(jarFilePath, "utf-8"); - String packagePath = jarInfo[1].substring(1); - - JarFile jarFile = new JarFile(decodedJarFilePath); - Enumeration entrys = jarFile.entries(); - while (entrys.hasMoreElements()) { - JarEntry jarEntry = entrys.nextElement(); - String entryName = jarEntry.getName(); - if (entryName.endsWith(".class")) { - if (searchInChildPackage) { - if (entryName.startsWith(packagePath)) { - entryName = entryName.replace("/", ".").substring(0, - entryName.lastIndexOf(".")); - classNames.add(entryName); - } - } else { - int index = entryName.lastIndexOf("/"); - String entryPath; - if (index != -1) { - entryPath = entryName.substring(0, index); - } else { - entryPath = entryName; - } - if (entryPath.equals(packagePath)) { - entryName = entryName.replace("/", ".").substring(0, - entryName.lastIndexOf(".")); - classNames.add(entryName); - } - } - } - } - jarFile.close(); - return classNames; - } - - private List getClassNameByJars(URL[] jarFiles, String packagePath, - boolean searchInChildPackage) throws Exception { - List classNames = new ArrayList(); - if (jarFiles != null) { - for (int i = 0; i < jarFiles.length; i++) { - URL jarFileUrl = jarFiles[i]; - String jarFilePath = jarFileUrl.getPath(); - if (jarFilePath.endsWith("classes/")) { - continue; - } - String jarPath = jarFilePath + "!/" + packagePath; - classNames.addAll(this.getClassNameByJar(jarPath, - searchInChildPackage)); - } - } - return classNames; - } public List getClassNames(String packageName, - boolean searchInchildPackage) { + boolean searchInChildPackage) { try { - List classNames = null; - ClassLoader classLoader = Thread.currentThread() - .getContextClassLoader(); - String packagePath = packageName.replace(".", "/"); - URL packageUrl = classLoader.getResource(packagePath); - if (packageUrl != null) { - String protocol = packageUrl.getProtocol(); - if (protocol.equals("file")) { - classNames = this.getClassNameByFile(packageName, - packageUrl.getPath(), null, searchInchildPackage); - } else if (protocol.equals("jar")) { - classNames = this.getClassNameByJar(packageUrl.getPath(), - searchInchildPackage); - } else { - classNames = null; + List classNames = new ArrayList(); + URLClassLoader classLoader = (URLClassLoader) Thread + .currentThread().getContextClassLoader(); + URL[] urls = classLoader.getURLs(); + for (URL url : urls) { + if (url.getProtocol().equals("file")) { + File file = new File(url.getFile()); + String root = file.getPath(); + if (file.isDirectory()) { + classNames.addAll(this.getClassNamesFromDirectory(root, + packageName, searchInChildPackage, file, true)); + } else if (file.getName().endsWith(".jar")) { + classNames.addAll(this.getClassNameFromJar(packageName, + searchInChildPackage, file)); + } } - } else { - classNames = this.getClassNameByJars( - ((URLClassLoader) classLoader).getURLs(), packagePath, - searchInchildPackage); } return classNames; } catch (Exception e) { @@ -133,4 +42,83 @@ public class ClassHelper { } } + private List getClassNamesFromDirectory(String root, + String packageName, boolean searchInChildPackage, File directory, + boolean continueSearch) { + List classNames = new ArrayList(); + File[] files = directory.listFiles(); + for (File file : files) { + if (file.isDirectory()) { + if (searchInChildPackage || continueSearch) { + boolean needToContinue = continueSearch; + if (file.getPath().endsWith(packageName.replace(".", "/"))) { + needToContinue = needToContinue & false; + } + classNames.addAll(this.getClassNamesFromDirectory(root, + packageName, searchInChildPackage, file, + needToContinue)); + } + } else { + if (file.getPath().endsWith(".class")) { + String classFileName = file.getPath().replace(root + "/", + ""); + if (classFileName.startsWith(packageName.replace(".", "/"))) { + String className = classFileName.substring(0, + classFileName.length() - ".class".length()) + .replace("/", "."); + classNames.add(className); + } + } + } + } + return classNames; + } + + private List getClassNameFromJar(String packageName, + boolean searchInChildPackage, File file) { + List classNames = new ArrayList(); + JarFile jarFile = null; + try { + jarFile = new JarFile(file); + Enumeration jarEntries = jarFile.entries(); + while (jarEntries.hasMoreElements()) { + JarEntry jarEntry = jarEntries.nextElement(); + String entryName = jarEntry.getName(); + if (entryName.endsWith(".class")) { + String packagePath = packageName.replace(".", "/") + "/"; + if (searchInChildPackage) { + if (entryName.startsWith(packagePath)) { + entryName = entryName.replace("/", ".").substring( + 0, entryName.lastIndexOf(".")); + classNames.add(entryName); + } + } else { + int index = entryName.lastIndexOf("/"); + String entryPath; + if (index != -1) { + entryPath = entryName.substring(0, index); + } else { + entryPath = entryName; + } + if (entryPath.equals(packagePath)) { + entryName = entryName.replace("/", ".").substring( + 0, entryName.lastIndexOf(".")); + classNames.add(entryName); + } + } + } + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (jarFile != null) { + try { + jarFile.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + return classNames; + } } diff --git a/src/test/java/haflow/test/ClassHelperTest.java b/src/test/java/haflow/test/ClassHelperTest.java new file mode 100644 index 0000000..70d64f1 --- /dev/null +++ b/src/test/java/haflow/test/ClassHelperTest.java @@ -0,0 +1,20 @@ +package haflow.test; + +import haflow.utility.ClassHelper; + +import org.junit.Test; + +import java.util.List; + +public class ClassHelperTest { + @Test + public void test() { + ClassHelper classHelper = new ClassHelper(); + List classes = classHelper.getClassNames("haflow.module", true); + if (classes != null) { + for (String c : classes) { + System.out.println(c); + } + } + } +}