protected boolean preprocessUsages(Ref<UsageInfo[]> refUsages) { final UsageInfo[] usages = refUsages.get(); MultiMap<PsiElement, String> conflicts = new MultiMap<PsiElement, String>(); final Set<PsiMember> members = new HashSet<PsiMember>(); members.add(myMethod); if (myTargetVariable instanceof PsiField) members.add((PsiMember) myTargetVariable); if (!myTargetClass.isInterface()) { RefactoringConflictsUtil.analyzeAccessibilityConflicts( members, myTargetClass, conflicts, myNewVisibility); } else { for (final UsageInfo usage : usages) { if (usage instanceof InheritorUsageInfo) { RefactoringConflictsUtil.analyzeAccessibilityConflicts( members, ((InheritorUsageInfo) usage).getInheritor(), conflicts, myNewVisibility); } } } if (myTargetVariable instanceof PsiParameter) { PsiParameter parameter = (PsiParameter) myTargetVariable; for (final UsageInfo usageInfo : usages) { if (usageInfo instanceof MethodCallUsageInfo) { final PsiElement methodCall = ((MethodCallUsageInfo) usageInfo).getMethodCallExpression(); if (methodCall instanceof PsiMethodCallExpression) { final PsiExpression[] expressions = ((PsiMethodCallExpression) methodCall).getArgumentList().getExpressions(); final int index = myMethod.getParameterList().getParameterIndex(parameter); if (index < expressions.length) { PsiExpression instanceValue = expressions[index]; instanceValue = RefactoringUtil.unparenthesizeExpression(instanceValue); if (instanceValue instanceof PsiLiteralExpression && ((PsiLiteralExpression) instanceValue).getValue() == null) { String message = RefactoringBundle.message( "0.contains.call.with.null.argument.for.parameter.1", RefactoringUIUtil.getDescription( ConflictsUtil.getContainer(methodCall), true), CommonRefactoringUtil.htmlEmphasize(parameter.getName())); conflicts.putValue(instanceValue, message); } } } else if (methodCall instanceof PsiMethodReferenceExpression) { conflicts.putValue(methodCall, "Method reference would be broken after move"); } } } } try { ConflictsUtil.checkMethodConflicts(myTargetClass, myMethod, getPatternMethod(), conflicts); } catch (IncorrectOperationException e) { } return showConflicts(conflicts, usages); }
@Override public void findExistingNameConflicts( PsiElement element, String newName, MultiMap<PsiElement, String> conflicts) { if (element instanceof PsiCompiledElement) return; if (element instanceof PsiField) { PsiField refactoredField = (PsiField) element; if (newName.equals(refactoredField.getName())) return; ConflictsUtil.checkFieldConflicts(refactoredField.getContainingClass(), newName, conflicts); } }
public static void detectAccessibilityConflicts( @Nullable GroovyPsiElement elementToProcess, final UsageInfo[] usages, MultiMap<PsiElement, String> conflicts, boolean replaceFieldsWithGetters, Project project) { if (elementToProcess == null) return; final ReferencedElementsCollector collector = new ReferencedElementsCollector(); elementToProcess.accept(collector); final List<PsiElement> result = collector.getResult(); if (result.isEmpty()) return; for (final UsageInfo usageInfo : usages) { if (!(usageInfo instanceof ExternalUsageInfo) || !IntroduceParameterUtil.isMethodUsage(usageInfo)) continue; final PsiElement place = usageInfo.getElement(); for (PsiElement element : result) { if (element instanceof PsiField && replaceFieldsWithGetters) { // check getter access instead final PsiClass psiClass = ((PsiField) element).getContainingClass(); LOG.assertTrue(psiClass != null); final PsiMethod method = GroovyPropertyUtils.findGetterForField((PsiField) element); if (method != null) { element = method; } } if (element instanceof PsiMember && !JavaPsiFacade.getInstance(project) .getResolveHelper() .isAccessible((PsiMember) element, place, null)) { String message = RefactoringBundle.message( "0.is.not.accessible.from.1.value.for.introduced.parameter.in.that.method.call.will.be.incorrect", RefactoringUIUtil.getDescription(element, true), RefactoringUIUtil.getDescription(ConflictsUtil.getContainer(place), true)); conflicts.putValue(element, message); } } } }
private void detectAccessibilityConflicts( final UsageInfo[] usageArray, MultiMap<PsiElement, String> conflicts) { if (myParameterInitializer != null) { final ReferencedElementsCollector collector = new ReferencedElementsCollector(); myParameterInitializer.accept(collector); final Set<PsiElement> result = collector.myResult; if (!result.isEmpty()) { for (final UsageInfo usageInfo : usageArray) { if (usageInfo instanceof ExternalUsageInfo && IntroduceParameterUtil.isMethodUsage(usageInfo)) { final PsiElement place = usageInfo.getElement(); for (PsiElement element : result) { if (element instanceof PsiField && myReplaceFieldsWithGetters != IntroduceParameterRefactoring .REPLACE_FIELDS_WITH_GETTERS_NONE) { // check getter access instead final PsiClass psiClass = ((PsiField) element).getContainingClass(); LOG.assertTrue(psiClass != null); final PsiMethod method = psiClass.findMethodBySignature( PropertyUtil.generateGetterPrototype((PsiField) element), true); if (method != null) { element = method; } } if (element instanceof PsiMember && !JavaPsiFacade.getInstance(myProject) .getResolveHelper() .isAccessible((PsiMember) element, place, null)) { String message = RefactoringBundle.message( "0.is.not.accessible.from.1.value.for.introduced.parameter.in.that.method.call.will.be.incorrect", RefactoringUIUtil.getDescription(element, true), RefactoringUIUtil.getDescription(ConflictsUtil.getContainer(place), true)); conflicts.putValue(element, message); } } } } } } }
private void addMethodConflicts(MultiMap<PsiElement, String> conflicts) { String newMethodName = myChangeInfo.getNewName(); if (!(myChangeInfo instanceof JavaChangeInfo)) { return; } try { PsiMethod prototype; final PsiMethod method = myChangeInfo.getMethod(); if (!StdLanguages.JAVA.equals(method.getLanguage())) return; PsiManager manager = method.getManager(); PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory(); final CanonicalTypes.Type returnType = myChangeInfo.getNewReturnType(); if (returnType != null) { prototype = factory.createMethod(newMethodName, returnType.getType(method, manager)); } else { prototype = factory.createConstructor(); prototype.setName(newMethodName); } JavaParameterInfo[] parameters = myChangeInfo.getNewParameters(); for (JavaParameterInfo info : parameters) { PsiType parameterType = info.createType(method, manager); if (parameterType == null) { parameterType = JavaPsiFacade.getElementFactory(method.getProject()) .createTypeFromText(CommonClassNames.JAVA_LANG_OBJECT, method); } PsiParameter param = factory.createParameter(info.getName(), parameterType); prototype.getParameterList().add(param); } ConflictsUtil.checkMethodConflicts( method.getContainingClass(), method, prototype, conflicts); } catch (IncorrectOperationException e) { LOG.error(e); } }
private void addInaccessibilityDescriptions( Set<UsageInfo> usages, MultiMap<PsiElement, String> conflictDescriptions) throws IncorrectOperationException { PsiMethod method = myChangeInfo.getMethod(); PsiModifierList modifierList = (PsiModifierList) method.getModifierList().copy(); VisibilityUtil.setVisibility(modifierList, myChangeInfo.getNewVisibility()); for (Iterator<UsageInfo> iterator = usages.iterator(); iterator.hasNext(); ) { UsageInfo usageInfo = iterator.next(); PsiElement element = usageInfo.getElement(); if (element != null) { if (element instanceof PsiQualifiedReference) { PsiClass accessObjectClass = null; PsiElement qualifier = ((PsiQualifiedReference) element).getQualifier(); if (qualifier instanceof PsiExpression) { accessObjectClass = (PsiClass) PsiUtil.getAccessObjectClass((PsiExpression) qualifier).getElement(); } if (!JavaPsiFacade.getInstance(element.getProject()) .getResolveHelper() .isAccessible(method, modifierList, element, accessObjectClass, null)) { String message = RefactoringBundle.message( "0.with.1.visibility.is.not.accessible.from.2", RefactoringUIUtil.getDescription(method, true), myChangeInfo.getNewVisibility(), RefactoringUIUtil.getDescription(ConflictsUtil.getContainer(element), true)); conflictDescriptions.putValue(method, message); if (!needToChangeCalls()) { iterator.remove(); } } } } } }
public Collection<String> findConflicts( @NotNull final PsiElement element, @NotNull final PsiElement[] allElementsToDelete) { if (element instanceof PsiMethod) { final PsiClass containingClass = ((PsiMethod) element).getContainingClass(); if (containingClass != null && !containingClass.hasModifierProperty(PsiModifier.ABSTRACT)) { final PsiMethod[] superMethods = ((PsiMethod) element).findSuperMethods(); for (PsiMethod superMethod : superMethods) { if (isInside(superMethod, allElementsToDelete)) continue; if (superMethod.hasModifierProperty(PsiModifier.ABSTRACT)) { String message = RefactoringBundle.message( "0.implements.1", RefactoringUIUtil.getDescription(element, true), RefactoringUIUtil.getDescription(superMethod, true)); return Collections.singletonList(message); } } } } else if (element instanceof PsiParameter) { final PsiElement scope = ((PsiParameter) element).getDeclarationScope(); if (scope instanceof PsiMethod) { final PsiMethod method = (PsiMethod) scope; final PsiClass containingClass = method.getContainingClass(); if (containingClass != null) { final int parameterIndex = method.getParameterList().getParameterIndex((PsiParameter) element); final PsiMethod methodCopy = (PsiMethod) method.copy(); methodCopy.getParameterList().getParameters()[parameterIndex].delete(); final MultiMap<PsiElement, String> conflicts = new MultiMap<>(); ConflictsUtil.checkMethodConflicts(containingClass, method, methodCopy, conflicts); return (Collection<String>) conflicts.values(); } } } return null; }