Skip to content

Commit 3da2c82

Browse files
authored
Use proper name for constructors in JarInfer (#1167)
1 parent 685065a commit 3da2c82

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

jar-infer/jar-infer-lib/src/main/java/com/uber/nullaway/jarinfer/DefinitelyDerefedParamsDriver.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -546,10 +546,15 @@ private static String getAstubxSignature(IMethod mtd) {
546546
argTypes[i] = getSourceLevelQualifiedTypeName(mtd.getParameterType(argi++));
547547
}
548548
}
549+
String methodName = mtd.getName().toString();
550+
if (methodName.equals("<init>")) {
551+
// use simple name of enclosing class
552+
methodName = classType.substring(classType.lastIndexOf('.') + 1);
553+
}
549554
return classType
550555
+ ":"
551556
+ (returnType == null ? "void " : returnType + " ")
552-
+ mtd.getName().toString()
557+
+ methodName
553558
+ "("
554559
+ String.join(", ", argTypes)
555560
+ ")";

jar-infer/jar-infer-lib/src/test/java/com/uber/nullaway/jarinfer/JarInferTest.java

+15
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,21 @@ public void nestedWildcard() throws Exception {
563563
"}");
564564
}
565565

566+
@Test
567+
public void constructors() throws Exception {
568+
testTemplate(
569+
"constructors",
570+
"toys",
571+
"TestConstructor",
572+
ImmutableMap.of(
573+
"toys.TestConstructor:void TestConstructor(java.lang.String)", Sets.newHashSet(0)),
574+
"public class TestConstructor {",
575+
" TestConstructor(String s) {",
576+
" s.toString();",
577+
" }",
578+
"}");
579+
}
580+
566581
@Test
567582
public void toyJARAnnotatingClasses() throws Exception {
568583
testAnnotationInJarTemplate(

jar-infer/nullaway-integration-test/src/test/java/com/uber/nullaway/jarinfer/JarInferIntegrationTest.java

+24
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,30 @@ public void wildcards() {
129129
.doTest();
130130
}
131131

132+
@Test
133+
public void constructors() {
134+
compilationHelper
135+
.setArgs(
136+
Arrays.asList(
137+
"-d",
138+
temporaryFolder.getRoot().getAbsolutePath(),
139+
"-XepOpt:NullAway:AnnotatedPackages=com.uber",
140+
"-XepOpt:NullAway:JarInferEnabled=true",
141+
"-XepOpt:NullAway:UnannotatedSubPackages=com.uber.nullaway.[a-zA-Z0-9.]+.unannotated"))
142+
.addSourceLines(
143+
"Test.java",
144+
"package com.uber;",
145+
"import org.jspecify.annotations.Nullable;",
146+
"import com.uber.nullaway.jarinfer.toys.unannotated.Toys;",
147+
"class Test {",
148+
" void test1() {",
149+
" // BUG: Diagnostic contains: passing @Nullable parameter 'null'",
150+
" Toys t = new Toys(null);",
151+
" }",
152+
"}")
153+
.doTest();
154+
}
155+
132156
@Test
133157
public void jarinferNullableReturnsTest() {
134158
compilationHelper

jar-infer/test-java-lib-jarinfer/src/main/java/com/uber/nullaway/jarinfer/toys/unannotated/Toys.java

+4
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,8 @@ public static void main(String arg[]) throws java.io.IOException {
9595
System.out.println(e.getMessage());
9696
}
9797
}
98+
99+
public Toys(String s) {
100+
System.out.println(s.toString());
101+
}
98102
}

0 commit comments

Comments
 (0)