get Class Instances By Super class - Android java.lang.reflect

Android examples for java.lang.reflect:Class

Description

get Class Instances By Super class

Demo Code


import java.io.File;
import java.util.ArrayList;

public class Main {
  public static <T> ArrayList<T> getClassInstancesBySuper(Class<T> superClass) {

    ArrayList<T> items = new ArrayList<T>();

    try {//w  w  w.jav a 2  s. c  om
      String packageName = superClass.getPackage().getName();
      Class[] classes = getClasses(packageName);

      for (Class _class : classes) {
        if (!_class.equals(superClass)) {
          Object instance = _class.newInstance();

          try {
            items.add((T) instance);
          } catch (ClassCastException ex) {
            // Ignore such error. I have no idea how to check.
          }
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
    }

    return items;
  }

  public static Class[] getClasses(String pckgname)
      throws ClassNotFoundException {
    ArrayList classes = new ArrayList();
    // Get a File object for the package
    File directory = null;
    try {
      directory = new File(Thread.currentThread().getContextClassLoader()
          .getResource('/' + pckgname.replace('.', '/')).getFile());
    } catch (NullPointerException x) {
      throw new ClassNotFoundException(pckgname
          + " does not appear to be a valid package");
    }
    if (directory.exists()) {
      // Get the list of the files contained in the package
      String[] files = directory.list();
      for (int i = 0; i < files.length; i++) {
        // we are only interested in .class files
        if (files[i].endsWith(".class")) {
          // removes the .class extension
          classes.add(Class.forName(pckgname + '.'
              + files[i].substring(0, files[i].length() - 6)));
        }
      }
    } else {
      throw new ClassNotFoundException(pckgname
          + " does not appear to be a valid package");
    }
    Class[] classesA = new Class[classes.size()];
    classes.toArray(classesA);
    return classesA;
  }
}

Related Tutorials