mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix code and add test
This commit is contained in:
parent
2c9c52b1b9
commit
b7746fb230
@ -14,6 +14,7 @@ package org.dromara.hutool.core.classloader;
|
||||
|
||||
import org.dromara.hutool.core.exception.HutoolException;
|
||||
import org.dromara.hutool.core.io.IORuntimeException;
|
||||
import org.dromara.hutool.core.io.file.FileNameUtil;
|
||||
import org.dromara.hutool.core.io.file.FileUtil;
|
||||
import org.dromara.hutool.core.net.url.UrlUtil;
|
||||
import org.dromara.hutool.core.reflect.method.MethodUtil;
|
||||
@ -125,10 +126,9 @@ public class JarClassLoader extends URLClassLoader {
|
||||
* @param jarFileOrDir jar文件或者jar文件所在目录
|
||||
* @return this
|
||||
*/
|
||||
@SuppressWarnings("resource")
|
||||
public JarClassLoader addJar(final File jarFileOrDir) {
|
||||
if (isJarFile(jarFileOrDir)) {
|
||||
return addURL(jarFileOrDir);
|
||||
}
|
||||
// loopJar方法中,如果传入的是jar文件,直接返回此文件
|
||||
final List<File> jars = loopJar(jarFileOrDir);
|
||||
for (final File jar : jars) {
|
||||
addURL(jar);
|
||||
@ -174,10 +174,8 @@ public class JarClassLoader extends URLClassLoader {
|
||||
* @since 4.4.2
|
||||
*/
|
||||
private static boolean isJarFile(final File file) {
|
||||
if (!FileUtil.isFile(file)) {
|
||||
return false;
|
||||
}
|
||||
return file.getPath().toLowerCase().endsWith(".jar");
|
||||
return FileUtil.isFile(file) &&
|
||||
FileNameUtil.isType(file.getName(), FileNameUtil.EXT_JAR);
|
||||
}
|
||||
// ------------------------------------------------------------------- Private method end
|
||||
}
|
||||
|
@ -12,10 +12,21 @@
|
||||
|
||||
package org.dromara.hutool.core.classloader;
|
||||
|
||||
import org.dromara.hutool.core.collection.iter.EnumerationIter;
|
||||
import org.dromara.hutool.core.io.file.FileUtil;
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.dromara.hutool.core.map.Dict;
|
||||
import org.dromara.hutool.core.reflect.ClassUtil;
|
||||
import org.dromara.hutool.core.reflect.FieldUtil;
|
||||
import org.dromara.hutool.core.reflect.method.MethodUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.sql.Driver;
|
||||
import java.sql.DriverManager;
|
||||
|
||||
public class ClassLoaderUtilTest {
|
||||
|
||||
@Test
|
||||
@ -61,4 +72,18 @@ public class ClassLoaderUtilTest {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
void loadClassFromJarTest() {
|
||||
final JarClassLoader classLoader = ClassLoaderUtil.getJarClassLoader(
|
||||
FileUtil.file("D:\\m2_repo\\com\\sap\\cloud\\db\\jdbc\\ngdbc\\2.18.13\\ngdbc-2.18.13.jar"));
|
||||
|
||||
final Class<?> aClass = ClassUtil.forName("com.sap.db.jdbc.Driver", true, classLoader);
|
||||
final Field instance = FieldUtil.getField(aClass, "INSTANCE");
|
||||
Console.log(FieldUtil.getFieldValue(null, instance));
|
||||
|
||||
final Field version = FieldUtil.getField(aClass, "JAVA_VERSION");
|
||||
Console.log(FieldUtil.getFieldValue(null, version));
|
||||
}
|
||||
}
|
||||
|
@ -292,6 +292,13 @@ public class FileUtilTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void loopFileTest() {
|
||||
final List<File> files = FileUtil.loopFiles("D:\\m2_repo\\cglib\\cglib\\3.3.0\\cglib-3.3.0.jar");
|
||||
Console.log(files);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void loopFilesTest2() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user