@@ -64,6 +64,9 @@ public J visitIf(J.If iff, ExecutionContext ctx) {
6464
6565 if (switchCandidate .isValidCandidate ()) {
6666 Object [] arguments = switchCandidate .buildTemplateArguments (getCursor ());
67+ if (arguments .length == 0 ) {
68+ return iff ;
69+ }
6770 String switchBody = switchCandidate .buildTemplate ();
6871 J .Switch switch_ = JavaTemplate .builder (switchBody )
6972 .javaParser (JavaParser .fromJavaVersion ().classpathFromResources (ctx ))
@@ -150,13 +153,21 @@ boolean isValidCandidate() {
150153 return potentialCandidate ;
151154 }
152155
153- String switchOn () {
154- return ((J .Identifier ) patternMatchers .keySet ().stream ().map (J .InstanceOf ::getExpression ).findAny ().get ()).getSimpleName ();
156+ @ Nullable String switchOn () {
157+ return patternMatchers .keySet ().stream ().map (J .InstanceOf ::getExpression ).findAny ()
158+ .filter (instanceOf -> instanceOf instanceof J .Identifier )
159+ .map (J .Identifier .class ::cast )
160+ .map (J .Identifier ::getSimpleName )
161+ .orElse (null );
155162 }
156163
157164 Object [] buildTemplateArguments (Cursor cursor ) {
165+ String switchOn = switchOn ();
166+ if (switchOn == null ) {
167+ return new Object [0 ];
168+ }
158169 Object [] arguments = new Object [1 + (nullCheckedParameter != null ? 1 : 0 ) + (patternMatchers .size () * 3 ) + (elze != null ? 1 : 0 )];
159- arguments [0 ] = switchOn () ;
170+ arguments [0 ] = switchOn ;
160171 int i = 1 ;
161172 if (nullCheckedParameter != null ) {
162173 // case null -> nullCheckedStatement
0 commit comments