Skip to content

Commit

Permalink
add ClassLoaderUtils.java
Browse files Browse the repository at this point in the history
  • Loading branch information
xlorne committed Sep 7, 2024
1 parent 04b76dc commit c48c61c
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>com.codingapi.springboot</groupId>
<artifactId>springboot-parent</artifactId>
<version>2.8.5</version>
<version>2.8.6</version>

<url>https://github.com/codingapi/springboot-framewrok</url>
<name>springboot-parent</name>
Expand Down
2 changes: 1 addition & 1 deletion springboot-starter-data-fast/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>springboot-parent</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>2.8.5</version>
<version>2.8.6</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion springboot-starter-security/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<artifactId>springboot-parent</artifactId>
<groupId>com.codingapi.springboot</groupId>
<version>2.8.5</version>
<version>2.8.6</version>
</parent>

<artifactId>springboot-starter-security</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion springboot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.codingapi.springboot</groupId>
<artifactId>springboot-parent</artifactId>
<version>2.8.5</version>
<version>2.8.6</version>
</parent>
<artifactId>springboot-starter</artifactId>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.codingapi.springboot.framework.utils;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class ClassLoaderUtils {

public static URLClassLoader createClassLoader(String jarPath) throws MalformedURLException {
File file = new File(jarPath);
if(!file.exists()){
throw new RuntimeException("jar file not found:"+jarPath);
}
URL[] urls = new URL[]{file.toURI().toURL()};
return new URLClassLoader(urls, ClassLoader.getSystemClassLoader());
}

public static List<String> findAllClasses(URLClassLoader classLoader) throws URISyntaxException, IOException {
List<String> classNames = new ArrayList<>();
URL[] urls = classLoader.getURLs();
for (URL url : urls) {
if (url.getProtocol().equals("file")) {
File file = new File(url.toURI());
if (file.isDirectory()) {
classNames.addAll(findClassesInDirectory(file, ""));
} else if (file.getName().endsWith(".jar")) {
classNames.addAll(findClassesInJar(new JarFile(file)));
}
}
}
return classNames;
}

public static List<Class<?>> findJarClasses(String jarPath) throws IOException, URISyntaxException {
URLClassLoader classLoader = createClassLoader(jarPath);
List<String> classList = ClassLoaderUtils.findAllClasses(classLoader);
List<Class<?>> classes = new ArrayList<>();
for(String className:classList){
try {
Class<?> driverClass = classLoader.loadClass(className);
classes.add(driverClass);
}catch (NoClassDefFoundError | ClassNotFoundException ignored){}
}
return classes;
}

public static List<Class<?>> findJarClass(String jarPath,Class<?> clazz) throws IOException, URISyntaxException {
URLClassLoader classLoader = createClassLoader(jarPath);
List<String> classList = ClassLoaderUtils.findAllClasses(classLoader);
List<Class<?>> classes = new ArrayList<>();
for(String className:classList){
try {
Class<?> driverClass = classLoader.loadClass(className);
if(clazz.isAssignableFrom(driverClass)){
classes.add(driverClass);
}
}catch (NoClassDefFoundError | ClassNotFoundException ignored){}
}
return classes;
}

private static List<String> findClassesInDirectory(File directory, String packageName) {
List<String> classNames = new ArrayList<>();
File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
classNames.addAll(findClassesInDirectory(file, packageName + file.getName() + "."));
} else if (file.getName().endsWith(".class")) {
String className = packageName + file.getName().replace(".class", "");
classNames.add(className);
}
}
}
return classNames;
}

private static List<String> findClassesInJar(JarFile jarFile) {
List<String> classNames = new ArrayList<>();
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String name = entry.getName();
if (name.endsWith(".class")) {
String className = name.replace('/', '.').replace(".class", "");
classNames.add(className);
}
}
return classNames;
}
}

0 comments on commit c48c61c

Please sign in to comment.