Hello everyone,
I would like to use reflection to display all the classes in a package
that extend a specific base class. I am using the examples as given
from here to work with Java Reflection API:
http://forums.sun.com/thread.jspa?threadID=341935&start=15&tstart=0
The sample class with a main method is given below:
public class ReflectionUtil{
public static List<Class> getClassesForPackage(String pckgname)
throws
ClassNotFoundException {
// This will hold a list of directories matching the pckgname.
//There may be more than one if a package is split over
multiple jars/paths
List<Class> classes = new ArrayList<Class>();
ArrayList<File> directories = new ArrayList<File>();
try {
ClassLoader cld =
Thread.currentThread().getContextClassLoader();
if (cld == null) {
throw new ClassNotFoundException("Can't get class
loader.");
}
// Ask for all resources for the path
Enumeration<URL> resources =
cld.getResources(pckgname.replace('.', '/'));
while (resources.hasMoreElements()) {
URL res = resources.nextElement();
if (res.getProtocol().equalsIgnoreCase("jar")){
JarURLConnection conn = (JarURLConnection)
res.openConnection();
JarFile jar = conn.getJarFile();
for (JarEntry e:Collections.list(jar.entries())){
if
(e.getName().startsWith(pckgname.replace('.', '/'))
&& e.getName().endsWith(".class") && !
e.getName().contains("$")){
String className =
e.getName().replace("/",".").substring(0,e.getName().length() - 6);
System.out.println(className);
classes.add(Class.forName(className));
}
}
}else
directories.add(new
File(URLDecoder.decode(res.getPath(), "UTF-8")));
}
} catch (NullPointerException x) {
throw new ClassNotFoundException(pckgname + " does not
appear to be " +
"a valid package (Null pointer exception)");
} catch (UnsupportedEncodingException encex) {
throw new ClassNotFoundException(pckgname + " does not
appear to be " +
"a valid package (Unsupported encoding)");
} catch (IOException ioex) {
throw new ClassNotFoundException("IOException was thrown
when trying " +
"to get all resources for " + pckgname);
}
// For every directory identified capture all the .class files
for (File directory : directories) {
if (directory.exists()) {
// Get the list of the files contained in the package
String[] files = directory.list();
for (String file : files) {
// we are only interested in .class files
if (file.endsWith(".class")) {
// removes the .class extension
classes.add(Class.forName(pckgname + '.'
+ file.substring(0, file.length() -
6)));
}
}
} else {
throw new ClassNotFoundException(pckgname + " (" +
directory.getPath() +
") does not appear to be a valid
package");
}
}
return classes;
}
public static List<Class> getClassessOfSuperClass(String
thePackage, Class superClass) {
List<Class> classList = new ArrayList<Class>();
try {
for (Class discovered : getClassesForPackage(thePackage))
{
if
(Arrays.asList(discovered.getSuperclass()).contains(superClass)) {
classList.add(discovered);
}
}
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
return classList;
}
public static void main(String[] args) {
try {
List<Class> classes =
getClassessOfSuperClass(Constants.ALG_PACKAGE_NAME,IAlgorithmPanel.class);
Iterator<Class> i = classes.iterator();
while(i.hasNext()){
Class c = i.next();
System.out.println("Classes:"+c.getSimpleName());
}
} catch (Exception ex) {
Logger.getLogger(DynamicClassLoading.class.getName()).log(Level.SEVERE,
null, ex);
}
}
}
However, i do not get any classes listed. I tested the same class as a
normal Java project and it works. I am sure that Android deploys the
application in a different way/context where i cannot refer to a
package name directly.
Could you please point me to the right direction.
Many thanks
Cheers,
Rahul
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en