use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.
the class GroovyInlineMethodUtil method replaceAllOccurrencesWithExpression.
private static void replaceAllOccurrencesWithExpression(GrMethod method, GrCallExpression call, GrExpression oldExpression, GrParameter parameter) {
Collection<PsiReference> refs = ReferencesSearch.search(parameter, new LocalSearchScope(method), false).findAll();
final GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(call.getProject());
GrExpression expression = elementFactory.createExpressionFromText(oldExpression.getText());
if (GroovyRefactoringUtil.hasSideEffect(expression) && refs.size() > 1 || !hasUnresolvableWriteAccess(refs, oldExpression)) {
final String oldName = parameter.getName();
final String newName = InlineMethodConflictSolver.suggestNewName(oldName, method, call);
expression = elementFactory.createExpressionFromText(newName);
final GrOpenBlock body = method.getBlock();
final GrStatement[] statements = body.getStatements();
GrStatement anchor = null;
if (statements.length > 0) {
anchor = statements[0];
}
body.addStatementBefore(elementFactory.createStatementFromText(createVariableDefinitionText(parameter, oldExpression, newName)), anchor);
}
for (PsiReference ref : refs) {
PsiElement element = ref.getElement();
if (element instanceof GrReferenceExpression) {
((GrReferenceExpression) element).replaceWithExpression(expression, true);
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.
the class GroovyMethodInliner method getAloneResultExpression.
/**
* Get method result expression (if it is alone in method)
*
* @return null if method has more or less than one return statement or has void type
*/
@Nullable
static GrExpression getAloneResultExpression(@NotNull GrMethod method) {
GrOpenBlock body = method.getBlock();
assert body != null;
GrStatement[] statements = body.getStatements();
if (statements.length == 1) {
if (statements[0] instanceof GrExpression)
return (GrExpression) statements[0];
if (statements[0] instanceof GrReturnStatement) {
GrExpression value = ((GrReturnStatement) statements[0]).getReturnValue();
if (value == null && !PsiType.VOID.equals(PsiUtil.getSmartReturnType(method))) {
return GroovyPsiElementFactory.getInstance(method.getProject()).createExpressionFromText("null");
}
return value;
}
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.
the class GrIntroduceFieldProcessor method initializeInSetup.
void initializeInSetup(@NotNull GrVariable field, @NotNull Collection<PsiElement> replaced) {
final PsiMethod setUpMethod = TestFrameworks.getInstance().findOrCreateSetUpMethod(((PsiClass) myContext.getScope()));
assert setUpMethod instanceof GrMethod;
final GrOpenBlock body = ((GrMethod) setUpMethod).getBlock();
final GrStatement anchor = findAnchorForAssignment(body, replaced);
generateAssignment(field, anchor, body, null);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.
the class ExpressionGenerator method isImplicitlyCastedToArray.
private static boolean isImplicitlyCastedToArray(GrListOrMap list) {
PsiElement parent = list.getParent();
GrControlFlowOwner owner = ControlFlowUtils.findControlFlowOwner(list);
if (!(owner instanceof GrOpenBlock && owner.getParent() instanceof GrMethod))
return false;
if (!(parent instanceof GrReturnStatement || ControlFlowUtils.isReturnValue(list, owner)))
return false;
PsiType type = ((GrMethod) owner.getParent()).getReturnType();
return type instanceof PsiArrayType;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.
the class CodeBlockGenerator method visitFinallyClause.
@Override
public void visitFinallyClause(@NotNull GrFinallyClause finallyClause) {
builder.append("finally ");
final GrOpenBlock body = finallyClause.getBody();
if (body != null) {
body.accept(this);
}
}
Aggregations