日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java 扫描类_Java扫描指定包中所有类

發布時間:2025/3/12 java 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 扫描类_Java扫描指定包中所有类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 掃描類

import java.io.File;

import java.io.FilenameFilter;

import java.io.IOException;

import java.net.JarURLConnection;

import java.net.URL;

import java.util.Enumeration;

import java.util.HashMap;

import java.util.Map;

import java.util.jar.JarEntry;

import java.util.jar.JarFile;

import com.cnp.andromeda.common.util.StringUtil;

/**

* @Author

* @Description 包掃描器

* @CopyRight

*/

public class ClassScanner{

private Map> classes = new HashMap>();

private FilenameFilter javaClassFilter; // 類文件過濾器,只掃描一級類

private final String CLASS_FILE_SUFFIX = ".class"; // Java字節碼文件后綴

private String packPrefix; // 包路徑根路勁

public ClassScanner(){

javaClassFilter = new FilenameFilter(){

@Override

public boolean accept(File dir, String name){

// 排除內部內

return !name.contains("$");

}

};

}

/**

* @Title: scanning

* @Description 掃描指定包(本地)

* @param basePath 包所在的根路徑

* @param packagePath 目標包路徑

* @return Integer 被掃描到的類的數量

* @throws ClassNotFoundException

*/

public Integer scanning(String basePath, String packagePath) throws ClassNotFoundException{

packPrefix = basePath;

String packTmp = packagePath.replace('.', '/');

File dir = new File(basePath, packTmp);

// 不是文件夾

if(dir.isDirectory()){

scan0(dir);

}

return classes.size();

}

/**

* @Title: scanning

* @Description 掃描指定包, Jar或本地

* @param packagePath 包路徑

* @param recursive 是否掃描子包

* @return Integer 類數量

*/

public Integer scanning(String packagePath, boolean recursive){

Enumeration dir;

String filePackPath = packagePath.replace('.', '/');

try{

// 得到指定路徑中所有的資源文件

dir = Thread.currentThread().getContextClassLoader().getResources(filePackPath);

packPrefix = Thread.currentThread().getContextClassLoader().getResource("").getPath();

if(System.getProperty("file.separator").equals("\\")){

packPrefix = packPrefix.substring(1);

}

// 遍歷資源文件

while(dir.hasMoreElements()){

URL url = dir.nextElement();

String protocol = url.getProtocol();

if("file".equals(protocol)){

File file = new File(url.getPath().substring(1));

scan0(file);

} else if("jar".equals(protocol)){

scanJ(url, recursive);

}

}

}

catch(Exception e){

throw new RuntimeException(e);

}

return classes.size();

}

/**

* @Title: scanJ

* @Description 掃描Jar包下所有class

* @param url jar-url路徑

* @param recursive 是否遞歸遍歷子包

* @throws IOException

* @throws ClassNotFoundException

*/

private void scanJ(URL url, boolean recursive) throws IOException, ClassNotFoundException{

JarURLConnection jarURLConnection = (JarURLConnection)url.openConnection();

JarFile jarFile = jarURLConnection.getJarFile();

// 遍歷Jar包

Enumeration entries = jarFile.entries();

while(entries.hasMoreElements()){

JarEntry jarEntry = (JarEntry)entries.nextElement();

String fileName = jarEntry.getName();

if (jarEntry.isDirectory()) {

if (recursive) {

}

continue;

}

// .class

if(fileName.endsWith(CLASS_FILE_SUFFIX)){

String className = fileName.substring(0, fileName.indexOf('.')).replace('/', '.');

classes.put(className, Class.forName(className));

}

}

}

/**

* @Title: scan0

* @Description 執行掃描

* @param dir Java包文件夾

* @throws ClassNotFoundException

*/

private void scan0(File dir) throws ClassNotFoundException{

File[] fs = dir.listFiles(javaClassFilter);

for(int i = 0; fs != null && i < fs.length; i++){

File f = fs[i];

String path = f.getAbsolutePath();

// 跳過其他文件

if(path.endsWith(CLASS_FILE_SUFFIX)){

String className = StringUtil.getPackageByPath(f, packPrefix); // 獲取包名

classes.put(className, Class.forName(className));

}

}

}

/**

* @Title: getClasses

* @Description 獲取包中所有類

* @return Map<String,Class<?>> K:類全名, V:Class字節碼

*/

public Map> getClasses(){

return classes;

}

public static void main(String[] args) throws ClassNotFoundException{

ClassScanner cs = new ClassScanner();

int c = cs.scanning("W:/IWiFI/Code/trunk/operation/target/classes/", "com/cnp/andromeda/common/util/");

System.out.println(c);

System.out.println(cs.getClasses().keySet());

ClassScanner cs2 = new ClassScanner();

int c2 = cs2.scanning("com.cnp.swarm", false);

System.out.println(c2);

System.out.println(cs2.getClasses().keySet());

}

}

2. 依賴工具類

import java.io.File;

/**

* @FileName: StringUtil.java

* @Author

* @Description:

* @Date 2014年11月16日 上午10:04:03

* @CopyRight

*/

/**

* 字符串工具類

*/

public final class StringUtil {

/**

* @Title: getMethodName

* @Description: 獲取對象類型屬性的get方法名

* @param propertyName

* 屬性名

* @return String "get"開頭且參數(propertyName)值首字母大寫的字符串

*/

public static String convertToReflectGetMethod(String propertyName) {

return "get" + toFirstUpChar(propertyName);

}

/**

* @Title: convertToReflectSetMethod

* @Description: 獲取對象類型屬性的set方法名

* @param propertyName

* 屬性名

* @return String "set"開頭且參數(propertyName)值首字母大寫的字符串

*/

public static String convertToReflectSetMethod(String propertyName) {

return "set" + toFirstUpChar(propertyName);

}

/**

* @Title: toFirstUpChar

* @Description: 將字符串的首字母大寫

* @param target

* 目標字符串

* @return String 首字母大寫的字符串

*/

public static String toFirstUpChar(String target) {

StringBuilder sb = new StringBuilder(target);

sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));

return sb.toString();

}

/**

* @Title: getFileSuffixName

* @Description: 獲取文件名后綴

* @param fileName

* 文件名

* @return String 文件名后綴。如:jpg

*/

public static String getFileSuffixName(String fileName) {

return fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();

}

/**

*

* @Title: checkStringIsNotEmpty

* @Description:驗證字符串是否不為空

* @param stringValue

* 傳入要驗證的字符串

* @return boolean true:不為 空 或 不為null; false:值為 空 或 為null

*/

public static boolean isNotEmpty(String stringValue) {

if (null == stringValue || "".equals(stringValue.trim())) {

return false;

}

return true;

}

/**

* @Title: getPackageByPath

* @Description 通過指定文件獲取類全名

* @param classFile 類文件

* @return String 類全名

*/

public static String getPackageByPath(File classFile, String exclude){

if(classFile == null || classFile.isDirectory()){

return null;

}

String path = classFile.getAbsolutePath().replace('\\', '/');

path = path.substring(path.indexOf(exclude) + exclude.length()).replace('/', '.');

if(path.startsWith(".")){

path = path.substring(1);

}

if(path.endsWith(".")){

path = path.substring(0, path.length() - 1);

}

return path.substring(0, path.lastIndexOf('.'));

}

}

總結

以上是生活随笔為你收集整理的java 扫描类_Java扫描指定包中所有类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。