Skip to content

Commit 4e1eb27

Browse files
authored
Fix NPE when type is null in RemoveBuiltInModuleRegistrations (#55)
Add null checks before calling TypeUtils.toString() since J.VariableDeclarations.getType() and Expression.getType() can return null, particularly for lambda parameters with inferred types.
1 parent f06ec74 commit 4e1eb27

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

src/main/java/org/openrewrite/java/jackson/RemoveBuiltInModuleRegistrations.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.openrewrite.java.search.UsesMethod;
2626
import org.openrewrite.java.tree.Expression;
2727
import org.openrewrite.java.tree.J;
28+
import org.openrewrite.java.tree.JavaType;
2829
import org.openrewrite.java.tree.TypeUtils;
2930

3031
import java.util.Arrays;
@@ -83,7 +84,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
8384
@Override
8485
public @Nullable J visitVariableDeclarations(J.VariableDeclarations multiVariable, ExecutionContext ctx) {
8586
J.VariableDeclarations mv = (J.VariableDeclarations) super.visitVariableDeclarations(multiVariable, ctx);
86-
if (BUILT_IN_MODULES.contains(TypeUtils.toString(mv.getType()))) {
87+
JavaType type = mv.getType();
88+
if (type != null && BUILT_IN_MODULES.contains(TypeUtils.toString(type))) {
8789
return null;
8890
}
8991
return mv;
@@ -93,10 +95,12 @@ private boolean isBuiltInModuleInstantiation(Expression expr) {
9395
if (expr instanceof J.NewClass) {
9496
J.NewClass newClass = (J.NewClass) expr;
9597
if (newClass.getClazz() != null) {
96-
return BUILT_IN_MODULES.contains(TypeUtils.toString(newClass.getClazz().getType()));
98+
JavaType type = newClass.getClazz().getType();
99+
return type != null && BUILT_IN_MODULES.contains(TypeUtils.toString(type));
97100
}
98101
}
99-
return BUILT_IN_MODULES.contains(TypeUtils.toString(expr.getType()));
102+
JavaType type = expr.getType();
103+
return type != null && BUILT_IN_MODULES.contains(TypeUtils.toString(type));
100104
}
101105
}
102106
);

0 commit comments

Comments
 (0)