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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.uber.nullaway.jarinfer;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import java.io.BufferedReader;
Expand All @@ -23,7 +24,6 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.List;
import java.util.Set;
import java.util.jar.JarEntry;
Expand Down Expand Up @@ -264,7 +264,6 @@ private static void copyAndAnnotateJarEntry(
* @param debug flag to output debug logs.
* @throws IOException if an error happens when reading or writing to jar or class streams.
*/
@SuppressWarnings("JdkObsolete")
public static void annotateBytecodeInJar(
JarFile inputJar,
JarOutputStream jarOS,
Expand All @@ -279,8 +278,9 @@ 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();
// 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())) {
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...)

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)

InputStream is = inputJar.getInputStream(jarEntry);
copyAndAnnotateJarEntry(
jarEntry,
Expand All @@ -305,7 +305,6 @@ public static void annotateBytecodeInJar(
* @param debug flag to output debug logs.
* @throws IOException if an error happens when reading or writing to AAR/JAR/class streams.
*/
@SuppressWarnings("JdkObsolete")
public static void annotateBytecodeInAar(
ZipFile inputZip,
ZipOutputStream zipOS,
Expand All @@ -317,9 +316,11 @@ public static void annotateBytecodeInAar(
BytecodeAnnotator.debug = debug;
LOG(debug, "DEBUG", "nullableReturns: " + nullableReturns);
LOG(debug, "DEBUG", "nonnullParams: " + nonnullParams);
for (Enumeration<? extends ZipEntry> entries = inputZip.entries();
entries.hasMoreElements(); ) {
ZipEntry zipEntry = entries.nextElement();
// Error Prone doesn't like usages of the old Java Enumerator APIs. ZipFile does not implement
// Iterable, and likely
// never will (see https://bugs.openjdk.java.net/browse/JDK-6581715). So this seems like the
// best remaining way:
for (ZipEntry zipEntry : inputZip.stream().collect(ImmutableList.toImmutableList())) {
InputStream is = inputZip.getInputStream(zipEntry);
zipOS.putNextEntry(new ZipEntry(zipEntry.getName()));
if (zipEntry.getName().equals("classes.jar")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
package com.uber.nullaway.jarinfer;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
Expand Down Expand Up @@ -50,14 +50,11 @@ public class AnnotationChecker {
* 'Expect*' annotations are present.
* @throws IOException if an error happens when reading the AAR file.
*/
@SuppressWarnings("JdkObsolete")
public static boolean checkMethodAnnotationsInAar(
String aarFile, Map<String, String> expectedToActualAnnotations) throws IOException {
Preconditions.checkArgument(aarFile.endsWith(".aar"), "invalid aar file: " + aarFile);
ZipFile zip = new ZipFile(aarFile);
Enumeration<? extends ZipEntry> zipEntries = zip.entries();
while (zipEntries.hasMoreElements()) {
ZipEntry zipEntry = zipEntries.nextElement();
for (ZipEntry zipEntry : zip.stream().collect(ImmutableList.toImmutableList())) {
if (zipEntry.getName().equals("classes.jar")) {
JarInputStream jarIS = new JarInputStream(zip.getInputStream(zipEntry));
JarEntry jarEntry = jarIS.getNextJarEntry();
Expand Down Expand Up @@ -86,14 +83,11 @@ public static boolean checkMethodAnnotationsInAar(
* 'Expect*' annotations are present.
* @throws IOException if an error happens when reading the jar file.
*/
@SuppressWarnings("JdkObsolete")
public static boolean checkMethodAnnotationsInJar(
String jarFile, Map<String, String> expectedToActualAnnotations) throws IOException {
Preconditions.checkArgument(jarFile.endsWith(".jar"), "invalid jar file: " + jarFile);
JarFile jar = new JarFile(jarFile);
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
for (JarEntry entry : jar.stream().collect(ImmutableList.toImmutableList())) {
if (entry.getName().endsWith(".class")
&& !checkMethodAnnotationsInClass(
jar.getInputStream(entry), expectedToActualAnnotations)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
package com.uber.nullaway.jarinfer;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarEntry;
Expand All @@ -40,20 +40,15 @@ public class EntriesComparator {
* @return True iff the entries present in the two jar files are the same.
* @throws IOException if an error happens when reading jar files.
*/
@SuppressWarnings("JdkObsolete")
public static boolean compareEntriesInJars(String jarFile1, String jarFile2) throws IOException {
Preconditions.checkArgument(jarFile1.endsWith(".jar"), "invalid jar file: " + jarFile1);
Preconditions.checkArgument(jarFile2.endsWith(".jar"), "invalid jar file: " + jarFile2);
JarFile jar1 = new JarFile(jarFile1);
Set<String> jar1Entries = new HashSet<>();
for (Enumeration<JarEntry> entries = jar1.entries(); entries.hasMoreElements(); ) {
jar1Entries.add(entries.nextElement().getName());
}
JarFile jar2 = new JarFile(jarFile2);
Set<String> jar2Entries = new HashSet<>();
for (Enumeration<JarEntry> entries = jar2.entries(); entries.hasMoreElements(); ) {
jar2Entries.add(entries.nextElement().getName());
}
Set<String> jar1Entries =
jar1.stream().map(ZipEntry::getName).collect(ImmutableSet.toImmutableSet());
Set<String> jar2Entries =
jar2.stream().map(ZipEntry::getName).collect(ImmutableSet.toImmutableSet());
return jar1Entries.equals(jar2Entries);
}

Expand All @@ -67,20 +62,15 @@ public static boolean compareEntriesInJars(String jarFile1, String jarFile2) thr
* "classes.jar" in the two aar files are the same.
* @throws IOException if an error happens when reading aar files.
*/
@SuppressWarnings("JdkObsolete")
public static boolean compareEntriesInAars(String aarFile1, String aarFile2) throws IOException {
Preconditions.checkArgument(aarFile1.endsWith(".aar"), "invalid aar file: " + aarFile1);
Preconditions.checkArgument(aarFile2.endsWith(".aar"), "invalid aar file: " + aarFile2);
ZipFile zip1 = new ZipFile(aarFile1);
Set<String> zip1Entries = new HashSet<>();
for (Enumeration<? extends ZipEntry> entries = zip1.entries(); entries.hasMoreElements(); ) {
zip1Entries.add(entries.nextElement().getName());
}
ZipFile zip2 = new ZipFile(aarFile2);
Set<String> zip2Entries = new HashSet<>();
for (Enumeration<? extends ZipEntry> entries = zip2.entries(); entries.hasMoreElements(); ) {
zip2Entries.add(entries.nextElement().getName());
}
Set<String> zip1Entries =
zip1.stream().map(ZipEntry::getName).collect(ImmutableSet.toImmutableSet());
Set<String> zip2Entries =
zip2.stream().map(ZipEntry::getName).collect(ImmutableSet.toImmutableSet());
if (!zip1Entries.equals(zip2Entries)) {
return false;
}
Expand Down
22 changes: 11 additions & 11 deletions nullaway/src/main/java/com/uber/nullaway/NullAway.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,7 @@ public class NullAway extends BugChecker
static final String INITIALIZATION_CHECK_NAME = "NullAway.Init";
static final String OPTIONAL_CHECK_NAME = "NullAway.Optional";

@SuppressWarnings("UnnecessaryLambda")
private static final Matcher<ExpressionTree> THIS_MATCHER =
(expressionTree, state) -> isThisIdentifier(expressionTree);
private static final Matcher<ExpressionTree> THIS_MATCHER = NullAway::isThisIdentifierMatcher;

private final Predicate<MethodInvocationNode> nonAnnotatedMethod;

Expand Down Expand Up @@ -233,15 +231,15 @@ public class NullAway extends BugChecker
public NullAway() {
config = new DummyOptionsConfig();
handler = Handlers.buildEmpty();
nonAnnotatedMethod = nonAnnotatedMethodCheck();
nonAnnotatedMethod = this::isMethodUnannotated;
customSuppressionAnnotations = ImmutableSet.of();
errorBuilder = new ErrorBuilder(config, "", ImmutableSet.of());
}

public NullAway(ErrorProneFlags flags) {
config = new ErrorProneCLIFlagsConfig(flags);
handler = Handlers.buildDefault(config);
nonAnnotatedMethod = nonAnnotatedMethodCheck();
nonAnnotatedMethod = this::isMethodUnannotated;
customSuppressionAnnotations = initCustomSuppressions();
errorBuilder = new ErrorBuilder(config, canonicalName(), allNames());
// workaround for Checker Framework static state bug;
Expand All @@ -263,12 +261,9 @@ private ImmutableSet<Class<? extends Annotation>> initCustomSuppressions() {
return builder.build();
}

@SuppressWarnings("UnnecessaryLambda")
private Predicate<MethodInvocationNode> nonAnnotatedMethodCheck() {
return invocationNode ->
invocationNode == null
|| NullabilityUtil.isUnannotated(
ASTHelpers.getSymbol(invocationNode.getTree()), config);
private boolean isMethodUnannotated(MethodInvocationNode invocationNode) {
return invocationNode == null
|| NullabilityUtil.isUnannotated(ASTHelpers.getSymbol(invocationNode.getTree()), config);
}

@Override
Expand Down Expand Up @@ -2102,6 +2097,11 @@ private static boolean isThisIdentifier(ExpressionTree expressionTree) {
&& ((IdentifierTree) expressionTree).getName().toString().equals("this");
}

private static boolean isThisIdentifierMatcher(
ExpressionTree expressionTree, VisitorState state) {
return isThisIdentifier(expressionTree);
}

public ErrorBuilder getErrorBuilder() {
return errorBuilder;
}
Expand Down