Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix JarInfer parameter indexes for instance methods #1071

Merged
merged 6 commits into from
Nov 12, 2024
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 @@ -172,11 +172,9 @@ private static void annotateBytecode(
}
Set<Integer> params = nonnullParams.get(methodSignature);
if (params != null) {
boolean isStatic = (method.access & Opcodes.ACC_STATIC) != 0;
for (Integer param : params) {
int paramNum = isStatic ? param : param - 1;
// Add a @Nonnull annotation on this parameter.
method.visitParameterAnnotation(paramNum, nonnullDesc, visible);
method.visitParameterAnnotation(param, nonnullDesc, visible);
LOG(
debug,
"DEBUG",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ private void analyzeFile(String pkgName, String inPath, boolean includeNonPublic
String sign = "";
try {
// Parameter analysis
if (mtd.getNumberOfParameters() > (mtd.isStatic() ? 0 : 1)) {
boolean isStatic = mtd.isStatic();
if (mtd.getNumberOfParameters() > (isStatic ? 0 : 1)) {
// For inferring parameter nullability, our criteria is based on finding
// unchecked dereferences of that parameter. We perform a quick bytecode
// check and skip methods containing no dereferences (i.e. method calls
Expand All @@ -283,6 +284,11 @@ private void analyzeFile(String pkgName, String inPath, boolean includeNonPublic
if (bytecodeHasAnyDereferences(mtd)) {
analysisDriver = getAnalysisDriver(mtd, options, cache);
Set<Integer> result = analysisDriver.analyze();
if (!isStatic) {
// subtract 1 from each parameter index to account for 'this' parameter
result =
result.stream().map(i -> i - 1).collect(ImmutableSet.toImmutableSet());
}
sign = getSignature(mtd);
LOG(DEBUG, "DEBUG", "analyzed method: " + sign);
if (!result.isEmpty() || DEBUG) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public void toyStatic() throws Exception {
"Test",
ImmutableMap.of(
"toys.Test:void test(java.lang.String, toys.Foo, toys.Bar)", Sets.newHashSet(0, 2),
"toys.Foo:boolean run(java.lang.String)", Sets.newHashSet(1)),
"toys.Foo:boolean run(java.lang.String)", Sets.newHashSet(0)),
"class Foo {",
" private String foo;",
" public Foo(String str) {",
Expand Down Expand Up @@ -267,7 +267,7 @@ public void toyNonStatic() throws Exception {
"toys",
"Foo",
ImmutableMap.of(
"toys.Foo:void test(java.lang.String, java.lang.String)", Sets.newHashSet(1)),
"toys.Foo:void test(java.lang.String, java.lang.String)", Sets.newHashSet(0)),
"class Foo {",
" private String foo;",
" public Foo(String str) {",
Expand Down Expand Up @@ -305,7 +305,7 @@ public void toyNullTestAPI() throws Exception {
"Foo",
ImmutableMap.of(
"toys.Foo:void test(java.lang.String, java.lang.String, java.lang.String)",
Sets.newHashSet(1, 3)),
Sets.newHashSet(0, 2)),
"import com.google.common.base.Preconditions;",
"import java.util.Objects;",
"import org.junit.Assert;",
Expand Down Expand Up @@ -336,7 +336,7 @@ public void toyConditionalFlow() throws Exception {
"Foo",
ImmutableMap.of(
"toys.Foo:void test(java.lang.String, java.lang.String, java.lang.String)",
Sets.newHashSet(1, 2)),
Sets.newHashSet(0, 1)),
"import com.google.common.base.Preconditions;",
"import java.util.Objects;",
"import org.junit.Assert;",
Expand Down Expand Up @@ -367,7 +367,7 @@ public void toyConditionalFlow2() throws Exception {
"Foo",
ImmutableMap.of(
"toys.Foo:void test(java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object)",
Sets.newHashSet(1, 4)),
Sets.newHashSet(0, 3)),
"import com.google.common.base.Preconditions;",
"import java.util.Objects;",
"import org.junit.Assert;",
Expand Down Expand Up @@ -407,7 +407,7 @@ public void toyReassigningTest() throws Exception {
"toys",
"Foo",
ImmutableMap.of(
"toys.Foo:void test(java.lang.String, java.lang.String)", Sets.newHashSet(1)),
"toys.Foo:void test(java.lang.String, java.lang.String)", Sets.newHashSet(0)),
"import com.google.common.base.Preconditions;",
"import java.util.Objects;",
"import org.junit.Assert;",
Expand Down Expand Up @@ -449,7 +449,7 @@ public void testGenericMethod() throws Exception {
"generic",
"TestGeneric",
ImmutableMap.of(
"generic.TestGeneric:java.lang.String foo(java.lang.Object)", Sets.newHashSet(1)),
"generic.TestGeneric:java.lang.String foo(java.lang.Object)", Sets.newHashSet(0)),
"public class TestGeneric<T> {",
" public String foo(T t) {",
" return t.toString();",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.uber.nullaway.NullAway;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand Down Expand Up @@ -121,6 +122,8 @@ public void jarinferNullableReturnsTest() {
* project which determines which SDK version's models are being tested.
*/
@Test
@Ignore(
"temporarily ignore while making some astubx format changes; see https://github.com/uber/NullAway/issues/1072")
public void jarInferAndroidSDKModels() {
compilationHelper
.setArgs(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ public Nullness[] onOverrideMethodInvocationParametersNullability(
for (Map.Entry<Integer, Set<String>> annotationEntry : methodArgAnnotations.entrySet()) {
if (annotationEntry.getKey() != RETURN
&& annotationEntry.getValue().contains("javax.annotation.Nonnull")) {
// Skip 'this' param for non-static methods
int nonNullPosition = annotationEntry.getKey() - (methodSymbol.isStatic() ? 0 : 1);
int nonNullPosition = annotationEntry.getKey();
jiNonNullParams.add(nonNullPosition);
argumentPositionNullness[nonNullPosition] = Nullness.NONNULL;
}
Expand Down