Skip to content

Commit

Permalink
Treat parameter of generated Record.equals() methods as @nullable (#825)
Browse files Browse the repository at this point in the history
Fixes #619
  • Loading branch information
msridhar committed Sep 5, 2023
1 parent a7b097a commit fcaf39e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,36 @@ public void testLocalRecord() {
"}")
.doTest();
}

@Test
public void recordEqualsNull() {
defaultCompilationHelper
.addSourceLines(
"Records.java",
"package com.uber;",
"import javax.annotation.Nullable;",
"class Records {",
" public void recordEqualsNull() {",
" record Rec() {",
" void print(Object o) { System.out.println(o.toString()); }",
" void equals(Integer i1, Integer i2) { }",
" boolean equals(String i1, String i2) { return false; }",
" boolean equals(Long l1) { return false; }",
" }",
" Object o = null;",
" // null can be passed to the generated equals() method taking an Object parameter",
" new Rec().equals(o);",
" // BUG: Diagnostic contains: passing @Nullable parameter 'null'",
" new Rec().print(null);",
" // BUG: Diagnostic contains: passing @Nullable parameter 'null'",
" new Rec().equals(null, Integer.valueOf(100));",
" // BUG: Diagnostic contains: passing @Nullable parameter 'null'",
" new Rec().equals(\"hello\", null);",
" Long l = null;",
" // BUG: Diagnostic contains: passing @Nullable parameter 'l'",
" new Rec().equals(l);",
" }",
"}")
.doTest();
}
}
28 changes: 28 additions & 0 deletions nullaway/src/main/java/com/uber/nullaway/Nullness.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package com.uber.nullaway;

import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.util.List;
import java.util.stream.Stream;
import javax.lang.model.element.AnnotationMirror;
import org.checkerframework.nullaway.dataflow.analysis.AbstractValue;
Expand Down Expand Up @@ -210,10 +212,36 @@ public static boolean hasNullableAnnotation(Symbol symbol, Config config) {
*/
public static boolean paramHasNullableAnnotation(
Symbol.MethodSymbol symbol, int paramInd, Config config) {
// We treat the (generated) equals() method of record types to have a @Nullable parameter, as
// the generated implementation handles null (as required by the contract of Object.equals())
if (isRecordEqualsParam(symbol, paramInd)) {
return true;
}
return hasNullableAnnotation(
NullabilityUtil.getAllAnnotationsForParameter(symbol, paramInd), config);
}

private static boolean isRecordEqualsParam(Symbol.MethodSymbol symbol, int paramInd) {
// Here we compare with toString() to preserve compatibility with JDK 11 (records only
// introduced in JDK 16)
if (!symbol.owner.getKind().toString().equals("RECORD")) {
return false;
}
if (!symbol.getSimpleName().contentEquals("equals")) {
return false;
}
// Check for a boolean return type and a single parameter of type java.lang.Object
Type type = symbol.type;
List<Type> parameterTypes = type.getParameterTypes();
if (!(type.getReturnType().toString().equals("boolean")
&& parameterTypes != null
&& parameterTypes.size() == 1
&& parameterTypes.get(0).toString().equals("java.lang.Object"))) {
return false;
}
return paramInd == 0;
}

/**
* Does the parameter of {@code symbol} at {@code paramInd} have a {@code @NonNull} declaration or
* type-use annotation? This method works for methods defined in either source or class files.
Expand Down

0 comments on commit fcaf39e

Please sign in to comment.