-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added JDK 9 version for JarEntries.getRealName
- Loading branch information
Showing
3 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
io/src/main/java9/io/smallrye/common/io/jar/JarEntries.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.smallrye.common.io.jar; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.jar.JarEntry; | ||
|
||
public class JarEntries { | ||
|
||
private static final Method REAL_NAME_METHOD; | ||
|
||
static { | ||
Method method; | ||
try { | ||
method = Class.forName("java.util.jar.JarFile$JarFileEntry").getDeclaredMethod("realName"); | ||
method.setAccessible(true); | ||
} catch (NoSuchMethodException | ClassNotFoundException e) { | ||
method = null; | ||
} | ||
REAL_NAME_METHOD = method; | ||
} | ||
|
||
/** | ||
* Returns the real name of this {@link JarEntry}. On Java 8, it returns the {@link JarEntry#getName()} | ||
* On Java 10+, a getRealName() method was added | ||
*/ | ||
public static String getRealName(JarEntry jarEntry) { | ||
if (REAL_NAME_METHOD != null) { | ||
try { | ||
return REAL_NAME_METHOD.invoke(jarEntry).toString(); | ||
} catch (Exception e) { | ||
// This should never happen | ||
} | ||
} | ||
// As a safe net, fallback to the original value | ||
return jarEntry.getName(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters