|
21 | 21 | import org.openrewrite.java.tree.Expression; |
22 | 22 | import org.openrewrite.java.tree.J; |
23 | 23 | import org.openrewrite.java.tree.JavaType; |
| 24 | +import org.openrewrite.java.tree.Statement; |
| 25 | + |
| 26 | +import java.util.List; |
| 27 | +import java.util.stream.Collectors; |
24 | 28 |
|
25 | 29 | import static lombok.AccessLevel.*; |
26 | 30 | import static org.openrewrite.java.tree.J.Modifier.Type.*; |
@@ -131,6 +135,43 @@ private static boolean hasMatchingSetterMethodName(J.MethodDeclaration method, S |
131 | 135 | return method.getSimpleName().equals("set" + StringUtils.capitalize(simpleName)); |
132 | 136 | } |
133 | 137 |
|
| 138 | + public static boolean isEffectivelySetter(J.MethodDeclaration method) { |
| 139 | + boolean isVoid = "void".equals(method.getType().toString()); |
| 140 | + List<Statement> actualParameters = method.getParameters().stream() |
| 141 | + .filter(s -> !(s instanceof J.Empty)) |
| 142 | + .collect(Collectors.toList()); |
| 143 | + boolean oneParam = actualParameters.size() == 1; |
| 144 | + if (!isVoid || !oneParam) |
| 145 | + return false; |
| 146 | + |
| 147 | + J.VariableDeclarations variableDeclarations = (J.VariableDeclarations) actualParameters.get(0); |
| 148 | + J.VariableDeclarations.NamedVariable param = variableDeclarations.getVariables().get(0); |
| 149 | + String paramName = param.getName().toString(); |
| 150 | + |
| 151 | + boolean singularStatement = method.getBody() != null //abstract methods can be null |
| 152 | + && method.getBody().getStatements().size() == 1 |
| 153 | + && method.getBody().getStatements().get(0) instanceof J.Assignment; |
| 154 | + |
| 155 | + if (!singularStatement) { |
| 156 | + return false; |
| 157 | + } |
| 158 | + J.Assignment assignment = (J.Assignment) method.getBody().getStatements().get(0); |
| 159 | + |
| 160 | + J.FieldAccess fieldAccess = (J.FieldAccess) assignment.getVariable(); |
| 161 | + |
| 162 | + return |
| 163 | + // assigned value is exactly the parameter |
| 164 | + assignment.getAssignment().toString().equals(paramName) |
| 165 | + |
| 166 | + // type of parameter and field have to match |
| 167 | + && param.getType().equals(fieldAccess.getType()); |
| 168 | + |
| 169 | + } |
| 170 | + |
| 171 | + public static String deriveSetterMethodName(JavaType.Variable fieldType) { |
| 172 | + return "set" + StringUtils.capitalize(fieldType.getName()); |
| 173 | + } |
| 174 | + |
134 | 175 | static AccessLevel getAccessLevel(J.MethodDeclaration methodDeclaration) { |
135 | 176 | if (methodDeclaration.hasModifier(Public)) { |
136 | 177 | return PUBLIC; |
|
0 commit comments