[Arakhnę-Dev] [38] Add getSubClasses() function into ReflectionUtil class. |
[ Thread Index |
Date Index
| More arakhne.org/dev Archives
]
Revision: 38
Author: galland
Date: 2009-04-03 01:04:53 +0200 (Fri, 03 Apr 2009)
Log Message:
-----------
Add getSubClasses() function into ReflectionUtil class.
Modified Paths:
--------------
trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ReflectionUtil.java
trunk/arakhneVmutils/java/src/main/resources/Changelog
trunk/arakhneVmutils/java/src/main/resources/VERSION
trunk/pom.xml
Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ReflectionUtil.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ReflectionUtil.java 2009-03-10 10:17:48 UTC (rev 37)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ReflectionUtil.java 2009-04-02 23:04:53 UTC (rev 38)
@@ -26,6 +26,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
+import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
@@ -333,4 +334,109 @@
}
}
+ /**
+ * Replies the list of all the subclasses of the given class
+ * in the current classpath..
+ * @param className is the name of the class to explore.
+ * @return the list of subclasses.
+ */
+ public static <T> Collection<Class<? extends T>> getSubClasses(Class<T> className) {
+ Collection<Class<? extends T>> classes = new ArrayList<Class<? extends T>>();
+
+ String[] entries = System.getProperty("java.class.path").split(System.getProperty("path.separator")); //$NON-NLS-1$//$NON-NLS-2$
+ String lentry;
+
+ for(String path : entries) {
+ lentry = path.toLowerCase();
+ if(lentry.endsWith(".jar") || lentry.endsWith(".war")) { //$NON-NLS-1$//$NON-NLS-2$
+ getSubClassesFromJar(classes, path, className);
+ }else{
+ getSubClassesFromFileSystem(classes, path, className);
+ }
+
+ }
+
+ return classes;
+ }
+
+ @SuppressWarnings("unchecked")
+ private static <T> void getSubClassesFromJar(Collection<Class<? extends T>> classes, String jarFilename, Class<T> className) {
+ try {
+ JarFile jarFile = new JarFile(jarFilename);
+ String classN = className.getCanonicalName();
+ if (classN!=null) {
+ Enumeration<JarEntry> entries = jarFile.entries();
+ JarEntry entry;
+ String entryPath, entryClassname;
+
+ while (entries.hasMoreElements()) {
+ entry = entries.nextElement();
+ entryPath = entry.getName();
+
+ // In package and not inner class
+ if (entryPath.endsWith(".class") //$NON-NLS-1$
+ && !entryPath.contains("$")) { //$NON-NLS-1$
+ entryClassname = entryPath.substring(0, entryPath.length()-6).replaceAll(File.separator, "."); //$NON-NLS-1$
+ try {
+ Class<?> clazz = Class.forName(entryClassname);
+ if (className.isAssignableFrom(clazz))
+ classes.add((Class<? extends T>)clazz);
+ }
+ catch(Throwable _) {
+ //
+ }
+ }
+ }
+ }
+ }
+ catch(IOException _) {
+ //
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private static <T> void getSubClassesFromFileSystem(Collection<Class<? extends T>> classes, String directory, Class<T> className) {
+ String classN = className.getCanonicalName();
+ if (classN!=null) {
+ List<String> directories = new ArrayList<String>();
+ directories.add(""); //$NON-NLS-1$
+
+ String ldir, entryClassname;
+ File dir, fullFile;
+
+ while (!directories.isEmpty()) {
+ ldir = directories.remove(0);
+ dir = new File(directory, ldir);
+ if (dir.isDirectory()) {
+ for(String entryPath : dir.list()) {
+
+ fullFile = new File(dir,entryPath);
+
+ if (fullFile.isDirectory()) {
+ if (ldir==null || "".equals(ldir)) { //$NON-NLS-1$
+ directories.add(entryPath);
+ }
+ else {
+ directories.add(new File(ldir,entryPath).toString());
+ }
+ }
+ // In package and not inner class
+ else if (entryPath.endsWith(".class") //$NON-NLS-1$
+ && !entryPath.contains("$")) { //$NON-NLS-1$
+ entryClassname = ldir.replaceAll(File.separator, ".") + "." + FileSystem.shortBasename(entryPath); //$NON-NLS-1$ //$NON-NLS-2$
+ try {
+ Class<?> clazz = Class.forName(entryClassname);
+ if (className.isAssignableFrom(clazz))
+ classes.add((Class<? extends T>)clazz);
+ }
+ catch(Throwable _) {
+ //
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
}
Modified: trunk/arakhneVmutils/java/src/main/resources/Changelog
===================================================================
--- trunk/arakhneVmutils/java/src/main/resources/Changelog 2009-03-10 10:17:48 UTC (rev 37)
+++ trunk/arakhneVmutils/java/src/main/resources/Changelog 2009-04-02 23:04:53 UTC (rev 38)
@@ -1,3 +1,9 @@
+arakhneVmutils-4.0
+
+* Add function getSubClasses() in ReflectionUtil class.
+
+-- Stephane GALLAND <galland@xxxxxxxxxxx> Fri, 03 Apr 2009 01:03:45 +0200
+
arakhneVmutils-3.0
* Add class ReflectionUtil.
Modified: trunk/arakhneVmutils/java/src/main/resources/VERSION
===================================================================
--- trunk/arakhneVmutils/java/src/main/resources/VERSION 2009-03-10 10:17:48 UTC (rev 37)
+++ trunk/arakhneVmutils/java/src/main/resources/VERSION 2009-04-02 23:04:53 UTC (rev 38)
@@ -1 +1 @@
-arakhneVmutils 3.0
+arakhneVmutils 4.0
Modified: trunk/pom.xml
===================================================================
--- trunk/pom.xml 2009-03-10 10:17:48 UTC (rev 37)
+++ trunk/pom.xml 2009-04-02 23:04:53 UTC (rev 38)
@@ -19,7 +19,7 @@
<version_myjdk>1.6</version_myjdk>
<version_arakhnelogger>1.1-SNAPSHOT</version_arakhnelogger>
<version_arakhnerefs>5.1-SNAPSHOT</version_arakhnerefs>
- <version_arakhnevmutils>3.0-SNAPSHOT</version_arakhnevmutils>
+ <version_arakhnevmutils>4.0-SNAPSHOT</version_arakhnevmutils>
</properties>
<dependencyManagement>