[Arakhnę-Dev] [39] Add a more generic getSubClasses() function in ReflectionUtil class. |
[ Thread Index |
Date Index
| More arakhne.org/dev Archives
]
Revision: 39
Author: galland
Date: 2009-04-03 20:13:45 +0200 (Fri, 03 Apr 2009)
Log Message:
-----------
Add a more generic getSubClasses() function in 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-04-02 23:04:53 UTC (rev 38)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ReflectionUtil.java 2009-04-03 18:13:45 UTC (rev 39)
@@ -23,6 +23,7 @@
import java.io.File;
import java.io.IOException;
+import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
@@ -341,26 +342,37 @@
* @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>>();
-
+ Collection<Class<? extends T>> list = new ArrayList<Class<? extends T>>();
+ getSubClasses(className, true, true, true, list);
+ return list;
+ }
+
+ /**
+ * 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.
+ * @param allowAbstract is <code>true</code> to allow abstract classes to be put in the replied list
+ * @param allowInterface is <code>true</code> to allow interfaces to be put in the replied list.
+ * @param allowEnum is <code>true</code> to allow enumeration to be put in the replied list.
+ * @param result is the list of subclasses which will be filled by this function.
+ */
+ public static <T> void getSubClasses(Class<T> className, boolean allowAbstract, boolean allowInterface, boolean allowEnum, Collection<Class<? extends T>> result) {
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);
+ getSubClassesFromJar(result, path, className, allowAbstract, allowInterface, allowEnum);
}else{
- getSubClassesFromFileSystem(classes, path, className);
+ getSubClassesFromFileSystem(result, path, className, allowAbstract, allowInterface, allowEnum);
}
}
-
- return classes;
}
@SuppressWarnings("unchecked")
- private static <T> void getSubClassesFromJar(Collection<Class<? extends T>> classes, String jarFilename, Class<T> className) {
+ private static <T> void getSubClassesFromJar(Collection<Class<? extends T>> classes, String jarFilename, Class<T> className, boolean allowAbstract, boolean allowInterface, boolean allowEnum) {
try {
JarFile jarFile = new JarFile(jarFilename);
String classN = className.getCanonicalName();
@@ -379,7 +391,10 @@
entryClassname = entryPath.substring(0, entryPath.length()-6).replaceAll(File.separator, "."); //$NON-NLS-1$
try {
Class<?> clazz = Class.forName(entryClassname);
- if (className.isAssignableFrom(clazz))
+ if ((className.isAssignableFrom(clazz))
+ &&(allowAbstract || !Modifier.isAbstract(clazz.getModifiers()))
+ &&(allowInterface || !clazz.isInterface())
+ &&(allowEnum || !clazz.isEnum()))
classes.add((Class<? extends T>)clazz);
}
catch(Throwable _) {
@@ -395,7 +410,7 @@
}
@SuppressWarnings("unchecked")
- private static <T> void getSubClassesFromFileSystem(Collection<Class<? extends T>> classes, String directory, Class<T> className) {
+ private static <T> void getSubClassesFromFileSystem(Collection<Class<? extends T>> classes, String directory, Class<T> className, boolean allowAbstract, boolean allowInterface, boolean allowEnum) {
String classN = className.getCanonicalName();
if (classN!=null) {
List<String> directories = new ArrayList<String>();
@@ -426,7 +441,10 @@
entryClassname = ldir.replaceAll(File.separator, ".") + "." + FileSystem.shortBasename(entryPath); //$NON-NLS-1$ //$NON-NLS-2$
try {
Class<?> clazz = Class.forName(entryClassname);
- if (className.isAssignableFrom(clazz))
+ if ((className.isAssignableFrom(clazz))
+ &&(allowAbstract || !Modifier.isAbstract(clazz.getModifiers()))
+ &&(allowInterface || !clazz.isInterface())
+ &&(allowEnum || !clazz.isEnum()))
classes.add((Class<? extends T>)clazz);
}
catch(Throwable _) {
Modified: trunk/arakhneVmutils/java/src/main/resources/Changelog
===================================================================
--- trunk/arakhneVmutils/java/src/main/resources/Changelog 2009-04-02 23:04:53 UTC (rev 38)
+++ trunk/arakhneVmutils/java/src/main/resources/Changelog 2009-04-03 18:13:45 UTC (rev 39)
@@ -1,3 +1,9 @@
+arakhneVmutils-4.1
+
+* Add a more generic getSubClasses() function in ReflectionUtil class.
+
+-- Stephane GALLAND <galland@xxxxxxxxxxx> Fri, 03 Apr 2009 20:12:44 +0200
+
arakhneVmutils-4.0
* Add function getSubClasses() in ReflectionUtil class.
Modified: trunk/arakhneVmutils/java/src/main/resources/VERSION
===================================================================
--- trunk/arakhneVmutils/java/src/main/resources/VERSION 2009-04-02 23:04:53 UTC (rev 38)
+++ trunk/arakhneVmutils/java/src/main/resources/VERSION 2009-04-03 18:13:45 UTC (rev 39)
@@ -1 +1 @@
-arakhneVmutils 4.0
+arakhneVmutils 4.1
Modified: trunk/pom.xml
===================================================================
--- trunk/pom.xml 2009-04-02 23:04:53 UTC (rev 38)
+++ trunk/pom.xml 2009-04-03 18:13:45 UTC (rev 39)
@@ -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>4.0-SNAPSHOT</version_arakhnevmutils>
+ <version_arakhnevmutils>4.1-SNAPSHOT</version_arakhnevmutils>
</properties>
<dependencyManagement>