Commit ad7cf484 authored by Andy Wilkinson's avatar Andy Wilkinson

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
parent 779649bf
...@@ -135,6 +135,7 @@ ...@@ -135,6 +135,7 @@
<annotations> <annotations>
<annotation>org.springframework.boot.loader.tools.UsesUnsafeJava</annotation> <annotation>org.springframework.boot.loader.tools.UsesUnsafeJava</annotation>
<annotation>org.springframework.lang.UsesJava7</annotation> <annotation>org.springframework.lang.UsesJava7</annotation>
<annotation>org.springframework.lang.UsesJava8</annotation>
</annotations> </annotations>
</configuration> </configuration>
</plugin> </plugin>
......
...@@ -29,6 +29,7 @@ import java.util.jar.JarFile; ...@@ -29,6 +29,7 @@ import java.util.jar.JarFile;
import java.util.jar.Manifest; import java.util.jar.Manifest;
import org.springframework.boot.loader.tools.JarWriter.EntryTransformer; 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 * Utility class that can be used to repackage an archive so that it can be executed using
...@@ -339,19 +340,49 @@ public class Repackager { ...@@ -339,19 +340,49 @@ public class Repackager {
} }
renamedEntry.setCompressedSize(entry.getCompressedSize()); renamedEntry.setCompressedSize(entry.getCompressedSize());
renamedEntry.setCrc(entry.getCrc()); renamedEntry.setCrc(entry.getCrc());
if (entry.getCreationTime() != null) { setCreationTimeIfPossible(entry, renamedEntry);
renamedEntry.setCreationTime(entry.getCreationTime());
}
if (entry.getExtra() != null) { if (entry.getExtra() != null) {
renamedEntry.setExtra(entry.getExtra()); renamedEntry.setExtra(entry.getExtra());
} }
if (entry.getLastAccessTime() != null) { setLastAccessTimeIfPossible(entry, renamedEntry);
renamedEntry.setLastAccessTime(entry.getLastAccessTime()); 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;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment