use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement 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.GrStatement in project intellij-community by JetBrains.
the class GroovyInlineMethodUtil method checkTailOpenBlock.
private static boolean checkTailOpenBlock(GrOpenBlock block, Collection<GrStatement> returnStatements) {
if (block == null)
return false;
GrStatement[] statements = block.getStatements();
if (statements.length == 0)
return false;
GrStatement last = statements[statements.length - 1];
if (returnStatements.contains(last)) {
returnStatements.remove(last);
return true;
}
if (last instanceof GrIfStatement) {
return checkTailIfStatement(((GrIfStatement) last), returnStatements);
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.
the class GroovyInlineMethodUtil method checkTailIfStatement.
public static boolean checkTailIfStatement(GrIfStatement ifStatement, Collection<GrStatement> returnStatements) {
GrStatement thenBranch = ifStatement.getThenBranch();
GrStatement elseBranch = ifStatement.getElseBranch();
if (elseBranch == null)
return false;
boolean tb = false;
boolean eb = false;
if (thenBranch instanceof GrReturnStatement) {
tb = returnStatements.remove(thenBranch);
} else if (thenBranch instanceof GrBlockStatement) {
tb = checkTailOpenBlock(((GrBlockStatement) thenBranch).getBlock(), returnStatements);
}
if (elseBranch instanceof GrReturnStatement) {
eb = returnStatements.remove(elseBranch);
} else if (elseBranch instanceof GrBlockStatement) {
eb = checkTailOpenBlock(((GrBlockStatement) elseBranch).getBlock(), returnStatements);
}
return tb && eb;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement 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.GrStatement in project intellij-community by JetBrains.
the class ExtractUtil method createResultStatement.
@NotNull
private static GrStatement[] createResultStatement(ExtractInfoHelper helper) {
VariableInfo[] outputVars = helper.getOutputVariableInfos();
PsiType type = helper.getOutputType();
GrStatement[] statements = helper.getStatements();
GrMethodCallExpression callExpression = createMethodCall(helper);
if ((outputVars.length == 0 || PsiType.VOID.equals(type)) && !helper.hasReturnValue())
return new GrStatement[] { callExpression };
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(helper.getProject());
if (helper.hasReturnValue()) {
return new GrStatement[] { factory.createStatementFromText("return " + callExpression.getText()) };
}
LOG.assertTrue(outputVars.length > 0);
final List<VariableInfo> mustAdd = mustAddVariableDeclaration(statements, outputVars);
if (mustAdd.isEmpty()) {
return new GrStatement[] { createAssignment(outputVars, callExpression, helper.getProject()) };
} else if (mustAdd.size() == outputVars.length && outputVars.length == 1) {
return new GrVariableDeclaration[] { factory.createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, callExpression, outputVars[0].getType(), outputVars[0].getName()) };
} else if (varsAreEqual(mustAdd, outputVars)) {
return createTupleDeclaration(outputVars, callExpression, helper.getProject());
} else {
final List<GrStatement> result = generateVarDeclarations(mustAdd, helper.getProject(), null);
result.add(createAssignment(outputVars, callExpression, helper.getProject()));
return result.toArray(new GrStatement[result.size()]);
}
}
Aggregations