zoukankan      html  css  js  c++  java
  • java 查找类的所有子类

    package _02;
    
    import java.io.File;
    import java.net.URL;
    
    public class MainTest_FindAllSubClass {
    	public static void main(String[] args) {
    
    		Class<?> clazz = MainTest_FindAllSubClass.class;
    		// 定位到当前的包路径
    		// URL url = Toy.class.getResource("");
    
    		// 定位到bin目录
    		URL url = clazz.getResource("/");
    		System.out.println(url);
    		
    		String fileName = url.getFile();
    		System.out.println(fileName);
    		
    		String pathName = fileName.replaceFirst("/", "");
    		System.out.println(pathName);
    		
    		File rootFile = new File(pathName);
    		
    		setSubList(rootFile, rootFile.getPath() + "\", clazz);
    	}
    
    	/**
    	 * 遍历bin目录下所有文件
    	 * 若是文件夹,则递归将文件夹内文件添加到文件数组
    	 * 若是文件,则将文件加载并强制类型转换为要查找子类的父类,转换无异常,说明加载类是父类的子类
    	 * (当遍历到类自身时,此处认为也属于子类,当然这是我的一厢情愿,我肯定不告诉你--〉我嫌麻烦。。
    	 * 	不过我还是加上了判断--〉自我要求比较高。。
    	 * )
    	 * */
    	public static <T> void setSubList(File rootFile, String parentDirectory,
    			Class<T> parentClass) {
    		if (rootFile.isDirectory()) {
    			File[] files = rootFile.listFiles();
    			for (File file : files) {
    				setSubList(file, parentDirectory, parentClass);
    			}
    		} else {
    			String className = null;
    			try {
    				if (rootFile.getPath().indexOf(".class") != -1) {
    					className = rootFile.getPath().replace(parentDirectory, "")
    							.replace(".class", "").replace("\", ".");
    					Class<?> classObject = Class.forName(className);
    					classObject.asSubclass(parentClass);
    					
    					// 要么是子类,要么是类本身
    					if (! className.equals(parentClass.getCanonicalName())){
    						System.out
    						.println(className + " extends " + parentClass);
    					}else{
    						System.out
    						.println(className + " is " + parentClass);
    					}
    				}
    			} catch (ClassNotFoundException e) {
    				System.err.println("can not find " + className);
    			} catch (ClassCastException e) {
    				System.err.println(className + " do not extends " + parentClass);
    			}
    		}
    	}
    }
    
    // 测试
    class Sub1 extends MainTest_FindAllSubClass {
    }
    
    //测试
    class Sub2 extends MainTest_FindAllSubClass {
    }
    

      

  • 相关阅读:
    Android消息机制(Handler)详述
    Android自定义组件-以饼状图并实现点击事件为例
    Markdown中tab的解析与4个空格 问题
    策略模式(Strategy)
    观察者模式(Observer)
    适配器模式(Adapter Class/Object)
    建造者模式(Builder)
    简单工厂模式、工厂方法模式、抽象工厂模式
    单例模式(Singleton)
    工具推荐:前后端一体化部署,效能提升开源“神器”
  • 原文地址:https://www.cnblogs.com/cnblogszs/p/6033494.html
Copyright © 2011-2022 走看看