`

Find Java classes implementing an interface

    博客分类:
  • java
 
阅读更多

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;
	}
	
}

 

分享到:
评论

相关推荐

    Java邮件开发Fundamentals of the JavaMail API

    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 ...

    JAVA面试题目 JAVA面试题目一览表

    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."例如:“可以将‘多态性’比作图书馆,...

    groovy_in_action_draft_ch_01.pdf

    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 ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Scoping Namespaces Nested Classes Nonmember, Static Member, and Global Functions Local Variables Static and Global Variables Classes Doing Work in Constructors Default Constructors Explicit ...

    Visual C++ 编程资源大全(英文源码 表单)

    72.zip Extension to the STL find_if and for_each 扩充STL库(5KB)&lt;END&gt;&lt;br&gt;73,73.zip Change from child window to popup window (and back) 将一个子窗口改成弹出式窗口(5KB)&lt;END&gt;&lt;br&gt;74,74.zip ...

    Lerner -- Python Workout. 50 Essential Exercises -- 2020.pdf

    - **Objective:** Convert an integer to its hexadecimal representation. - **Key Concepts:** - Using the `hex()` built-in function. - Understanding hexadecimal number system. 5. **Pig Latin** - **...

    JProfiler Helper

    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 9.1 5270 ReadMe 中文 汉化 中英文对照版 01d

    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 9.1 5270 Beta 中文 汉化 中英文对照版

    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 ...

    DbfDotNet_version_1.0_Source

    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...

    C# Game Programming Cookbook for Unity 3D - 2014

    Acknowledgments xiii Introduction xv 1. Making Games the Modular Way 1 1.1 Important Programming Concepts.....................................2 1.1.1 Manager and Controller Scripts.......................

    BobBuilder_app

    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 ...

Global site tag (gtag.js) - Google Analytics