`

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

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

    Introduction.to.Android.Application.Development(4th,2013.12) pdf

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

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

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

    VB.NET Developer's Guide(4574).pdf

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

    pro_apache_third_edition..pdf

    IP Addresses and Network Classes...................................................................19 Special IP Addresses.................................................................................

Global site tag (gtag.js) - Google Analytics