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
pull/5289/head
Andy Wilkinson 9 years ago
parent 779649bfdb
commit ad7cf48497

@ -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>

@ -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,19 +340,49 @@ 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());
setLastAccessTimeIfPossible(entry, renamedEntry);
setLastModifiedTimeIfPossible(entry, renamedEntry);
return renamedEntry;
}
if (entry.getLastModifiedTime() != null) {
renamedEntry.setLastModifiedTime(entry.getLastModifiedTime());
@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.
}
return renamedEntry;
}
}

Loading…
Cancel
Save