use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.
the class ConvertSimpleGetterToPropertyIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
GrMethod method = (GrMethod) element.getParent();
GrOpenBlock block = method.getBlock();
if (block == null)
return;
GrStatement statement = block.getStatements()[0];
GrExpression value;
if (statement instanceof GrReturnStatement) {
value = ((GrReturnStatement) statement).getReturnValue();
} else {
value = (GrExpression) statement;
}
String fieldName = GroovyPropertyUtils.getPropertyNameByGetter(method);
if (fieldName == null)
return;
PsiClass aClass = method.getContainingClass();
if (aClass == null)
return;
List<String> modifiers = ContainerUtil.newArrayList();
for (String modifier : MODIFIERS_TO_CHECK) {
if (method.hasModifierProperty(modifier))
modifiers.add(modifier);
}
modifiers.add(PsiModifier.FINAL);
GrTypeElement returnTypeElement = method.getReturnTypeElementGroovy();
PsiType returnType = returnTypeElement == null ? null : returnTypeElement.getType();
GrVariableDeclaration declaration = GroovyPsiElementFactory.getInstance(project).createFieldDeclaration(ArrayUtil.toStringArray(modifiers), fieldName, value, returnType);
PsiElement replaced = method.replace(declaration);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(replaced);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.
the class MergeElseIfIntention method processIntention.
@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrIfStatement parentStatement = (GrIfStatement) element;
GrBlockStatement elseBlockStatement = (GrBlockStatement) parentStatement.getElseBranch();
assert elseBlockStatement != null;
final GrOpenBlock elseBranch = elseBlockStatement.getBlock();
final GrStatement elseBranchContents = elseBranch.getStatements()[0];
PsiImplUtil.replaceStatement("if(" + parentStatement.getCondition().getText() + ")" + parentStatement.getThenBranch().getText() + "else " + elseBranchContents.getText(), parentStatement);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.
the class GroovyExtractMethodHandler method renameParameterOccurrences.
private static void renameParameterOccurrences(GrMethod method, ExtractMethodInfoHelper helper) throws IncorrectOperationException {
GrOpenBlock block = method.getBlock();
if (block == null)
return;
GrStatement[] statements = block.getStatements();
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(helper.getProject());
for (ParameterInfo info : helper.getParameterInfos()) {
final String oldName = info.getOriginalName();
final String newName = info.getName();
final ArrayList<GrExpression> result = new ArrayList<>();
if (!oldName.equals(newName)) {
for (final GrStatement statement : statements) {
statement.accept(new PsiRecursiveElementVisitor() {
@Override
public void visitElement(final PsiElement element) {
super.visitElement(element);
if (element instanceof GrReferenceExpression) {
GrReferenceExpression expr = (GrReferenceExpression) element;
if (!expr.isQualified() && oldName.equals(expr.getReferenceName())) {
result.add(expr);
}
}
}
});
for (GrExpression expr : result) {
expr.replaceWithExpression(factory.createExpressionFromText(newName), false);
}
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.
the class GroovyMethodInliner method isOnExpressionOrReturnPlace.
/*
Method call is used as expression in some enclosing expression or
is method return result
*/
private static boolean isOnExpressionOrReturnPlace(@NotNull GrCallExpression call) {
PsiElement parent = call.getParent();
if (!(parent instanceof GrVariableDeclarationOwner)) {
return true;
}
// tail calls in methods and closures
GrVariableDeclarationOwner owner = (GrVariableDeclarationOwner) parent;
if (owner instanceof GrClosableBlock || owner instanceof GrOpenBlock && owner.getParent() instanceof GrMethod) {
GrStatement[] statements = ((GrCodeBlock) owner).getStatements();
assert statements.length > 0;
GrStatement last = statements[statements.length - 1];
if (last == call)
return true;
if (last instanceof GrReturnStatement && call == ((GrReturnStatement) last).getReturnValue()) {
return true;
}
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock in project intellij-community by JetBrains.
the class GroovyInlineMethodUtil method isTailMethodCall.
/**
* Checks whether given method call is tail call of other method or closure
*
* @param call [tail?] Method call
* @return
*/
static boolean isTailMethodCall(GrCallExpression call) {
GrStatement stmt = call;
PsiElement parent = call.getParent();
// return statement
if (parent instanceof GrReturnStatement) {
stmt = ((GrReturnStatement) parent);
parent = parent.getParent();
}
// method body result
if (parent instanceof GrOpenBlock) {
if (parent.getParent() instanceof GrMethod) {
GrStatement[] statements = ((GrOpenBlock) parent).getStatements();
return statements.length > 0 && stmt == statements[statements.length - 1];
}
}
// closure result
if (parent instanceof GrClosableBlock) {
GrStatement[] statements = ((GrClosableBlock) parent).getStatements();
return statements.length > 0 && stmt == statements[statements.length - 1];
}
// todo test me!
if (stmt instanceof GrReturnStatement) {
GrMethod method = PsiTreeUtil.getParentOfType(stmt, GrMethod.class);
if (method != null) {
Collection<GrStatement> returnStatements = ControlFlowUtils.collectReturns(method.getBlock());
return returnStatements.contains(stmt) && !hasBadReturns(method);
}
}
return false;
}
Aggregations