New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
8258117: jar tool sets the time stamp of module-info.class entries to the current time #5486
Closed
+285
−22
Closed
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e219308
8258117: jar tool sets the time stamp of module-info.class entries to…
jaikiran d91f02a
Merge latest from master branch
jaikiran 18b3bc5
Merge latest from master branch
jaikiran 57e1753
convert test to testng
jaikiran 5c0d89e
Remove "final" usage from test
jaikiran 2c0246f
use testng asserts
jaikiran 945fde6
Merge latest from master branch
jaikiran 1196acc
review suggestion - store e.getValue() and reuse the stored value
jaikiran 4f6b6df
review suggestion - Use Path.of instead of Paths.get in testcase
jaikiran 6fb7f95
review suggestion - split into multiple statements to make it easily …
jaikiran 8ff7583
review suggestion - use FileTime instead of Long to prevent any poten…
jaikiran 9ab5ad8
use proper FileTime centric APIs
jaikiran 6f1c1b6
Merge latest from master branch
jaikiran e189338
review suggestion - use if/else instead of ternary operator
jaikiran e668ef7
review suggestion - change javadoc to mention module-info.class inste…
jaikiran bdc741c
review suggestion - format code in ModuleInfoEntry to use 4 space ind…
jaikiran File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -44,6 +44,7 @@ | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardCopyOption; | ||
import java.nio.file.attribute.FileTime; | ||
import java.text.MessageFormat; | ||
import java.util.*; | ||
import java.util.function.Consumer; | ||
@@ -120,7 +121,7 @@ public int hashCode() { | ||
Set<Entry> entries = new LinkedHashSet<>(); | ||
|
||
// module-info.class entries need to be added/updated. | ||
Map<String,byte[]> moduleInfos = new HashMap<>(); | ||
Map<String, ModuleInfoEntry> moduleInfos = new HashMap<>(); | ||
|
||
// A paths Set for each version, where each Set contains directories | ||
// specified by the "-C" operation. | ||
@@ -806,7 +807,10 @@ private void expand(File dir, String[] files, Set<String> cpaths, int version) | ||
if (f.isFile()) { | ||
Entry e = new Entry(f, name, false); | ||
if (isModuleInfoEntry(name)) { | ||
moduleInfos.putIfAbsent(name, Files.readAllBytes(f.toPath())); | ||
Path path = f.toPath(); | ||
byte[] fileContent = Files.readAllBytes(path); | ||
ModuleInfoEntry mie = new StreamedModuleInfoEntry(name, fileContent, Files.getLastModifiedTime(path)); | ||
moduleInfos.putIfAbsent(name, mie); | ||
if (uflag) { | ||
entryMap.put(name, e); | ||
} | ||
@@ -907,7 +911,7 @@ private boolean equalsIgnoreCase(String s, String upper) { | ||
*/ | ||
boolean update(InputStream in, OutputStream out, | ||
InputStream newManifest, | ||
Map<String,byte[]> moduleInfos, | ||
Map<String, ModuleInfoEntry> moduleInfos, | ||
JarIndex jarIndex) throws IOException | ||
{ | ||
ZipInputStream zis = new ZipInputStream(in); | ||
@@ -956,7 +960,7 @@ boolean update(InputStream in, OutputStream out, | ||
return false; | ||
} | ||
} else if (moduleInfos != null && isModuleInfoEntry) { | ||
moduleInfos.putIfAbsent(name, zis.readAllBytes()); | ||
moduleInfos.putIfAbsent(name, new StreamedModuleInfoEntry(name, zis.readAllBytes(), e.getLastModifiedTime())); | ||
} else { | ||
boolean isDir = e.isDirectory(); | ||
if (!entryMap.containsKey(name)) { // copy the old stuff | ||
@@ -1040,15 +1044,17 @@ private void addIndex(JarIndex index, ZipOutputStream zos) | ||
zos.closeEntry(); | ||
} | ||
|
||
private void updateModuleInfo(Map<String,byte[]> moduleInfos, ZipOutputStream zos) | ||
private void updateModuleInfo(Map<String, ModuleInfoEntry> moduleInfos, ZipOutputStream zos) | ||
throws IOException | ||
{ | ||
String fmt = uflag ? "out.update.module-info": "out.added.module-info"; | ||
for (Map.Entry<String,byte[]> mi : moduleInfos.entrySet()) { | ||
for (Map.Entry<String, ModuleInfoEntry> mi : moduleInfos.entrySet()) { | ||
String name = mi.getKey(); | ||
byte[] bytes = mi.getValue(); | ||
ModuleInfoEntry mie = mi.getValue(); | ||
byte[] bytes = mie.readAllBytes(); | ||
ZipEntry e = new ZipEntry(name); | ||
e.setTime(System.currentTimeMillis()); | ||
FileTime lastModified = mie.getLastModifiedTime(); | ||
e.setLastModifiedTime(lastModified != null ? lastModified : FileTime.fromMillis(System.currentTimeMillis())); | ||
if (flag0) { | ||
crc32ModuleInfo(e, bytes); | ||
} | ||
@@ -1743,12 +1749,23 @@ private File createTemporaryFile(String tmpbase, String suffix) { | ||
|
||
/** | ||
* Associates a module descriptor's zip entry name along with its | ||
* bytes and an optional URI. Used when describing modules. | ||
* bytes and an optional URI. | ||
*/ | ||
interface ModuleInfoEntry { | ||
String name(); | ||
Optional<String> uriString(); | ||
InputStream bytes() throws IOException; | ||
/** | ||
* @return Returns the last modified time of the module descriptor. | ||
* Returns null if the last modified time is unknown or cannot be | ||
* determined. | ||
*/ | ||
FileTime getLastModifiedTime(); | ||
default byte[] readAllBytes() throws IOException { | ||
try (InputStream is = bytes()) { | ||
return is.readAllBytes(); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the comment would be clear if it says "the last modified time of the module-info.class". |
||
} | ||
|
||
static class ZipFileModuleInfoEntry implements ModuleInfoEntry { | ||
@@ -1762,6 +1779,12 @@ static class ZipFileModuleInfoEntry implements ModuleInfoEntry { | ||
@Override public InputStream bytes() throws IOException { | ||
return zipFile.getInputStream(entry); | ||
} | ||
|
||
@Override | ||
public FileTime getLastModifiedTime() { | ||
return entry.getLastModifiedTime(); | ||
} | ||
|
||
/** Returns an optional containing the effective URI. */ | ||
@Override public Optional<String> uriString() { | ||
String uri = (Paths.get(zipFile.getName())).toUri().toString(); | ||
@@ -1773,14 +1796,28 @@ static class ZipFileModuleInfoEntry implements ModuleInfoEntry { | ||
static class StreamedModuleInfoEntry implements ModuleInfoEntry { | ||
private final String name; | ||
private final byte[] bytes; | ||
StreamedModuleInfoEntry(String name, byte[] bytes) { | ||
private final FileTime lastModifiedTime; | ||
|
||
StreamedModuleInfoEntry(String name, byte[] bytes, FileTime lastModifiedTime) { | ||
this.name = name; | ||
this.bytes = bytes; | ||
this.lastModifiedTime = lastModifiedTime; | ||
} | ||
@Override public String name() { return name; } | ||
@Override public InputStream bytes() throws IOException { | ||
return new ByteArrayInputStream(bytes); | ||
} | ||
|
||
@Override | ||
public byte[] readAllBytes() throws IOException { | ||
return bytes; | ||
} | ||
|
||
@Override | ||
public FileTime getLastModifiedTime() { | ||
return lastModifiedTime; | ||
} | ||
|
||
/** Returns an empty optional. */ | ||
@Override public Optional<String> uriString() { | ||
return Optional.empty(); // no URI can be derived | ||
@@ -1832,7 +1869,7 @@ private boolean describeModuleFromStream(FileInputStream fis) | ||
while ((e = zis.getNextEntry()) != null) { | ||
String ename = e.getName(); | ||
if (isModuleInfoEntry(ename)) { | ||
infos.add(new StreamedModuleInfoEntry(ename, zis.readAllBytes())); | ||
infos.add(new StreamedModuleInfoEntry(ename, zis.readAllBytes(), e.getLastModifiedTime())); | ||
} | ||
} | ||
} | ||
@@ -2045,14 +2082,14 @@ static String toBinaryName(String classname) { | ||
return (classname.replace('.', '/')) + ".class"; | ||
} | ||
|
||
private boolean checkModuleInfo(byte[] moduleInfoBytes, Set<String> entries) | ||
private boolean checkModuleInfo(ModuleInfoEntry moduleInfoEntry, Set<String> entries) | ||
throws IOException | ||
{ | ||
boolean ok = true; | ||
if (moduleInfoBytes != null) { // no root module-info.class if null | ||
if (moduleInfoEntry != null) { // no root module-info.class if null | ||
try { | ||
// ModuleDescriptor.read() checks open/exported pkgs vs packages | ||
ModuleDescriptor md = ModuleDescriptor.read(ByteBuffer.wrap(moduleInfoBytes)); | ||
ModuleDescriptor md = ModuleDescriptor.read(moduleInfoEntry.bytes()); | ||
// A module must have the implementation class of the services it 'provides'. | ||
if (md.provides().stream().map(Provides::providers).flatMap(List::stream) | ||
.filter(p -> !entries.contains(toBinaryName(p))) | ||
@@ -2070,15 +2107,19 @@ private boolean checkModuleInfo(byte[] moduleInfoBytes, Set<String> entries) | ||
|
||
/** | ||
* Adds extended modules attributes to the given module-info's. The given | ||
* Map values are updated in-place. Returns false if an error occurs. | ||
* Map values are updated in-place. | ||
*/ | ||
private void addExtendedModuleAttributes(Map<String,byte[]> moduleInfos, | ||
private void addExtendedModuleAttributes(Map<String, ModuleInfoEntry> moduleInfos, | ||
Set<String> packages) | ||
throws IOException | ||
{ | ||
for (Map.Entry<String,byte[]> e: moduleInfos.entrySet()) { | ||
ModuleDescriptor md = ModuleDescriptor.read(ByteBuffer.wrap(e.getValue())); | ||
e.setValue(extendedInfoBytes(md, e.getValue(), packages)); | ||
for (Map.Entry<String, ModuleInfoEntry> e: moduleInfos.entrySet()) { | ||
ModuleInfoEntry mie = e.getValue(); | ||
byte[] bytes = mie.readAllBytes(); | ||
ModuleDescriptor md = ModuleDescriptor.read(ByteBuffer.wrap(bytes)); | ||
byte[] extended = extendedInfoBytes(md, bytes, packages); | ||
// replace the entry value with the extended bytes | ||
e.setValue(new StreamedModuleInfoEntry(mie.name(), extended, mie.getLastModifiedTime())); | ||
} | ||
} | ||
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would be a bit more readable if it were if-then-else.