fixed ClassUtil getResource return null bug

This commit is contained in:
胡金宇 2017-08-29 09:52:58 +08:00
parent ffcee5949f
commit 2d84c8daa3

View File

@ -42,8 +42,7 @@ public final class ClassUtil {
String packageFileName = packageName.replace(POINT, File.separator); String packageFileName = packageName.replace(POINT, File.separator);
URL fullPath = getDefaultClassLoader().getResource(packageFileName); URL fullPath = getDefaultClassLoader().getResource(packageFileName);
if (fullPath == null) { if (fullPath == null) {
fullPath = ClassUtil.class.getClassLoader().getResource( fullPath = ClassUtil.class.getProtectionDomain().getCodeSource().getLocation();
packageFileName);
} }
String protocol = fullPath.getProtocol(); String protocol = fullPath.getProtocol();
if (protocol.equals(ServerToolkits.PROTOCOL_FILE)) { if (protocol.equals(ServerToolkits.PROTOCOL_FILE)) {
@ -55,15 +54,12 @@ public final class ClassUtil {
} }
} else if (protocol.equals(ServerToolkits.PROTOCOL_JAR)) { } else if (protocol.equals(ServerToolkits.PROTOCOL_JAR)) {
try { try {
return findClassesByJar( return findClassesByJar(((JarURLConnection) fullPath.openConnection()).getJarFile(), packageName);
((JarURLConnection) fullPath.openConnection())
.getJarFile(),
packageName);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
return null; throw new RuntimeException("the " + packageName + " not found classes.");
} }
/** /**
@ -86,12 +82,10 @@ public final class ClassUtil {
if (files != null) { if (files != null) {
for (File file : files) { for (File file : files) {
if (file.isDirectory()) { if (file.isDirectory()) {
classes.addAll(findClassesByFile(file, packageName + POINT classes.addAll(findClassesByFile(file, packageName + POINT + file.getName()));
+ file.getName()));
} else { } else {
try { try {
classes.add(Class.forName(packageName + POINT classes.add(Class.forName(packageName + POINT + file.getName().replace(CLASS, "")));
+ file.getName().replace(CLASS, "")));
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
; ;
} }
@ -110,8 +104,7 @@ public final class ClassUtil {
* 包的全限类名 * 包的全限类名
* @return * @return
*/ */
private static List<Class<?>> findClassesByJar(JarFile jar, private static List<Class<?>> findClassesByJar(JarFile jar, String packageName) {
String packageName) {
List<Class<?>> classes = new ArrayList<Class<?>>(); List<Class<?>> classes = new ArrayList<Class<?>>();
Enumeration<JarEntry> jarEntries = jar.entries(); Enumeration<JarEntry> jarEntries = jar.entries();
while (jarEntries.hasMoreElements()) { while (jarEntries.hasMoreElements()) {
@ -119,10 +112,8 @@ public final class ClassUtil {
if (jarEntry.isDirectory()) { if (jarEntry.isDirectory()) {
continue; continue;
} }
String className = jarEntry.getName() String className = jarEntry.getName().replace(File.separator, POINT);
.replace(File.separator, POINT); if (!className.startsWith(packageName) || !className.endsWith(CLASS)) {
if (!className.startsWith(packageName)
|| !className.endsWith(CLASS)) {
continue; continue;
} }
try { try {