use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GroovyIntroduceParameterUtil method processChangedMethodCall.
public static void processChangedMethodCall(PsiElement element, GrIntroduceParameterSettings settings, Project project) {
if (!(element.getParent() instanceof GrMethodCallExpression)) {
LOG.error(element.getParent());
return;
}
GrMethodCallExpression methodCall = (GrMethodCallExpression) element.getParent();
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
final String name = settings.getName();
LOG.assertTrue(name != null);
GrExpression expression = factory.createExpressionFromText(name, null);
final GrArgumentList argList = methodCall.getArgumentList();
final PsiElement[] exprs = argList.getAllArguments();
if (exprs.length > 0) {
argList.addAfter(expression, exprs[exprs.length - 1]);
} else {
argList.add(expression);
}
removeParametersFromCall(methodCall, settings);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GroovyIntroduceParameterMethodUsagesProcessor method processChangeMethodUsage.
@Override
public boolean processChangeMethodUsage(IntroduceParameterData data, UsageInfo usage, UsageInfo[] usages) throws IncorrectOperationException {
GrCall callExpression = GroovyRefactoringUtil.getCallExpressionByMethodReference(usage.getElement());
if (callExpression == null)
return true;
GrArgumentList argList = callExpression.getArgumentList();
GrExpression[] oldArgs = argList.getExpressionArguments();
final GrExpression anchor;
if (!data.getMethodToSearchFor().isVarArgs()) {
anchor = getLast(oldArgs);
} else {
final PsiParameter[] parameters = data.getMethodToSearchFor().getParameterList().getParameters();
if (parameters.length > oldArgs.length) {
anchor = getLast(oldArgs);
} else {
final int lastNonVararg = parameters.length - 2;
anchor = lastNonVararg >= 0 ? oldArgs[lastNonVararg] : null;
}
}
PsiMethod method = PsiTreeUtil.getParentOfType(argList, PsiMethod.class);
GrClosureSignature signature = GrClosureSignatureUtil.createSignature(callExpression);
if (signature == null)
signature = GrClosureSignatureUtil.createSignature(data.getMethodToSearchFor(), PsiSubstitutor.EMPTY);
final GrClosureSignatureUtil.ArgInfo<PsiElement>[] actualArgs = GrClosureSignatureUtil.mapParametersToArguments(signature, callExpression.getNamedArguments(), callExpression.getExpressionArguments(), callExpression.getClosureArguments(), callExpression, false, true);
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(data.getProject());
if (method != null && IntroduceParameterUtil.isMethodInUsages(data, method, usages)) {
argList.addAfter(factory.createExpressionFromText(data.getParameterName()), anchor);
} else {
final PsiElement _expr = data.getParameterInitializer().getExpression();
PsiElement initializer = ExpressionConverter.getExpression(_expr, GroovyLanguage.INSTANCE, data.getProject());
LOG.assertTrue(initializer instanceof GrExpression);
GrExpression newArg = GroovyIntroduceParameterUtil.addClosureToCall(initializer, argList);
if (newArg == null) {
final PsiElement dummy = argList.addAfter(factory.createExpressionFromText("1"), anchor);
newArg = ((GrExpression) dummy).replaceWithExpression((GrExpression) initializer, true);
}
final PsiMethod methodToReplaceIn = data.getMethodToReplaceIn();
new OldReferencesResolver(callExpression, newArg, methodToReplaceIn, data.getReplaceFieldsWithGetters(), initializer, signature, actualArgs, methodToReplaceIn.getParameterList().getParameters()).resolve();
ChangeContextUtil.clearContextInfo(initializer);
//newArg can be replaced by OldReferenceResolver
if (newArg.isValid()) {
JavaCodeStyleManager.getInstance(newArg.getProject()).shortenClassReferences(newArg);
CodeStyleManager.getInstance(data.getProject()).reformat(newArg);
}
}
if (actualArgs == null) {
removeParamsFromUnresolvedCall(callExpression, data);
} else {
removeParametersFromCall(actualArgs, data.getParametersToRemove());
}
if (argList.getAllArguments().length == 0 && PsiImplUtil.hasClosureArguments(callExpression)) {
final GrArgumentList emptyArgList = ((GrMethodCallExpression) factory.createExpressionFromText("foo{}")).getArgumentList();
argList.replace(emptyArgList);
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class CompleteReferenceExpression method getBindings.
private void getBindings() {
final PsiClass containingClass = PsiTreeUtil.getParentOfType(myRefExpr, PsiClass.class);
if (containingClass != null)
return;
final PsiFile file = FileContextUtil.getContextFile(myRefExpr);
if (file instanceof GroovyFile) {
((GroovyFile) file).accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitAssignmentExpression(@NotNull GrAssignmentExpression expression) {
super.visitAssignmentExpression(expression);
final GrExpression value = expression.getLValue();
if (value instanceof GrReferenceExpression && !((GrReferenceExpression) value).isQualified()) {
final PsiElement resolved = ((GrReferenceExpression) value).resolve();
if (resolved instanceof GrBindingVariable) {
myProcessor.execute(resolved, ResolveState.initial());
} else if (resolved == null) {
myProcessor.execute(new GrBindingVariable((GroovyFile) file, ((GrReferenceExpression) value).getReferenceName(), true), ResolveState.initial());
}
}
}
@Override
public void visitTypeDefinition(@NotNull GrTypeDefinition typeDefinition) {
//don't go into classes
}
});
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GdslClosureCompleter method getParameterInfos.
@Override
protected List<ClosureParameterInfo> getParameterInfos(InsertionContext context, PsiMethod method, PsiSubstitutor substitutor, PsiElement place) {
final ArrayList<ClosureDescriptor> descriptors = new ArrayList<>();
GrReferenceExpression ref = (GrReferenceExpression) place;
PsiType qtype = PsiImplUtil.getQualifierType(ref);
if (qtype == null)
return null;
GrExpression qualifier = ref.getQualifier();
if (qualifier != null) {
PsiType type = qualifier.getType();
if (type == null)
return null;
processExecutors(qtype, ref, descriptors);
} else {
PsiElementFactory factory = JavaPsiFacade.getElementFactory(context.getProject());
for (PsiElement parent = ref.getParent(); parent != null; parent = parent.getParent()) {
if (parent instanceof GrClosableBlock) {
processExecutors(TypesUtil.createTypeByFQClassName(GroovyCommonClassNames.GROOVY_LANG_CLOSURE, ref), ref, descriptors);
} else if (parent instanceof GrTypeDefinition) {
processExecutors(factory.createType(((GrTypeDefinition) parent), PsiType.EMPTY_ARRAY), ref, descriptors);
}
}
}
for (ClosureDescriptor descriptor : descriptors) {
if (descriptor.isMethodApplicable(method, ref)) {
return descriptor.getParameters();
}
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GrRefactoringConflictsUtil method checkUsedElements.
public static void checkUsedElements(PsiMember member, PsiElement scope, @NotNull Set<GrMember> membersToMove, @Nullable Set<PsiMethod> abstractMethods, @Nullable PsiClass targetClass, @NotNull PsiElement context, MultiMap<PsiElement, String> conflicts) {
final Set<PsiMember> moving = new HashSet<>(membersToMove);
if (abstractMethods != null) {
moving.addAll(abstractMethods);
}
if (scope instanceof GrReferenceExpression) {
GrReferenceExpression refExpr = (GrReferenceExpression) scope;
PsiElement refElement = refExpr.resolve();
if (refElement instanceof PsiMember) {
if (!RefactoringHierarchyUtil.willBeInTargetClass(refElement, moving, targetClass, false)) {
GrExpression qualifier = refExpr.getQualifierExpression();
PsiClass accessClass = (PsiClass) (qualifier != null ? PsiUtil.getAccessObjectClass(qualifier).getElement() : null);
RefactoringConflictsUtil.checkAccessibility((PsiMember) refElement, context, accessClass, member, conflicts);
}
}
} else if (scope instanceof GrNewExpression) {
final GrNewExpression newExpression = (GrNewExpression) scope;
final GrAnonymousClassDefinition anonymousClass = newExpression.getAnonymousClassDefinition();
if (anonymousClass != null) {
if (!RefactoringHierarchyUtil.willBeInTargetClass(anonymousClass, moving, targetClass, false)) {
RefactoringConflictsUtil.checkAccessibility(anonymousClass, context, anonymousClass, member, conflicts);
}
} else {
final PsiMethod refElement = newExpression.resolveMethod();
if (refElement != null) {
if (!RefactoringHierarchyUtil.willBeInTargetClass(refElement, moving, targetClass, false)) {
RefactoringConflictsUtil.checkAccessibility(refElement, context, null, member, conflicts);
}
}
}
} else if (scope instanceof GrCodeReferenceElement) {
GrCodeReferenceElement refExpr = (GrCodeReferenceElement) scope;
PsiElement refElement = refExpr.resolve();
if (refElement instanceof PsiMember) {
if (!RefactoringHierarchyUtil.willBeInTargetClass(refElement, moving, targetClass, false)) {
RefactoringConflictsUtil.checkAccessibility((PsiMember) refElement, context, null, member, conflicts);
}
}
}
for (PsiElement child : scope.getChildren()) {
if (child instanceof PsiWhiteSpace || child instanceof PsiComment)
continue;
checkUsedElements(member, child, membersToMove, abstractMethods, targetClass, context, conflicts);
}
}
Aggregations