Skip to content

Commit f3b9181

Browse files
authored
Fix for explicitly-typed lambda parameters in JDK 24+ (uber#1452)
This case worked before JDK 24 since somehow our code that looked at the owner of a symbol found the annotation; for some reason it stopped working. Looking at annotations on the `type` field of the symbol seems to find what we need. Fixes uber#1448 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Bug Fixes * Enhanced nullability annotation detection to recognize type-use annotations on symbol types, improving coverage for explicitly-annotated lambda parameters that were previously undetected. ## Tests * Added test coverage for enhanced annotation detection in generic lambda scenarios. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 1167221 commit f3b9181

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

nullaway/src/main/java/com/uber/nullaway/NullabilityUtil.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ public static boolean hasAnyAnnotationMatching(
211211
return true;
212212
}
213213
}
214+
// we need this loop over the type's annotation mirrors in cases like explicitly-annotated
215+
// lambda parameters, possibly due to bugs in javac
216+
for (AnnotationMirror annotationMirror : symbol.type.getAnnotationMirrors()) {
217+
if (predicate.test(annotationMirror.getAnnotationType().toString())) {
218+
return true;
219+
}
220+
}
214221
// to handle bytecodes, also check direct type-use annotations stored in attributes
215222
Symbol typeAnnotationOwner =
216223
symbol.getKind().equals(ElementKind.PARAMETER) ? symbol.owner : symbol;

nullaway/src/test/java/com/uber/nullaway/jspecify/GenericLambdaTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,31 @@ void testNegative(Foo<String> foo) {
133133
.doTest();
134134
}
135135

136+
@Test
137+
public void issue1448() {
138+
makeHelper()
139+
.addSourceLines(
140+
"Test.java",
141+
"""
142+
package org.example;
143+
144+
import org.jspecify.annotations.NullMarked;
145+
import org.jspecify.annotations.Nullable;
146+
147+
@NullMarked
148+
class Test {
149+
interface Func<T> {
150+
void apply( @Nullable T t );
151+
}
152+
153+
class Bar {
154+
Func<Integer> baz = ( @Nullable Integer i ) -> {};
155+
}
156+
}
157+
""")
158+
.doTest();
159+
}
160+
136161
private CompilationTestHelper makeHelper() {
137162
return makeTestHelperWithArgs(
138163
JSpecifyJavacConfig.withJSpecifyModeArgs(

0 commit comments

Comments
 (0)