-
Notifications
You must be signed in to change notification settings - Fork 300
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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
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. 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! 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. 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); | ||
} | ||
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( | ||
"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; | ||
} | ||
} | ||
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; | ||
} | ||
} | ||
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> { | ||
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. Same here, I think this is strictly extracting code from the original file. LGTM 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. 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 Codecov / codecov/patchnullaway/src/main/java/com/uber/nullaway/generics/GenericTypePrettyPrintingVisitor.java#L30-L31
|
||
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 Codecov / codecov/patchnullaway/src/main/java/com/uber/nullaway/generics/GenericTypePrettyPrintingVisitor.java#L33
|
||
} | ||
return sb.toString(); | ||
Check warning on line 35 in nullaway/src/main/java/com/uber/nullaway/generics/GenericTypePrettyPrintingVisitor.java Codecov / codecov/patchnullaway/src/main/java/com/uber/nullaway/generics/GenericTypePrettyPrintingVisitor.java#L35
|
||
} | ||
|
||
@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 Codecov / codecov/patchnullaway/src/main/java/com/uber/nullaway/generics/GenericTypePrettyPrintingVisitor.java#L62
|
||
} | ||
|
||
@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(); | ||
} | ||
} |
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 assume this is for future uses?
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.
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").