Skip to content

Commit

Permalink
Added JDK 9 version for JarEntries.getRealName
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Jul 16, 2020
1 parent 40dab94 commit 8ef8d4c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
17 changes: 17 additions & 0 deletions io/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M5</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>coverage</id>
Expand Down
36 changes: 36 additions & 0 deletions io/src/main/java9/io/smallrye/common/io/jar/JarEntries.java
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@

import org.junit.Test;

public class JarEntriesTest {
// This needs to be run as an integration-test
public class JarEntriesIT {

@Test
public void shouldUseRealName() throws IOException {
File tmpFile = MultiReleaseJarGenerator.generateMultiReleaseJar();
JarFile jarFile = JarFiles.create(tmpFile);
JarEntry jarEntry = jarFile.getJarEntry("foo.txt");
assertEquals(jarEntry.getRealName(), JarEntries.getRealName(jarEntry));
assertEquals("META-INF/versions/9/foo.txt", JarEntries.getRealName(jarEntry));
}

}

0 comments on commit 8ef8d4c

Please sign in to comment.