From df2147cd7c0a3d3924be5c5172dc9f336b62aaba Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 16 Jul 2021 01:34:27 +0800 Subject: [PATCH] add method --- CHANGELOG.md | 3 +- .../java/cn/hutool/core/io/ManifestUtil.java | 88 +++++++++++++------ .../hutool/core/io/resource/ResourceUtil.java | 3 +- .../cn/hutool/core/io/ManifestUtilTest.java | 15 ++++ 4 files changed, 78 insertions(+), 31 deletions(-) create mode 100644 hutool-core/src/test/java/cn/hutool/core/io/ManifestUtilTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e06a0da3..11c82dfc2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ------------------------------------------------------------------------------------------------------------- -# 5.7.5 (2021-07-14) +# 5.7.5 (2021-07-16) ### 🐣新特性 * 【core 】 DateUtil增加ceiling重载,可选是否归零毫秒 @@ -13,6 +13,7 @@ * 【core 】 StrUtil.insert支持负数index * 【core 】 Calculator类支持取模运算(issue#I40DUW@Gitee) * 【core 】 增加Base64.isBase64方法(issue#1710@Github) +* 【core 】 ManifestUtil新增方法getManifest(Class cls)(pr#370@Gitee) ### 🐞Bug修复 * 【core 】 修复FileUtil.normalize处理上级路径的问题(issue#I3YPEH@Gitee) diff --git a/hutool-core/src/main/java/cn/hutool/core/io/ManifestUtil.java b/hutool-core/src/main/java/cn/hutool/core/io/ManifestUtil.java index d97b0abc9..6cb985f0c 100644 --- a/hutool-core/src/main/java/cn/hutool/core/io/ManifestUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/ManifestUtil.java @@ -1,6 +1,6 @@ package cn.hutool.core.io; -import cn.hutool.core.util.ClassUtil; +import cn.hutool.core.io.resource.ResourceUtil; import java.io.File; import java.io.FileInputStream; @@ -22,44 +22,45 @@ public class ManifestUtil { private static final String[] MANIFEST_NAMES = {"Manifest.mf", "manifest.mf", "MANIFEST.MF"}; /** - * 根据 class 获取 jar 包文件的 Manifest + * 根据 class 获取 所在 jar 包文件的 Manifest
+ * 此方法主要利用class定位jar包,如引入hutool-all,则传入hutool中任意一个类即可获取这个jar的Manifest信息
+ * 如果这个类不在jar包中,返回{@code null} * * @param cls 类 * @return Manifest + * @throws IORuntimeException IO异常 */ - public static Manifest getManifest(Class cls) throws IOException { - URL url = ClassUtil.getResourceUrl("", cls); - JarFile jarFile = null; + public static Manifest getManifest(Class cls) throws IORuntimeException { + URL url = ResourceUtil.getResource(null, cls); + URLConnection connection; try { - URLConnection connection = url.openConnection(); - if (connection instanceof JarURLConnection) { - JarURLConnection urlConnection = (JarURLConnection) connection; - jarFile = urlConnection.getJarFile(); - return jarFile.getManifest(); - } - } finally { - IoUtil.close(jarFile); + connection = url.openConnection(); + }catch (final IOException e) { + throw new IORuntimeException(e); + } + + if (connection instanceof JarURLConnection) { + JarURLConnection conn = (JarURLConnection) connection; + return getManifest(conn); } return null; } /** - * 获取 jar 包文件的 Manifest + * 获取 jar 包文件或项目目录下的 Manifest * * @param classpathItem 文件路径 * @return Manifest + * @throws IORuntimeException IO异常 */ - public static Manifest getManifest(File classpathItem) { + public static Manifest getManifest(File classpathItem) throws IORuntimeException{ Manifest manifest = null; if (classpathItem.isFile()) { - JarFile jar = null; - try { - jar = new JarFile(classpathItem); - manifest = jar.getManifest(); - } catch (final IOException ignore) { - } finally { - IoUtil.close(jar); + try (JarFile jarFile = new JarFile(classpathItem)){ + manifest = getManifest(jarFile); + } catch (final IOException e) { + throw new IORuntimeException(e); } } else { final File metaDir = new File(classpathItem, "META-INF"); @@ -73,18 +74,47 @@ public class ManifestUtil { } } } - if (manifestFile != null) { - FileInputStream fis = null; - try { - fis = new FileInputStream(manifestFile); + if (null != manifestFile) { + try(FileInputStream fis = new FileInputStream(manifestFile)){ manifest = new Manifest(fis); - } catch (final IOException ignore) { - } finally { - IoUtil.close(fis); + } catch (final IOException e) { + throw new IORuntimeException(e); } } } return manifest; } + + /** + * 根据 {@link JarURLConnection} 获取 jar 包文件的 Manifest + * + * @param connection {@link JarURLConnection} + * @return Manifest + * @throws IORuntimeException IO异常 + */ + public static Manifest getManifest(JarURLConnection connection) throws IORuntimeException{ + final JarFile jarFile; + try { + jarFile = connection.getJarFile(); + } catch (IOException e) { + throw new IORuntimeException(e); + } + return getManifest(jarFile); + } + + /** + * 根据 {@link JarURLConnection} 获取 jar 包文件的 Manifest + * + * @param jarFile {@link JarURLConnection} + * @return Manifest + * @throws IORuntimeException IO异常 + */ + public static Manifest getManifest(JarFile jarFile) throws IORuntimeException { + try { + return jarFile.getManifest(); + } catch (IOException e) { + throw new IORuntimeException(e); + } + } } diff --git a/hutool-core/src/main/java/cn/hutool/core/io/resource/ResourceUtil.java b/hutool-core/src/main/java/cn/hutool/core/io/resource/ResourceUtil.java index 659ef9155..c6b18ac44 100644 --- a/hutool-core/src/main/java/cn/hutool/core/io/resource/ResourceUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/resource/ResourceUtil.java @@ -174,11 +174,12 @@ public class ResourceUtil { /** * 获得资源相对路径对应的URL * - * @param resource 资源相对路径 + * @param resource 资源相对路径,{@code null}和""都表示classpath根路径 * @param baseClass 基准Class,获得的相对路径相对于此Class所在路径,如果为{@code null}则相对ClassPath * @return {@link URL} */ public static URL getResource(String resource, Class baseClass) { + resource = StrUtil.nullToEmpty(resource); return (null != baseClass) ? baseClass.getResource(resource) : ClassLoaderUtil.getClassLoader().getResource(resource); } diff --git a/hutool-core/src/test/java/cn/hutool/core/io/ManifestUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/io/ManifestUtilTest.java new file mode 100644 index 000000000..86f90ad85 --- /dev/null +++ b/hutool-core/src/test/java/cn/hutool/core/io/ManifestUtilTest.java @@ -0,0 +1,15 @@ +package cn.hutool.core.io; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.jar.Manifest; + +public class ManifestUtilTest { + + @Test + public void getManiFestTest(){ + final Manifest manifest = ManifestUtil.getManifest(Test.class); + Assert.assertNotNull(manifest); + } +}