use of com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo in project intellij-community by JetBrains.
the class GrChageSignatureUsageSearcher method findSimpleUsagesWithoutParameters.
private PsiMethod[] findSimpleUsagesWithoutParameters(final PsiMethod method, final ArrayList<UsageInfo> result, boolean isToModifyArgs, boolean isToThrowExceptions, boolean isOriginal) {
GlobalSearchScope projectScope = GlobalSearchScope.projectScope(method.getProject());
PsiMethod[] overridingMethods = OverridingMethodsSearch.search(method).toArray(PsiMethod.EMPTY_ARRAY);
for (PsiMethod overridingMethod : overridingMethods) {
if (GroovyLanguage.INSTANCE.equals(overridingMethod.getLanguage())) {
result.add(new OverriderUsageInfo(overridingMethod, method, isOriginal, isToModifyArgs, isToThrowExceptions));
}
}
boolean needToChangeCalls = !myChangeInfo.isGenerateDelegate() && (myChangeInfo.isNameChanged() || myChangeInfo.isParameterSetOrOrderChanged() || myChangeInfo.isExceptionSetOrOrderChanged() || myChangeInfo.isVisibilityChanged());
if (needToChangeCalls) {
PsiReference[] refs = MethodReferencesSearch.search(method, projectScope, true).toArray(PsiReference.EMPTY_ARRAY);
for (PsiReference ref : refs) {
PsiElement element = ref.getElement();
if (!GroovyLanguage.INSTANCE.equals(element.getLanguage()))
continue;
boolean isToCatchExceptions = isToThrowExceptions && needToCatchExceptions(RefactoringUtil.getEnclosingMethod(element));
if (PsiUtil.isMethodUsage(element)) {
result.add(new GrMethodCallUsageInfo(element, isToModifyArgs, isToCatchExceptions, method));
} else if (element instanceof GrDocTagValueToken) {
result.add(new UsageInfo(ref.getElement()));
} else if (element instanceof GrMethod && ((GrMethod) element).isConstructor()) {
DefaultConstructorImplicitUsageInfo implicitUsageInfo = new DefaultConstructorImplicitUsageInfo((GrMethod) element, ((GrMethod) element).getContainingClass(), method);
result.add(implicitUsageInfo);
} else if (element instanceof PsiClass) {
LOG.assertTrue(method.isConstructor());
final PsiClass psiClass = (PsiClass) element;
if (psiClass instanceof GrAnonymousClassDefinition) {
result.add(new GrMethodCallUsageInfo(element, isToModifyArgs, isToCatchExceptions, method));
continue;
}
/*if (!(myChangeInfo instanceof JavaChangeInfoImpl)) continue; todo propagate methods
if (shouldPropagateToNonPhysicalMethod(method, result, psiClass,
((JavaChangeInfoImpl)myChangeInfo).propagateParametersMethods)) {
continue;
}
if (shouldPropagateToNonPhysicalMethod(method, result, psiClass,
((JavaChangeInfoImpl)myChangeInfo).propagateExceptionsMethods)) {
continue;
}*/
result.add(new NoConstructorClassUsageInfo(psiClass));
} else if (ref instanceof PsiCallReference) {
result.add(new CallReferenceUsageInfo((PsiCallReference) ref));
} else {
result.add(new MoveRenameUsageInfo(element, ref, method));
}
}
} else if (myChangeInfo.isParameterTypesChanged()) {
PsiReference[] refs = MethodReferencesSearch.search(method, projectScope, true).toArray(PsiReference.EMPTY_ARRAY);
for (PsiReference reference : refs) {
final PsiElement element = reference.getElement();
if (element instanceof GrDocTagValueToken) {
result.add(new UsageInfo(reference));
}
}
}
// Conflicts
if (method instanceof GrMethod) {
detectLocalsCollisionsInMethod((GrMethod) method, result, isOriginal);
}
for (final PsiMethod overridingMethod : overridingMethods) {
if (overridingMethod instanceof GrMethod) {
detectLocalsCollisionsInMethod((GrMethod) overridingMethod, result, isOriginal);
}
}
return overridingMethods;
}
use of com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo in project intellij-community by JetBrains.
the class IntroduceParameterProcessor method findUsages.
@NotNull
protected UsageInfo[] findUsages() {
ArrayList<UsageInfo> result = new ArrayList<>();
PsiMethod[] overridingMethods = OverridingMethodsSearch.search(myMethodToSearchFor).toArray(PsiMethod.EMPTY_ARRAY);
for (PsiMethod overridingMethod : overridingMethods) {
result.add(new UsageInfo(overridingMethod));
}
if (!myGenerateDelegate) {
PsiReference[] refs = MethodReferencesSearch.search(myMethodToSearchFor, GlobalSearchScope.projectScope(myProject), true).toArray(PsiReference.EMPTY_ARRAY);
for (PsiReference ref1 : refs) {
PsiElement ref = ref1.getElement();
if (ref instanceof PsiMethod && ((PsiMethod) ref).isConstructor()) {
DefaultConstructorImplicitUsageInfo implicitUsageInfo = new DefaultConstructorImplicitUsageInfo((PsiMethod) ref, ((PsiMethod) ref).getContainingClass(), myMethodToSearchFor);
result.add(implicitUsageInfo);
} else if (ref instanceof PsiClass) {
result.add(new NoConstructorClassUsageInfo((PsiClass) ref));
} else if (!IntroduceParameterUtil.insideMethodToBeReplaced(ref, myMethodToReplaceIn)) {
result.add(new ExternalUsageInfo(ref));
} else {
result.add(new ChangedMethodCallInfo(ref));
}
}
}
if (myReplaceAllOccurrences) {
for (PsiElement expr : getOccurrences()) {
result.add(new InternalUsageInfo(expr));
}
} else {
if (myExpressionToSearch != null && myExpressionToSearch.isValid()) {
result.add(new InternalUsageInfo(myExpressionToSearch));
}
}
final UsageInfo[] usageInfos = result.toArray(new UsageInfo[result.size()]);
return UsageViewUtil.removeDuplicatedUsages(usageInfos);
}
use of com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo in project intellij-community by JetBrains.
the class JavaChangeSignatureUsageProcessor method processUsage.
@Override
public boolean processUsage(ChangeInfo changeInfo, UsageInfo usage, boolean beforeMethodChange, UsageInfo[] usages) {
if (!isJavaUsage(usage))
return false;
if (!(changeInfo instanceof JavaChangeInfo))
return false;
if (beforeMethodChange) {
if (usage instanceof CallerUsageInfo) {
final CallerUsageInfo callerUsageInfo = (CallerUsageInfo) usage;
processCallerMethod((JavaChangeInfo) changeInfo, callerUsageInfo.getMethod(), null, callerUsageInfo.isToInsertParameter(), callerUsageInfo.isToInsertException());
return true;
} else if (usage instanceof OverriderUsageInfo) {
OverriderUsageInfo info = (OverriderUsageInfo) usage;
final PsiMethod method = info.getOverridingMethod();
final PsiMethod baseMethod = info.getBaseMethod();
if (info.isOriginalOverrider()) {
processPrimaryMethod((JavaChangeInfo) changeInfo, method, baseMethod, false);
} else {
processCallerMethod((JavaChangeInfo) changeInfo, method, baseMethod, info.isToInsertArgs(), info.isToCatchExceptions());
}
return true;
} else if (usage instanceof MethodReferenceUsageInfo && MethodReferenceUsageInfo.needToExpand((JavaChangeInfo) changeInfo)) {
final PsiElement element = usage.getElement();
if (element instanceof PsiMethodReferenceExpression) {
final PsiExpression expression = LambdaRefactoringUtil.convertToMethodCallInLambdaBody((PsiMethodReferenceExpression) element);
if (expression instanceof PsiCallExpression) {
((MethodReferenceUsageInfo) usage).setCallExpression((PsiCallExpression) expression);
return true;
}
}
} else if (usage instanceof FunctionalInterfaceChangedUsageInfo) {
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(usage.getProject());
final PsiElement element = usage.getElement();
final PsiMethod interfaceMethod = ((FunctionalInterfaceChangedUsageInfo) usage).getMethod();
if (element instanceof PsiLambdaExpression) {
processMethodParams((JavaChangeInfo) changeInfo, interfaceMethod, elementFactory, PsiSubstitutor.EMPTY, ((PsiLambdaExpression) element).getParameterList(), ((PsiLambdaExpression) element).getBody());
} else if (element instanceof PsiMethodReferenceExpression) {
final PsiLambdaExpression lambdaExpression = LambdaRefactoringUtil.convertMethodReferenceToLambda((PsiMethodReferenceExpression) element, false, true);
if (lambdaExpression != null) {
processMethodParams(((JavaChangeInfo) changeInfo), interfaceMethod, elementFactory, PsiSubstitutor.EMPTY, lambdaExpression.getParameterList(), lambdaExpression.getBody());
}
}
return true;
}
} else {
PsiElement element = usage.getElement();
LOG.assertTrue(element != null);
if (usage instanceof DefaultConstructorImplicitUsageInfo) {
final DefaultConstructorImplicitUsageInfo defConstructorUsage = (DefaultConstructorImplicitUsageInfo) usage;
PsiMethod constructor = defConstructorUsage.getConstructor();
if (!constructor.isPhysical()) {
final boolean toPropagate = changeInfo instanceof JavaChangeInfoImpl && ((JavaChangeInfoImpl) changeInfo).propagateParametersMethods.remove(constructor);
final PsiClass containingClass = defConstructorUsage.getContainingClass();
constructor = (PsiMethod) containingClass.add(constructor);
PsiUtil.setModifierProperty(constructor, VisibilityUtil.getVisibilityModifier(containingClass.getModifierList()), true);
if (toPropagate) {
((JavaChangeInfoImpl) changeInfo).propagateParametersMethods.add(constructor);
}
}
addSuperCall((JavaChangeInfo) changeInfo, constructor, defConstructorUsage.getBaseConstructor(), usages);
return true;
} else if (usage instanceof NoConstructorClassUsageInfo) {
addDefaultConstructor(((JavaChangeInfo) changeInfo), ((NoConstructorClassUsageInfo) usage).getPsiClass(), usages);
return true;
} else if (usage instanceof MethodReferenceUsageInfo && MethodReferenceUsageInfo.needToExpand((JavaChangeInfo) changeInfo)) {
final MethodCallUsageInfo methodCallInfo = ((MethodReferenceUsageInfo) usage).createMethodCallInfo();
if (methodCallInfo != null) {
processMethodUsage(methodCallInfo.getElement(), (JavaChangeInfo) changeInfo, methodCallInfo.isToChangeArguments(), methodCallInfo.isToCatchExceptions(), methodCallInfo.getReferencedMethod(), methodCallInfo.getSubstitutor(), usages);
return true;
}
} else if (usage instanceof MethodCallUsageInfo) {
final MethodCallUsageInfo methodCallInfo = (MethodCallUsageInfo) usage;
processMethodUsage(methodCallInfo.getElement(), (JavaChangeInfo) changeInfo, methodCallInfo.isToChangeArguments(), methodCallInfo.isToCatchExceptions(), methodCallInfo.getReferencedMethod(), methodCallInfo.getSubstitutor(), usages);
return true;
} else if (usage instanceof ChangeSignatureParameterUsageInfo) {
String newName = ((ChangeSignatureParameterUsageInfo) usage).newParameterName;
String oldName = ((ChangeSignatureParameterUsageInfo) usage).oldParameterName;
processParameterUsage((PsiReferenceExpression) element, oldName, newName);
return true;
} else if (usage instanceof CallReferenceUsageInfo) {
((CallReferenceUsageInfo) usage).getReference().handleChangeSignature(changeInfo);
return true;
} else if (element instanceof PsiEnumConstant) {
fixActualArgumentsList(((PsiEnumConstant) element).getArgumentList(), (JavaChangeInfo) changeInfo, true, PsiSubstitutor.EMPTY);
return true;
} else if (!(usage instanceof OverriderUsageInfo)) {
PsiReference reference = usage instanceof MoveRenameUsageInfo ? usage.getReference() : element.getReference();
if (reference != null) {
PsiElement target = changeInfo.getMethod();
if (target != null) {
reference.bindToElement(target);
}
}
}
}
return false;
}
use of com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo in project intellij-community by JetBrains.
the class IntroduceParameterUtil method processUsages.
public static void processUsages(UsageInfo[] usages, IntroduceParameterData data) {
PsiManager manager = PsiManager.getInstance(data.getProject());
List<UsageInfo> methodUsages = new ArrayList<>();
for (UsageInfo usage : usages) {
if (usage instanceof InternalUsageInfo)
continue;
if (usage instanceof DefaultConstructorImplicitUsageInfo) {
addSuperCall(usage, usages, data);
} else if (usage instanceof NoConstructorClassUsageInfo) {
addDefaultConstructor(usage, usages, data);
} else {
PsiElement element = usage.getElement();
if (element instanceof PsiMethod) {
if (!manager.areElementsEquivalent(element, data.getMethodToReplaceIn())) {
methodUsages.add(usage);
}
} else if (!data.isGenerateDelegate()) {
changeExternalUsage(usage, usages, data);
}
}
}
for (UsageInfo usage : methodUsages) {
changeMethodSignatureAndResolveFieldConflicts(usage, usages, data);
}
}
use of com.intellij.refactoring.util.usageInfo.DefaultConstructorImplicitUsageInfo in project intellij-community by JetBrains.
the class GrIntroduceParameterProcessor method findUsages.
@NotNull
@Override
protected UsageInfo[] findUsages() {
ArrayList<UsageInfo> result = new ArrayList<>();
final PsiMethod toSearchFor = ((PsiMethod) mySettings.getToSearchFor());
if (!mySettings.generateDelegate()) {
Collection<PsiReference> refs = MethodReferencesSearch.search(toSearchFor, GlobalSearchScope.projectScope(myProject), true).findAll();
for (PsiReference ref1 : refs) {
PsiElement ref = ref1.getElement();
if (ref instanceof PsiMethod && ((PsiMethod) ref).isConstructor()) {
DefaultConstructorImplicitUsageInfo implicitUsageInfo = new DefaultConstructorImplicitUsageInfo((PsiMethod) ref, ((PsiMethod) ref).getContainingClass(), toSearchFor);
result.add(implicitUsageInfo);
} else if (ref instanceof PsiClass) {
if (ref instanceof GrAnonymousClassDefinition) {
result.add(new ExternalUsageInfo(((GrAnonymousClassDefinition) ref).getBaseClassReferenceGroovy()));
} else if (ref instanceof PsiAnonymousClass) {
result.add(new ExternalUsageInfo(((PsiAnonymousClass) ref).getBaseClassReference()));
} else {
result.add(new NoConstructorClassUsageInfo((PsiClass) ref));
}
} else if (!PsiTreeUtil.isAncestor(mySettings.getToReplaceIn(), ref, false)) {
result.add(new ExternalUsageInfo(ref));
} else {
result.add(new ChangedMethodCallInfo(ref));
}
}
}
if (mySettings.replaceAllOccurrences()) {
if (mySettings.getVar() != null) {
for (PsiElement element : GrIntroduceHandlerBase.collectVariableUsages(mySettings.getVar(), mySettings.getToReplaceIn())) {
result.add(new InternalUsageInfo(element));
}
} else {
PsiElement[] exprs = GroovyIntroduceParameterUtil.getOccurrences(mySettings);
for (PsiElement expr : exprs) {
result.add(new InternalUsageInfo(expr));
}
}
} else {
if (mySettings.getExpression() != null) {
result.add(new InternalUsageInfo(mySettings.getExpression()));
}
}
Collection<PsiMethod> overridingMethods = OverridingMethodsSearch.search(toSearchFor).findAll();
for (PsiMethod overridingMethod : overridingMethods) {
result.add(new UsageInfo(overridingMethod));
}
final UsageInfo[] usageInfos = result.toArray(new UsageInfo[result.size()]);
return UsageViewUtil.removeDuplicatedUsages(usageInfos);
}
Aggregations