Skip to content

Commit ec0f65e

Browse files
committed
fix ClassCastException
1 parent 427fa89 commit ec0f65e

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,17 @@ private static boolean[] getTypeParamsWithNullableUpperBound(
160160
*/
161161
public static void checkGenericMethodCallTypeArguments(
162162
Tree tree, VisitorState state, NullAway analysis, Config config, Handler handler) {
163-
List<? extends Tree> typeArguments = ((MethodInvocationTree) tree).getTypeArguments();
163+
List<? extends Tree> typeArguments;
164+
switch (tree.getKind()) {
165+
case METHOD_INVOCATION:
166+
typeArguments = ((MethodInvocationTree) tree).getTypeArguments();
167+
break;
168+
case NEW_CLASS:
169+
typeArguments = ((NewClassTree) tree).getTypeArguments();
170+
break;
171+
default:
172+
throw new RuntimeException("Unexpected tree kind: " + tree.getKind());
173+
}
164174
if (typeArguments.isEmpty()) {
165175
return;
166176
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,24 @@ public void issue1035() {
190190
.doTest();
191191
}
192192

193+
@Test
194+
public void issue1138() {
195+
makeHelper()
196+
.addSourceLines(
197+
"Test.java",
198+
"package com.uber;",
199+
"import org.jspecify.annotations.NullMarked;",
200+
"@NullMarked",
201+
"class Foo {",
202+
" <T> Foo(T source) {",
203+
" }",
204+
" static <T> Foo create(T in) {",
205+
" return new Foo(in);",
206+
" }",
207+
"}")
208+
.doTest();
209+
}
210+
193211
private CompilationTestHelper makeHelper() {
194212
return makeTestHelperWithArgs(
195213
Arrays.asList(

0 commit comments

Comments
 (0)