1. 程式人生 > >技術總結:用抽象方法制作包掃描工具

技術總結:用抽象方法制作包掃描工具

用抽象方法實現包掃描,並封裝為一個工具。使用者只需實現抽象方法即可對class進行操作。程式碼如下:

/**
 * @author 	yc
 * time 	2018/10/02
 * 包掃描
 */
package yc_util.core;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public abstract class ScannerPackage {
	
	public ScannerPackage() {
	}
	
	public abstract void dealClass(Class<?> klass);

	private void packageScan(File currentfile, String rootPackage) {
		File[] filelist = currentfile.listFiles(new FileFilter() {
			
			@Override
			public boolean accept(File pathname) {
				//挑選出目錄和class檔案;
				if(pathname.isDirectory()) {
					return true;
				}
				return pathname.getName().endsWith(".class");
			}
		});
		
		for(File file : filelist) {
			if(file.isDirectory()) {
				packageScan(file, rootPackage + "." + file.getName());
			} else {
				String fileName = file.getName().replaceAll(".class", "");
				String className = rootPackage + "." + fileName;
				try {
					Class<?> klass = Class.forName(className);
					if(klass.isAnnotation()
							|| klass.isEnum()
							|| klass.isInterface()
							|| klass.isPrimitive()) {
						continue;
					}
					dealClass(klass);
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	private void packageScan(URL url) throws IOException {
		JarURLConnection jarURLConnection = 
                                    (JarURLConnection) url.openConnection();
		JarFile jarFile = jarURLConnection.getJarFile();
		Enumeration<JarEntry> jarEntries = jarFile.entries();
		while (jarEntries.hasMoreElements()) {
			JarEntry jarEntry = jarEntries.nextElement();
			String jarName = jarEntry.getName();
			if(!jarName.endsWith(".class")) {
				continue;
			}
			String className = jarName.replace(".class", "").replace("/", ".");
			try {
				Class<?> klass = Class.forName(className);
				if(klass.isAnnotation()
						|| klass.isEnum()
						|| klass.isInterface()
						|| klass.isPrimitive()) {
					continue;
				}
				dealClass(klass);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void scannerPackage(String rootPackage) {
		String path = rootPackage.replace(".", "/");
		System.out.println(path);
		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
		try {
			Enumeration<URL> urls = classLoader.getResources(path);
			while (urls.hasMoreElements()) {
				URL url = urls.nextElement();
				String urlProtocol = url.getProtocol();
				if(urlProtocol.equals("file")) {
					File file = new File(url.toURI());
					if(!file.exists()) {
						continue;
					}
					packageScan(file, rootPackage);
				} else if(urlProtocol.equals("jar")) {
					System.out.println("11");
					packageScan(url);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
	}
}

具體使用如下:

/**
 * @author 	yc
 * time 	2018/10/02
 * 包掃描
 */
package yc_util.demo;

import yc_util.core.ScannerPackage;

public class Demo {
	public static void main(String[] args) {
		new ScannerPackage() {
			
			@Override
			public void dealClass(Class<?> klass) {
				System.out.println(klass);
			}

		}.scannerPackage("此處輸入包名即可");
	}
}