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

Create com.uber.nullaway.generics package #855

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nullaway/src/main/java/com/uber/nullaway/ErrorBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public class ErrorBuilder {
* expression into a @NonNull target, and this parameter is the Symbol for that target.
* @return the error description
*/
Description createErrorDescription(
public Description createErrorDescription(
Copy link
Collaborator

Choose a reason for hiding this comment

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

I assume this is for future uses?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is because the PR moves the generics code into its own new package, so it cannot see this method anymore (default visibility for Java methods is "package private").

ErrorMessage errorMessage,
Description.Builder descriptionBuilder,
VisitorState state,
Expand Down
1 change: 1 addition & 0 deletions nullaway/src/main/java/com/uber/nullaway/NullAway.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
import com.uber.nullaway.ErrorMessage.MessageTypes;
import com.uber.nullaway.dataflow.AccessPathNullnessAnalysis;
import com.uber.nullaway.dataflow.EnclosingEnvironmentNullness;
import com.uber.nullaway.generics.GenericsChecks;
import com.uber.nullaway.handlers.Handler;
import com.uber.nullaway.handlers.Handlers;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
import com.sun.tools.javac.code.TypeTag;
import com.uber.nullaway.CodeAnnotationInfo;
import com.uber.nullaway.Config;
import com.uber.nullaway.GenericsChecks;
import com.uber.nullaway.NullabilityUtil;
import com.uber.nullaway.Nullness;
import com.uber.nullaway.generics.GenericsChecks;
import com.uber.nullaway.handlers.Handler;
import com.uber.nullaway.handlers.Handler.NullnessHint;
import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.uber.nullaway.generics;

import com.google.errorprone.VisitorState;
import com.sun.tools.javac.code.Attribute;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Types;
import java.util.List;

/**
* Visitor that checks equality of nullability annotations for all nested generic type arguments
* within a type. Compares the Type it is called upon, i.e. the LHS type and the Type passed as an
Comment on lines +10 to +11
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is the only part that got updated, where the rest of the code is copied verbatim from the original file.

Makes sense to me!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, exactly, we extracted this nested class to a new source file

* argument, i.e. The RHS type.
*/
public class CompareNullabilityVisitor extends Types.DefaultTypeVisitor<Boolean, Type> {
private final VisitorState state;

CompareNullabilityVisitor(VisitorState state) {
this.state = state;
}

@Override
public Boolean visitClassType(Type.ClassType lhsType, Type rhsType) {
Types types = state.getTypes();
// The base type of rhsType may be a subtype of lhsType's base type. In such cases, we must
// compare lhsType against the supertype of rhsType with a matching base type.
rhsType = (Type.ClassType) types.asSuper(rhsType, lhsType.tsym);
// This is impossible, considering the fact that standard Java subtyping succeeds before
// running NullAway
if (rhsType == null) {
throw new RuntimeException("Did not find supertype of " + rhsType + " matching " + lhsType);

Check warning on line 30 in nullaway/src/main/java/com/uber/nullaway/generics/CompareNullabilityVisitor.java

View check run for this annotation

Codecov / codecov/patch

nullaway/src/main/java/com/uber/nullaway/generics/CompareNullabilityVisitor.java#L30

Added line #L30 was not covered by tests
}
List<Type> lhsTypeArguments = lhsType.getTypeArguments();
List<Type> rhsTypeArguments = rhsType.getTypeArguments();
// This is impossible, considering the fact that standard Java subtyping succeeds before
// running NullAway
if (lhsTypeArguments.size() != rhsTypeArguments.size()) {
throw new RuntimeException(

Check warning on line 37 in nullaway/src/main/java/com/uber/nullaway/generics/CompareNullabilityVisitor.java

View check run for this annotation

Codecov / codecov/patch

nullaway/src/main/java/com/uber/nullaway/generics/CompareNullabilityVisitor.java#L37

Added line #L37 was not covered by tests
"Number of types arguments in " + rhsType + " does not match " + lhsType);
}
for (int i = 0; i < lhsTypeArguments.size(); i++) {
Type lhsTypeArgument = lhsTypeArguments.get(i);
Type rhsTypeArgument = rhsTypeArguments.get(i);
boolean isLHSNullableAnnotated = false;
List<Attribute.TypeCompound> lhsAnnotations = lhsTypeArgument.getAnnotationMirrors();
// To ensure that we are checking only jspecify nullable annotations
for (Attribute.TypeCompound annotation : lhsAnnotations) {
if (annotation.getAnnotationType().toString().equals(GenericsChecks.NULLABLE_NAME)) {
isLHSNullableAnnotated = true;
break;
}
}

Check warning on line 51 in nullaway/src/main/java/com/uber/nullaway/generics/CompareNullabilityVisitor.java

View check run for this annotation

Codecov / codecov/patch

nullaway/src/main/java/com/uber/nullaway/generics/CompareNullabilityVisitor.java#L51

Added line #L51 was not covered by tests
boolean isRHSNullableAnnotated = false;
List<Attribute.TypeCompound> rhsAnnotations = rhsTypeArgument.getAnnotationMirrors();
// To ensure that we are checking only jspecify nullable annotations
for (Attribute.TypeCompound annotation : rhsAnnotations) {
if (annotation.getAnnotationType().toString().equals(GenericsChecks.NULLABLE_NAME)) {
isRHSNullableAnnotated = true;
break;
}
}

Check warning on line 60 in nullaway/src/main/java/com/uber/nullaway/generics/CompareNullabilityVisitor.java

View check run for this annotation

Codecov / codecov/patch

nullaway/src/main/java/com/uber/nullaway/generics/CompareNullabilityVisitor.java#L60

Added line #L60 was not covered by tests
if (isLHSNullableAnnotated != isRHSNullableAnnotated) {
return false;
}
// nested generics
if (!lhsTypeArgument.accept(this, rhsTypeArgument)) {
return false;
}
}
// If there is an enclosing type (for non-static inner classes), its type argument nullability
// should also match. When there is no enclosing type, getEnclosingType() returns a NoType
// object, which gets handled by the fallback visitType() method
return lhsType.getEnclosingType().accept(this, rhsType.getEnclosingType());
}

@Override
public Boolean visitArrayType(Type.ArrayType lhsType, Type rhsType) {
Type.ArrayType arrRhsType = (Type.ArrayType) rhsType;
return lhsType.getComponentType().accept(this, arrRhsType.getComponentType());
}

@Override
public Boolean visitType(Type t, Type type) {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.uber.nullaway.generics;

import static java.util.stream.Collectors.joining;

import com.google.errorprone.VisitorState;
import com.google.errorprone.util.ASTHelpers;
import com.sun.tools.javac.code.Attribute;
import com.sun.tools.javac.code.BoundKind;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Types;

/**
* A visitor that pretty prints a generic type including its type-use nullability annotations, for
* use in error messages.
*
* <p>This code is a modified and extended version of code in {@link
* com.google.errorprone.util.Signatures}
*/
final class GenericTypePrettyPrintingVisitor extends Types.DefaultTypeVisitor<String, Void> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here, I think this is strictly extracting code from the original file. LGTM

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yup, no code changes, just moving code to a top-level source file


private final VisitorState state;

GenericTypePrettyPrintingVisitor(VisitorState state) {
this.state = state;
}

@Override
public String visitWildcardType(Type.WildcardType t, Void unused) {
// NOTE: we have not tested this code yet as we do not yet support wildcard types
StringBuilder sb = new StringBuilder();
sb.append(t.kind);

Check warning on line 31 in nullaway/src/main/java/com/uber/nullaway/generics/GenericTypePrettyPrintingVisitor.java

View check run for this annotation

Codecov / codecov/patch

nullaway/src/main/java/com/uber/nullaway/generics/GenericTypePrettyPrintingVisitor.java#L30-L31

Added lines #L30 - L31 were not covered by tests
if (t.kind != BoundKind.UNBOUND) {
sb.append(t.type.accept(this, null));

Check warning on line 33 in nullaway/src/main/java/com/uber/nullaway/generics/GenericTypePrettyPrintingVisitor.java

View check run for this annotation

Codecov / codecov/patch

nullaway/src/main/java/com/uber/nullaway/generics/GenericTypePrettyPrintingVisitor.java#L33

Added line #L33 was not covered by tests
}
return sb.toString();

Check warning on line 35 in nullaway/src/main/java/com/uber/nullaway/generics/GenericTypePrettyPrintingVisitor.java

View check run for this annotation

Codecov / codecov/patch

nullaway/src/main/java/com/uber/nullaway/generics/GenericTypePrettyPrintingVisitor.java#L35

Added line #L35 was not covered by tests
}

@Override
public String visitClassType(Type.ClassType t, Void s) {
StringBuilder sb = new StringBuilder();
Type enclosingType = t.getEnclosingType();
if (!ASTHelpers.isSameType(enclosingType, Type.noType, state)) {
sb.append(enclosingType.accept(this, null)).append('.');
}
for (Attribute.TypeCompound compound : t.getAnnotationMirrors()) {
sb.append('@');
sb.append(compound.type.accept(this, null));
sb.append(' ');
}
sb.append(t.tsym.getSimpleName());
if (t.getTypeArguments().nonEmpty()) {
sb.append('<');
sb.append(
t.getTypeArguments().stream().map(a -> a.accept(this, null)).collect(joining(", ")));
sb.append(">");
}
return sb.toString();
}

@Override
public String visitCapturedType(Type.CapturedType t, Void s) {
return t.wildcard.accept(this, null);

Check warning on line 62 in nullaway/src/main/java/com/uber/nullaway/generics/GenericTypePrettyPrintingVisitor.java

View check run for this annotation

Codecov / codecov/patch

nullaway/src/main/java/com/uber/nullaway/generics/GenericTypePrettyPrintingVisitor.java#L62

Added line #L62 was not covered by tests
}

@Override
public String visitArrayType(Type.ArrayType t, Void unused) {
// TODO properly print cases like int @Nullable[]
return t.elemtype.accept(this, null) + "[]";
}

@Override
public String visitType(Type t, Void s) {
return t.toString();
}
}
Loading