@@ -43,45 +43,32 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
43
43
@ Override
44
44
public J .MethodDeclaration visitMethodDeclaration (J .MethodDeclaration method , ExecutionContext ctx ) {
45
45
J .MethodDeclaration m = super .visitMethodDeclaration (method , ctx );
46
- if (TypeUtils .asPrimitive (m .getType ()) == JavaType .Primitive .Void ) {
47
- return m .withBody (maybeRemoveReturnFromBlock (m .getBody ()));
46
+ if (TypeUtils .asPrimitive (m .getType ()) == JavaType .Primitive .Void && m .getBody () != null ) {
47
+ return m .withBody (m .getBody ().withStatements (ListUtils .mapLast (m .getBody ().getStatements (),
48
+ this ::maybeRemoveReturn )));
48
49
}
49
50
return m ;
50
51
}
51
52
52
- private J . @ Nullable Block maybeRemoveReturnFromBlock ( J . @ Nullable Block b ) {
53
- if (b == null ) {
53
+ private < S extends Statement > @ Nullable Statement maybeRemoveReturn ( S s ) {
54
+ if (s instanceof J . Return && (( J . Return ) s ). getExpression () == null ) {
54
55
return null ;
55
- }
56
-
57
- return b .withStatements (ListUtils .mapLast (b .getStatements (), lastStatement -> {
58
- if (lastStatement instanceof J .Return && ((J .Return ) lastStatement ).getExpression () == null ) {
59
- return null ;
60
- } else if (lastStatement instanceof J .If ) {
61
- return maybeRemoveReturnFromIf ((J .If ) lastStatement );
62
- } else {
63
- return lastStatement ;
64
- }
65
- }));
66
- }
67
-
68
- private J .If maybeRemoveReturnFromIf (J .If ifStatement ) {
69
- ifStatement = ifStatement .withThenPart (maybeRemoveReturnFromStatement (ifStatement .getThenPart ()));
70
- J .If .Else elze = ifStatement .getElsePart ();
71
- if (elze != null ) {
72
- return ifStatement .withElsePart (elze .withBody (maybeRemoveReturnFromStatement (elze .getBody ())));
73
- }
74
- return ifStatement ;
75
- }
76
-
77
- private Statement maybeRemoveReturnFromStatement (Statement s ) {
78
- if (s instanceof J .Block ) {
79
- return maybeRemoveReturnFromBlock ((J .Block ) s );
56
+ } else if (s instanceof J .Block ) {
57
+ J .Block block = (J .Block ) s ;
58
+ return block .withStatements (ListUtils .mapLast (block .getStatements (), this ::maybeRemoveReturn ));
80
59
} else if (s instanceof J .If ) {
81
- return maybeRemoveReturnFromIf ((J .If ) s );
82
- } else {
83
- return s ;
60
+ J .If ifStatement = (J .If ) s ;
61
+ Statement trimmedThen = maybeRemoveReturn (ifStatement .getThenPart ());
62
+ if (trimmedThen != ifStatement .getThenPart () && trimmedThen != null ) {
63
+ ifStatement = ifStatement .withThenPart (trimmedThen );
64
+ }
65
+ if (ifStatement .getElsePart () != null ) {
66
+ Statement trimmedElse = maybeRemoveReturn (ifStatement .getElsePart ().getBody ());
67
+ return ifStatement .withElsePart (trimmedElse == null ? null : ifStatement .getElsePart ().withBody (trimmedElse ));
68
+ }
69
+ return ifStatement ;
84
70
}
71
+ return s ;
85
72
}
86
73
};
87
74
}
0 commit comments