2 * Copyright 2000-2010 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.jetbrains.plugins.groovy.refactoring.changeSignature;
18 import com.intellij.psi.PsiElement;
19 import com.intellij.psi.PsiMethod;
20 import com.intellij.psi.PsiSubstitutor;
21 import com.intellij.usageView.UsageInfo;
22 import org.jetbrains.annotations.Nullable;
23 import org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult;
24 import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrConstructorInvocation;
25 import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList;
26 import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement;
27 import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
28 import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression;
29 import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression;
30 import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEnumConstant;
31 import org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureSignature;
32 import org.jetbrains.plugins.groovy.lang.psi.impl.types.GrClosureSignatureUtil;
33 import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil;
36 * @author Maxim.Medvedev
38 public class GrMethodCallUsageInfo extends UsageInfo {
39 private final boolean myToChangeArguments;
40 private final boolean myToCatchExceptions;
41 private final PsiMethod myReferencedMethod;
42 private GrClosureSignatureUtil.ArgInfo[] myMapToArguments;
43 private PsiSubstitutor mySubstitutor;
45 public boolean isToCatchExceptions() {
46 return myToCatchExceptions;
49 public boolean isToChangeArguments() {
50 return myToChangeArguments;
53 public GrMethodCallUsageInfo(PsiElement element, boolean isToChangeArguments, boolean isToCatchExceptions) {
55 myToChangeArguments = isToChangeArguments;
56 myToCatchExceptions = isToCatchExceptions;
57 final GroovyResolveResult resolveResult = resolveMethod(element);
58 myReferencedMethod = (PsiMethod)resolveResult.getElement();
59 mySubstitutor = resolveResult.getSubstitutor();
60 final GrArgumentList list = PsiUtil.getArgumentsList(element);
62 myMapToArguments = GrClosureSignatureUtil.ArgInfo.EMPTY_ARRAY;
65 final GrClosureSignature signature = GrClosureSignatureUtil.createSignature(myReferencedMethod, mySubstitutor);
67 GrClosureSignatureUtil.mapParametersToArguments(signature, list, element.getManager(), myReferencedMethod.getResolveScope());
72 private static GroovyResolveResult resolveMethod(final PsiElement ref) {
73 if (ref instanceof GrEnumConstant) return ((GrEnumConstant)ref).resolveConstructorGenerics();
74 PsiElement parent = ref.getParent();
75 if (parent instanceof GrCallExpression) {
76 return ((GrCallExpression)parent).getMethodVariants()[0];
78 else if (parent instanceof GrApplicationStatement) {
79 final GrExpression expression = ((GrApplicationStatement)parent).getFunExpression();
80 if (expression instanceof GrReferenceExpression) {
81 return ((GrReferenceExpression)expression).advancedResolve();
84 else if (parent instanceof GrConstructorInvocation) {
85 return ((GrConstructorInvocation)parent).resolveConstructorGenerics();
91 public PsiMethod getReferencedMethod() {
92 return myReferencedMethod;
95 public GrClosureSignatureUtil.ArgInfo[] getMapToArguments() {
96 return myMapToArguments;
99 public PsiSubstitutor getSubstitutor() {
100 return mySubstitutor;