Skip to content
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

Fix new Error Prone 2.4.0 detected issues. #403

Merged
merged 2 commits into from Jun 24, 2020

Conversation

lazaroclapp
Copy link
Collaborator

This removes the suppressions introduced in #400 and makes more permanent fixes.

Fixes #402.

Copy link
Collaborator

@msridhar msridhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple comments / questions

@@ -279,8 +278,7 @@ public static void annotateBytecodeInJar(
// Do not use JarInputStream in place of JarFile/JarEntry. JarInputStream misses MANIFEST.MF
// while iterating over the entries in the stream.
// Reference: https://bugs.openjdk.java.net/browse/JDK-8215788
for (Enumeration<JarEntry> entries = inputJar.entries(); entries.hasMoreElements(); ) {
JarEntry jarEntry = entries.nextElement();
for (JarEntry jarEntry : inputJar.stream().collect(ImmutableList.toImmutableList())) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In these cases can we do a forEach() on the stream rather than collecting in a list and looping over that? That seems a bit cleaner and avoids copying to another list.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we can't. The code bellow can throw IOException. If that happens within a lambda, we need to catch it there, if it happens within an iteration we can propagate it up.

Copy link
Collaborator

@msridhar msridhar Jun 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a code comment to that effect? Or not if you think it's obvious, up to you

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also done 😄 (before I saw your comment, but not sure if before you made it...)

for (Enumeration<JarEntry> entries = jar1.entries(); entries.hasMoreElements(); ) {
jar1Entries.add(entries.nextElement().getName());
}
jar1.stream().forEach(entry -> jar1Entries.add(entry.getName()));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I would do something like Set<String> jar1Entries = jar1.stream().map(Entry::getName).collect(Collectors::toSet). Not sure of exact syntax but something like that. Same for the other similar cases.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Collaborator

@msridhar msridhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@lazaroclapp lazaroclapp merged commit 7f9aacb into master Jun 24, 2020
@lazaroclapp lazaroclapp deleted the lazaro_fix_new_ep_warnings branch June 24, 2020 18:55
Copy link

@Stephan202 Stephan202 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Late to the party, but maybe useful :)

JarEntry jarEntry = entries.nextElement();
// Note: we can't just put the code below inside stream().forach(), because it can throw
// IOException.
for (JarEntry jarEntry : inputJar.stream().collect(ImmutableList.toImmutableList())) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and below: the list collection (= copy) can be avoided e.g. as follows:

Suggested change
for (JarEntry jarEntry : inputJar.stream().collect(ImmutableList.toImmutableList())) {
for (JarEntry jarEntry : (Iterable<JarEntry>) inputJar.stream()::iterator) {

This could be relevant when dealing with very large JAR files.

(Due to generics this trick doesn't seem to quite work for the ZipFile loops 🤔.)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah very cool! I didn't know you could convert streams to iterators (and iterables) so easily!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Just so you know: aware of this note and planning to do a follow up PR, but I haven't had a chance :) Unless someone else wants to go for it, I'll try to follow up early next week)

lazaroclapp added a commit that referenced this pull request Jun 29, 2020
See #403 discussion, credit for this optimization goes to @Stephan202.

Note the comment edit for the AAR case. ZipFile is just surprisingly
hostile to copy-free for-each iteration.
lazaroclapp added a commit that referenced this pull request Jun 29, 2020
See #403 discussion, credit for this optimization goes to @Stephan202.

Note the comment edit for the AAR case. ZipFile is just surprisingly
hostile to copy-free for-each iteration.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Cleanup] Remove suppressions added in #400 and fix corresponding code
3 participants