Update Repackager to use Java 8 APIs safely

Previously, Repackager used Java 8 APIs without protecting against the
possibility of a NoSuchMethodError on earlier versions of Java.
This commit wraps the Java 8 APIs in try-catch blocks to ensure
that they do not cause a failure on Java versions before 8, while
still making full use of Java 8's capabilities when available.

Closes gh-5280
This commit is contained in:
Andy Wilkinson 2016-02-29 15:11:28 +00:00
parent 779649bfdb
commit ad7cf48497
2 changed files with 41 additions and 9 deletions

View File

@ -135,6 +135,7 @@
<annotations>
<annotation>org.springframework.boot.loader.tools.UsesUnsafeJava</annotation>
<annotation>org.springframework.lang.UsesJava7</annotation>
<annotation>org.springframework.lang.UsesJava8</annotation>
</annotations>
</configuration>
</plugin>

View File

@ -29,6 +29,7 @@ import java.util.jar.JarFile;
import java.util.jar.Manifest;
import org.springframework.boot.loader.tools.JarWriter.EntryTransformer;
import org.springframework.lang.UsesJava8;
/**
* Utility class that can be used to repackage an archive so that it can be executed using
@ -339,21 +340,51 @@ public class Repackager {
}
renamedEntry.setCompressedSize(entry.getCompressedSize());
renamedEntry.setCrc(entry.getCrc());
if (entry.getCreationTime() != null) {
renamedEntry.setCreationTime(entry.getCreationTime());
}
setCreationTimeIfPossible(entry, renamedEntry);
if (entry.getExtra() != null) {
renamedEntry.setExtra(entry.getExtra());
}
if (entry.getLastAccessTime() != null) {
renamedEntry.setLastAccessTime(entry.getLastAccessTime());
}
if (entry.getLastModifiedTime() != null) {
renamedEntry.setLastModifiedTime(entry.getLastModifiedTime());
}
setLastAccessTimeIfPossible(entry, renamedEntry);
setLastModifiedTimeIfPossible(entry, renamedEntry);
return renamedEntry;
}
@UsesJava8
private void setCreationTimeIfPossible(JarEntry source, JarEntry target) {
try {
if (source.getCreationTime() != null) {
target.setCreationTime(source.getCreationTime());
}
}
catch (NoSuchMethodError ex) {
// Not running on Java 8. Continue.
}
}
@UsesJava8
private void setLastAccessTimeIfPossible(JarEntry source, JarEntry target) {
try {
if (source.getLastAccessTime() != null) {
target.setLastAccessTime(source.getLastAccessTime());
}
}
catch (NoSuchMethodError ex) {
// Not running on Java 8. Continue.
}
}
@UsesJava8
private void setLastModifiedTimeIfPossible(JarEntry source, JarEntry target) {
try {
if (source.getLastModifiedTime() != null) {
target.setLastModifiedTime(source.getLastModifiedTime());
}
}
catch (NoSuchMethodError ex) {
// Not running on Java 8. Continue.
}
}
}
}