Find Java classes implementing an interface or Class
Call the method "getAllClassByInterface" with a Class or interface, return all classes implemetnting it.
class ClassUtils{ public static List<Class> getAllClassByInterface(Class c) { List<Class> returnClassList = new ArrayList<Class>(); try{ if(c.isInterface()){ String packageName = c.getPackage().getName(); List<Class> allClass = getClasses(packageName); for(int i=0;i<allClass.size();i++){ if(c.isAssignableFrom(allClass.get(i))){ if(!c.equals(allClass.get(i))){ returnClassList.add(allClass.get(i)); } } }}} catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return returnClassList; } private static List<Class> getClasses(String packageName) throws ClassNotFoundException,IOException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); String path = packageName.replace(".", "/"); Enumeration<URL> resources = classLoader.getResources(path); List<File> dirs = new ArrayList<File>(); while(resources.hasMoreElements()) { URL resource = resources.nextElement(); dirs.add(new File(resource.getFile())); } ArrayList<Class> classes = new ArrayList<Class>(); for(File directory : dirs){ classes.addAll(findClasses(directory,packageName)); } return classes; } private static List<Class> findClasses(File directory, String packageName) throws ClassNotFoundException { List<Class> classes = new ArrayList<Class>(); directory = new File(directory.getPath().replace("%20", " ")); if(!directory.exists()){ return classes; } File[] files = directory.listFiles(); for(File file : files){ if(file.isDirectory()){ assert !file.getName().contains("."); classes.addAll(findClasses(file,packageName+"."+file.getName())); }else if(file.getName().endsWith(".class")){ classes.add(Class.forName(packageName+"."+file.getName().split("\\.")[0])); } } return classes; } }
相关推荐
implementation comes with an SMTP and IMAP4 provider, besides the core classes. If you want to access a POP server with JavaMail 1.1.3, download and install a POP3 provider. Sun has one available ...
Scoping Namespaces Nested Classes Nonmember, Static Member, and Global Functions Local Variables Static and Global Variables Classes Doing Work in Constructors Default Constructors Explicit ...
Where to Find More Information 6 Conventions Used in This Book 7 Contacting the Authors 7 I: An Overview of the Android Platform 1 Introducing Android 11 A Brief History of Mobile Software ...
72.zip Extension to the STL find_if and for_each 扩充STL库(5KB)<END><br>73,73.zip Change from child window to popup window (and back) 将一个子窗口改成弹出式窗口(5KB)<END><br>74,74.zip ...
WTL provides support for implementing many user interface elements, from frame and popup windows, to MDI, standard and common controls, common dialogs, property sheets and pages, GDI objects, UI ...
Creating Multiple Document Interface Applications 297 Creating an MDI Parent Form 297 Creating MDI Child Forms 298 Exercise 7.1 Creating an MDI Child Form 298 Determining the Active MDI Child Form...
WTL provides support for implementing many user interface elements, from frame and popup windows, to MDI, standard and common controls, common dialogs, property sheets and pages, GDI objects, UI ...
In DbfDotNet you manipulate classes with native field types. All data conversion plumbing is done automatically. Very simple entity framework Creating a record and accessing its propery is only what...
Acknowledgments xiii Introduction xv 1. Making Games the Modular Way 1 1.1 Important Programming Concepts.....................................2 1.1.1 Manager and Controller Scripts.......................
Because the data pages are separate from the Page List structure, implementing locking is easy and isolated within a page and not the whole index, not so for normal trees. Splitting a page when full ...
IP Addresses and Network Classes...................................................................19 Special IP Addresses.................................................................................