Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import com.sun.source.tree.Tree;
import com.sun.source.tree.TryTree;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Symbol.VarSymbol;
import com.sun.tools.javac.code.Type;
Expand Down Expand Up @@ -145,7 +146,23 @@ private Description match(
return;
}

state.reportMatch(describeMatch(argTree));
String ownerName = methodSymbol.owner.getSimpleName().toString();
if (ownerName.isEmpty() && methodSymbol.owner instanceof ClassSymbol) {
/*
* TODO(cpovirk): Add a test for this once we fix the
* hasExtraParameterForEnclosingInstance case.
*/
ownerName =
((ClassSymbol) methodSymbol.owner).getSuperclass().tsym.getSimpleName().toString();
}

String message =
String.format(
"Null is not permitted for parameter '%s' of %s '%s'.",
paramSymbol.getSimpleName(),
methodSymbol.isConstructor() ? "constructor" : "method",
methodSymbol.isConstructor() ? ownerName : methodSymbol.getSimpleName());
state.reportMatch(buildDescription(argTree).setMessage(message).build());
});

return NO_MATCH; // Any matches were reported through state.reportMatch.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,27 @@ public class NullArgumentForNonNullParameterTest {
CompilationTestHelper.newInstance(NullArgumentForNonNullParameter.class, getClass())
.setArgs("-XepOpt:Nullness:Conservative=false");

@Test
public void positiveConstructor() {
aggressiveHelper
.addSourceLines(
"Foo.java",
"""
import org.jspecify.annotations.NullMarked;

@NullMarked
class Foo {
Foo(String s) {}

void foo() {
// BUG: Diagnostic contains: parameter 's' of constructor 'Foo'
new Foo(null);
}
}
""")
.doTest();
}

@Test
public void positivePrimitive() {
conservativeHelper
Expand All @@ -42,7 +63,7 @@ class Foo {
void consume(int i) {}

void foo(Optional<Integer> o) {
// BUG: Diagnostic contains:
// BUG: Diagnostic contains: parameter 'i' of method 'consume'
consume(o.orElse(null));
}
}
Expand Down Expand Up @@ -99,7 +120,7 @@ public void positiveJavaOptionalOf() {

class Foo {
void foo() {
// BUG: Diagnostic contains:
// BUG: Diagnostic contains: parameter 'value' of method 'of'
Optional.of(null);
}
}
Expand Down
Loading