use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class MyPredicate method checkForReturnFromMethod.
public static boolean checkForReturnFromMethod(GrExpression replacedNewExpression) {
final PsiElement parent = PsiUtil.skipParentheses(replacedNewExpression.getParent(), true);
final GrMethod method = PsiTreeUtil.getParentOfType(replacedNewExpression, GrMethod.class, true, GrClosableBlock.class);
if (method == null)
return false;
if (!(parent instanceof GrReturnStatement)) {
//check for return expression
final List<GrStatement> returns = ControlFlowUtils.collectReturns(method.getBlock());
final PsiElement expr = PsiUtil.skipParentheses(replacedNewExpression, true);
if (!(returns.contains(expr)))
return false;
}
return !(!ApplicationManager.getApplication().isUnitTestMode() && Messages.showYesNoDialog(replacedNewExpression.getProject(), GroovyIntentionsBundle.message("do.you.want.to.change.method.return.type", method.getName()), GroovyIntentionsBundle.message("convert.map.to.class.intention.name"), Messages.getQuestionIcon()) != Messages.YES);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class ConvertSimpleGetterToPropertyIntention method getElementPredicate.
@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
return new PsiElementPredicate() {
@Override
public boolean satisfiedBy(PsiElement element) {
PsiElement parent = element.getParent();
if (!(parent instanceof GrMethod) || ((GrMethod) parent).getNameIdentifierGroovy() != element)
return false;
GrMethod method = (GrMethod) parent;
GrOpenBlock block = method.getBlock();
if (block == null)
return false;
GrStatement[] statements = block.getStatements();
if (statements.length != 1)
return false;
if (!GroovyPropertyUtils.isSimplePropertyGetter(method))
return false;
if (GroovyPropertyUtils.findFieldForAccessor(method, true) != null)
return false;
GrStatement statement = statements[0];
if (!(statement instanceof GrReturnStatement && ((GrReturnStatement) statement).getReturnValue() != null || statement instanceof GrExpression && !PsiType.VOID.equals(((GrExpression) statement).getType()))) {
return false;
}
return true;
}
};
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod 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.typedef.members.GrMethod in project intellij-community by JetBrains.
the class GroovyExtractMethodDialog method doOKAction.
@Override
protected void doOKAction() {
myHelper.setForceReturn(myForceReturnCheckBox.isSelected());
String name = getEnteredName();
if (name == null)
return;
GrMethod method = ExtractUtil.createMethod(myHelper);
if (method != null && !validateMethod(method, myHelper)) {
return;
}
final GroovyApplicationSettings settings = GroovyApplicationSettings.getInstance();
if (myCbSpecifyType.isEnabled()) {
settings.EXTRACT_METHOD_SPECIFY_TYPE = myCbSpecifyType.isSelected();
}
if (myForceReturnCheckBox.isEnabled()) {
settings.FORCE_RETURN = myForceReturnCheckBox.isSelected();
}
settings.EXTRACT_METHOD_VISIBILITY = myVisibilityPanel.getVisibility();
super.doOKAction();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod 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;
}
Aggregations