use of org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrAssertStatement in project intellij-community by JetBrains.
the class ConvertJunitAssertionToAssertStatementIntention method getReplacementElement.
@Nullable
private static GrStatement getReplacementElement(@NotNull PsiMethod method, @NotNull GrMethodCall methodCall) {
String replacementStatement = getReplacementStatement(method, methodCall);
if (replacementStatement == null)
return null;
GrExpression[] arguments = methodCall.getArgumentList().getExpressionArguments();
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(method.getProject());
GrAssertStatement statement = (GrAssertStatement) factory.createStatementFromText(replacementStatement);
final Map<GrExpression, GrExpression> replaceMap = new HashMap<>();
statement.acceptChildren(new GroovyRecursiveElementVisitor() {
@Override
public void visitExpression(@NotNull GrExpression expression) {
Matcher matcher = PATTERN.matcher(expression.getText());
if (matcher.matches()) {
int index = Integer.parseInt(matcher.group(1));
replaceMap.put(expression, arguments[index]);
} else {
super.visitExpression(expression);
}
}
});
for (Map.Entry<GrExpression, GrExpression> entry : replaceMap.entrySet()) {
entry.getKey().replaceWithExpression(entry.getValue(), true);
}
return statement;
}
Aggregations