Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
msridhar committed May 6, 2023
1 parent 3bbd1d6 commit ee73b04
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
40 changes: 28 additions & 12 deletions nullaway/src/main/java/com/uber/nullaway/GenericsChecks.java
Original file line number Diff line number Diff line change
Expand Up @@ -613,23 +613,42 @@ public static Nullness getOverriddenMethodReturnTypeNullness(
* variable, its nullness depends on the nullability of the corresponding type parameter in the
* receiver's type.
*
* <p>Consider the following example:
*
* <pre>
* interface Fn<P extends @Nullable Object, R extends @Nullable Object> {
* R apply(P p);
* }
* class C implements Fn<@Nullable String, String> {
* public String apply(@Nullable String p) {
* return "";
* }
* }
* static void m() {
* Fn<@Nullable String, String> f = new C();
* f.apply(null);
* }
* </pre>
*
* The declared type of {@code f} passes {@code Nullable String} as the type parameter for type
* variable {@code P}. So, it is legal to pass {@code null} as a parameter to {@code f.apply}.
*
* @param paramIndex parameter index
* @param methodSymbol symbol for the invoked method
* @param invokedMethodSymbol symbol for the invoked method
* @param tree the tree for the invocation
* @return Nullness of parameter at {@code paramIndex}, or {@code NONNULL} if the call does not
* invoke an instance method
*/
public Nullness getGenericParameterNullnessAtInvocation(
int paramIndex, Symbol.MethodSymbol methodSymbol, MethodInvocationTree tree) {
int paramIndex, Symbol.MethodSymbol invokedMethodSymbol, MethodInvocationTree tree) {
if (!(tree.getMethodSelect() instanceof MemberSelectTree)) {
return Nullness.NONNULL;
}
Type methodReceiverType =
castToNonNull(
ASTHelpers.getType(((MemberSelectTree) tree.getMethodSelect()).getExpression()));
Type methodType = state.getTypes().memberType(methodReceiverType, methodSymbol);
Type formalParamType = methodType.getParameterTypes().get(paramIndex);
return getTypeNullness(formalParamType, config);
return getOverriddenMethodParameterNullness(
paramIndex, invokedMethodSymbol, methodReceiverType);
}

/**
Expand All @@ -640,15 +659,12 @@ public Nullness getGenericParameterNullnessAtInvocation(
*
* @param parameterIndex An index of the method parameter to get the Nullness
* @param overriddenMethod A symbol of the overridden method
* @param overridingMethodParam A paramIndex th parameter of the overriding method
* @param enclosingType A paramIndex th parameter of the overriding method
* @return Returns Nullness of the parameterIndex th parameter of the overridden method
*/
public Nullness getOverriddenMethodArgNullness(
int parameterIndex,
Symbol.MethodSymbol overriddenMethod,
Symbol.VarSymbol overridingMethodParam) {
Type methodType =
state.getTypes().memberType(overridingMethodParam.owner.owner.type, overriddenMethod);
public Nullness getOverriddenMethodParameterNullness(
int parameterIndex, Symbol.MethodSymbol overriddenMethod, Type enclosingType) {
Type methodType = state.getTypes().memberType(enclosingType, overriddenMethod);
Type paramType = methodType.getParameterTypes().get(parameterIndex);
return getTypeNullness(paramType, config);
}
Expand Down
4 changes: 2 additions & 2 deletions nullaway/src/main/java/com/uber/nullaway/NullAway.java
Original file line number Diff line number Diff line change
Expand Up @@ -729,8 +729,8 @@ private Description checkParamOverriding(
? Nullness.NULLABLE
: (config.isJSpecifyMode()
? new GenericsChecks(state, config, this)
.getOverriddenMethodArgNullness(
i, overriddenMethod, overridingParamSymbols.get(i))
.getOverriddenMethodParameterNullness(
i, overriddenMethod, overridingParamSymbols.get(i).owner.owner.type)
: Nullness.NONNULL);
}
}
Expand Down

0 comments on commit ee73b04

Please sign in to comment.