diff --git a/nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java b/nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java
index 1134c7f2a9..801df21506 100644
--- a/nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java
+++ b/nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java
@@ -44,6 +44,7 @@
import com.google.errorprone.VisitorState;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.ExpressionTree;
+import com.sun.source.tree.MemberSelectTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Type;
@@ -301,7 +302,7 @@ public boolean onOverrideMayBeNullExpr(
if (isNullableFieldInLibraryModels(exprSymbol)) {
return true;
}
- if (!(expr instanceof MethodInvocationTree
+ if (!(expr instanceof MethodInvocationTree invocation
&& exprSymbol instanceof Symbol.MethodSymbol methodSymbol)) {
return exprMayBeNull;
}
@@ -310,6 +311,11 @@ public boolean onOverrideMayBeNullExpr(
if (NullabilityUtil.isMapGetOrDefault(methodSymbol, state)) {
return true;
}
+
+ if (isCanonicalNameOfClassLiteral(invocation, methodSymbol, state)) {
+ return false;
+ }
+
OptimizedLibraryModels optLibraryModels = getOptLibraryModels(state.context);
// A model of a method's return nullness matches the exact signature when the method is
// annotated: the overriding method carries its own annotations, and an inherited model must
@@ -396,6 +402,10 @@ public NullnessHint onDataflowVisitMethodInvocation(
}
return anyNull ? NullnessHint.HINT_NULLABLE : NullnessHint.FORCE_NONNULL;
}
+
+ if (isCanonicalNameOfClassLiteral(castToNonNull(node.getTree()), callee, state)) {
+ return NullnessHint.FORCE_NONNULL;
+ }
if (optLibraryModels.hasNonNullReturn(callee, types, !isMethodAnnotated)) {
return NullnessHint.FORCE_NONNULL;
} else if (optLibraryModels.hasNullableReturn(callee, types, !isMethodAnnotated)) {
@@ -430,6 +440,40 @@ private boolean isNullableFieldInLibraryModels(@Nullable Symbol symbol) {
return false;
}
+ /**
+ * Checks whether {@code invocation} calls {@link Class#getCanonicalName()} directly on a class
+ * literal whose type has a canonical name, in which case the result is never {@code null}.
+ *
+ *
Per JLS 6.7, a class has no canonical name if it is local or anonymous, or if it is a member
+ * of a class without a canonical name; an array type has a canonical name iff its element type
+ * does. Anonymous classes have no class literal, so for a literal it suffices to check that the
+ * (element) type is not directly or indirectly local. Receivers that are not class literals, e.g.
+ * {@code clazz.getCanonicalName()} or {@code obj.getClass().getCanonicalName()}, never match.
+ *
+ *
No check on the owner of {@code callee} is needed: a class literal always has type {@code
+ * Class}, so the only {@code getCanonicalName} method it can invoke is {@code
+ * Class#getCanonicalName()}.
+ *
+ * @param invocation the method invocation to check
+ * @param callee the symbol of the invoked method
+ * @param state the visitor state
+ * @return true if the invocation is known to return a non-null value
+ */
+ private static boolean isCanonicalNameOfClassLiteral(
+ MethodInvocationTree invocation, Symbol.MethodSymbol callee, VisitorState state) {
+ if (!callee.name.contentEquals("getCanonicalName")
+ || !(ASTHelpers.getReceiver(invocation) instanceof MemberSelectTree literal)
+ || !literal.getIdentifier().contentEquals("class")) {
+ return false;
+ }
+ Types types = state.getTypes();
+ Type type = castToNonNull(ASTHelpers.getType(literal.getExpression()));
+ while (types.isArray(type)) {
+ type = types.elemtype(type);
+ }
+ return !type.tsym.isDirectlyOrIndirectlyLocal();
+ }
+
private void setConditionalArgumentNullness(
AccessPathNullnessPropagation.Updates thenUpdates,
AccessPathNullnessPropagation.Updates elseUpdates,
diff --git a/nullaway/src/test/java/com/uber/nullaway/jspecify/JSpecifyLibraryModelsTests.java b/nullaway/src/test/java/com/uber/nullaway/jspecify/JSpecifyLibraryModelsTests.java
index bc5ced5cdb..e50e3018aa 100644
--- a/nullaway/src/test/java/com/uber/nullaway/jspecify/JSpecifyLibraryModelsTests.java
+++ b/nullaway/src/test/java/com/uber/nullaway/jspecify/JSpecifyLibraryModelsTests.java
@@ -247,6 +247,164 @@ public Optional getNameOpt() {
.doTest();
}
+ @Test
+ public void getCanonicalNameOnClassLiteral() {
+ makeHelper()
+ .addSourceLines(
+ "Test.java",
+ """
+ import java.util.Map;
+ import java.util.function.IntSupplier;
+ import org.jspecify.annotations.NullMarked;
+
+ @NullMarked
+ class Test {
+ static class Member {
+ static class MemberOfMember {}
+ }
+
+ Class> field = String.class;
+
+ static void takesNonNull(String s) {}
+
+ // ---- class literals of types with a canonical name: non-null ----
+
+ int topLevelClass() {
+ return String.class.getCanonicalName().length();
+ }
+
+ int memberClass() {
+ return Map.Entry.class.getCanonicalName().length();
+ }
+
+ int memberOfMemberClass() {
+ return Member.MemberOfMember.class.getCanonicalName().length();
+ }
+
+ int primitiveType() {
+ return int.class.getCanonicalName().length();
+ }
+
+ int voidType() {
+ return void.class.getCanonicalName().length();
+ }
+
+ int arrayOfTopLevelClass() {
+ return String[].class.getCanonicalName().length();
+ }
+
+ int arrayOfPrimitive() {
+ return int[][].class.getCanonicalName().length();
+ }
+
+ int viaLocalVariable() {
+ String name = String.class.getCanonicalName();
+ return name.length();
+ }
+
+ String returnedFromNonNullMethod() {
+ return String.class.getCanonicalName();
+ }
+
+ void passedAsNonNullArgument() {
+ takesNonNull(String.class.getCanonicalName());
+ }
+
+ // ---- class literals of types without a canonical name: nullable ----
+
+ int localClass() {
+ class Local {}
+ // BUG: Diagnostic contains: dereferenced expression 'Local.class.getCanonicalName()' is @Nullable
+ return Local.class.getCanonicalName().length();
+ }
+
+ int memberOfLocalClass() {
+ class Local {
+ class Inner {}
+ }
+ // BUG: Diagnostic contains: dereferenced expression 'Local.Inner.class.getCanonicalName()' is @Nullable
+ return Local.Inner.class.getCanonicalName().length();
+ }
+
+ int arrayOfLocalClass() {
+ class Local {}
+ // BUG: Diagnostic contains: dereferenced expression 'Local[].class.getCanonicalName()' is @Nullable
+ return Local[].class.getCanonicalName().length();
+ }
+
+ int localRecord() {
+ record LocalRecord() {}
+ // BUG: Diagnostic contains: dereferenced expression 'LocalRecord.class.getCanonicalName()' is @Nullable
+ return LocalRecord.class.getCanonicalName().length();
+ }
+
+ int localEnum() {
+ enum LocalEnum { A }
+ // BUG: Diagnostic contains: dereferenced expression 'LocalEnum.class.getCanonicalName()' is @Nullable
+ return LocalEnum.class.getCanonicalName().length();
+ }
+
+ int localInterface() {
+ interface LocalInterface {}
+ // BUG: Diagnostic contains: dereferenced expression 'LocalInterface.class.getCanonicalName()' is @Nullable
+ return LocalInterface.class.getCanonicalName().length();
+ }
+
+ int localClassInLambda() {
+ IntSupplier supplier = () -> {
+ class InLambda {}
+ // BUG: Diagnostic contains: dereferenced expression 'InLambda.class.getCanonicalName()' is @Nullable
+ return InLambda.class.getCanonicalName().length();
+ };
+ return supplier.getAsInt();
+ }
+
+ int localClassViaLocalVariable() {
+ class Local {}
+ String name = Local.class.getCanonicalName();
+ // BUG: Diagnostic contains: dereferenced expression 'name' is @Nullable
+ return name.length();
+ }
+
+ String localClassReturned() {
+ class Local {}
+ // BUG: Diagnostic contains: returning @Nullable expression from method with @NonNull return type
+ return Local.class.getCanonicalName();
+ }
+
+ void localClassPassedAsArgument() {
+ class Local {}
+ // BUG: Diagnostic contains: passing @Nullable parameter 'Local.class.getCanonicalName()' where @NonNull is required
+ takesNonNull(Local.class.getCanonicalName());
+ }
+
+ // ---- receivers that are not class literals: remain nullable ----
+
+ int classTypedParameter(Class> clazz) {
+ // BUG: Diagnostic contains: dereferenced expression 'clazz.getCanonicalName()' is @Nullable
+ return clazz.getCanonicalName().length();
+ }
+
+ int fieldReceiver() {
+ // BUG: Diagnostic contains: dereferenced expression 'this.field.getCanonicalName()' is @Nullable
+ return this.field.getCanonicalName().length();
+ }
+
+ int getClassReceiver(Object o) {
+ // BUG: Diagnostic contains: dereferenced expression 'o.getClass().getCanonicalName()' is @Nullable
+ return o.getClass().getCanonicalName().length();
+ }
+
+ int literalStoredInLocalVariable() {
+ Class> clazz = String.class;
+ // BUG: Diagnostic contains: dereferenced expression 'clazz.getCanonicalName()' is @Nullable
+ return clazz.getCanonicalName().length();
+ }
+ }
+ """)
+ .doTest();
+ }
+
private CompilationTestHelper makeHelper() {
return makeTestHelperWithArgs(
JSpecifyJavacConfig.withJSpecifyModeArgs(