Skip to content

Commit c48c61c

Browse files
committed
add ClassLoaderUtils.java
1 parent 04b76dc commit c48c61c

File tree

5 files changed

+103
-4
lines changed

5 files changed

+103
-4
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<groupId>com.codingapi.springboot</groupId>
1414
<artifactId>springboot-parent</artifactId>
15-
<version>2.8.5</version>
15+
<version>2.8.6</version>
1616

1717
<url>https://github.com/codingapi/springboot-framewrok</url>
1818
<name>springboot-parent</name>

springboot-starter-data-fast/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>springboot-parent</artifactId>
77
<groupId>com.codingapi.springboot</groupId>
8-
<version>2.8.5</version>
8+
<version>2.8.6</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

springboot-starter-security/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<artifactId>springboot-parent</artifactId>
88
<groupId>com.codingapi.springboot</groupId>
9-
<version>2.8.5</version>
9+
<version>2.8.6</version>
1010
</parent>
1111

1212
<artifactId>springboot-starter-security</artifactId>

springboot-starter/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.codingapi.springboot</groupId>
77
<artifactId>springboot-parent</artifactId>
8-
<version>2.8.5</version>
8+
<version>2.8.6</version>
99
</parent>
1010
<artifactId>springboot-starter</artifactId>
1111

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.codingapi.springboot.framework.utils;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.net.MalformedURLException;
6+
import java.net.URISyntaxException;
7+
import java.net.URL;
8+
import java.net.URLClassLoader;
9+
import java.util.ArrayList;
10+
import java.util.Enumeration;
11+
import java.util.List;
12+
import java.util.jar.JarEntry;
13+
import java.util.jar.JarFile;
14+
15+
public class ClassLoaderUtils {
16+
17+
public static URLClassLoader createClassLoader(String jarPath) throws MalformedURLException {
18+
File file = new File(jarPath);
19+
if(!file.exists()){
20+
throw new RuntimeException("jar file not found:"+jarPath);
21+
}
22+
URL[] urls = new URL[]{file.toURI().toURL()};
23+
return new URLClassLoader(urls, ClassLoader.getSystemClassLoader());
24+
}
25+
26+
public static List<String> findAllClasses(URLClassLoader classLoader) throws URISyntaxException, IOException {
27+
List<String> classNames = new ArrayList<>();
28+
URL[] urls = classLoader.getURLs();
29+
for (URL url : urls) {
30+
if (url.getProtocol().equals("file")) {
31+
File file = new File(url.toURI());
32+
if (file.isDirectory()) {
33+
classNames.addAll(findClassesInDirectory(file, ""));
34+
} else if (file.getName().endsWith(".jar")) {
35+
classNames.addAll(findClassesInJar(new JarFile(file)));
36+
}
37+
}
38+
}
39+
return classNames;
40+
}
41+
42+
public static List<Class<?>> findJarClasses(String jarPath) throws IOException, URISyntaxException {
43+
URLClassLoader classLoader = createClassLoader(jarPath);
44+
List<String> classList = ClassLoaderUtils.findAllClasses(classLoader);
45+
List<Class<?>> classes = new ArrayList<>();
46+
for(String className:classList){
47+
try {
48+
Class<?> driverClass = classLoader.loadClass(className);
49+
classes.add(driverClass);
50+
}catch (NoClassDefFoundError | ClassNotFoundException ignored){}
51+
}
52+
return classes;
53+
}
54+
55+
public static List<Class<?>> findJarClass(String jarPath,Class<?> clazz) throws IOException, URISyntaxException {
56+
URLClassLoader classLoader = createClassLoader(jarPath);
57+
List<String> classList = ClassLoaderUtils.findAllClasses(classLoader);
58+
List<Class<?>> classes = new ArrayList<>();
59+
for(String className:classList){
60+
try {
61+
Class<?> driverClass = classLoader.loadClass(className);
62+
if(clazz.isAssignableFrom(driverClass)){
63+
classes.add(driverClass);
64+
}
65+
}catch (NoClassDefFoundError | ClassNotFoundException ignored){}
66+
}
67+
return classes;
68+
}
69+
70+
private static List<String> findClassesInDirectory(File directory, String packageName) {
71+
List<String> classNames = new ArrayList<>();
72+
File[] files = directory.listFiles();
73+
if (files != null) {
74+
for (File file : files) {
75+
if (file.isDirectory()) {
76+
classNames.addAll(findClassesInDirectory(file, packageName + file.getName() + "."));
77+
} else if (file.getName().endsWith(".class")) {
78+
String className = packageName + file.getName().replace(".class", "");
79+
classNames.add(className);
80+
}
81+
}
82+
}
83+
return classNames;
84+
}
85+
86+
private static List<String> findClassesInJar(JarFile jarFile) {
87+
List<String> classNames = new ArrayList<>();
88+
Enumeration<JarEntry> entries = jarFile.entries();
89+
while (entries.hasMoreElements()) {
90+
JarEntry entry = entries.nextElement();
91+
String name = entry.getName();
92+
if (name.endsWith(".class")) {
93+
String className = name.replace('/', '.').replace(".class", "");
94+
classNames.add(className);
95+
}
96+
}
97+
return classNames;
98+
}
99+
}

0 commit comments

Comments
 (0)