From 2d84c8daa3edfe64ead1d202b869f627dab7fe82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E9=87=91=E5=AE=87?= Date: Tue, 29 Aug 2017 09:52:58 +0800 Subject: [PATCH] fixed ClassUtil getResource return null bug --- .../com/foxinmy/weixin4j/util/ClassUtil.java | 25 ++++++------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/util/ClassUtil.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/util/ClassUtil.java index 54fcd9b7..0ea5ee2c 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/util/ClassUtil.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/util/ClassUtil.java @@ -42,8 +42,7 @@ public final class ClassUtil { String packageFileName = packageName.replace(POINT, File.separator); URL fullPath = getDefaultClassLoader().getResource(packageFileName); if (fullPath == null) { - fullPath = ClassUtil.class.getClassLoader().getResource( - packageFileName); + fullPath = ClassUtil.class.getProtectionDomain().getCodeSource().getLocation(); } String protocol = fullPath.getProtocol(); if (protocol.equals(ServerToolkits.PROTOCOL_FILE)) { @@ -55,15 +54,12 @@ public final class ClassUtil { } } else if (protocol.equals(ServerToolkits.PROTOCOL_JAR)) { try { - return findClassesByJar( - ((JarURLConnection) fullPath.openConnection()) - .getJarFile(), - packageName); + return findClassesByJar(((JarURLConnection) fullPath.openConnection()).getJarFile(), packageName); } catch (IOException 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) { for (File file : files) { if (file.isDirectory()) { - classes.addAll(findClassesByFile(file, packageName + POINT - + file.getName())); + classes.addAll(findClassesByFile(file, packageName + POINT + file.getName())); } else { try { - classes.add(Class.forName(packageName + POINT - + file.getName().replace(CLASS, ""))); + classes.add(Class.forName(packageName + POINT + file.getName().replace(CLASS, ""))); } catch (ClassNotFoundException e) { ; } @@ -110,8 +104,7 @@ public final class ClassUtil { * 包的全限类名 * @return */ - private static List> findClassesByJar(JarFile jar, - String packageName) { + private static List> findClassesByJar(JarFile jar, String packageName) { List> classes = new ArrayList>(); Enumeration jarEntries = jar.entries(); while (jarEntries.hasMoreElements()) { @@ -119,10 +112,8 @@ public final class ClassUtil { if (jarEntry.isDirectory()) { continue; } - String className = jarEntry.getName() - .replace(File.separator, POINT); - if (!className.startsWith(packageName) - || !className.endsWith(CLASS)) { + String className = jarEntry.getName().replace(File.separator, POINT); + if (!className.startsWith(packageName) || !className.endsWith(CLASS)) { continue; } try {