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 ...
In JAVA, 'polymorphism' is like having different types of books (classes) that can fit on the same shelf (interface), making it easier to manage and access them."例如:“可以将‘多态性’比作图书馆,...
It offers several advanced features such as closures, dynamic typing, and the meta-object protocol (MOP), making it an attractive choice for developers who are already familiar with Java. This ...
Scoping Namespaces Nested Classes Nonmember, Static Member, and Global Functions Local Variables Static and Global Variables Classes Doing Work in Constructors Default Constructors Explicit ...
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 ...
- **Objective:** Convert an integer to its hexadecimal representation. - **Key Concepts:** - Using the `hex()` built-in function. - Understanding hexadecimal number system. 5. **Pig Latin** - **...
JProfiler is an advanced performance monitoring and profiling tool designed for Java applications. It provides developers with a detailed analysis of their Java programs' performance, enabling them to...
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 ...
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 ...