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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 获取包名类名_获取指定包名下的所有类的类名(全名)

發(fā)布時間:2025/5/22 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 获取包名类名_获取指定包名下的所有类的类名(全名) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

參考來源:

以下代碼一鍵運行:

package test;

import java.io.File;

import java.io.IOException;

import java.net.JarURLConnection;

import java.net.URL;

import java.net.URLClassLoader;

import java.util.Enumeration;

import java.util.HashSet;

import java.util.Set;

import java.util.jar.JarEntry;

import java.util.jar.JarFile;

public class ClassUtils {

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

String packageName = "org.objectweb.asm";

Set classNames = getClassName(packageName, false);

if (classNames != null) {

for (String className : classNames) {

System.out.println(className);

}

}

}

/**

* 獲取某包下所有類

* @param packageName 包名

* @param isRecursion 是否遍歷子包

* @return 類的完整名稱

*/

public static Set getClassName(String packageName, boolean isRecursion) {

Set classNames = null;

ClassLoader loader = Thread.currentThread().getContextClassLoader();

String packagePath = packageName.replace(".", "/");

URL url = loader.getResource(packagePath);

if (url != null) {

String protocol = url.getProtocol();

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

classNames = getClassNameFromDir(url.getPath(), packageName, isRecursion);

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

JarFile jarFile = null;

try{

jarFile = ((JarURLConnection) url.openConnection()).getJarFile();

} catch(Exception e){

e.printStackTrace();

}

if(jarFile != null){

getClassNameFromJar(jarFile.entries(), packageName, isRecursion);

}

}

} else {

/*從所有的jar包中查找包名*/

classNames = getClassNameFromJars(((URLClassLoader)loader).getURLs(), packageName, isRecursion);

}

return classNames;

}

/**

* 從項目文件獲取某包下所有類

* @param filePath 文件路徑

* @param className 類名集合

* @param isRecursion 是否遍歷子包

* @return 類的完整名稱

*/

private static Set getClassNameFromDir(String filePath, String packageName, boolean isRecursion) {

Set className = new HashSet();

File file = new File(filePath);

File[] files = file.listFiles();

for (File childFile : files) {

if (childFile.isDirectory()) {

if (isRecursion) {

className.addAll(getClassNameFromDir(childFile.getPath(), packageName+"."+childFile.getName(), isRecursion));

}

} else {

String fileName = childFile.getName();

if (fileName.endsWith(".class") && !fileName.contains("$")) {

className.add(packageName+ "." + fileName.replace(".class", ""));

}

}

}

return className;

}

/**

* @param jarEntries

* @param packageName

* @param isRecursion

* @return

*/

private static Set getClassNameFromJar(Enumeration jarEntries, String packageName, boolean isRecursion){

Set classNames = new HashSet();

while (jarEntries.hasMoreElements()) {

JarEntry jarEntry = jarEntries.nextElement();

if(!jarEntry.isDirectory()){

/*

* 這里是為了方便,先把"/" 轉成 "." 再判斷 ".class" 的做法可能會有bug

* (FIXME: 先把"/" 轉成 "." 再判斷 ".class" 的做法可能會有bug)

*/

String entryName = jarEntry.getName().replace("/", ".");

if (entryName.endsWith(".class") && !entryName.contains("$") && entryName.startsWith(packageName)) {

entryName = entryName.replace(".class", "");

if(isRecursion){

classNames.add(entryName);

} else if(!entryName.replace(packageName+".", "").contains(".")){

classNames.add(entryName);

}

}

}

}

return classNames;

}

/**

* 從所有jar中搜索該包,并獲取該包下所有類

* @param urls URL集合

* @param packageName 包路徑

* @param isRecursion 是否遍歷子包

* @return 類的完整名稱

*/

private static Set getClassNameFromJars(URL[] urls, String packageName, boolean isRecursion) {

Set classNames = new HashSet();

for (int i = 0; i < urls.length; i++) {

String classPath = urls[i].getPath();

//不必搜索classes文件夾

if (classPath.endsWith("classes/")) {continue;}

JarFile jarFile = null;

try {

jarFile = new JarFile(classPath.substring(classPath.indexOf("/")));

} catch (IOException e) {

e.printStackTrace();

}

if (jarFile != null) {

classNames.addAll(getClassNameFromJar(jarFile.entries(), packageName, isRecursion));

}

}

return classNames;

}

}

總結

以上是生活随笔為你收集整理的java 获取包名类名_获取指定包名下的所有类的类名(全名)的全部內容,希望文章能夠幫你解決所遇到的問題。

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